MDL-53383 navigation: Do not require $type in remove()

navigation_node_collection -> remove function broken when $type is null
This commit is contained in:
James
2016-03-21 12:18:19 +08:00
committed by Andrew Nicols
parent d6b44eeea2
commit 4dde5d381e
2 changed files with 44 additions and 1 deletions
+1 -1
View File
@@ -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;
+43
View File
@@ -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()));
}
}