MDL-26229 restore - prevent tag-conflicting structures to be created
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Sat, 12 Feb 2011 01:40:38 +0000 (02:40 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Sat, 12 Feb 2011 01:40:38 +0000 (02:40 +0100)
backup/util/structure/base_nested_element.class.php
backup/util/structure/simpletest/testbasenestedelement.php

index 1cd23b0..b48defc 100644 (file)
@@ -94,6 +94,10 @@ abstract class base_nested_element extends base_final_element {
     }
 
     protected function check_and_set_used($element) {
+        // First of all, check the element being added doesn't conflict with own final elements
+        if (array_key_exists($element->get_name(), $this->final_elements)) {
+            throw new base_element_struct_exception('baseelementchildnameconflict', $element->get_name());
+        }
         $grandparent = $this->get_grandoptigroupelement_or_grandparent();
         if ($existing = array_intersect($grandparent->get_used(), $element->get_used())) { // Check the element isn't being used already
             throw new base_element_struct_exception('baseelementexisting', implode($existing));
index 0f5ed64..206fab0 100644 (file)
@@ -393,5 +393,16 @@ class base_nested_element_test extends UnitTestCase {
             $this->assertTrue($e instanceof base_element_parent_exception);
         }
 
+        // Add child element already used by own final elements
+        $nested = new mock_base_nested_element('PARENT1', null, array('FINAL1', 'FINAL2'));
+        $child = new mock_base_nested_element('FINAL2', null, array('FINAL3', 'FINAL4'));
+        try {
+            $nested->add_child($child);
+            $this->fail("Expecting base_element_struct_exception exception, none occurred");
+        } catch (Exception $e) {
+            $this->assertTrue($e instanceof base_element_struct_exception);
+            $this->assertEqual($e->errorcode, 'baseelementchildnameconflict');
+            $this->assertEqual($e->a, 'FINAL2');
+        }
     }
 }