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