~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/libs/model/behaviors/tree.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * TreeBehaviorTest file
 
4
 *
 
5
 * Holds several Test Cases
 
6
 *
 
7
 * PHP versions 4 and 5
 
8
 *
 
9
 * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
 
10
 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
11
 *
 
12
 *  Licensed under The Open Group Test Suite License
 
13
 *  Redistributions of files must retain the above copyright notice.
 
14
 *
 
15
 * @copyright     Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
16
 * @link          http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
 
17
 * @package       cake
 
18
 * @subpackage    cake.tests.cases.libs.model.behaviors
 
19
 * @since         CakePHP(tm) v 1.2.0.5330
 
20
 * @license       http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
 
21
 */
 
22
App::import('Core', array('AppModel', 'Model'));
 
23
require_once(dirname(dirname(__FILE__)) . DS . 'models.php');
 
24
 
 
25
/**
 
26
 * NumberTreeTest class
 
27
 *
 
28
 * @package       cake
 
29
 * @subpackage    cake.tests.cases.libs.model.behaviors
 
30
 */
 
31
class NumberTreeTest extends CakeTestCase {
 
32
 
 
33
/**
 
34
 * settings property
 
35
 *
 
36
 * @var array
 
37
 * @access public
 
38
 */
 
39
        var $settings = array(
 
40
                'modelClass' => 'NumberTree',
 
41
                'leftField' => 'lft',
 
42
                'rightField' => 'rght',
 
43
                'parentField' => 'parent_id'
 
44
        );
 
45
 
 
46
/**
 
47
 * fixtures property
 
48
 *
 
49
 * @var array
 
50
 * @access public
 
51
 */
 
52
        var $fixtures = array('core.number_tree');
 
53
 
 
54
/**
 
55
 * testInitialize method
 
56
 *
 
57
 * @access public
 
58
 * @return void
 
59
 */
 
60
        function testInitialize() {
 
61
                extract($this->settings);
 
62
                $this->Tree =& new $modelClass();
 
63
                $this->Tree->initialize(2, 2);
 
64
 
 
65
                $result = $this->Tree->find('count');
 
66
                $this->assertEqual($result, 7);
 
67
 
 
68
                $validTree = $this->Tree->verify();
 
69
                $this->assertIdentical($validTree, true);
 
70
        }
 
71
 
 
72
/**
 
73
 * testDetectInvalidLeft method
 
74
 *
 
75
 * @access public
 
76
 * @return void
 
77
 */
 
78
        function testDetectInvalidLeft() {
 
79
                extract($this->settings);
 
80
                $this->Tree =& new $modelClass();
 
81
                $this->Tree->initialize(2, 2);
 
82
 
 
83
                $result = $this->Tree->findByName('1.1');
 
84
 
 
85
                $save[$modelClass]['id'] = $result[$modelClass]['id'];
 
86
                $save[$modelClass][$leftField] = 0;
 
87
 
 
88
                $this->Tree->save($save);
 
89
                $result = $this->Tree->verify();
 
90
                $this->assertNotIdentical($result, true);
 
91
 
 
92
                $result = $this->Tree->recover();
 
93
                $this->assertIdentical($result, true);
 
94
 
 
95
                $result = $this->Tree->verify();
 
96
                $this->assertIdentical($result, true);
 
97
        }
 
98
 
 
99
/**
 
100
 * testDetectInvalidRight method
 
101
 *
 
102
 * @access public
 
103
 * @return void
 
104
 */
 
105
        function testDetectInvalidRight() {
 
106
                extract($this->settings);
 
107
                $this->Tree =& new $modelClass();
 
108
                $this->Tree->initialize(2, 2);
 
109
 
 
110
                $result = $this->Tree->findByName('1.1');
 
111
 
 
112
                $save[$modelClass]['id'] = $result[$modelClass]['id'];
 
113
                $save[$modelClass][$rightField] = 0;
 
114
 
 
115
                $this->Tree->save($save);
 
116
                $result = $this->Tree->verify();
 
117
                $this->assertNotIdentical($result, true);
 
118
 
 
119
                $result = $this->Tree->recover();
 
120
                $this->assertIdentical($result, true);
 
121
 
 
122
                $result = $this->Tree->verify();
 
123
                $this->assertIdentical($result, true);
 
124
        }
 
125
 
 
126
/**
 
127
 * testDetectInvalidParent method
 
128
 *
 
129
 * @access public
 
130
 * @return void
 
131
 */
 
132
        function testDetectInvalidParent() {
 
133
                extract($this->settings);
 
134
                $this->Tree =& new $modelClass();
 
135
                $this->Tree->initialize(2, 2);
 
136
 
 
137
                $result = $this->Tree->findByName('1.1');
 
138
 
 
139
                // Bypass behavior and any other logic
 
140
                $this->Tree->updateAll(array($parentField => null), array('id' => $result[$modelClass]['id']));
 
141
 
 
142
                $result = $this->Tree->verify();
 
143
                $this->assertNotIdentical($result, true);
 
144
 
 
145
                $result = $this->Tree->recover();
 
146
                $this->assertIdentical($result, true);
 
147
 
 
148
                $result = $this->Tree->verify();
 
149
                $this->assertIdentical($result, true);
 
150
        }
 
151
 
 
152
/**
 
153
 * testDetectNoneExistantParent method
 
154
 *
 
155
 * @access public
 
156
 * @return void
 
157
 */
 
158
        function testDetectNoneExistantParent() {
 
159
                extract($this->settings);
 
160
                $this->Tree =& new $modelClass();
 
161
                $this->Tree->initialize(2, 2);
 
162
 
 
163
                $result = $this->Tree->findByName('1.1');
 
164
                $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
 
165
 
 
166
                $result = $this->Tree->verify();
 
167
                $this->assertNotIdentical($result, true);
 
168
 
 
169
                $result = $this->Tree->recover('MPTT');
 
170
                $this->assertIdentical($result, true);
 
171
 
 
172
                $result = $this->Tree->verify();
 
173
                $this->assertIdentical($result, true);
 
174
        }
 
175
 
 
176
/**
 
177
 * testRecoverFromMissingParent method
 
178
 *
 
179
 * @access public
 
180
 * @return void
 
181
 */
 
182
        function testRecoverFromMissingParent() {
 
183
                extract($this->settings);
 
184
                $this->Tree =& new $modelClass();
 
185
                $this->Tree->initialize(2, 2);
 
186
 
 
187
                $result = $this->Tree->findByName('1.1');
 
188
                $this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
 
189
 
 
190
                $result = $this->Tree->verify();
 
191
                $this->assertNotIdentical($result, true);
 
192
 
 
193
                $result = $this->Tree->recover();
 
194
                $this->assertIdentical($result, true);
 
195
 
 
196
                $result = $this->Tree->verify();
 
197
                $this->assertIdentical($result, true);
 
198
        }
 
199
 
 
200
/**
 
201
 * testDetectInvalidParents method
 
202
 *
 
203
 * @access public
 
204
 * @return void
 
205
 */
 
206
        function testDetectInvalidParents() {
 
207
                extract($this->settings);
 
208
                $this->Tree =& new $modelClass();
 
209
                $this->Tree->initialize(2, 2);
 
210
 
 
211
                $this->Tree->updateAll(array($parentField => null));
 
212
 
 
213
                $result = $this->Tree->verify();
 
214
                $this->assertNotIdentical($result, true);
 
215
 
 
216
                $result = $this->Tree->recover();
 
217
                $this->assertIdentical($result, true);
 
218
 
 
219
                $result = $this->Tree->verify();
 
220
                $this->assertIdentical($result, true);
 
221
        }
 
222
 
 
223
/**
 
224
 * testDetectInvalidLftsRghts method
 
225
 *
 
226
 * @access public
 
227
 * @return void
 
228
 */
 
229
        function testDetectInvalidLftsRghts() {
 
230
                extract($this->settings);
 
231
                $this->Tree =& new $modelClass();
 
232
                $this->Tree->initialize(2, 2);
 
233
 
 
234
                $this->Tree->updateAll(array($leftField => 0, $rightField => 0));
 
235
 
 
236
                $result = $this->Tree->verify();
 
237
                $this->assertNotIdentical($result, true);
 
238
 
 
239
                $this->Tree->recover();
 
240
 
 
241
                $result = $this->Tree->verify();
 
242
                $this->assertIdentical($result, true);
 
243
        }
 
244
 
 
245
/**
 
246
 * Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
 
247
 *
 
248
 * @access public
 
249
 * @return void
 
250
 */
 
251
        function testDetectEqualLftsRghts() {
 
252
                extract($this->settings);
 
253
                $this->Tree =& new $modelClass();
 
254
                $this->Tree->initialize(1, 3);
 
255
 
 
256
                $result = $this->Tree->findByName('1.1');
 
257
                $this->Tree->updateAll(array($rightField => $result[$modelClass][$leftField]), array('id' => $result[$modelClass]['id']));
 
258
                $this->Tree->updateAll(array($leftField => $this->Tree->escapeField($leftField) . ' -1'),
 
259
                        array($leftField . ' >' => $result[$modelClass][$leftField]));
 
260
                $this->Tree->updateAll(array($rightField => $this->Tree->escapeField($rightField) . ' -1'),
 
261
                        array($rightField . ' >' => $result[$modelClass][$leftField]));
 
262
 
 
263
                $result = $this->Tree->verify();
 
264
                $this->assertNotIdentical($result, true);
 
265
 
 
266
                $result = $this->Tree->recover();
 
267
                $this->assertTrue($result);
 
268
 
 
269
                $result = $this->Tree->verify();
 
270
                $this->assertTrue($result);
 
271
        }
 
272
 
 
273
/**
 
274
 * testAddOrphan method
 
275
 *
 
276
 * @access public
 
277
 * @return void
 
278
 */
 
279
        function testAddOrphan() {
 
280
                extract($this->settings);
 
281
                $this->Tree =& new $modelClass();
 
282
                $this->Tree->initialize(2, 2);
 
283
 
 
284
                $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
 
285
                $result = $this->Tree->find(null, array('name', $parentField), $modelClass . '.' . $leftField . ' desc');
 
286
                $expected = array($modelClass => array('name' => 'testAddOrphan', $parentField => null));
 
287
                $this->assertEqual($result, $expected);
 
288
 
 
289
                $validTree = $this->Tree->verify();
 
290
                $this->assertIdentical($validTree, true);
 
291
        }
 
292
 
 
293
/**
 
294
 * testAddMiddle method
 
295
 *
 
296
 * @access public
 
297
 * @return void
 
298
 */
 
299
        function testAddMiddle() {
 
300
                extract($this->settings);
 
301
                $this->Tree =& new $modelClass();
 
302
                $this->Tree->initialize(2, 2);
 
303
 
 
304
                $data= $this->Tree->find(array($modelClass . '.name' => '1.1'), array('id'));
 
305
                $initialCount = $this->Tree->find('count');
 
306
 
 
307
                $this->Tree->create();
 
308
                $result = $this->Tree->save(array($modelClass => array('name' => 'testAddMiddle', $parentField => $data[$modelClass]['id'])));
 
309
                $expected = array_merge(array($modelClass => array('name' => 'testAddMiddle', $parentField => '2')), $result);
 
310
                $this->assertIdentical($result, $expected);
 
311
 
 
312
                $laterCount = $this->Tree->find('count');
 
313
 
 
314
                $laterCount = $this->Tree->find('count');
 
315
                $this->assertEqual($initialCount + 1, $laterCount);
 
316
 
 
317
                $children = $this->Tree->children($data[$modelClass]['id'], true, array('name'));
 
318
                $expects = array(array($modelClass => array('name' => '1.1.1')),
 
319
                        array($modelClass => array('name' => '1.1.2')),
 
320
                        array($modelClass => array('name' => 'testAddMiddle')));
 
321
                $this->assertIdentical($children, $expects);
 
322
 
 
323
                $validTree = $this->Tree->verify();
 
324
                $this->assertIdentical($validTree, true);
 
325
        }
 
326
 
 
327
/**
 
328
 * testAddInvalid method
 
329
 *
 
330
 * @access public
 
331
 * @return void
 
332
 */
 
333
        function testAddInvalid() {
 
334
                extract($this->settings);
 
335
                $this->Tree =& new $modelClass();
 
336
                $this->Tree->initialize(2, 2);
 
337
                $this->Tree->id = null;
 
338
 
 
339
                $initialCount = $this->Tree->find('count');
 
340
                //$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
 
341
 
 
342
                $saveSuccess = $this->Tree->save(array($modelClass => array('name' => 'testAddInvalid', $parentField => 99999)));
 
343
                $this->assertIdentical($saveSuccess, false);
 
344
 
 
345
                $laterCount = $this->Tree->find('count');
 
346
                $this->assertIdentical($initialCount, $laterCount);
 
347
 
 
348
                $validTree = $this->Tree->verify();
 
349
                $this->assertIdentical($validTree, true);
 
350
        }
 
351
 
 
352
/**
 
353
 * testAddNotIndexedByModel method
 
354
 *
 
355
 * @access public
 
356
 * @return void
 
357
 */
 
358
        function testAddNotIndexedByModel() {
 
359
                extract($this->settings);
 
360
                $this->Tree =& new $modelClass();
 
361
                $this->Tree->initialize(2, 2);
 
362
 
 
363
                $this->Tree->save(array('name' => 'testAddNotIndexed', $parentField => null));
 
364
                $result = $this->Tree->find(null, array('name', $parentField), $modelClass . '.' . $leftField . ' desc');
 
365
                $expected = array($modelClass => array('name' => 'testAddNotIndexed', $parentField => null));
 
366
                $this->assertEqual($result, $expected);
 
367
 
 
368
                $validTree = $this->Tree->verify();
 
369
                $this->assertIdentical($validTree, true);
 
370
}
 
371
 
 
372
/**
 
373
 * testMovePromote method
 
374
 *
 
375
 * @access public
 
376
 * @return void
 
377
 */
 
378
        function testMovePromote() {
 
379
                extract($this->settings);
 
380
                $this->Tree =& new $modelClass();
 
381
                $this->Tree->initialize(2, 2);
 
382
                $this->Tree->id = null;
 
383
 
 
384
                $parent = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
 
385
                $parent_id = $parent[$modelClass]['id'];
 
386
 
 
387
                $data = $this->Tree->find(array($modelClass . '.name' => '1.1.1'), array('id'));
 
388
                $this->Tree->id= $data[$modelClass]['id'];
 
389
                $this->Tree->saveField($parentField, $parent_id);
 
390
                $direct = $this->Tree->children($parent_id, true, array('id', 'name', $parentField, $leftField, $rightField));
 
391
                $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
 
392
                        array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
 
393
                        array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
 
394
                $this->assertEqual($direct, $expects);
 
395
                $validTree = $this->Tree->verify();
 
396
                $this->assertIdentical($validTree, true);
 
397
        }
 
398
 
 
399
/**
 
400
 * testMoveWithWhitelist method
 
401
 *
 
402
 * @access public
 
403
 * @return void
 
404
 */
 
405
        function testMoveWithWhitelist() {
 
406
                extract($this->settings);
 
407
                $this->Tree =& new $modelClass();
 
408
                $this->Tree->initialize(2, 2);
 
409
                $this->Tree->id = null;
 
410
 
 
411
                $parent = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
 
412
                $parent_id = $parent[$modelClass]['id'];
 
413
 
 
414
                $data = $this->Tree->find(array($modelClass . '.name' => '1.1.1'), array('id'));
 
415
                $this->Tree->id = $data[$modelClass]['id'];
 
416
                $this->Tree->whitelist = array($parentField, 'name', 'description');
 
417
                $this->Tree->saveField($parentField, $parent_id);
 
418
 
 
419
                $result = $this->Tree->children($parent_id, true, array('id', 'name', $parentField, $leftField, $rightField));
 
420
                $expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
 
421
                        array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
 
422
                        array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
 
423
                $this->assertEqual($result, $expected);
 
424
                $this->assertTrue($this->Tree->verify());
 
425
        }
 
426
 
 
427
/**
 
428
 * testInsertWithWhitelist method
 
429
 *
 
430
 * @access public
 
431
 * @return void
 
432
 */
 
433
        function testInsertWithWhitelist() {
 
434
                extract($this->settings);
 
435
                $this->Tree =& new $modelClass();
 
436
                $this->Tree->initialize(2, 2);
 
437
 
 
438
                $this->Tree->whitelist = array('name', $parentField);
 
439
                $this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
 
440
                $result = $this->Tree->findByName('testAddOrphan', array('name', $parentField, $leftField, $rightField));
 
441
                $expected = array('name' => 'testAddOrphan', $parentField => null, $leftField => '15', $rightField => 16);
 
442
                $this->assertEqual($result[$modelClass], $expected);
 
443
                $this->assertIdentical($this->Tree->verify(), true);
 
444
        }
 
445
 
 
446
/**
 
447
 * testMoveBefore method
 
448
 *
 
449
 * @access public
 
450
 * @return void
 
451
 */
 
452
        function testMoveBefore() {
 
453
                extract($this->settings);
 
454
                $this->Tree =& new $modelClass();
 
455
                $this->Tree->initialize(2, 2);
 
456
                $this->Tree->id = null;
 
457
 
 
458
                $parent = $this->Tree->find(array($modelClass . '.name' => '1.1'));
 
459
                $parent_id = $parent[$modelClass]['id'];
 
460
 
 
461
                $data= $this->Tree->find(array($modelClass . '.name' => '1.2'), array('id'));
 
462
                $this->Tree->id = $data[$modelClass]['id'];
 
463
                $this->Tree->saveField($parentField, $parent_id);
 
464
 
 
465
                $result = $this->Tree->children($parent_id, true, array('name'));
 
466
                $expects = array(array($modelClass => array('name' => '1.1.1')),
 
467
                        array($modelClass => array('name' => '1.1.2')),
 
468
                        array($modelClass => array('name' => '1.2')));
 
469
                $this->assertEqual($result, $expects);
 
470
 
 
471
                $validTree = $this->Tree->verify();
 
472
                $this->assertIdentical($validTree, true);
 
473
        }
 
474
 
 
475
/**
 
476
 * testMoveAfter method
 
477
 *
 
478
 * @access public
 
479
 * @return void
 
480
 */
 
481
        function testMoveAfter() {
 
482
                extract($this->settings);
 
483
                $this->Tree =& new $modelClass();
 
484
                $this->Tree->initialize(2, 2);
 
485
                $this->Tree->id = null;
 
486
 
 
487
                $parent = $this->Tree->find(array($modelClass . '.name' => '1.2'));
 
488
                $parent_id = $parent[$modelClass]['id'];
 
489
 
 
490
                $data= $this->Tree->find(array($modelClass . '.name' => '1.1'), array('id'));
 
491
                $this->Tree->id = $data[$modelClass]['id'];
 
492
                $this->Tree->saveField($parentField, $parent_id);
 
493
 
 
494
                $result = $this->Tree->children($parent_id, true, array('name'));
 
495
                $expects = array(array($modelClass => array('name' => '1.2.1')),
 
496
                        array($modelClass => array('name' => '1.2.2')),
 
497
                        array($modelClass => array('name' => '1.1')));
 
498
                $this->assertEqual($result, $expects);
 
499
 
 
500
                $validTree = $this->Tree->verify();
 
501
                $this->assertIdentical($validTree, true);
 
502
        }
 
503
 
 
504
/**
 
505
 * testMoveDemoteInvalid method
 
506
 *
 
507
 * @access public
 
508
 * @return void
 
509
 */
 
510
        function testMoveDemoteInvalid() {
 
511
                extract($this->settings);
 
512
                $this->Tree =& new $modelClass();
 
513
                $this->Tree->initialize(2, 2);
 
514
                $this->Tree->id = null;
 
515
 
 
516
                $parent = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
 
517
                $parent_id = $parent[$modelClass]['id'];
 
518
 
 
519
                $data = $this->Tree->find(array($modelClass . '.name' => '1.1.1'), array('id'));
 
520
 
 
521
                $expects = $this->Tree->find('all');
 
522
                $before = $this->Tree->read(null, $data[$modelClass]['id']);
 
523
 
 
524
                $this->Tree->id = $parent_id;
 
525
                //$this->expectError('Trying to save a node under itself in TreeBehavior::beforeSave');
 
526
                $this->Tree->saveField($parentField, $data[$modelClass]['id']);
 
527
 
 
528
                $results = $this->Tree->find('all');
 
529
                $after = $this->Tree->read(null, $data[$modelClass]['id']);
 
530
 
 
531
                $this->assertEqual($results, $expects);
 
532
                $this->assertEqual($before, $after);
 
533
 
 
534
                $validTree = $this->Tree->verify();
 
535
                $this->assertIdentical($validTree, true);
 
536
        }
 
537
 
 
538
/**
 
539
 * testMoveInvalid method
 
540
 *
 
541
 * @access public
 
542
 * @return void
 
543
 */
 
544
        function testMoveInvalid() {
 
545
                extract($this->settings);
 
546
                $this->Tree =& new $modelClass();
 
547
                $this->Tree->initialize(2, 2);
 
548
                $this->Tree->id = null;
 
549
 
 
550
                $initialCount = $this->Tree->find('count');
 
551
                $data= $this->Tree->findByName('1.1');
 
552
 
 
553
                //$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
 
554
                $this->Tree->id = $data[$modelClass]['id'];
 
555
                $this->Tree->saveField($parentField, 999999);
 
556
 
 
557
                //$this->assertIdentical($saveSuccess, false);
 
558
                $laterCount = $this->Tree->find('count');
 
559
                $this->assertIdentical($initialCount, $laterCount);
 
560
 
 
561
                $validTree = $this->Tree->verify();
 
562
                $this->assertIdentical($validTree, true);
 
563
        }
 
564
 
 
565
/**
 
566
 * testMoveSelfInvalid method
 
567
 *
 
568
 * @access public
 
569
 * @return void
 
570
 */
 
571
        function testMoveSelfInvalid() {
 
572
                extract($this->settings);
 
573
                $this->Tree =& new $modelClass();
 
574
                $this->Tree->initialize(2, 2);
 
575
                $this->Tree->id = null;
 
576
 
 
577
                $initialCount = $this->Tree->find('count');
 
578
                $data= $this->Tree->findByName('1.1');
 
579
 
 
580
                //$this->expectError('Trying to set a node to be the parent of itself in TreeBehavior::beforeSave');
 
581
                $this->Tree->id = $data[$modelClass]['id'];
 
582
                $saveSuccess = $this->Tree->saveField($parentField, $this->Tree->id);
 
583
 
 
584
                $this->assertIdentical($saveSuccess, false);
 
585
                $laterCount = $this->Tree->find('count');
 
586
                $this->assertIdentical($initialCount, $laterCount);
 
587
 
 
588
                $validTree = $this->Tree->verify();
 
589
                $this->assertIdentical($validTree, true);
 
590
        }
 
591
 
 
592
/**
 
593
 * testMoveUpSuccess method
 
594
 *
 
595
 * @access public
 
596
 * @return void
 
597
 */
 
598
        function testMoveUpSuccess() {
 
599
                extract($this->settings);
 
600
                $this->Tree =& new $modelClass();
 
601
                $this->Tree->initialize(2, 2);
 
602
 
 
603
                $data = $this->Tree->find(array($modelClass . '.name' => '1.2'), array('id'));
 
604
                $this->Tree->moveUp($data[$modelClass]['id']);
 
605
 
 
606
                $parent = $this->Tree->findByName('1. Root', array('id'));
 
607
                $this->Tree->id = $parent[$modelClass]['id'];
 
608
                $result = $this->Tree->children(null, true, array('name'));
 
609
                $expected = array(array($modelClass => array('name' => '1.2', )),
 
610
                        array($modelClass => array('name' => '1.1', )));
 
611
                $this->assertIdentical($result, $expected);
 
612
        }
 
613
 
 
614
/**
 
615
 * testMoveUpFail method
 
616
 *
 
617
 * @access public
 
618
 * @return void
 
619
 */
 
620
        function testMoveUpFail() {
 
621
                extract($this->settings);
 
622
                $this->Tree =& new $modelClass();
 
623
                $this->Tree->initialize(2, 2);
 
624
 
 
625
                $data = $this->Tree->find(array($modelClass . '.name' => '1.1'));
 
626
 
 
627
                $this->Tree->moveUp($data[$modelClass]['id']);
 
628
 
 
629
                $parent = $this->Tree->findByName('1. Root', array('id'));
 
630
                $this->Tree->id = $parent[$modelClass]['id'];
 
631
                $result = $this->Tree->children(null, true, array('name'));
 
632
                $expected = array(array($modelClass => array('name' => '1.1', )),
 
633
                        array($modelClass => array('name' => '1.2', )));
 
634
                $this->assertIdentical($result, $expected);
 
635
        }
 
636
 
 
637
/**
 
638
 * testMoveUp2 method
 
639
 *
 
640
 * @access public
 
641
 * @return void
 
642
 */
 
643
        function testMoveUp2() {
 
644
                extract($this->settings);
 
645
                $this->Tree =& new $modelClass();
 
646
                $this->Tree->initialize(1, 10);
 
647
 
 
648
                $data = $this->Tree->find(array($modelClass . '.name' => '1.5'), array('id'));
 
649
                $this->Tree->moveUp($data[$modelClass]['id'], 2);
 
650
 
 
651
                $parent = $this->Tree->findByName('1. Root', array('id'));
 
652
                $this->Tree->id = $parent[$modelClass]['id'];
 
653
                $result = $this->Tree->children(null, true, array('name'));
 
654
                $expected = array(
 
655
                        array($modelClass => array('name' => '1.1', )),
 
656
                        array($modelClass => array('name' => '1.2', )),
 
657
                        array($modelClass => array('name' => '1.5', )),
 
658
                        array($modelClass => array('name' => '1.3', )),
 
659
                        array($modelClass => array('name' => '1.4', )),
 
660
                        array($modelClass => array('name' => '1.6', )),
 
661
                        array($modelClass => array('name' => '1.7', )),
 
662
                        array($modelClass => array('name' => '1.8', )),
 
663
                        array($modelClass => array('name' => '1.9', )),
 
664
                        array($modelClass => array('name' => '1.10', )));
 
665
                $this->assertIdentical($result, $expected);
 
666
        }
 
667
 
 
668
/**
 
669
 * testMoveUpFirst method
 
670
 *
 
671
 * @access public
 
672
 * @return void
 
673
 */
 
674
        function testMoveUpFirst() {
 
675
                extract($this->settings);
 
676
                $this->Tree =& new $modelClass();
 
677
                $this->Tree->initialize(1, 10);
 
678
 
 
679
                $data = $this->Tree->find(array($modelClass . '.name' => '1.5'), array('id'));
 
680
                $this->Tree->moveUp($data[$modelClass]['id'], true);
 
681
 
 
682
                $parent = $this->Tree->findByName('1. Root', array('id'));
 
683
                $this->Tree->id = $parent[$modelClass]['id'];
 
684
                $result = $this->Tree->children(null, true, array('name'));
 
685
                $expected = array(
 
686
                        array($modelClass => array('name' => '1.5', )),
 
687
                        array($modelClass => array('name' => '1.1', )),
 
688
                        array($modelClass => array('name' => '1.2', )),
 
689
                        array($modelClass => array('name' => '1.3', )),
 
690
                        array($modelClass => array('name' => '1.4', )),
 
691
                        array($modelClass => array('name' => '1.6', )),
 
692
                        array($modelClass => array('name' => '1.7', )),
 
693
                        array($modelClass => array('name' => '1.8', )),
 
694
                        array($modelClass => array('name' => '1.9', )),
 
695
                        array($modelClass => array('name' => '1.10', )));
 
696
                $this->assertIdentical($result, $expected);
 
697
        }
 
698
 
 
699
/**
 
700
 * testMoveDownSuccess method
 
701
 *
 
702
 * @access public
 
703
 * @return void
 
704
 */
 
705
        function testMoveDownSuccess() {
 
706
                extract($this->settings);
 
707
                $this->Tree =& new $modelClass();
 
708
                $this->Tree->initialize(2, 2);
 
709
 
 
710
                $data = $this->Tree->find(array($modelClass . '.name' => '1.1'), array('id'));
 
711
                $this->Tree->moveDown($data[$modelClass]['id']);
 
712
 
 
713
                $parent = $this->Tree->findByName('1. Root', array('id'));
 
714
                $this->Tree->id = $parent[$modelClass]['id'];
 
715
                $result = $this->Tree->children(null, true, array('name'));
 
716
                $expected = array(array($modelClass => array('name' => '1.2', )),
 
717
                        array($modelClass => array('name' => '1.1', )));
 
718
                $this->assertIdentical($result, $expected);
 
719
        }
 
720
 
 
721
/**
 
722
 * testMoveDownFail method
 
723
 *
 
724
 * @access public
 
725
 * @return void
 
726
 */
 
727
        function testMoveDownFail() {
 
728
                extract($this->settings);
 
729
                $this->Tree =& new $modelClass();
 
730
                $this->Tree->initialize(2, 2);
 
731
 
 
732
                $data = $this->Tree->find(array($modelClass . '.name' => '1.2'));
 
733
                $this->Tree->moveDown($data[$modelClass]['id']);
 
734
 
 
735
                $parent = $this->Tree->findByName('1. Root', array('id'));
 
736
                $this->Tree->id = $parent[$modelClass]['id'];
 
737
                $result = $this->Tree->children(null, true, array('name'));
 
738
                $expected = array(array($modelClass => array('name' => '1.1', )),
 
739
                        array($modelClass => array('name' => '1.2', )));
 
740
                $this->assertIdentical($result, $expected);
 
741
        }
 
742
 
 
743
/**
 
744
 * testMoveDownLast method
 
745
 *
 
746
 * @access public
 
747
 * @return void
 
748
 */
 
749
        function testMoveDownLast() {
 
750
                extract($this->settings);
 
751
                $this->Tree =& new $modelClass();
 
752
                $this->Tree->initialize(1, 10);
 
753
 
 
754
                $data = $this->Tree->find(array($modelClass . '.name' => '1.5'), array('id'));
 
755
                $this->Tree->moveDown($data[$modelClass]['id'], true);
 
756
 
 
757
                $parent = $this->Tree->findByName('1. Root', array('id'));
 
758
                $this->Tree->id = $parent[$modelClass]['id'];
 
759
                $result = $this->Tree->children(null, true, array('name'));
 
760
                $expected = array(
 
761
                        array($modelClass => array('name' => '1.1', )),
 
762
                        array($modelClass => array('name' => '1.2', )),
 
763
                        array($modelClass => array('name' => '1.3', )),
 
764
                        array($modelClass => array('name' => '1.4', )),
 
765
                        array($modelClass => array('name' => '1.6', )),
 
766
                        array($modelClass => array('name' => '1.7', )),
 
767
                        array($modelClass => array('name' => '1.8', )),
 
768
                        array($modelClass => array('name' => '1.9', )),
 
769
                        array($modelClass => array('name' => '1.10', )),
 
770
                        array($modelClass => array('name' => '1.5', )));
 
771
                $this->assertIdentical($result, $expected);
 
772
        }
 
773
 
 
774
/**
 
775
 * testMoveDown2 method
 
776
 *
 
777
 * @access public
 
778
 * @return void
 
779
 */
 
780
        function testMoveDown2() {
 
781
                extract($this->settings);
 
782
                $this->Tree =& new $modelClass();
 
783
                $this->Tree->initialize(1, 10);
 
784
 
 
785
                $data = $this->Tree->find(array($modelClass . '.name' => '1.5'), array('id'));
 
786
                $this->Tree->moveDown($data[$modelClass]['id'], 2);
 
787
 
 
788
                $parent = $this->Tree->findByName('1. Root', array('id'));
 
789
                $this->Tree->id = $parent[$modelClass]['id'];
 
790
                $result = $this->Tree->children(null, true, array('name'));
 
791
                $expected = array(
 
792
                        array($modelClass => array('name' => '1.1', )),
 
793
                        array($modelClass => array('name' => '1.2', )),
 
794
                        array($modelClass => array('name' => '1.3', )),
 
795
                        array($modelClass => array('name' => '1.4', )),
 
796
                        array($modelClass => array('name' => '1.6', )),
 
797
                        array($modelClass => array('name' => '1.7', )),
 
798
                        array($modelClass => array('name' => '1.5', )),
 
799
                        array($modelClass => array('name' => '1.8', )),
 
800
                        array($modelClass => array('name' => '1.9', )),
 
801
                        array($modelClass => array('name' => '1.10', )));
 
802
                $this->assertIdentical($result, $expected);
 
803
        }
 
804
 
 
805
/**
 
806
 * testSaveNoMove method
 
807
 *
 
808
 * @access public
 
809
 * @return void
 
810
 */
 
811
        function testSaveNoMove() {
 
812
                extract($this->settings);
 
813
                $this->Tree =& new $modelClass();
 
814
                $this->Tree->initialize(1, 10);
 
815
 
 
816
                $data = $this->Tree->find(array($modelClass . '.name' => '1.5'), array('id'));
 
817
                $this->Tree->id = $data[$modelClass]['id'];
 
818
                $this->Tree->saveField('name', 'renamed');
 
819
                $parent = $this->Tree->findByName('1. Root', array('id'));
 
820
                $this->Tree->id = $parent[$modelClass]['id'];
 
821
                $result = $this->Tree->children(null, true, array('name'));
 
822
                $expected = array(
 
823
                        array($modelClass => array('name' => '1.1', )),
 
824
                        array($modelClass => array('name' => '1.2', )),
 
825
                        array($modelClass => array('name' => '1.3', )),
 
826
                        array($modelClass => array('name' => '1.4', )),
 
827
                        array($modelClass => array('name' => 'renamed', )),
 
828
                        array($modelClass => array('name' => '1.6', )),
 
829
                        array($modelClass => array('name' => '1.7', )),
 
830
                        array($modelClass => array('name' => '1.8', )),
 
831
                        array($modelClass => array('name' => '1.9', )),
 
832
                        array($modelClass => array('name' => '1.10', )));
 
833
                $this->assertIdentical($result, $expected);
 
834
        }
 
835
 
 
836
/**
 
837
 * testMoveToRootAndMoveUp method
 
838
 *
 
839
 * @access public
 
840
 * @return void
 
841
 */
 
842
        function testMoveToRootAndMoveUp() {
 
843
                extract($this->settings);
 
844
                $this->Tree =& new $modelClass();
 
845
                $this->Tree->initialize(1, 1);
 
846
                $data = $this->Tree->find(array($modelClass . '.name' => '1.1'), array('id'));
 
847
                $this->Tree->id = $data[$modelClass]['id'];
 
848
                $this->Tree->save(array($parentField => null));
 
849
 
 
850
                $result = $this->Tree->verify();
 
851
                $this->assertIdentical($result, true);
 
852
 
 
853
                $this->Tree->moveup();
 
854
 
 
855
                $result = $this->Tree->find('all', array('fields' => 'name', 'order' => $modelClass . '.' . $leftField . ' ASC'));
 
856
                $expected = array(array($modelClass => array('name' => '1.1')),
 
857
                        array($modelClass => array('name' => '1. Root')));
 
858
                $this->assertIdentical($result, $expected);
 
859
        }
 
860
 
 
861
/**
 
862
 * testDelete method
 
863
 *
 
864
 * @access public
 
865
 * @return void
 
866
 */
 
867
        function testDelete() {
 
868
                extract($this->settings);
 
869
                $this->Tree =& new $modelClass();
 
870
                $this->Tree->initialize(2, 2);
 
871
 
 
872
                $initialCount = $this->Tree->find('count');
 
873
                $result = $this->Tree->findByName('1.1.1');
 
874
 
 
875
                $return = $this->Tree->delete($result[$modelClass]['id']);
 
876
                $this->assertEqual($return, true);
 
877
 
 
878
                $laterCount = $this->Tree->find('count');
 
879
                $this->assertEqual($initialCount - 1, $laterCount);
 
880
 
 
881
                $validTree= $this->Tree->verify();
 
882
                $this->assertIdentical($validTree, true);
 
883
 
 
884
                $initialCount = $this->Tree->find('count');
 
885
                $result= $this->Tree->findByName('1.1');
 
886
 
 
887
                $return = $this->Tree->delete($result[$modelClass]['id']);
 
888
                $this->assertEqual($return, true);
 
889
 
 
890
                $laterCount = $this->Tree->find('count');
 
891
                $this->assertEqual($initialCount - 2, $laterCount);
 
892
 
 
893
                $validTree = $this->Tree->verify();
 
894
                $this->assertIdentical($validTree, true);
 
895
        }
 
896
 
 
897
/**
 
898
 * testRemove method
 
899
 *
 
900
 * @access public
 
901
 * @return void
 
902
 */
 
903
        function testRemove() {
 
904
                extract($this->settings);
 
905
                $this->Tree =& new $modelClass();
 
906
                $this->Tree->initialize(2, 2);
 
907
                $initialCount = $this->Tree->find('count');
 
908
                $result = $this->Tree->findByName('1.1');
 
909
 
 
910
                $this->Tree->removeFromTree($result[$modelClass]['id']);
 
911
 
 
912
                $laterCount = $this->Tree->find('count');
 
913
                $this->assertEqual($initialCount, $laterCount);
 
914
 
 
915
                $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'));
 
916
                $expects = array(array($modelClass => array('name' => '1.1.1')),
 
917
                        array($modelClass => array('name' => '1.1.2')),
 
918
                        array($modelClass => array('name' => '1.2')));
 
919
                $this->assertEqual($children, $expects);
 
920
 
 
921
                $topNodes = $this->Tree->children(false, true,array('name'));
 
922
                $expects = array(array($modelClass => array('name' => '1. Root')),
 
923
                        array($modelClass => array('name' => '1.1')));
 
924
                $this->assertEqual($topNodes, $expects);
 
925
 
 
926
                $validTree = $this->Tree->verify();
 
927
                $this->assertIdentical($validTree, true);
 
928
        }
 
929
 
 
930
/**
 
931
 * testRemoveLastTopParent method
 
932
 *
 
933
 * @access public
 
934
 * @return void
 
935
 */
 
936
        function testRemoveLastTopParent() {
 
937
                extract($this->settings);
 
938
                $this->Tree =& new $modelClass();
 
939
                $this->Tree->initialize(2, 2);
 
940
 
 
941
                $initialCount = $this->Tree->find('count');
 
942
                $initialTopNodes = $this->Tree->childCount(false);
 
943
 
 
944
                $result = $this->Tree->findByName('1. Root');
 
945
                $this->Tree->removeFromTree($result[$modelClass]['id']);
 
946
 
 
947
                $laterCount = $this->Tree->find('count');
 
948
                $laterTopNodes = $this->Tree->childCount(false);
 
949
 
 
950
                $this->assertEqual($initialCount, $laterCount);
 
951
                $this->assertEqual($initialTopNodes, $laterTopNodes);
 
952
 
 
953
                $topNodes = $this->Tree->children(false, true,array('name'));
 
954
                $expects = array(array($modelClass => array('name' => '1.1')),
 
955
                        array($modelClass => array('name' => '1.2')),
 
956
                        array($modelClass => array('name' => '1. Root')));
 
957
 
 
958
                $this->assertEqual($topNodes, $expects);
 
959
 
 
960
                $validTree = $this->Tree->verify();
 
961
                $this->assertIdentical($validTree, true);
 
962
        }
 
963
 
 
964
/**
 
965
 * testRemoveNoChildren method
 
966
 *
 
967
 * @return void
 
968
 * @access public
 
969
 */
 
970
        function testRemoveNoChildren() {
 
971
                extract($this->settings);
 
972
                $this->Tree =& new $modelClass();
 
973
                $this->Tree->initialize(2, 2);
 
974
                $initialCount = $this->Tree->find('count');
 
975
 
 
976
                $result = $this->Tree->findByName('1.1.1');
 
977
                $this->Tree->removeFromTree($result[$modelClass]['id']);
 
978
 
 
979
                $laterCount = $this->Tree->find('count');
 
980
                $this->assertEqual($initialCount, $laterCount);
 
981
 
 
982
                $nodes = $this->Tree->find('list', array('order' => $leftField));
 
983
                $expects = array(
 
984
                        1 => '1. Root',
 
985
                        2 => '1.1',
 
986
                        4 => '1.1.2',
 
987
                        5 => '1.2',
 
988
                        6 => '1.2.1',
 
989
                        7 => '1.2.2',
 
990
                        3 => '1.1.1',
 
991
                );
 
992
 
 
993
                $this->assertEqual($nodes, $expects);
 
994
 
 
995
                $validTree = $this->Tree->verify();
 
996
                $this->assertIdentical($validTree, true);
 
997
        }
 
998
 
 
999
/**
 
1000
 * testRemoveAndDelete method
 
1001
 *
 
1002
 * @access public
 
1003
 * @return void
 
1004
 */
 
1005
        function testRemoveAndDelete() {
 
1006
                extract($this->settings);
 
1007
                $this->Tree =& new $modelClass();
 
1008
                $this->Tree->initialize(2, 2);
 
1009
 
 
1010
                $initialCount = $this->Tree->find('count');
 
1011
                $result = $this->Tree->findByName('1.1');
 
1012
 
 
1013
                $this->Tree->removeFromTree($result[$modelClass]['id'], true);
 
1014
 
 
1015
                $laterCount = $this->Tree->find('count');
 
1016
                $this->assertEqual($initialCount-1, $laterCount);
 
1017
 
 
1018
                $children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'), $leftField . ' asc');
 
1019
                $expects= array(array($modelClass => array('name' => '1.1.1')),
 
1020
                        array($modelClass => array('name' => '1.1.2')),
 
1021
                        array($modelClass => array('name' => '1.2')));
 
1022
                $this->assertEqual($children, $expects);
 
1023
 
 
1024
                $topNodes = $this->Tree->children(false, true,array('name'));
 
1025
                $expects = array(array($modelClass => array('name' => '1. Root')));
 
1026
                $this->assertEqual($topNodes, $expects);
 
1027
 
 
1028
                $validTree = $this->Tree->verify();
 
1029
                $this->assertIdentical($validTree, true);
 
1030
        }
 
1031
 
 
1032
/**
 
1033
 * testRemoveAndDeleteNoChildren method
 
1034
 *
 
1035
 * @return void
 
1036
 * @access public
 
1037
 */
 
1038
        function testRemoveAndDeleteNoChildren() {
 
1039
                extract($this->settings);
 
1040
                $this->Tree =& new $modelClass();
 
1041
                $this->Tree->initialize(2, 2);
 
1042
                $initialCount = $this->Tree->find('count');
 
1043
 
 
1044
                $result = $this->Tree->findByName('1.1.1');
 
1045
                $this->Tree->removeFromTree($result[$modelClass]['id'], true);
 
1046
 
 
1047
                $laterCount = $this->Tree->find('count');
 
1048
                $this->assertEqual($initialCount - 1, $laterCount);
 
1049
 
 
1050
                $nodes = $this->Tree->find('list', array('order' => $leftField));
 
1051
                $expects = array(
 
1052
                        1 => '1. Root',
 
1053
                        2 => '1.1',
 
1054
                        4 => '1.1.2',
 
1055
                        5 => '1.2',
 
1056
                        6 => '1.2.1',
 
1057
                        7 => '1.2.2',
 
1058
                );
 
1059
                $this->assertEqual($nodes, $expects);
 
1060
 
 
1061
                $validTree = $this->Tree->verify();
 
1062
                $this->assertIdentical($validTree, true);
 
1063
        }
 
1064
 
 
1065
/**
 
1066
 * testChildren method
 
1067
 *
 
1068
 * @access public
 
1069
 * @return void
 
1070
 */
 
1071
        function testChildren() {
 
1072
                extract($this->settings);
 
1073
                $this->Tree =& new $modelClass();
 
1074
                $this->Tree->initialize(2, 2);
 
1075
 
 
1076
                $data = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
 
1077
                $this->Tree->id= $data[$modelClass]['id'];
 
1078
 
 
1079
                $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
 
1080
                $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
 
1081
                        array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
 
1082
                $this->assertEqual($direct, $expects);
 
1083
 
 
1084
                $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
 
1085
                $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
 
1086
                        array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
 
1087
                        array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
 
1088
                        array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
 
1089
                        array($modelClass => array( 'id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
 
1090
                        array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12)));
 
1091
                $this->assertEqual($total, $expects);
 
1092
 
 
1093
                $this->assertEqual(array(), $this->Tree->children(10000));
 
1094
        }
 
