diff --git a/lib/navigationlib.php b/lib/navigationlib.php index f48ee135139..f45e5bcb3af 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -920,7 +920,7 @@ class navigation_node_collection implements IteratorAggregate { $child = $this->get($key, $type); if ($child !== false) { foreach ($this->collection as $colkey => $node) { - if ($node->key === $key && $node->type == $type) { + if ($node->key === $key && (is_null($type) || $node->type == $type)) { unset($this->collection[$colkey]); $this->collection = array_values($this->collection); break; diff --git a/lib/tests/navigationlib_test.php b/lib/tests/navigationlib_test.php index de22b4e2b02..085e7d9eef4 100644 --- a/lib/tests/navigationlib_test.php +++ b/lib/tests/navigationlib_test.php @@ -499,6 +499,49 @@ class core_navigationlib_testcase extends advanced_testcase { $this->assertFalse($node->exposed_in_alternative_role()); } + + + public function test_navigation_node_collection_remove_with_no_type() { + $navigationnodecollection = new navigation_node_collection(); + $this->setup_node(); + $this->node->key = 100; + + // Test it's empty + $this->assertEquals(0, count($navigationnodecollection->get_key_list())); + + // Add a node + $navigationnodecollection->add($this->node); + + // Test it's not empty + $this->assertEquals(1, count($navigationnodecollection->get_key_list())); + + // Remove a node - passing key only! + $this->assertTrue($navigationnodecollection->remove(100)); + + // Test it's empty again! + $this->assertEquals(0, count($navigationnodecollection->get_key_list())); + } + + public function test_navigation_node_collection_remove_with_type() { + $navigationnodecollection = new navigation_node_collection(); + $this->setup_node(); + $this->node->key = 100; + + // Test it's empty + $this->assertEquals(0, count($navigationnodecollection->get_key_list())); + + // Add a node + $navigationnodecollection->add($this->node); + + // Test it's not empty + $this->assertEquals(1, count($navigationnodecollection->get_key_list())); + + // Remove a node - passing type + $this->assertTrue($navigationnodecollection->remove(100, 1)); + + // Test it's empty again! + $this->assertEquals(0, count($navigationnodecollection->get_key_list())); + } }