3
* TreeBehaviorTest file
5
* Holds several Test Cases
9
* CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
10
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
12
* Licensed under The Open Group Test Suite License
13
* Redistributions of files must retain the above copyright notice.
15
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
16
* @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
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
22
App::import('Core', array('AppModel', 'Model'));
23
require_once(dirname(dirname(__FILE__)) . DS . 'models.php');
26
* NumberTreeTest class
29
* @subpackage cake.tests.cases.libs.model.behaviors
31
class NumberTreeTest extends CakeTestCase {
39
var $settings = array(
40
'modelClass' => 'NumberTree',
42
'rightField' => 'rght',
43
'parentField' => 'parent_id'
52
var $fixtures = array('core.number_tree');
55
* testInitialize method
60
function testInitialize() {
61
extract($this->settings);
62
$this->Tree =& new $modelClass();
63
$this->Tree->initialize(2, 2);
65
$result = $this->Tree->find('count');
66
$this->assertEqual($result, 7);
68
$validTree = $this->Tree->verify();
69
$this->assertIdentical($validTree, true);
73
* testDetectInvalidLeft method
78
function testDetectInvalidLeft() {
79
extract($this->settings);
80
$this->Tree =& new $modelClass();
81
$this->Tree->initialize(2, 2);
83
$result = $this->Tree->findByName('1.1');
85
$save[$modelClass]['id'] = $result[$modelClass]['id'];
86
$save[$modelClass][$leftField] = 0;
88
$this->Tree->save($save);
89
$result = $this->Tree->verify();
90
$this->assertNotIdentical($result, true);
92
$result = $this->Tree->recover();
93
$this->assertIdentical($result, true);
95
$result = $this->Tree->verify();
96
$this->assertIdentical($result, true);
100
* testDetectInvalidRight method
105
function testDetectInvalidRight() {
106
extract($this->settings);
107
$this->Tree =& new $modelClass();
108
$this->Tree->initialize(2, 2);
110
$result = $this->Tree->findByName('1.1');
112
$save[$modelClass]['id'] = $result[$modelClass]['id'];
113
$save[$modelClass][$rightField] = 0;
115
$this->Tree->save($save);
116
$result = $this->Tree->verify();
117
$this->assertNotIdentical($result, true);
119
$result = $this->Tree->recover();
120
$this->assertIdentical($result, true);
122
$result = $this->Tree->verify();
123
$this->assertIdentical($result, true);
127
* testDetectInvalidParent method
132
function testDetectInvalidParent() {
133
extract($this->settings);
134
$this->Tree =& new $modelClass();
135
$this->Tree->initialize(2, 2);
137
$result = $this->Tree->findByName('1.1');
139
// Bypass behavior and any other logic
140
$this->Tree->updateAll(array($parentField => null), array('id' => $result[$modelClass]['id']));
142
$result = $this->Tree->verify();
143
$this->assertNotIdentical($result, true);
145
$result = $this->Tree->recover();
146
$this->assertIdentical($result, true);
148
$result = $this->Tree->verify();
149
$this->assertIdentical($result, true);
153
* testDetectNoneExistantParent method
158
function testDetectNoneExistantParent() {
159
extract($this->settings);
160
$this->Tree =& new $modelClass();
161
$this->Tree->initialize(2, 2);
163
$result = $this->Tree->findByName('1.1');
164
$this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
166
$result = $this->Tree->verify();
167
$this->assertNotIdentical($result, true);
169
$result = $this->Tree->recover('MPTT');
170
$this->assertIdentical($result, true);
172
$result = $this->Tree->verify();
173
$this->assertIdentical($result, true);
177
* testRecoverFromMissingParent method
182
function testRecoverFromMissingParent() {
183
extract($this->settings);
184
$this->Tree =& new $modelClass();
185
$this->Tree->initialize(2, 2);
187
$result = $this->Tree->findByName('1.1');
188
$this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
190
$result = $this->Tree->verify();
191
$this->assertNotIdentical($result, true);
193
$result = $this->Tree->recover();
194
$this->assertIdentical($result, true);
196
$result = $this->Tree->verify();
197
$this->assertIdentical($result, true);
201
* testDetectInvalidParents method
206
function testDetectInvalidParents() {
207
extract($this->settings);
208
$this->Tree =& new $modelClass();
209
$this->Tree->initialize(2, 2);
211
$this->Tree->updateAll(array($parentField => null));
213
$result = $this->Tree->verify();
214
$this->assertNotIdentical($result, true);
216
$result = $this->Tree->recover();
217
$this->assertIdentical($result, true);
219
$result = $this->Tree->verify();
220
$this->assertIdentical($result, true);
224
* testDetectInvalidLftsRghts method
229
function testDetectInvalidLftsRghts() {
230
extract($this->settings);
231
$this->Tree =& new $modelClass();
232
$this->Tree->initialize(2, 2);
234
$this->Tree->updateAll(array($leftField => 0, $rightField => 0));
236
$result = $this->Tree->verify();
237
$this->assertNotIdentical($result, true);
239
$this->Tree->recover();
241
$result = $this->Tree->verify();
242
$this->assertIdentical($result, true);
246
* Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
251
function testDetectEqualLftsRghts() {
252
extract($this->settings);
253
$this->Tree =& new $modelClass();
254
$this->Tree->initialize(1, 3);
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]));
263
$result = $this->Tree->verify();
264
$this->assertNotIdentical($result, true);
266
$result = $this->Tree->recover();
267
$this->assertTrue($result);
269
$result = $this->Tree->verify();
270
$this->assertTrue($result);
274
* testAddOrphan method
279
function testAddOrphan() {
280
extract($this->settings);
281
$this->Tree =& new $modelClass();
282
$this->Tree->initialize(2, 2);
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);
289
$validTree = $this->Tree->verify();
290
$this->assertIdentical($validTree, true);
294
* testAddMiddle method
299
function testAddMiddle() {
300
extract($this->settings);
301
$this->Tree =& new $modelClass();
302
$this->Tree->initialize(2, 2);
304
$data= $this->Tree->find(array($modelClass . '.name' => '1.1'), array('id'));
305
$initialCount = $this->Tree->find('count');
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);
312
$laterCount = $this->Tree->find('count');
314
$laterCount = $this->Tree->find('count');
315
$this->assertEqual($initialCount + 1, $laterCount);
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);
323
$validTree = $this->Tree->verify();
324
$this->assertIdentical($validTree, true);
328
* testAddInvalid method
333
function testAddInvalid() {
334
extract($this->settings);
335
$this->Tree =& new $modelClass();
336
$this->Tree->initialize(2, 2);
337
$this->Tree->id = null;
339
$initialCount = $this->Tree->find('count');
340
//$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
342
$saveSuccess = $this->Tree->save(array($modelClass => array('name' => 'testAddInvalid', $parentField => 99999)));
343
$this->assertIdentical($saveSuccess, false);
345
$laterCount = $this->Tree->find('count');
346
$this->assertIdentical($initialCount, $laterCount);
348
$validTree = $this->Tree->verify();
349
$this->assertIdentical($validTree, true);
353
* testAddNotIndexedByModel method
358
function testAddNotIndexedByModel() {
359
extract($this->settings);
360
$this->Tree =& new $modelClass();
361
$this->Tree->initialize(2, 2);
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);
368
$validTree = $this->Tree->verify();
369
$this->assertIdentical($validTree, true);
373
* testMovePromote method
378
function testMovePromote() {
379
extract($this->settings);
380
$this->Tree =& new $modelClass();
381
$this->Tree->initialize(2, 2);
382
$this->Tree->id = null;
384
$parent = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
385
$parent_id = $parent[$modelClass]['id'];
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);
400
* testMoveWithWhitelist method
405
function testMoveWithWhitelist() {
406
extract($this->settings);
407
$this->Tree =& new $modelClass();
408
$this->Tree->initialize(2, 2);
409
$this->Tree->id = null;
411
$parent = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
412
$parent_id = $parent[$modelClass]['id'];
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);
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());
428
* testInsertWithWhitelist method
433
function testInsertWithWhitelist() {
434
extract($this->settings);
435
$this->Tree =& new $modelClass();
436
$this->Tree->initialize(2, 2);
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);
447
* testMoveBefore method
452
function testMoveBefore() {
453
extract($this->settings);
454
$this->Tree =& new $modelClass();
455
$this->Tree->initialize(2, 2);
456
$this->Tree->id = null;
458
$parent = $this->Tree->find(array($modelClass . '.name' => '1.1'));
459
$parent_id = $parent[$modelClass]['id'];
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);
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);
471
$validTree = $this->Tree->verify();
472
$this->assertIdentical($validTree, true);
476
* testMoveAfter method
481
function testMoveAfter() {
482
extract($this->settings);
483
$this->Tree =& new $modelClass();
484
$this->Tree->initialize(2, 2);
485
$this->Tree->id = null;
487
$parent = $this->Tree->find(array($modelClass . '.name' => '1.2'));
488
$parent_id = $parent[$modelClass]['id'];
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);
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);
500
$validTree = $this->Tree->verify();
501
$this->assertIdentical($validTree, true);
505
* testMoveDemoteInvalid method
510
function testMoveDemoteInvalid() {
511
extract($this->settings);
512
$this->Tree =& new $modelClass();
513
$this->Tree->initialize(2, 2);
514
$this->Tree->id = null;
516
$parent = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
517
$parent_id = $parent[$modelClass]['id'];
519
$data = $this->Tree->find(array($modelClass . '.name' => '1.1.1'), array('id'));
521
$expects = $this->Tree->find('all');
522
$before = $this->Tree->read(null, $data[$modelClass]['id']);
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']);
528
$results = $this->Tree->find('all');
529
$after = $this->Tree->read(null, $data[$modelClass]['id']);
531
$this->assertEqual($results, $expects);
532
$this->assertEqual($before, $after);
534
$validTree = $this->Tree->verify();
535
$this->assertIdentical($validTree, true);
539
* testMoveInvalid method
544
function testMoveInvalid() {
545
extract($this->settings);
546
$this->Tree =& new $modelClass();
547
$this->Tree->initialize(2, 2);
548
$this->Tree->id = null;
550
$initialCount = $this->Tree->find('count');
551
$data= $this->Tree->findByName('1.1');
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);
557
//$this->assertIdentical($saveSuccess, false);
558
$laterCount = $this->Tree->find('count');
559
$this->assertIdentical($initialCount, $laterCount);
561
$validTree = $this->Tree->verify();
562
$this->assertIdentical($validTree, true);
566
* testMoveSelfInvalid method
571
function testMoveSelfInvalid() {
572
extract($this->settings);
573
$this->Tree =& new $modelClass();
574
$this->Tree->initialize(2, 2);
575
$this->Tree->id = null;
577
$initialCount = $this->Tree->find('count');
578
$data= $this->Tree->findByName('1.1');
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);
584
$this->assertIdentical($saveSuccess, false);
585
$laterCount = $this->Tree->find('count');
586
$this->assertIdentical($initialCount, $laterCount);
588
$validTree = $this->Tree->verify();
589
$this->assertIdentical($validTree, true);
593
* testMoveUpSuccess method
598
function testMoveUpSuccess() {
599
extract($this->settings);
600
$this->Tree =& new $modelClass();
601
$this->Tree->initialize(2, 2);
603
$data = $this->Tree->find(array($modelClass . '.name' => '1.2'), array('id'));
604
$this->Tree->moveUp($data[$modelClass]['id']);
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);
615
* testMoveUpFail method
620
function testMoveUpFail() {
621
extract($this->settings);
622
$this->Tree =& new $modelClass();
623
$this->Tree->initialize(2, 2);
625
$data = $this->Tree->find(array($modelClass . '.name' => '1.1'));
627
$this->Tree->moveUp($data[$modelClass]['id']);
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);
643
function testMoveUp2() {
644
extract($this->settings);
645
$this->Tree =& new $modelClass();
646
$this->Tree->initialize(1, 10);
648
$data = $this->Tree->find(array($modelClass . '.name' => '1.5'), array('id'));
649
$this->Tree->moveUp($data[$modelClass]['id'], 2);
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'));
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);
669
* testMoveUpFirst method
674
function testMoveUpFirst() {
675
extract($this->settings);
676
$this->Tree =& new $modelClass();
677
$this->Tree->initialize(1, 10);
679
$data = $this->Tree->find(array($modelClass . '.name' => '1.5'), array('id'));
680
$this->Tree->moveUp($data[$modelClass]['id'], true);
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'));
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);
700
* testMoveDownSuccess method
705
function testMoveDownSuccess() {
706
extract($this->settings);
707
$this->Tree =& new $modelClass();
708
$this->Tree->initialize(2, 2);
710
$data = $this->Tree->find(array($modelClass . '.name' => '1.1'), array('id'));
711
$this->Tree->moveDown($data[$modelClass]['id']);
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);
722
* testMoveDownFail method
727
function testMoveDownFail() {
728
extract($this->settings);
729
$this->Tree =& new $modelClass();
730
$this->Tree->initialize(2, 2);
732
$data = $this->Tree->find(array($modelClass . '.name' => '1.2'));
733
$this->Tree->moveDown($data[$modelClass]['id']);
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);
744
* testMoveDownLast method
749
function testMoveDownLast() {
750
extract($this->settings);
751
$this->Tree =& new $modelClass();
752
$this->Tree->initialize(1, 10);
754
$data = $this->Tree->find(array($modelClass . '.name' => '1.5'), array('id'));
755
$this->Tree->moveDown($data[$modelClass]['id'], true);
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'));
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);
775
* testMoveDown2 method
780
function testMoveDown2() {
781
extract($this->settings);
782
$this->Tree =& new $modelClass();
783
$this->Tree->initialize(1, 10);
785
$data = $this->Tree->find(array($modelClass . '.name' => '1.5'), array('id'));
786
$this->Tree->moveDown($data[$modelClass]['id'], 2);
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'));
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);
806
* testSaveNoMove method
811
function testSaveNoMove() {
812
extract($this->settings);
813
$this->Tree =& new $modelClass();
814
$this->Tree->initialize(1, 10);
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'));
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);
837
* testMoveToRootAndMoveUp method
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));
850
$result = $this->Tree->verify();
851
$this->assertIdentical($result, true);
853
$this->Tree->moveup();
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);
867
function testDelete() {
868
extract($this->settings);
869
$this->Tree =& new $modelClass();
870
$this->Tree->initialize(2, 2);
872
$initialCount = $this->Tree->find('count');
873
$result = $this->Tree->findByName('1.1.1');
875
$return = $this->Tree->delete($result[$modelClass]['id']);
876
$this->assertEqual($return, true);
878
$laterCount = $this->Tree->find('count');
879
$this->assertEqual($initialCount - 1, $laterCount);
881
$validTree= $this->Tree->verify();
882
$this->assertIdentical($validTree, true);
884
$initialCount = $this->Tree->find('count');
885
$result= $this->Tree->findByName('1.1');
887
$return = $this->Tree->delete($result[$modelClass]['id']);
888
$this->assertEqual($return, true);
890
$laterCount = $this->Tree->find('count');
891
$this->assertEqual($initialCount - 2, $laterCount);
893
$validTree = $this->Tree->verify();
894
$this->assertIdentical($validTree, true);
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');
910
$this->Tree->removeFromTree($result[$modelClass]['id']);
912
$laterCount = $this->Tree->find('count');
913
$this->assertEqual($initialCount, $laterCount);
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);
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);
926
$validTree = $this->Tree->verify();
927
$this->assertIdentical($validTree, true);
931
* testRemoveLastTopParent method
936
function testRemoveLastTopParent() {
937
extract($this->settings);
938
$this->Tree =& new $modelClass();
939
$this->Tree->initialize(2, 2);
941
$initialCount = $this->Tree->find('count');
942
$initialTopNodes = $this->Tree->childCount(false);
944
$result = $this->Tree->findByName('1. Root');
945
$this->Tree->removeFromTree($result[$modelClass]['id']);
947
$laterCount = $this->Tree->find('count');
948
$laterTopNodes = $this->Tree->childCount(false);
950
$this->assertEqual($initialCount, $laterCount);
951
$this->assertEqual($initialTopNodes, $laterTopNodes);
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')));
958
$this->assertEqual($topNodes, $expects);
960
$validTree = $this->Tree->verify();
961
$this->assertIdentical($validTree, true);
965
* testRemoveNoChildren method
970
function testRemoveNoChildren() {
971
extract($this->settings);
972
$this->Tree =& new $modelClass();
973
$this->Tree->initialize(2, 2);
974
$initialCount = $this->Tree->find('count');
976
$result = $this->Tree->findByName('1.1.1');
977
$this->Tree->removeFromTree($result[$modelClass]['id']);
979
$laterCount = $this->Tree->find('count');
980
$this->assertEqual($initialCount, $laterCount);
982
$nodes = $this->Tree->find('list', array('order' => $leftField));
993
$this->assertEqual($nodes, $expects);
995
$validTree = $this->Tree->verify();
996
$this->assertIdentical($validTree, true);
1000
* testRemoveAndDelete method
1005
function testRemoveAndDelete() {
1006
extract($this->settings);
1007
$this->Tree =& new $modelClass();
1008
$this->Tree->initialize(2, 2);
1010
$initialCount = $this->Tree->find('count');
1011
$result = $this->Tree->findByName('1.1');
1013
$this->Tree->removeFromTree($result[$modelClass]['id'], true);
1015
$laterCount = $this->Tree->find('count');
1016
$this->assertEqual($initialCount-1, $laterCount);
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);
1024
$topNodes = $this->Tree->children(false, true,array('name'));
1025
$expects = array(array($modelClass => array('name' => '1. Root')));
1026
$this->assertEqual($topNodes, $expects);
1028
$validTree = $this->Tree->verify();
1029
$this->assertIdentical($validTree, true);
1033
* testRemoveAndDeleteNoChildren method
1038
function testRemoveAndDeleteNoChildren() {
1039
extract($this->settings);
1040
$this->Tree =& new $modelClass();
1041
$this->Tree->initialize(2, 2);
1042
$initialCount = $this->Tree->find('count');
1044
$result = $this->Tree->findByName('1.1.1');
1045
$this->Tree->removeFromTree($result[$modelClass]['id'], true);
1047
$laterCount = $this->Tree->find('count');
1048
$this->assertEqual($initialCount - 1, $laterCount);
1050
$nodes = $this->Tree->find('list', array('order' => $leftField));
1059
$this->assertEqual($nodes, $expects);
1061
$validTree = $this->Tree->verify();
1062
$this->assertIdentical($validTree, true);
1066
* testChildren method
1071
function testChildren() {
1072
extract($this->settings);
1073
$this->Tree =& new $modelClass();
1074
$this->Tree->initialize(2, 2);
1076
$data = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
1077
$this->Tree->id= $data[$modelClass]['id'];
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);
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);
1093
$this->assertEqual(array(), $this->Tree->children(10000));
1097
* testCountChildren method
1102
function testCountChildren() {
1103
extract($this->settings);
1104
$this->Tree =& new $modelClass();
1105
$this->Tree->initialize(2, 2);
1107
$data = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
1108
$this->Tree->id = $data[$modelClass]['id'];
1110
$direct = $this->Tree->childCount(null, true);
1111
$this->assertEqual($direct, 2);
1113
$total = $this->Tree->childCount();
1114
$this->assertEqual($total, 6);
1118
* testGetParentNode method
1123
function testGetParentNode() {
1124
extract($this->settings);
1125
$this->Tree =& new $modelClass();
1126
$this->Tree->initialize(2, 2);
1128
$data = $this->Tree->find(array($modelClass . '.name' => '1.2.2'));
1129
$this->Tree->id= $data[$modelClass]['id'];
1131
$result = $this->Tree->getparentNode(null, array('name'));
1132
$expects = array($modelClass => array('name' => '1.2'));
1133
$this->assertIdentical($result, $expects);
1137
* testGetPath method
1142
function testGetPath() {
1143
extract($this->settings);
1144
$this->Tree =& new $modelClass();
1145
$this->Tree->initialize(2, 2);
1147
$data = $this->Tree->find(array($modelClass . '.name' => '1.2.2'));
1148
$this->Tree->id= $data[$modelClass]['id'];
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);
1158
* testNoAmbiguousColumn method
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);
1170
$data = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
1171
$this->Tree->id= $data[$modelClass]['id'];
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);
1178
$total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
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))
1187
$this->assertEqual($total, $expects);
1191
* testReorderTree method
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));
1202
$data = $this->Tree->find(array($modelClass . '.name' => '1.1'), array('id'));
1203
$this->Tree->moveDown($data[$modelClass]['id']);
1205
$data = $this->Tree->find(array($modelClass . '.name' => '1.2.1'), array('id'));
1206
$this->Tree->moveDown($data[$modelClass]['id']);
1208
$data = $this->Tree->find(array($modelClass . '.name' => '1.3.2.2'), array('id'));
1209
$this->Tree->moveDown($data[$modelClass]['id']);
1211
$unsortedNodes = $this->Tree->find('list', array('order' => $leftField));
1212
$this->assertNotIdentical($nodes, $unsortedNodes);
1214
$this->Tree->reorder();
1215
$sortedNodes = $this->Tree->find('list', array('order' => $leftField));
1216
$this->assertIdentical($nodes, $sortedNodes);
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
1227
function testReorderBigTreeWithQueryCaching() {
1228
extract($this->settings);
1229
$this->Tree =& new $modelClass();
1230
$this->Tree->initialize(2, 10);
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;
1239
* testGenerateTreeListWithSelfJoin method
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);
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);
1257
* testArraySyntax method
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)));
1273
* ScopedTreeTest class
1276
* @subpackage cake.tests.cases.libs.model.behaviors
1278
class ScopedTreeTest extends NumberTreeTest {
1286
var $settings = array(
1287
'modelClass' => 'FlagTree',
1288
'leftField' => 'lft',
1289
'rightField' => 'rght',
1290
'parentField' => 'parent_id'
1299
var $fixtures = array('core.flag_tree', 'core.ad', 'core.campaign', 'core.translate', 'core.number_tree_two');
1302
* testStringScope method
1307
function testStringScope() {
1308
$this->Tree =& new FlagTree();
1309
$this->Tree->initialize(2, 3);
1311
$this->Tree->id = 1;
1312
$this->Tree->saveField('flag', 1);
1313
$this->Tree->id = 2;
1314
$this->Tree->saveField('flag', 1);
1316
$result = $this->Tree->children();
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'))
1322
$this->assertEqual($result, $expected);
1324
$this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
1325
$this->assertEqual($this->Tree->children(), array());
1327
$this->Tree->id = 1;
1328
$this->Tree->Behaviors->attach('Tree', array('scope' => 'FlagTree.flag = 1'));
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);
1334
$this->assertTrue($this->Tree->delete());
1335
$this->assertEqual($this->Tree->find('count'), 11);
1339
* testArrayScope method
1344
function testArrayScope() {
1345
$this->Tree =& new FlagTree();
1346
$this->Tree->initialize(2, 3);
1348
$this->Tree->id = 1;
1349
$this->Tree->saveField('flag', 1);
1350
$this->Tree->id = 2;
1351
$this->Tree->saveField('flag', 1);
1353
$result = $this->Tree->children();
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'))
1359
$this->assertEqual($result, $expected);
1361
$this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
1362
$this->assertEqual($this->Tree->children(), array());
1364
$this->Tree->id = 1;
1365
$this->Tree->Behaviors->attach('Tree', array('scope' => array('FlagTree.flag' => 1)));
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);
1371
$this->assertTrue($this->Tree->delete());
1372
$this->assertEqual($this->Tree->find('count'), 11);
1376
* testMoveUpWithScope method
1381
function testMoveUpWithScope() {
1382
$this->Ad =& new Ad();
1383
$this->Ad->Behaviors->attach('Tree', array('scope'=>'Campaign'));
1384
$this->Ad->moveUp(6);
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));
1393
* testMoveDownWithScope method
1398
function testMoveDownWithScope() {
1399
$this->Ad =& new Ad();
1400
$this->Ad->Behaviors->attach('Tree', array('scope' => 'Campaign'));
1401
$this->Ad->moveDown(6);
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));
1410
* Tests the interaction (non-interference) between TreeBehavior and other behaviors with respect
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'));
1423
$this->Tree->locale = 'eng';
1424
$data = array('FlagTree' => array(
1425
'name' => 'name #1',
1427
'parent_id' => null,
1429
$this->Tree->save($data);
1430
$result = $this->Tree->find('all');
1431
$expected = array(array('FlagTree' => array(
1433
'name' => 'name #1',
1434
'parent_id' => null,
1440
$this->assertEqual($result, $expected);
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(
1450
'name' => 'Named 2',
1451
'parent_id' => null,
1457
$this->assertEqual($result, $expected);
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(
1465
'parent_id' => null,
1466
'name' => 'namen #1',
1469
$this->Tree->save($data);
1471
$this->Tree->locale = 'deu';
1472
$result = $this->Tree->find('all');
1473
$expected = array(array('FlagTree' => array(
1475
'name' => 'namen #1',
1476
'parent_id' => null,
1482
$this->assertEqual($result, $expected);
1484
//Save with bindTranslation
1485
$this->Tree->locale = 'eng';
1487
'name' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
1490
$this->Tree->create($data);
1491
$this->Tree->save();
1493
$this->Tree->unbindTranslation();
1494
$translations = array('name' => 'Name');
1495
$this->Tree->bindTranslation($translations, false);
1496
$this->Tree->locale = array('eng', 'spa');
1498
$result = $this->Tree->read();
1500
'FlagTree' => array('id' => 2, 'parent_id' => null, 'locale' => 'eng', 'name' => 'New title', 'flag' => 0, 'lft' => 3, 'rght' => 4),
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')
1506
$this->assertEqual($result, $expected);
1510
* testGenerateTreeListWithSelfJoin method
1515
function testAliasesWithScopeInTwoTreeAssociations() {
1516
extract($this->settings);
1517
$this->Tree =& new $modelClass();
1518
$this->Tree->initialize(2, 2);
1520
$this->TreeTwo =& new NumberTreeTwo();
1522
$record = $this->Tree->find('first');
1524
$this->Tree->bindModel(array(
1526
'SecondTree' => array(
1527
'className' => 'NumberTreeTwo',
1528
'foreignKey' => 'number_tree_id'
1532
$this->TreeTwo->bindModel(array(
1533
'belongsTo' => array(
1534
'FirstTree' => array(
1535
'className' => $modelClass,
1536
'foreignKey' => 'number_tree_id'
1540
$this->TreeTwo->Behaviors->attach('Tree', array(
1541
'scope' => 'FirstTree'
1545
'NumberTreeTwo' => array(
1547
'number_tree_id' => $record['FlagTree']['id']
1550
$this->TreeTwo->create();
1551
$this->assertTrue($this->TreeTwo->save($data));
1553
$result = $this->TreeTwo->find('first');
1554
$expected = array('NumberTreeTwo' => array(
1557
'number_tree_id' => $record['FlagTree']['id'],
1558
'parent_id' => null,
1562
$this->assertEqual($result, $expected);
1567
* AfterTreeTest class
1570
* @subpackage cake.tests.cases.libs.model.behaviors
1572
class AfterTreeTest extends NumberTreeTest {
1580
var $settings = array(
1581
'modelClass' => 'AfterTree',
1582
'leftField' => 'lft',
1583
'rightField' => 'rght',
1584
'parentField' => 'parent_id'
1593
var $fixtures = array('core.after_tree');
1596
* Tests the afterSave callback in the model
1601
function testAftersaveCallback() {
1602
$this->Tree =& new AfterTree();
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);
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);
1615
* UnconventionalTreeTest class
1618
* @subpackage cake.tests.cases.libs.model.behaviors
1620
class UnconventionalTreeTest extends NumberTreeTest {
1628
var $settings = array(
1629
'modelClass' => 'UnconventionalTree',
1630
'leftField' => 'left',
1631
'rightField' => 'right',
1632
'parentField' => 'join'
1641
var $fixtures = array('core.unconventional_tree');
1645
* UuidTreeTest class
1648
* @subpackage cake.tests.cases.libs.model.behaviors
1650
class UuidTreeTest extends NumberTreeTest {
1658
var $settings = array(
1659
'modelClass' => 'UuidTree',
1660
'leftField' => 'lft',
1661
'rightField' => 'rght',
1662
'parentField' => 'parent_id'
1671
var $fixtures = array('core.uuid_tree');
1674
* testMovePromote method
1679
function testMovePromote() {
1680
extract($this->settings);
1681
$this->Tree =& new $modelClass();
1682
$this->Tree->initialize(2, 2);
1683
$this->Tree->id = null;
1685
$parent = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
1686
$parent_id = $parent[$modelClass]['id'];
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);
1701
* testMoveWithWhitelist method
1706
function testMoveWithWhitelist() {
1707
extract($this->settings);
1708
$this->Tree =& new $modelClass();
1709
$this->Tree->initialize(2, 2);
1710
$this->Tree->id = null;
1712
$parent = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
1713
$parent_id = $parent[$modelClass]['id'];
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);
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());
1729
* testRemoveNoChildren method
1734
function testRemoveNoChildren() {
1735
extract($this->settings);
1736
$this->Tree =& new $modelClass();
1737
$this->Tree->initialize(2, 2);
1738
$initialCount = $this->Tree->find('count');
1740
$result = $this->Tree->findByName('1.1.1');
1741
$this->Tree->removeFromTree($result[$modelClass]['id']);
1743
$laterCount = $this->Tree->find('count');
1744
$this->assertEqual($initialCount, $laterCount);
1746
$nodes = $this->Tree->find('list', array('order' => $leftField));
1757
$this->assertEqual(array_values($nodes), $expects);
1759
$validTree = $this->Tree->verify();
1760
$this->assertIdentical($validTree, true);
1764
* testRemoveAndDeleteNoChildren method
1769
function testRemoveAndDeleteNoChildren() {
1770
extract($this->settings);
1771
$this->Tree =& new $modelClass();
1772
$this->Tree->initialize(2, 2);
1773
$initialCount = $this->Tree->find('count');
1775
$result = $this->Tree->findByName('1.1.1');
1776
$this->Tree->removeFromTree($result[$modelClass]['id'], true);
1778
$laterCount = $this->Tree->find('count');
1779
$this->assertEqual($initialCount - 1, $laterCount);
1781
$nodes = $this->Tree->find('list', array('order' => $leftField));
1790
$this->assertEqual(array_values($nodes), $expects);
1792
$validTree = $this->Tree->verify();
1793
$this->assertIdentical($validTree, true);
1797
* testChildren method
1802
function testChildren() {
1803
extract($this->settings);
1804
$this->Tree =& new $modelClass();
1805
$this->Tree->initialize(2, 2);
1807
$data = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
1808
$this->Tree->id = $data[$modelClass]['id'];
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);
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);
1826
* testNoAmbiguousColumn method
1831
function testNoAmbiguousColumn() {
1832
extract($this->settings);
1833
$this->Tree =& new $modelClass();
1834
$this->Tree->initialize(2, 2);
1836
$this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
1837
array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
1839
$data = $this->Tree->find(array($modelClass . '.name' => '1. Root'));
1840
$this->Tree->id = $data[$modelClass]['id'];
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);
1847
$total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
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))
1856
$this->assertEqual($total, $expects);
1860
* testGenerateTreeListWithSelfJoin method
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);
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);