1095
 
 
1096
/**
 
1097
 * testCountChildren method
 
1098
 *
 
1099
 * @access public
 
1100
 * @return void
 
1101
 */
 
1102
        function testCountChildren() {
 
1103
                extract($this->settings);
 
1104
                $this->Tree =& new $modelClass();
 
1105
                $this->Tree->initialize(2, 2);
 
1106
 
 
1107
                $data = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
 
1108
                $this->Tree->id = $data[$modelClass]['id'];
 
1109
 
 
1110
                $direct = $this->Tree->childCount(null, true);
 
1111
                $this->assertEqual($direct, 2);
 
1112
 
 
1113
                $total = $this->Tree->childCount();
 
1114
                $this->assertEqual($total, 6);
 
1115
        }
 
1116
 
 
1117
/**
 
1118
 * testGetParentNode method
 
1119
 *
 
1120
 * @access public
 
1121
 * @return void
 
1122
 */
 
1123
        function testGetParentNode() {
 
1124
                extract($this->settings);
 
1125
                $this->Tree =& new $modelClass();
 
1126
                $this->Tree->initialize(2, 2);
 
1127
 
 
1128
                $data = $this->Tree->find(array($modelClass . '.name' => '1.2.2'));
 
1129
                $this->Tree->id= $data[$modelClass]['id'];
 
1130
 
 
1131
                $result = $this->Tree->getparentNode(null, array('name'));
 
1132
                $expects = array($modelClass => array('name' => '1.2'));
 
1133
                $this->assertIdentical($result, $expects);
 
1134
        }
 
