|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 // Prevent direct access to this file 00028 if (!defined('MOODLE_INTERNAL')) { 00029 die('Direct access to this script is forbidden.'); 00030 } 00031 00032 require_once($CFG->dirroot . '/backup/util/structure/base_atom.class.php'); 00033 require_once($CFG->dirroot . '/backup/util/structure/base_attribute.class.php'); 00034 require_once($CFG->dirroot . '/backup/util/structure/base_final_element.class.php'); 00035 require_once($CFG->dirroot . '/backup/util/structure/base_nested_element.class.php'); 00036 require_once($CFG->dirroot . '/backup/util/structure/simpletest/fixtures/structuremocks.php'); 00037 00041 class base_nested_element_test extends UnitTestCase { 00042 00043 public static $includecoverage = array( 00044 'backup/util/structure/base_nested_element.class.php', 00045 'backup/util/structure/base_final_element.class.php' 00046 ); 00047 00051 function test_creation() { 00052 // Create instance with name, attributes and values and check all them 00053 $instance = new mock_base_nested_element('NAME', array('ATTR1', 'ATTR2'), array('VAL1', 'VAL2', 'VAL3')); 00054 $this->assertIsA($instance, 'base_nested_element'); 00055 $this->assertEqual($instance->get_name(), 'NAME'); 00056 $attrs = $instance->get_attributes(); 00057 $this->assertTrue(is_array($attrs)); 00058 $this->assertEqual(count($attrs), 2); 00059 $this->assertIsA($attrs['ATTR1'], 'base_attribute'); 00060 $this->assertEqual($attrs['ATTR1']->get_name(), 'ATTR1'); 00061 $this->assertNull($attrs['ATTR1']->get_value()); 00062 $this->assertEqual($attrs['ATTR2']->get_name(), 'ATTR2'); 00063 $this->assertNull($attrs['ATTR2']->get_value()); 00064 $finals = $instance->get_final_elements(); 00065 $this->assertTrue(is_array($finals)); 00066 $this->assertEqual(count($finals), 3); 00067 $this->assertIsA($finals['VAL1'], 'base_final_element'); 00068 $this->assertEqual($finals['VAL1']->get_name(), 'VAL1'); 00069 $this->assertNull($finals['VAL1']->get_value()); 00070 $this->assertEqual($finals['VAL1']->get_level(), 2); 00071 $this->assertIsA($finals['VAL1']->get_parent(), 'base_nested_element'); 00072 $this->assertEqual($finals['VAL2']->get_name(), 'VAL2'); 00073 $this->assertNull($finals['VAL2']->get_value()); 00074 $this->assertEqual($finals['VAL2']->get_level(), 2); 00075 $this->assertIsA($finals['VAL1']->get_parent(), 'base_nested_element'); 00076 $this->assertEqual($finals['VAL3']->get_name(), 'VAL3'); 00077 $this->assertNull($finals['VAL3']->get_value()); 00078 $this->assertEqual($finals['VAL3']->get_level(), 2); 00079 $this->assertIsA($finals['VAL1']->get_parent(), 'base_nested_element'); 00080 $this->assertNull($instance->get_parent()); 00081 $this->assertEqual($instance->get_children(), array()); 00082 $this->assertEqual($instance->get_level(), 1); 00083 00084 // Create instance with name only 00085 $instance = new mock_base_nested_element('NAME'); 00086 $this->assertIsA($instance, 'base_nested_element'); 00087 $this->assertEqual($instance->get_name(), 'NAME'); 00088 $this->assertEqual($instance->get_attributes(), array()); 00089 $this->assertEqual($instance->get_final_elements(), array()); 00090 $this->assertNull($instance->get_parent()); 00091 $this->assertEqual($instance->get_children(), array()); 00092 $this->assertEqual($instance->get_level(), 1); 00093 00094 // Add some attributes 00095 $instance->add_attributes(array('ATTR1', 'ATTR2')); 00096 $attrs = $instance->get_attributes(); 00097 $this->assertTrue(is_array($attrs)); 00098 $this->assertEqual(count($attrs), 2); 00099 $this->assertEqual($attrs['ATTR1']->get_name(), 'ATTR1'); 00100 $this->assertNull($attrs['ATTR1']->get_value()); 00101 $this->assertEqual($attrs['ATTR2']->get_name(), 'ATTR2'); 00102 $this->assertNull($attrs['ATTR2']->get_value()); 00103 00104 // And some more atributes 00105 $instance->add_attributes(array('ATTR3', 'ATTR4')); 00106 $attrs = $instance->get_attributes(); 00107 $this->assertTrue(is_array($attrs)); 00108 $this->assertEqual(count($attrs), 4); 00109 $this->assertEqual($attrs['ATTR1']->get_name(), 'ATTR1'); 00110 $this->assertNull($attrs['ATTR1']->get_value()); 00111 $this->assertEqual($attrs['ATTR2']->get_name(), 'ATTR2'); 00112 $this->assertNull($attrs['ATTR2']->get_value()); 00113 $this->assertEqual($attrs['ATTR3']->get_name(), 'ATTR3'); 00114 $this->assertNull($attrs['ATTR3']->get_value()); 00115 $this->assertEqual($attrs['ATTR4']->get_name(), 'ATTR4'); 00116 $this->assertNull($attrs['ATTR4']->get_value()); 00117 00118 // Add some final elements 00119 $instance->add_final_elements(array('VAL1', 'VAL2', 'VAL3')); 00120 $finals = $instance->get_final_elements(); 00121 $this->assertTrue(is_array($finals)); 00122 $this->assertEqual(count($finals), 3); 00123 $this->assertEqual($finals['VAL1']->get_name(), 'VAL1'); 00124 $this->assertNull($finals['VAL1']->get_value()); 00125 $this->assertEqual($finals['VAL2']->get_name(), 'VAL2'); 00126 $this->assertNull($finals['VAL2']->get_value()); 00127 $this->assertEqual($finals['VAL3']->get_name(), 'VAL3'); 00128 $this->assertNull($finals['VAL3']->get_value()); 00129 00130 // Add some more final elements 00131 $instance->add_final_elements('VAL4'); 00132 $finals = $instance->get_final_elements(); 00133 $this->assertTrue(is_array($finals)); 00134 $this->assertEqual(count($finals), 4); 00135 $this->assertEqual($finals['VAL1']->get_name(), 'VAL1'); 00136 $this->assertNull($finals['VAL1']->get_value()); 00137 $this->assertEqual($finals['VAL2']->get_name(), 'VAL2'); 00138 $this->assertNull($finals['VAL2']->get_value()); 00139 $this->assertEqual($finals['VAL3']->get_name(), 'VAL3'); 00140 $this->assertNull($finals['VAL3']->get_value()); 00141 $this->assertEqual($finals['VAL4']->get_name(), 'VAL4'); 00142 $this->assertNull($finals['VAL4']->get_value()); 00143 00144 // Get to_string() results (with values) 00145 $instance = new mock_base_nested_element('PARENT', array('ATTR1', 'ATTR2'), array('FINAL1', 'FINAL2', 'FINAL3')); 00146 $child1 = new mock_base_nested_element('CHILD1', null, new mock_base_final_element('FINAL4')); 00147 $child2 = new mock_base_nested_element('CHILD2', null, new mock_base_final_element('FINAL5')); 00148 $instance->add_child($child1); 00149 $instance->add_child($child2); 00150 $children = $instance->get_children(); 00151 $final_elements = $children['CHILD1']->get_final_elements(); 00152 $final_elements['FINAL4']->set_value('final4value'); 00153 $final_elements['FINAL4']->add_attributes('ATTR4'); 00154 $grandchild = new mock_base_nested_element('GRANDCHILD', new mock_base_attribute('ATTR5')); 00155 $child2->add_child($grandchild); 00156 $attrs = $grandchild->get_attributes(); 00157 $attrs['ATTR5']->set_value('attr5value'); 00158 $tostring = $instance->to_string(true); 00159 $this->assertTrue(strpos($tostring, 'PARENT (level: 1)') !== false); 00160 $this->assertTrue(strpos($tostring, ' => ') !== false); 00161 $this->assertTrue(strpos($tostring, '#FINAL4 (level: 3) => final4value') !== false); 00162 $this->assertTrue(strpos($tostring, '@ATTR5 => attr5value') !== false); 00163 $this->assertTrue(strpos($tostring, '#FINAL5 (level: 3) => not set') !== false); 00164 00165 // Clean values 00166 $instance = new mock_base_nested_element('PARENT', array('ATTR1', 'ATTR2'), array('FINAL1', 'FINAL2', 'FINAL3')); 00167 $child1 = new mock_base_nested_element('CHILD1', null, new mock_base_final_element('FINAL4')); 00168 $child2 = new mock_base_nested_element('CHILD2', null, new mock_base_final_element('FINAL4')); 00169 $instance->add_child($child1); 00170 $instance->add_child($child2); 00171 $children = $instance->get_children(); 00172 $final_elements = $children['CHILD1']->get_final_elements(); 00173 $final_elements['FINAL4']->set_value('final4value'); 00174 $final_elements['FINAL4']->add_attributes('ATTR4'); 00175 $grandchild = new mock_base_nested_element('GRANDCHILD', new mock_base_attribute('ATTR4')); 00176 $child2->add_child($grandchild); 00177 $attrs = $grandchild->get_attributes(); 00178 $attrs['ATTR4']->set_value('attr4value'); 00179 $this->assertEqual($final_elements['FINAL4']->get_value(), 'final4value'); 00180 $this->assertEqual($attrs['ATTR4']->get_value(), 'attr4value'); 00181 $instance->clean_values(); 00182 $this->assertNull($final_elements['FINAL4']->get_value()); 00183 $this->assertNull($attrs['ATTR4']->get_value()); 00184 } 00185 00189 function test_wrong_creation() { 00190 00191 // Create instance with invalid name 00192 try { 00193 $instance = new mock_base_nested_element(''); 00194 $this->fail("Expecting base_atom_struct_exception exception, none occurred"); 00195 } catch (Exception $e) { 00196 $this->assertTrue($e instanceof base_atom_struct_exception); 00197 } 00198 00199 // Create instance with incorrect (object) final element 00200 try { 00201 $obj = new stdClass; 00202 $obj->name = 'test_attr'; 00203 $instance = new mock_base_nested_element('TEST', null, $obj); 00204 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00205 } catch (Exception $e) { 00206 $this->assertTrue($e instanceof base_element_struct_exception); 00207 } 00208 00209 // Create instance with array containing incorrect (object) final element 00210 try { 00211 $obj = new stdClass; 00212 $obj->name = 'test_attr'; 00213 $instance = new mock_base_nested_element('TEST', null, array($obj)); 00214 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00215 } catch (Exception $e) { 00216 $this->assertTrue($e instanceof base_element_struct_exception); 00217 } 00218 00219 // Create instance with array containing duplicate final elements 00220 try { 00221 $instance = new mock_base_nested_element('TEST', null, array('VAL1', 'VAL2', 'VAL1')); 00222 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00223 } catch (Exception $e) { 00224 $this->assertTrue($e instanceof base_element_struct_exception); 00225 } 00226 00227 // Try to get value of base_nested_element 00228 $instance = new mock_base_nested_element('TEST'); 00229 try { 00230 $instance->get_value(); 00231 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00232 } catch (Exception $e) { 00233 $this->assertTrue($e instanceof base_element_struct_exception); 00234 } 00235 00236 // Try to set value of base_nested_element 00237 $instance = new mock_base_nested_element('TEST'); 00238 try { 00239 $instance->set_value('some_value'); 00240 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00241 } catch (Exception $e) { 00242 $this->assertTrue($e instanceof base_element_struct_exception); 00243 } 00244 00245 // Try to clean one value of base_nested_element 00246 $instance = new mock_base_nested_element('TEST'); 00247 try { 00248 $instance->clean_value('some_value'); 00249 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00250 } catch (Exception $e) { 00251 $this->assertTrue($e instanceof base_element_struct_exception); 00252 } 00253 } 00254 00258 function test_tree() { 00259 00260 // Create parent and child instances, tree-ing them 00261 $parent = new mock_base_nested_element('PARENT'); 00262 $child = new mock_base_nested_element('CHILD'); 00263 $parent->add_child($child); 00264 $this->assertEqual($parent->get_children(), array('CHILD' => $child)); 00265 $this->assertEqual($child->get_parent(), $parent); 00266 $check_children = $parent->get_children(); 00267 $check_child = $check_children['CHILD']; 00268 $check_parent = $check_child->get_parent(); 00269 $this->assertEqual($check_child->get_name(), 'CHILD'); 00270 $this->assertEqual($check_parent->get_name(), 'PARENT'); 00271 $this->assertEqual($check_child->get_level(), 2); 00272 $this->assertEqual($check_parent->get_level(), 1); 00273 $this->assertEqual($check_parent->get_children(), array('CHILD' => $child)); 00274 $this->assertEqual($check_child->get_parent(), $parent); 00275 00276 // Add parent to grandparent 00277 $grandparent = new mock_base_nested_element('GRANDPARENT'); 00278 $grandparent->add_child($parent); 00279 $this->assertEqual($grandparent->get_children(), array('PARENT' => $parent)); 00280 $this->assertEqual($parent->get_parent(), $grandparent); 00281 $this->assertEqual($parent->get_children(), array('CHILD' => $child)); 00282 $this->assertEqual($child->get_parent(), $parent); 00283 $this->assertEqual($child->get_level(), 3); 00284 $this->assertEqual($parent->get_level(), 2); 00285 $this->assertEqual($grandparent->get_level(), 1); 00286 00287 // Add grandchild to child 00288 $grandchild = new mock_base_nested_element('GRANDCHILD'); 00289 $child->add_child($grandchild); 00290 $this->assertEqual($child->get_children(), array('GRANDCHILD' => $grandchild)); 00291 $this->assertEqual($grandchild->get_parent(), $child); 00292 $this->assertEqual($grandchild->get_level(), 4); 00293 $this->assertEqual($child->get_level(), 3); 00294 $this->assertEqual($parent->get_level(), 2); 00295 $this->assertEqual($grandparent->get_level(), 1); 00296 00297 // Add another child to parent 00298 $child2 = new mock_base_nested_element('CHILD2'); 00299 $parent->add_child($child2); 00300 $this->assertEqual($parent->get_children(), array('CHILD' => $child, 'CHILD2' => $child2)); 00301 $this->assertEqual($child2->get_parent(), $parent); 00302 $this->assertEqual($grandchild->get_level(), 4); 00303 $this->assertEqual($child->get_level(), 3); 00304 $this->assertEqual($child2->get_level(), 3); 00305 $this->assertEqual($parent->get_level(), 2); 00306 $this->assertEqual($grandparent->get_level(), 1); 00307 } 00308 00312 function test_wrong_tree() { 00313 00314 // Add null object child 00315 $parent = new mock_base_nested_element('PARENT'); 00316 $child = null; 00317 try { 00318 $parent->add_child($child); 00319 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00320 } catch (Exception $e) { 00321 $this->assertTrue($e instanceof base_element_struct_exception); 00322 } 00323 00324 // Add non base_element object child 00325 $parent = new mock_base_nested_element('PARENT'); 00326 $child = new stdClass(); 00327 try { 00328 $parent->add_child($child); 00329 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00330 } catch (Exception $e) { 00331 $this->assertTrue($e instanceof base_element_struct_exception); 00332 } 00333 00334 // Add existing element (being parent) 00335 $parent = new mock_base_nested_element('PARENT'); 00336 $child = new mock_base_nested_element('PARENT'); 00337 try { 00338 $parent->add_child($child); 00339 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00340 } catch (Exception $e) { 00341 $this->assertTrue($e instanceof base_element_struct_exception); 00342 } 00343 00344 // Add existing element (being grandparent) 00345 $grandparent = new mock_base_nested_element('GRANDPARENT'); 00346 $parent = new mock_base_nested_element('PARENT'); 00347 $child = new mock_base_nested_element('GRANDPARENT'); 00348 $grandparent->add_child($parent); 00349 try { 00350 $parent->add_child($child); 00351 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00352 } catch (Exception $e) { 00353 $this->assertTrue($e instanceof base_element_struct_exception); 00354 } 00355 00356 // Add existing element (being grandchild) 00357 $grandparent = new mock_base_nested_element('GRANDPARENT'); 00358 $parent = new mock_base_nested_element('PARENT'); 00359 $child = new mock_base_nested_element('GRANDPARENT'); 00360 $parent->add_child($child); 00361 try { 00362 $grandparent->add_child($parent); 00363 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00364 } catch (Exception $e) { 00365 $this->assertTrue($e instanceof base_element_struct_exception); 00366 } 00367 00368 // Add existing element (being cousin) 00369 $grandparent = new mock_base_nested_element('GRANDPARENT'); 00370 $parent1 = new mock_base_nested_element('PARENT1'); 00371 $parent2 = new mock_base_nested_element('PARENT2'); 00372 $child1 = new mock_base_nested_element('CHILD1'); 00373 $child2 = new mock_base_nested_element('CHILD1'); 00374 $grandparent->add_child($parent1); 00375 $parent1->add_child($child1); 00376 $parent2->add_child($child2); 00377 try { 00378 $grandparent->add_child($parent2); 00379 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00380 } catch (Exception $e) { 00381 $this->assertTrue($e instanceof base_element_struct_exception); 00382 } 00383 00384 // Add element to two parents 00385 $parent1 = new mock_base_nested_element('PARENT1'); 00386 $parent2 = new mock_base_nested_element('PARENT2'); 00387 $child = new mock_base_nested_element('CHILD'); 00388 $parent1->add_child($child); 00389 try { 00390 $parent2->add_child($child); 00391 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00392 } catch (Exception $e) { 00393 $this->assertTrue($e instanceof base_element_parent_exception); 00394 } 00395 00396 // Add child element already used by own final elements 00397 $nested = new mock_base_nested_element('PARENT1', null, array('FINAL1', 'FINAL2')); 00398 $child = new mock_base_nested_element('FINAL2', null, array('FINAL3', 'FINAL4')); 00399 try { 00400 $nested->add_child($child); 00401 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 00402 } catch (Exception $e) { 00403 $this->assertTrue($e instanceof base_element_struct_exception); 00404 $this->assertEqual($e->errorcode, 'baseelementchildnameconflict'); 00405 $this->assertEqual($e->a, 'FINAL2'); 00406 } 00407 } 00408 }