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