1135
 
 
1136
/**
 
1137
 * testGetPath method
 
1138
 *
 
1139
 * @access public
 
1140
 * @return void
 
1141
 */
 
1142
        function testGetPath() {
 
1143
                extract($this->settings);
 
1144
                $this->Tree =& new $modelClass();
 
1145
                $this->Tree->initialize(2, 2);
 
1146
 
 
1147
                $data = $this->Tree->find(array($modelClass . '.name' => '1.2.2'));
 
1148
                $this->Tree->id= $data[$modelClass]['id'];
 
1149
 
 
1150
                $result = $this->Tree->getPath(null, array('name'));
 
1151
                $expects = array(array($modelClass => array('name' => '1. Root')),
 
1152
                        array($modelClass => array('name' => '1.2')),
 
1153
                        array($modelClass => array('name' => '1.2.2')));
 
1154
                $this->assertIdentical($result, $expects);
 
1155
        }
 
1156
 
 
1157
/**
 
1158
 * testNoAmbiguousColumn method
 
1159
 *
 
1160
 * @access public
 
1161
 * @return void
 
1162
 */
 
1163
        function testNoAmbiguousColumn() {
 
1164
                extract($this->settings);
 
1165
                $this->Tree =& new $modelClass();
 
1166
                $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
 
1167
                        array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
 
1168
                $this->Tree->initialize(2, 2);
 
1169
 
 
1170
                $data = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
 
1171
                $this->Tree->id= $data[$modelClass]['id'];
 
1172
 
 
1173
                $direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
 
1174
                $expects = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
 
1175
                        array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
 
1176
                $this->assertEqual($direct, $expects);
 
1177
 
 
1178
                $total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
 
1179
                $expects = array(
 
1180
                        array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
 
1181
                        array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
 
1182
                        array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
 
1183
                        array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
 
1184
                        array($modelClass => array( 'id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
 
1185
                        array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12))
 
1186
                );
 
1187
                $this->assertEqual($total, $expects);
 
1188
        }
 
