Differences Between: [Versions 310 and 400] [Versions 39 and 400]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace core_backup; 18 19 use base_atom_struct_exception; 20 use base_element_parent_exception; 21 use base_element_struct_exception; 22 use mock_base_attribute; 23 use mock_base_final_element; 24 use mock_base_nested_element; 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 // Include all the needed stuff 29 require_once (__DIR__.'/fixtures/structure_fixtures.php'); 30 31 32 /** 33 * Unit test case the base_nested_element class. 34 * 35 * Note: highly imbricated with base_final_element class 36 * 37 * @package core_backup 38 * @category test 39 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 */ 42 class basenestedelement_test extends \basic_testcase { 43 44 /** 45 * Correct creation tests (attributes and final elements) 46 */ 47 public function test_creation() { 48 // Create instance with name, attributes and values and check all them 49 $instance = new mock_base_nested_element('NAME', array('ATTR1', 'ATTR2'), array('VAL1', 'VAL2', 'VAL3')); 50 $this->assertInstanceOf('base_nested_element', $instance); 51 $this->assertEquals($instance->get_name(), 'NAME'); 52 $attrs = $instance->get_attributes(); 53 $this->assertTrue(is_array($attrs)); 54 $this->assertEquals(count($attrs), 2); 55 $this->assertInstanceOf('base_attribute', $attrs['ATTR1']); 56 $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1'); 57 $this->assertNull($attrs['ATTR1']->get_value()); 58 $this->assertEquals($attrs['ATTR2']->get_name(), 'ATTR2'); 59 $this->assertNull($attrs['ATTR2']->get_value()); 60 $finals = $instance->get_final_elements(); 61 $this->assertTrue(is_array($finals)); 62 $this->assertEquals(count($finals), 3); 63 $this->assertInstanceOf('base_final_element', $finals['VAL1']); 64 $this->assertEquals($finals['VAL1']->get_name(), 'VAL1'); 65 $this->assertNull($finals['VAL1']->get_value()); 66 $this->assertEquals($finals['VAL1']->get_level(), 2); 67 $this->assertInstanceOf('base_nested_element', $finals['VAL1']->get_parent()); 68 $this->assertEquals($finals['VAL2']->get_name(), 'VAL2'); 69 $this->assertNull($finals['VAL2']->get_value()); 70 $this->assertEquals($finals['VAL2']->get_level(), 2); 71 $this->assertInstanceOf('base_nested_element', $finals['VAL1']->get_parent()); 72 $this->assertEquals($finals['VAL3']->get_name(), 'VAL3'); 73 $this->assertNull($finals['VAL3']->get_value()); 74 $this->assertEquals($finals['VAL3']->get_level(), 2); 75 $this->assertInstanceOf('base_nested_element', $finals['VAL1']->get_parent()); 76 $this->assertNull($instance->get_parent()); 77 $this->assertEquals($instance->get_children(), array()); 78 $this->assertEquals($instance->get_level(), 1); 79 80 // Create instance with name only 81 $instance = new mock_base_nested_element('NAME'); 82 $this->assertInstanceOf('base_nested_element', $instance); 83 $this->assertEquals($instance->get_name(), 'NAME'); 84 $this->assertEquals($instance->get_attributes(), array()); 85 $this->assertEquals($instance->get_final_elements(), array()); 86 $this->assertNull($instance->get_parent()); 87 $this->assertEquals($instance->get_children(), array()); 88 $this->assertEquals($instance->get_level(), 1); 89 90 // Add some attributes 91 $instance->add_attributes(array('ATTR1', 'ATTR2')); 92 $attrs = $instance->get_attributes(); 93 $this->assertTrue(is_array($attrs)); 94 $this->assertEquals(count($attrs), 2); 95 $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1'); 96 $this->assertNull($attrs['ATTR1']->get_value()); 97 $this->assertEquals($attrs['ATTR2']->get_name(), 'ATTR2'); 98 $this->assertNull($attrs['ATTR2']->get_value()); 99 100 // And some more atributes 101 $instance->add_attributes(array('ATTR3', 'ATTR4')); 102 $attrs = $instance->get_attributes(); 103 $this->assertTrue(is_array($attrs)); 104 $this->assertEquals(count($attrs), 4); 105 $this->assertEquals($attrs['ATTR1']->get_name(), 'ATTR1'); 106 $this->assertNull($attrs['ATTR1']->get_value()); 107 $this->assertEquals($attrs['ATTR2']->get_name(), 'ATTR2'); 108 $this->assertNull($attrs['ATTR2']->get_value()); 109 $this->assertEquals($attrs['ATTR3']->get_name(), 'ATTR3'); 110 $this->assertNull($attrs['ATTR3']->get_value()); 111 $this->assertEquals($attrs['ATTR4']->get_name(), 'ATTR4'); 112 $this->assertNull($attrs['ATTR4']->get_value()); 113 114 // Add some final elements 115 $instance->add_final_elements(array('VAL1', 'VAL2', 'VAL3')); 116 $finals = $instance->get_final_elements(); 117 $this->assertTrue(is_array($finals)); 118 $this->assertEquals(count($finals), 3); 119 $this->assertEquals($finals['VAL1']->get_name(), 'VAL1'); 120 $this->assertNull($finals['VAL1']->get_value()); 121 $this->assertEquals($finals['VAL2']->get_name(), 'VAL2'); 122 $this->assertNull($finals['VAL2']->get_value()); 123 $this->assertEquals($finals['VAL3']->get_name(), 'VAL3'); 124 $this->assertNull($finals['VAL3']->get_value()); 125 126 // Add some more final elements 127 $instance->add_final_elements('VAL4'); 128 $finals = $instance->get_final_elements(); 129 $this->assertTrue(is_array($finals)); 130 $this->assertEquals(count($finals), 4); 131 $this->assertEquals($finals['VAL1']->get_name(), 'VAL1'); 132 $this->assertNull($finals['VAL1']->get_value()); 133 $this->assertEquals($finals['VAL2']->get_name(), 'VAL2'); 134 $this->assertNull($finals['VAL2']->get_value()); 135 $this->assertEquals($finals['VAL3']->get_name(), 'VAL3'); 136 $this->assertNull($finals['VAL3']->get_value()); 137 $this->assertEquals($finals['VAL4']->get_name(), 'VAL4'); 138 $this->assertNull($finals['VAL4']->get_value()); 139 140 // Get to_string() results (with values) 141 $instance = new mock_base_nested_element('PARENT', array('ATTR1', 'ATTR2'), array('FINAL1', 'FINAL2', 'FINAL3')); 142 $child1 = new mock_base_nested_element('CHILD1', null, new mock_base_final_element('FINAL4')); 143 $child2 = new mock_base_nested_element('CHILD2', null, new mock_base_final_element('FINAL5')); 144 $instance->add_child($child1); 145 $instance->add_child($child2); 146 $children = $instance->get_children(); 147 $final_elements = $children['CHILD1']->get_final_elements(); 148 $final_elements['FINAL4']->set_value('final4value'); 149 $final_elements['FINAL4']->add_attributes('ATTR4'); 150 $grandchild = new mock_base_nested_element('GRANDCHILD', new mock_base_attribute('ATTR5')); 151 $child2->add_child($grandchild); 152 $attrs = $grandchild->get_attributes(); 153 $attrs['ATTR5']->set_value('attr5value'); 154 $tostring = $instance->to_string(true); 155 $this->assertTrue(strpos($tostring, 'PARENT (level: 1)') !== false); 156 $this->assertTrue(strpos($tostring, ' => ') !== false); 157 $this->assertTrue(strpos($tostring, '#FINAL4 (level: 3) => final4value') !== false); 158 $this->assertTrue(strpos($tostring, '@ATTR5 => attr5value') !== false); 159 $this->assertTrue(strpos($tostring, '#FINAL5 (level: 3) => not set') !== false); 160 161 // Clean values 162 $instance = new mock_base_nested_element('PARENT', array('ATTR1', 'ATTR2'), array('FINAL1', 'FINAL2', 'FINAL3')); 163 $child1 = new mock_base_nested_element('CHILD1', null, new mock_base_final_element('FINAL4')); 164 $child2 = new mock_base_nested_element('CHILD2', null, new mock_base_final_element('FINAL4')); 165 $instance->add_child($child1); 166 $instance->add_child($child2); 167 $children = $instance->get_children(); 168 $final_elements = $children['CHILD1']->get_final_elements(); 169 $final_elements['FINAL4']->set_value('final4value'); 170 $final_elements['FINAL4']->add_attributes('ATTR4'); 171 $grandchild = new mock_base_nested_element('GRANDCHILD', new mock_base_attribute('ATTR4')); 172 $child2->add_child($grandchild); 173 $attrs = $grandchild->get_attributes(); 174 $attrs['ATTR4']->set_value('attr4value'); 175 $this->assertEquals($final_elements['FINAL4']->get_value(), 'final4value'); 176 $this->assertEquals($attrs['ATTR4']->get_value(), 'attr4value'); 177 $instance->clean_values(); 178 $this->assertNull($final_elements['FINAL4']->get_value()); 179 $this->assertNull($attrs['ATTR4']->get_value()); 180 } 181 182 /** 183 * Incorrect creation tests (attributes and final elements) 184 */ 185 function test_wrong_creation() { 186 187 // Create instance with invalid name 188 try { 189 $instance = new mock_base_nested_element(''); 190 $this->fail("Expecting base_atom_struct_exception exception, none occurred"); 191 } catch (\Exception $e) { 192 $this->assertTrue($e instanceof base_atom_struct_exception); 193 } 194 195 // Create instance with incorrect (object) final element 196 try { 197 $obj = new \stdClass; 198 $obj->name = 'test_attr'; 199 $instance = new mock_base_nested_element('TEST', null, $obj); 200 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 201 } catch (\Exception $e) { 202 $this->assertTrue($e instanceof base_element_struct_exception); 203 } 204 205 // Create instance with array containing incorrect (object) final element 206 try { 207 $obj = new \stdClass; 208 $obj->name = 'test_attr'; 209 $instance = new mock_base_nested_element('TEST', null, array($obj)); 210 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 211 } catch (\Exception $e) { 212 $this->assertTrue($e instanceof base_element_struct_exception); 213 } 214 215 // Create instance with array containing duplicate final elements 216 try { 217 $instance = new mock_base_nested_element('TEST', null, array('VAL1', 'VAL2', 'VAL1')); 218 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 219 } catch (\Exception $e) { 220 $this->assertTrue($e instanceof base_element_struct_exception); 221 } 222 223 // Try to get value of base_nested_element 224 $instance = new mock_base_nested_element('TEST'); 225 try { 226 $instance->get_value(); 227 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 228 } catch (\Exception $e) { 229 $this->assertTrue($e instanceof base_element_struct_exception); 230 } 231 232 // Try to set value of base_nested_element 233 $instance = new mock_base_nested_element('TEST'); 234 try { 235 $instance->set_value('some_value'); 236 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 237 } catch (\Exception $e) { 238 $this->assertTrue($e instanceof base_element_struct_exception); 239 } 240 241 // Try to clean one value of base_nested_element 242 $instance = new mock_base_nested_element('TEST'); 243 try { 244 $instance->clean_value('some_value'); 245 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 246 } catch (\Exception $e) { 247 $this->assertTrue($e instanceof base_element_struct_exception); 248 } 249 } 250 251 /** 252 * Correct tree tests (children stuff) 253 */ 254 function test_tree() { 255 256 // Create parent and child instances, tree-ing them 257 $parent = new mock_base_nested_element('PARENT'); 258 $child = new mock_base_nested_element('CHILD'); 259 $parent->add_child($child); 260 $this->assertEquals($parent->get_children(), array('CHILD' => $child)); 261 $this->assertEquals($child->get_parent(), $parent); 262 $check_children = $parent->get_children(); 263 $check_child = $check_children['CHILD']; 264 $check_parent = $check_child->get_parent(); 265 $this->assertEquals($check_child->get_name(), 'CHILD'); 266 $this->assertEquals($check_parent->get_name(), 'PARENT'); 267 $this->assertEquals($check_child->get_level(), 2); 268 $this->assertEquals($check_parent->get_level(), 1); 269 $this->assertEquals($check_parent->get_children(), array('CHILD' => $child)); 270 $this->assertEquals($check_child->get_parent(), $parent); 271 272 // Add parent to grandparent 273 $grandparent = new mock_base_nested_element('GRANDPARENT'); 274 $grandparent->add_child($parent); 275 $this->assertEquals($grandparent->get_children(), array('PARENT' => $parent)); 276 $this->assertEquals($parent->get_parent(), $grandparent); 277 $this->assertEquals($parent->get_children(), array('CHILD' => $child)); 278 $this->assertEquals($child->get_parent(), $parent); 279 $this->assertEquals($child->get_level(), 3); 280 $this->assertEquals($parent->get_level(), 2); 281 $this->assertEquals($grandparent->get_level(), 1); 282 283 // Add grandchild to child 284 $grandchild = new mock_base_nested_element('GRANDCHILD'); 285 $child->add_child($grandchild); 286 $this->assertEquals($child->get_children(), array('GRANDCHILD' => $grandchild)); 287 $this->assertEquals($grandchild->get_parent(), $child); 288 $this->assertEquals($grandchild->get_level(), 4); 289 $this->assertEquals($child->get_level(), 3); 290 $this->assertEquals($parent->get_level(), 2); 291 $this->assertEquals($grandparent->get_level(), 1); 292 293 // Add another child to parent 294 $child2 = new mock_base_nested_element('CHILD2'); 295 $parent->add_child($child2); 296 $this->assertEquals($parent->get_children(), array('CHILD' => $child, 'CHILD2' => $child2)); 297 $this->assertEquals($child2->get_parent(), $parent); 298 $this->assertEquals($grandchild->get_level(), 4); 299 $this->assertEquals($child->get_level(), 3); 300 $this->assertEquals($child2->get_level(), 3); 301 $this->assertEquals($parent->get_level(), 2); 302 $this->assertEquals($grandparent->get_level(), 1); 303 } 304 305 /** 306 * Incorrect tree tests (children stuff) 307 */ 308 function test_wrong_tree() { 309 310 // Add null object child 311 $parent = new mock_base_nested_element('PARENT'); 312 $child = null; 313 try { 314 $parent->add_child($child); 315 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 316 } catch (\Exception $e) { 317 $this->assertTrue($e instanceof base_element_struct_exception); 318 } 319 320 // Add non base_element object child 321 $parent = new mock_base_nested_element('PARENT'); 322 $child = new \stdClass(); 323 try { 324 $parent->add_child($child); 325 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 326 } catch (\Exception $e) { 327 $this->assertTrue($e instanceof base_element_struct_exception); 328 } 329 330 // Add existing element (being parent) 331 $parent = new mock_base_nested_element('PARENT'); 332 $child = new mock_base_nested_element('PARENT'); 333 try { 334 $parent->add_child($child); 335 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 336 } catch (\Exception $e) { 337 $this->assertTrue($e instanceof base_element_struct_exception); 338 } 339 340 // Add existing element (being grandparent) 341 $grandparent = new mock_base_nested_element('GRANDPARENT'); 342 $parent = new mock_base_nested_element('PARENT'); 343 $child = new mock_base_nested_element('GRANDPARENT'); 344 $grandparent->add_child($parent); 345 try { 346 $parent->add_child($child); 347 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 348 } catch (\Exception $e) { 349 $this->assertTrue($e instanceof base_element_struct_exception); 350 } 351 352 // Add existing element (being grandchild) 353 $grandparent = new mock_base_nested_element('GRANDPARENT'); 354 $parent = new mock_base_nested_element('PARENT'); 355 $child = new mock_base_nested_element('GRANDPARENT'); 356 $parent->add_child($child); 357 try { 358 $grandparent->add_child($parent); 359 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 360 } catch (\Exception $e) { 361 $this->assertTrue($e instanceof base_element_struct_exception); 362 } 363 364 // Add existing element (being cousin) 365 $grandparent = new mock_base_nested_element('GRANDPARENT'); 366 $parent1 = new mock_base_nested_element('PARENT1'); 367 $parent2 = new mock_base_nested_element('PARENT2'); 368 $child1 = new mock_base_nested_element('CHILD1'); 369 $child2 = new mock_base_nested_element('CHILD1'); 370 $grandparent->add_child($parent1); 371 $parent1->add_child($child1); 372 $parent2->add_child($child2); 373 try { 374 $grandparent->add_child($parent2); 375 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 376 } catch (\Exception $e) { 377 $this->assertTrue($e instanceof base_element_struct_exception); 378 } 379 380 // Add element to two parents 381 $parent1 = new mock_base_nested_element('PARENT1'); 382 $parent2 = new mock_base_nested_element('PARENT2'); 383 $child = new mock_base_nested_element('CHILD'); 384 $parent1->add_child($child); 385 try { 386 $parent2->add_child($child); 387 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 388 } catch (\Exception $e) { 389 $this->assertTrue($e instanceof base_element_parent_exception); 390 } 391 392 // Add child element already used by own final elements 393 $nested = new mock_base_nested_element('PARENT1', null, array('FINAL1', 'FINAL2')); 394 $child = new mock_base_nested_element('FINAL2', null, array('FINAL3', 'FINAL4')); 395 try { 396 $nested->add_child($child); 397 $this->fail("Expecting base_element_struct_exception exception, none occurred"); 398 } catch (\Exception $e) { 399 $this->assertTrue($e instanceof base_element_struct_exception); 400 $this->assertEquals($e->errorcode, 'baseelementchildnameconflict'); 401 $this->assertEquals($e->a, 'FINAL2'); 402 } 403 } 404 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body