Merge branch 'MDL-73978-master' of https://github.com/peterRd/moodle

This commit is contained in:
Ilya Tregubov 2022-02-28 09:18:27 +02:00
commit 127655326e
2 changed files with 69 additions and 12 deletions

View File

@ -303,12 +303,15 @@ class secondary extends view {
*
* @param navigation_node $node The node which should be added to secondary
* @param navigation_node $basenode The original parent node
* @param navigation_node|null $root The parent node nodes are to be added/removed to.
* @param bool $forceadd Whether or not to bypass the external action check and force add all nodes
*/
protected function add_external_nodes_to_secondary(navigation_node $node, navigation_node $basenode, bool $forceadd = false) {
protected function add_external_nodes_to_secondary(navigation_node $node, navigation_node $basenode,
?navigation_node $root = null, bool $forceadd = false) {
$root = $root ?? $this;
// Add the first node.
if ($node->has_action() && !$this->get($node->key)) {
$this->add_node(clone $node);
$root->add_node(clone $node);
}
// If the node has an external action add all children to the secondary navigation.
@ -316,15 +319,15 @@ class secondary extends view {
if ($node->has_children()) {
foreach ($node->children as $child) {
if ($child->has_children()) {
$this->add_external_nodes_to_secondary($child, $basenode, true);
$this->add_external_nodes_to_secondary($child, $basenode, $root, true);
} else if ($child->has_action() && !$this->get($child->key)) {
// Check whether the basenode matches a child's url.
// This would have happened in get_first_action_for_node.
// In these cases, we prefer the specific child content.
if ($basenode->has_action() && $basenode->action()->compare($child->action())) {
$this->children->remove($basenode->key, $basenode->type);
$root->children->remove($basenode->key, $basenode->type);
}
$this->add_node(clone $child);
$root->add_node(clone $child);
}
}
}
@ -400,10 +403,10 @@ class secondary extends view {
foreach ($courseadminnode->children as $other) {
if (array_search($other->key, $expectedcourseadmin) === false) {
$othernode = $this->get_first_action_for_node($other);
$recursivenode = $othernode && !$this->get($othernode->key) ? $othernode : $other;
$recursivenode = $othernode && !$rootnode->get($othernode->key) ? $othernode : $other;
// Get the first node and check whether it's been added already.
// Also check if the first node is an external link. If it is, add all children.
$this->add_external_nodes_to_secondary($recursivenode, $recursivenode);
$this->add_external_nodes_to_secondary($recursivenode, $recursivenode, $rootnode);
}
}
}
@ -765,7 +768,7 @@ class secondary extends view {
// We have found the first node with an action.
if ($leftovernode) {
$this->add_external_nodes_to_secondary($leftovernode, $leftovernode);
$this->add_external_nodes_to_secondary($leftovernode, $leftovernode, $rootnode);
}
}
}

View File

@ -16,6 +16,7 @@
namespace core\navigation\views;
use booktool_print\output\renderer;
use navigation_node;
use ReflectionMethod;
use moodle_url;
@ -665,9 +666,10 @@ class secondary_test extends \advanced_testcase {
*
* @param array $structure The structure of the navigation node tree to setup with.
* @param array $expectednodes The expected nodes added to the secondary navigation
* @param bool $separatenode Whether or not to create a separate node to add nodes to.
* @dataProvider add_external_nodes_to_secondary_provider
*/
public function test_add_external_nodes_to_secondary(array $structure, array $expectednodes) {
public function test_add_external_nodes_to_secondary(array $structure, array $expectednodes, bool $separatenode = false) {
global $PAGE;
$this->resetAfterTest();
@ -680,13 +682,17 @@ class secondary_test extends \advanced_testcase {
$secondary = new secondary($PAGE);
$secondary->add_node($node);
$firstnode = $node->get('parentnode1');
$customparent = null;
if ($separatenode) {
$customparent = navigation_node::create('Custom parent');
}
$method = new ReflectionMethod('core\navigation\views\secondary', 'add_external_nodes_to_secondary');
$method->setAccessible(true);
$method->invoke($secondary, $firstnode, $firstnode);
$method->invoke($secondary, $firstnode, $firstnode, $customparent);
$test = $secondary->get_children_key_list();
$this->assertEquals($expectednodes, $test);
$actualnodes = $separatenode ? $customparent->get_children_key_list() : $secondary->get_children_key_list();
$this->assertEquals($expectednodes, $actualnodes);
}
/**
@ -744,6 +750,54 @@ class secondary_test extends \advanced_testcase {
],
['parentnode', 'parentnode1']
],
"Container node with internal action and external children adding to custom node" => [
[
'parentnode1' => [
'action' => '/test.php',
'children' => [
'child2.1' => 'https://example.org',
'child2.2' => 'https://example.net',
]
]
],
['parentnode1'], true
],
"Container node with external action and external children adding to custom node" => [
[
'parentnode1' => [
'action' => 'https://example.com',
'children' => [
'child2.1' => 'https://example.org',
'child2.2' => 'https://example.net',
]
]
],
['parentnode1', 'child2.1', 'child2.2'], true
],
"Container node with external action and internal children adding to custom node" => [
[
'parentnode1' => [
'action' => 'https://example.org',
'children' => [
'child2.1' => '/view/course.php',
'child2.2' => '/view/admin.php',
]
]
],
['parentnode1', 'child2.1', 'child2.2'], true
],
"Container node with internal actions and internal children adding to custom node" => [
[
'parentnode1' => [
'action' => '/test.php',
'children' => [
'child2.1' => '/course.php',
'child2.2' => '/admin.php',
]
]
],
['parentnode1'], true
],
];
}