1189
 
 
1190
/**
 
1191
 * testReorderTree method
 
1192
 *
 
1193
 * @access public
 
1194
 * @return void
 
1195
 */
 
1196
        function testReorderTree() {
 
1197
                extract($this->settings);
 
1198
                $this->Tree =& new $modelClass();
 
1199
                $this->Tree->initialize(3, 3);
 
1200
                $nodes = $this->Tree->find('list', array('order' => $leftField));
 
1201
 
 
1202
                $data = $this->Tree->find(array($modelClass . '.name' => '1.1'), array('id'));
 
1203
                $this->Tree->moveDown($data[$modelClass]['id']);
 
1204
 
 
1205
                $data = $this->Tree->find(array($modelClass . '.name' => '1.2.1'), array('id'));
 
1206
                $this->Tree->moveDown($data[$modelClass]['id']);
 
1207
 
 
1208
                $data = $this->Tree->find(array($modelClass . '.name' => '1.3.2.2'), array('id'));
 
1209
                $this->Tree->moveDown($data[$modelClass]['id']);
 
1210
 
 
1211
                $unsortedNodes = $this->Tree->find('list', array('order' => $leftField));
 
1212
                $this->assertNotIdentical($nodes, $unsortedNodes);
 
1213
 
 
1214
                $this->Tree->reorder();
 
1215
                $sortedNodes = $this->Tree->find('list', array('order' => $leftField));
 
1216
                $this->assertIdentical($nodes, $sortedNodes);
 
1217
        }
 
