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-08 11:47:58 +00:00 committed by Andrew Nicols
parent fed66ad9e2
commit eb9a3e9379
2 changed files with 44 additions and 1 deletions

View File

@ -923,7 +923,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;

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()));
}
}