1218
 
 
1219
/**
 
1220
 * test reordering large-ish trees with cacheQueries = true.
 
1221
 * This caused infinite loops when moving down elements as stale data is returned
 
1222
 * from the memory cache
 
1223
 *
 
1224
 * @access public
 
1225
 * @return void
 
1226
 */
 
1227
        function testReorderBigTreeWithQueryCaching() {
 
1228
                extract($this->settings);
 
1229
                $this->Tree =& new $modelClass();
 
1230
                $this->Tree->initialize(2, 10);
 
1231
 
 
1232
                $original = $this->Tree->cacheQueries;
 
1233
                $this->Tree->cacheQueries = true;
 
1234
                $this->Tree->reorder(array('field' => 'name', 'direction' => 'DESC'));
 
1235
                $this->assertTrue($this->Tree->cacheQueries, 'cacheQueries was not restored after reorder(). %s');
 
1236
                $this->Tree->cacheQueries = $original;
 
1237
        }
 
1238
/**
 
1239
 * testGenerateTreeListWithSelfJoin method
 
1240
 *
 
1241
 * @access public
 
1242
 * @return void
 
1243
 */
 
1244
        function testGenerateTreeListWithSelfJoin() {
 
1245
                extract($this->settings);
 
1246
                $this->Tree =& new $modelClass();
 
1247
                $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
 
1248
                        array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
 
1249
                $this->Tree->initialize(2, 2);
 
1250
 
 
1251
                $result = $this->Tree->generateTreeList();
 
1252
                $expected = array(1 => '1. Root', 2 => '_1.1', 3 => '__1.1.1', 4 => '__1.1.2', 5 => '_1.2', 6 => '__1.2.1', 7 => '__1.2.2');
 
1253
                $this->assertIdentical($result, $expected);
 
1254
        }
 
1255
 
 
1256
/**
 
1257
 * testArraySyntax method
 
1258
 *
 
1259
 * @access public
 
1260
 * @return void
 
1261
 */
 
1262
        function testArraySyntax() {
 
1263
                extract($this->settings);
 
1264
                $this->Tree =& new $modelClass();
 
1265
                $this->Tree->initialize(3, 3);
 
1266
                $this->assertIdentical($this->Tree->childCount(2), $this->Tree->childCount(array('id' => 2)));
 
1267
                $this->assertIdentical($this->Tree->getParentNode(2), $this->Tree->getParentNode(array('id' => 2)));
 
1268
                $this->assertIdentical($this->Tree->getPath(4), $this->Tree->getPath(array('id' => 4)));
 
1269
        }
 
1270
}
 
1271
 
 
1272
/**
 
1273
 * ScopedTreeTest class
 
1274
 *
 
1275
 * @package       cake
 
1276
 * @subpackage    cake.tests.cases.libs.model.behaviors
 
1277
 */
 
1278
class ScopedTreeTest extends NumberTreeTest {
 
1279
 
 
1280
/**
 
1281
 * settings property
 
1282
 *
 
1283
 * @var array
 
1284
 * @access public
 
1285
 */
 
1286
        var $settings = array(
 
1287
                'modelClass' => 'FlagTree',
 
1288
                'leftField' => 'lft',
 
1289
                'rightField' => 'rght',
 
1290
                'parentField' => 'parent_id'
 
1291
        );
 
1292
 
 
1293
/**
 
1294
 * fixtures property
 
1295
 *
 
1296
 * @var array
 
1297
 * @access public
 
1298
 */
 
1299
        var $fixtures = array('core.flag_tree', 'core.ad', 'core.campaign', 'core.translate', 'core.number_tree_two');
 
1300
 
 
1301
/**
 
1302
 * testStringScope method
 
1303
 *
 
1304
 * @access public
 
1305
 * @return void
 
1306
 */
 
1307
        function testStringScope() {
 
1308
                $this->Tree =& new FlagTree();
 
1309
                $this->Tree->initialize(2, 3);
 
1310
 
 
1311
                $this->Tree->id = 1;
 
1312
                $this->Tree->saveField('flag', 1);
 
1313
                $this->Tree->id = 2;
 
1314
                $this->Tree->saveField('flag', 1);
 
1315
 
 
1316
                $result = $this->Tree->children();
 
1317
                $expected = array(
 
1318
                        array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
 
1319
                        array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
 
1320
                        array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
 
1321
                );
 
1322
                $this->assertEqual($result, $expected);
 
1323
 
 
1324
                $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
 
1325
                $this->assertEqual($this->Tree->children(), array());
 
1326
 
 
1327
                $this->Tree->id = 1;
 
1328
                $this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
 
1329
 
 
1330
                $result = $this->Tree->children();
 
1331
                $expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
 
1332
                $this->assertEqual($result, $expected);
 
1333
 
 
1334
                $this->assertTrue($this->Tree->delete());
 
1335
                $this->assertEqual($this->Tree->find('count'), 11);
 
1336
        }
 
1337
 
 
1338
/**
 
1339
 * testArrayScope method
 
1340
 *
 
1341
 * @access public
 
1342
 * @return void
 
1343
 */
 
1344
        function testArrayScope() {
 
1345
                $this->Tree =& new FlagTree();
 
1346
                $this->Tree->initialize(2, 3);
 
1347
 
 
1348
                $this->Tree->id = 1;
 
1349
                $this->Tree->saveField('flag', 1);
 
1350
                $this->Tree->id = 2;
 
1351
                $this->Tree->saveField('flag', 1);
 
1352
 
 
1353
                $result = $this->Tree->children();
 
1354
                $expected = array(
 
1355
                        array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
 
1356
                        array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
 
1357
                        array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
 
1358
                );
 
1359
                $this->assertEqual($result, $expected);
 
1360
 
 
1361
                $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
 
1362
                $this->assertEqual($this->Tree->children(), array());
 
1363
 
 
1364
                $this->Tree->id = 1;
 
1365
                $this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
 
1366
 
 
1367
                $result = $this->Tree->children();
 
1368
                $expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
 
1369
                $this->assertEqual($result, $expected);
 
1370
 
 
1371
                $this->assertTrue($this->Tree->delete());
 
1372
                $this->assertEqual($this->Tree->find('count'), 11);
 
1373
        }
 
1374
 
 
1375
/**
 
1376
 * testMoveUpWithScope method
 
1377
 *
 
1378
 * @access public
 
1379
 * @return void
 
1380
 */
 
1381
        function testMoveUpWithScope() {
 
1382
                $this->Ad =& new Ad();
 
1383
                $this->Ad->Behaviors->attach('Tree', array('scope'=>'Campaign'));
 
1384
                $this->Ad->moveUp(6);
 
1385
 
 
1386
                $this->Ad->id = 4;
 
1387
                $result = $this->Ad->children();
 
1388
                $this->assertEqual(Set::extract('/Ad/id', $result), array(6, 5));
 
1389
                $this->assertEqual(Set::extract('/Campaign/id', $result), array(2, 2));
 
1390
        }
 
1391
 
 
1392
/**
 
1393
 * testMoveDownWithScope method
 
1394
 *
 
1395
 * @access public
 
1396
 * @return void
 
1397
 */
 
1398
        function testMoveDownWithScope() {
 
1399
                $this->Ad =& new Ad();
 
1400
                $this->Ad->Behaviors->attach('Tree', array('scope' => 'Campaign'));
 
1401
                $this->Ad->moveDown(6);
 
1402
 
 
1403
                $this->Ad->id = 4;
 
1404
                $result = $this->Ad->children();
 
1405
                $this->assertEqual(Set::extract('/Ad/id', $result), array(5, 6));
 
1406
                $this->assertEqual(Set::extract('/Campaign/id', $result), array(2, 2));
 
1407
        }
 
1408
 
 
1409
/**
 
1410
 * Tests the interaction (non-interference) between TreeBehavior and other behaviors with respect
 
1411
 * to callback hooks
 
1412
 *
 
1413
 * @access public
 
1414
 * @return void
 
1415
 */
 
1416
        function testTranslatingTree() {
 
1417
                $this->Tree =& new FlagTree();
 
1418
                $this->Tree->cacheQueries = false;
 
1419
                $this->Tree->translateModel = 'TranslateTreeTestModel';
 
1420
                $this->Tree->Behaviors->attach('Translate', array('name'));
 
1421
 
 
1422
                //Save
 
1423
                $this->Tree->locale = 'eng';
 
1424
                $data = array('FlagTree' => array(
 
1425
                        'name' => 'name #1',
 
1426
                        'locale' => 'eng',
 
1427
                        'parent_id' => null,
 
1428
                ));
 
1429
                $this->Tree->save($data);
 
1430
                $result = $this->Tree->find('all');
 
1431
                $expected = array(array('FlagTree' => array(
 
1432
                        'id' => 1,
 
1433
                        'name' => 'name #1',
 
1434
                        'parent_id' => null,
 
1435
                        'lft' => 1,
 
1436
                        'rght' => 2,
 
1437
                        'flag' => 0,
 
1438
                        'locale' => 'eng',
 
1439
                )));
 
1440
                $this->assertEqual($result, $expected);
 
1441
 
 
1442
                //update existing record, same locale
 
1443
                $this->Tree->create();
 
1444
                $data['FlagTree']['name'] = 'Named 2';
 
1445
                $this->Tree->id = 1;
 
1446
                $this->Tree->save($data);
 
1447
                $result = $this->Tree->find('all');
 
1448
                $expected = array(array('FlagTree' => array(
 
1449
                        'id' => 1,
 
1450
                        'name' => 'Named 2',
 
1451
                        'parent_id' => null,
 
1452
                        'lft' => 1,
 
1453
                        'rght' => 2,
 
1454
                        'flag' => 0,
 
1455
                        'locale' => 'eng',
 
1456
                )));
 
1457
                $this->assertEqual($result, $expected);
 
1458
 
 
1459
                //update different locale, same record
 
1460
                $this->Tree->create();
 
1461
                $this->Tree->locale = 'deu';
 
1462
                $this->Tree->id = 1;
 
1463
                $data = array('FlagTree' => array(
 
1464
                        'id' => 1,
 
1465
                        'parent_id' => null,
 
1466
                        'name' => 'namen #1',
 
1467
                        'locale' => 'deu',
 
1468
                ));
 
1469
                $this->Tree->save($data);
 
1470
 
 
1471
                $this->Tree->locale = 'deu';
 
1472
                $result = $this->Tree->find('all');
 
1473
                $expected = array(array('FlagTree' => array(
 
1474
                        'id' => 1,
 
1475
                        'name' => 'namen #1',
 
1476
                        'parent_id' => null,
 
1477
                        'lft' => 1,
 
1478
                        'rght' => 2,
 
1479
                        'flag' => 0,
 
1480
                        'locale' => 'deu',
 
1481
                )));
 
1482
                $this->assertEqual($result, $expected);
 
1483
 
 
1484
                //Save with bindTranslation
 
1485
                $this->Tree->locale = 'eng';
 
1486
                $data = array(
 
1487
                        'name' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
 
1488
                        'parent_id' => null
 
1489
                );
 
1490
                $this->Tree->create($data);
 
1491
                $this->Tree->save();
 
1492
 
 
1493
                $this->Tree->unbindTranslation();
 
1494
                $translations = array('name' => 'Name');
 
1495
                $this->Tree->bindTranslation($translations, false);
 
1496
                $this->Tree->locale = array('eng', 'spa');
 
1497
 
 
1498
                $result = $this->Tree->read();
 
1499
                $expected = array(
 
1500
                        'FlagTree' => array('id' => 2, 'parent_id' => null, 'locale' => 'eng', 'name' => 'New title', 'flag' => 0, 'lft' => 3, 'rght' => 4),
 
1501
                        'Name' => array(
 
1502
                        array('id' => 21, 'locale' => 'eng', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'New title'),
 
1503
                        array('id' => 22, 'locale' => 'spa', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'name', 'content' => 'Nuevo leyenda')
 
1504
                        ),
 
1505
                );
 
1506
                $this->assertEqual($result, $expected);
 
1507
        }
 
1508
 
 
1509
/**
 
1510
 * testGenerateTreeListWithSelfJoin method
 
1511
 *
 
1512
 * @return void
 
1513
 * @access public
 
1514
 */
 
1515
        function testAliasesWithScopeInTwoTreeAssociations() {
 
1516
                extract($this->settings);
 
1517
                $this->Tree =& new $modelClass();
 
1518
                $this->Tree->initialize(2, 2);
 
1519
 
 
1520
                $this->TreeTwo =& new NumberTreeTwo();
 
1521
 
 
1522
                $record = $this->Tree->find('first');
 
1523
 
 
1524
                $this->Tree->bindModel(array(
 
1525
                        'hasMany' => array(
 
1526
                                'SecondTree' => array(
 
1527
                                        'className' => 'NumberTreeTwo',
 
1528
                                        'foreignKey' => 'number_tree_id'
 
1529
                                )
 
1530
                        )
 
1531
                ));
 
1532
                $this->TreeTwo->bindModel(array(
 
1533
                        'belongsTo' => array(
 
1534
                                'FirstTree' => array(
 
1535
                                        'className' => $modelClass,
 
1536
                                        'foreignKey' => 'number_tree_id'
 
1537
                                )
 
1538
                        )
 
1539
                ));
 
1540
                $this->TreeTwo->Behaviors->attach('Tree', array(
 
1541
                        'scope' => 'FirstTree'
 
1542
                ));
 
1543
 
 
1544
                $data = array(
 
1545
                        'NumberTreeTwo' => array(
 
1546
                                'name' => 'First',
 
1547
                                'number_tree_id' => $record['FlagTree']['id']
 
1548
                        )
 
1549
                );
 
1550
                $this->TreeTwo->create();
 
1551
                $this->assertTrue($this->TreeTwo->save($data));
 
1552
 
 
1553
                $result = $this->TreeTwo->find('first');
 
1554
                $expected = array('NumberTreeTwo' => array(
 
1555
                        'id' => 1,
 
1556
                        'name' => 'First',
 
1557
                        'number_tree_id' => $record['FlagTree']['id'],
 
1558
                        'parent_id' => null,
 
1559
                        'lft' => 1,
 
1560
                        'rght' => 2
 
1561
                ));
 
1562
                $this->assertEqual($result, $expected);
 
1563
        }
 
1564
}
 
1565
 
 
1566
/**
 
1567
 * AfterTreeTest class
 
1568
 *
 
1569
 * @package       cake
 
1570
 * @subpackage    cake.tests.cases.libs.model.behaviors
 
1571
 */
 
1572
class AfterTreeTest extends NumberTreeTest {
 
1573
 
 
1574
/**
 
1575
 * settings property
 
1576
 *
 
1577
 * @var array
 
1578
 * @access public
 
1579
 */
 
1580
        var $settings = array(
 
1581
                'modelClass' => 'AfterTree',
 
1582
                'leftField' => 'lft',
 
1583
                'rightField' => 'rght',
 
1584
                'parentField' => 'parent_id'
 
1585
        );
 
1586
 
 
1587
/**
 
1588
 * fixtures property
 
1589
 *
 
1590
 * @var array
 
1591
 * @access public
 
1592
 */
 
1593
        var $fixtures = array('core.after_tree');
 
1594
 
 
1595
/**
 
1596
 * Tests the afterSave callback in the model
 
1597
 *
 
1598
 * @access public
 
1599
 * @return void
 
1600
 */
 
1601
        function testAftersaveCallback() {
 
1602
                $this->Tree =& new AfterTree();
 
1603
 
 
1604
                $expected = array('AfterTree' => array('name' => 'Six and One Half Changed in AfterTree::afterSave() but not in database', 'parent_id' => 6, 'lft' => 11, 'rght' => 12));
 
1605
                $result = $this->Tree->save(array('AfterTree' => array('name' => 'Six and One Half', 'parent_id' => 6)));
 
1606
                $this->assertEqual($result, $expected);
 
1607
 
 
1608
                $expected = array('AfterTree' => array('name' => 'Six and One Half', 'parent_id' => 6, 'lft' => 11, 'rght' => 12, 'id' => 8));
 
1609
                $result = $this->Tree->find('all');
 
1610
                $this->assertEqual($result[7], $expected);
 
1611
        }
 
1612
}
 
1613
 
 
1614
/**
 
1615
 * UnconventionalTreeTest class
 
1616
 *
 
1617
 * @package       cake
 
1618
 * @subpackage    cake.tests.cases.libs.model.behaviors
 
1619
 */
 
1620
class UnconventionalTreeTest extends NumberTreeTest {
 
1621
 
 
1622
/**
 
1623
 * settings property
 
1624
 *
 
1625
 * @var array
 
1626
 * @access public
 
1627
 */
 
1628
        var $settings = array(
 
1629
                'modelClass' => 'UnconventionalTree',
 
1630
                'leftField' => 'left',
 
1631
                'rightField' => 'right',
 
1632
                'parentField' => 'join'
 
1633
        );
 
1634
 
 
1635
/**
 
1636
 * fixtures property
 
1637
 *
 
1638
 * @var array
 
1639
 * @access public
 
1640
 */
 
1641
        var $fixtures = array('core.unconventional_tree');
 
1642
}
 
1643
 
 
1644
/**
 
1645
 * UuidTreeTest class
 
1646
 *
 
1647
 * @package       cake
 
1648
 * @subpackage    cake.tests.cases.libs.model.behaviors
 
1649
 */
 
1650
class UuidTreeTest extends NumberTreeTest {
 
1651
 
 
1652
/**
 
1653
 * settings property
 
1654
 *
 
1655
 * @var array
 
1656
 * @access public
 
1657
 */
 
1658
        var $settings = array(
 
1659
                'modelClass' => 'UuidTree',
 
1660
                'leftField' => 'lft',
 
1661
                'rightField' => 'rght',
 
1662
                'parentField' => 'parent_id'
 
1663
        );
 
1664
 
 
1665
/**
 
1666
 * fixtures property
 
1667
 *
 
1668
 * @var array
 
1669
 * @access public
 
1670
 */
 
1671
        var $fixtures = array('core.uuid_tree');
 
1672
 
 
1673
/**
 
1674
 * testMovePromote method
 
1675
 *
 
1676
 * @return void
 
1677
 * @access public
 
1678
 */
 
1679
        function testMovePromote() {
 
1680
                extract($this->settings);
 
1681
                $this->Tree =& new $modelClass();
 
1682
                $this->Tree->initialize(2, 2);
 
1683
                $this->Tree->id = null;
 
1684
 
 
1685
                $parent = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
 
1686
                $parent_id = $parent[$modelClass]['id'];
 
1687
 
 
1688
                $data = $this->Tree->find(array($modelClass . '.name' => '1.1.1'), array('id'));
 
1689
                $this->Tree->id= $data[$modelClass]['id'];
 
1690
                $this->Tree->saveField($parentField, $parent_id);
 
1691
                $direct = $this->Tree->children($parent_id, true, array('name', $leftField, $rightField));
 
1692
                $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
 
1693
                        array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
 
1694
                        array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
 
1695
                $this->assertEqual($direct, $expects);
 
1696
                $validTree = $this->Tree->verify();
 
1697
                $this->assertIdentical($validTree, true);
 
1698
        }
 
1699
 
 
1700
/**
 
1701
 * testMoveWithWhitelist method
 
1702
 *
 
1703
 * @return void
 
1704
 * @access public
 
1705
 */
 
1706
        function testMoveWithWhitelist() {
 
1707
                extract($this->settings);
 
1708
                $this->Tree =& new $modelClass();
 
1709
                $this->Tree->initialize(2, 2);
 
1710
                $this->Tree->id = null;
 
1711
 
 
1712
                $parent = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
 
1713
                $parent_id = $parent[$modelClass]['id'];
 
1714
 
 
1715
                $data = $this->Tree->find(array($modelClass . '.name' => '1.1.1'), array('id'));
 
1716
                $this->Tree->id = $data[$modelClass]['id'];
 
1717
                $this->Tree->whitelist = array($parentField, 'name', 'description');
 
1718
                $this->Tree->saveField($parentField, $parent_id);
 
1719
 
 
1720
                $result = $this->Tree->children($parent_id, true, array('name', $leftField, $rightField));
 
1721
                $expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
 
1722
                        array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
 
1723
                        array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
 
1724
                $this->assertEqual($result, $expected);
 
1725
                $this->assertTrue($this->Tree->verify());
 
1726
        }
 
1727
 
 
1728
/**
 
1729
 * testRemoveNoChildren method
 
1730
 *
 
1731
 * @return void
 
1732
 * @access public
 
1733
 */
 
1734
        function testRemoveNoChildren() {
 
1735
                extract($this->settings);
 
1736
                $this->Tree =& new $modelClass();
 
1737
                $this->Tree->initialize(2, 2);
 
1738
                $initialCount = $this->Tree->find('count');
 
1739
 
 
1740
                $result = $this->Tree->findByName('1.1.1');
 
1741
                $this->Tree->removeFromTree($result[$modelClass]['id']);
 
1742
 
 
1743
                $laterCount = $this->Tree->find('count');
 
1744
                $this->assertEqual($initialCount, $laterCount);
 
1745
 
 
1746
                $nodes = $this->Tree->find('list', array('order' => $leftField));
 
1747
                $expects = array(
 
1748
                        '1. Root',
 
1749
                        '1.1',
 
1750
                        '1.1.2',
 
1751
                        '1.2',
 
1752
                        '1.2.1',
 
1753
                        '1.2.2',
 
1754
                        '1.1.1',
 
1755
                );
 
1756
 
 
1757
                $this->assertEqual(array_values($nodes), $expects);
 
1758
 
 
1759
                $validTree = $this->Tree->verify();
 
1760
                $this->assertIdentical($validTree, true);
 
1761
        }
 
1762
 
 
1763
/**
 
1764
 * testRemoveAndDeleteNoChildren method
 
1765
 *
 
1766
 * @return void
 
1767
 * @access public
 
1768
 */
 
1769
        function testRemoveAndDeleteNoChildren() {
 
1770
                extract($this->settings);
 
1771
                $this->Tree =& new $modelClass();
 
1772
                $this->Tree->initialize(2, 2);
 
1773
                $initialCount = $this->Tree->find('count');
 
1774
 
 
1775
                $result = $this->Tree->findByName('1.1.1');
 
1776
                $this->Tree->removeFromTree($result[$modelClass]['id'], true);
 
1777
 
 
1778
                $laterCount = $this->Tree->find('count');
 
1779
                $this->assertEqual($initialCount - 1, $laterCount);
 
1780
 
 
1781
                $nodes = $this->Tree->find('list', array('order' => $leftField));
 
1782
                $expects = array(
 
1783
                        '1. Root',
 
1784
                        '1.1',
 
1785
                        '1.1.2',
 
1786
                        '1.2',
 
1787
                        '1.2.1',
 
1788
                        '1.2.2',
 
1789
                );
 
1790
                $this->assertEqual(array_values($nodes), $expects);
 
1791
 
 
1792
                $validTree = $this->Tree->verify();
 
1793
                $this->assertIdentical($validTree, true);
 
1794
        }
 
1795
 
 
1796
/**
 
1797
 * testChildren method
 
1798
 *
 
1799
 * @return void
 
1800
 * @access public
 
1801
 */
 
1802
        function testChildren() {
 
1803
                extract($this->settings);
 
1804
                $this->Tree =& new $modelClass();
 
1805
                $this->Tree->initialize(2, 2);
 
1806
 
 
1807
                $data = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
 
1808
                $this->Tree->id = $data[$modelClass]['id'];
 
1809
 
 
1810
                $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
 
1811
                $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
 
1812
                        array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
 
1813
                $this->assertEqual($direct, $expects);
 
1814
 
 
1815
                $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
 
1816
                $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
 
1817
                        array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
 
1818
                        array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
 
1819
                        array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
 
1820
                        array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
 
1821
                        array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12)));
 
1822
                $this->assertEqual($total, $expects);
 
1823
        }
 
1824
 
 
1825
/**
 
1826
 * testNoAmbiguousColumn method
 
1827
 *
 
1828
 * @return void
 
1829
 * @access public
 
1830
 */
 
1831
        function testNoAmbiguousColumn() {
 
1832
                extract($this->settings);
 
1833
                $this->Tree =& new $modelClass();
 
1834
                $this->Tree->initialize(2, 2);
 
1835
 
 
1836
                $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
 
1837
                        array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
 
1838
 
 
1839
                $data = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
 
1840
                $this->Tree->id = $data[$modelClass]['id'];
 
1841
 
 
1842
                $direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
 
1843
                $expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
 
1844
                        array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
 
1845
                $this->assertEqual($direct, $expects);
 
1846
 
 
1847
                $total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
 
1848
                $expects = array(
 
1849
                        array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
 
1850
                        array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
 
1851
                        array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
 
1852
                        array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
 
1853
                        array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
 
1854
                        array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12))
 
1855
                );
 
1856
                $this->assertEqual($total, $expects);
 
1857
        }
 
1858
 
 
1859
/**
 
1860
 * testGenerateTreeListWithSelfJoin method
 
1861
 *
 
1862
 * @return void
 
1863
 * @access public
 
1864
 */
 
1865
        function testGenerateTreeListWithSelfJoin() {
 
1866
                extract($this->settings);
 
1867
                $this->Tree =& new $modelClass();
 
1868
                $this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
 
1869
                        array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
 
1870
                $this->Tree->initialize(2, 2);
 
1871
 
 
1872
                $result = $this->Tree->generateTreeList();
 
1873
                $expected = array('1. Root', '_1.1', '__1.1.1', '__1.1.2', '_1.2', '__1.2.1', '__1.2.2');
 
1874
                $this->assertIdentical(array_values($result), $expected);
 
1875
        }
 
1876
}