Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * TreeBehaviorNumberTest file
4
 *
5
 * This is the basic Tree behavior test
6
 *
7
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * For full copyright and license information, please see the LICENSE.txt
12
 * Redistributions of files must retain the above copyright notice
13
 *
14
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
15
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
16
 * @package       Cake.Test.Case.Model.Behavior
17
 * @since         CakePHP(tm) v 1.2.0.5330
18
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
19
 */
20
 
21
App::uses('Model', 'Model');
22
App::uses('AppModel', 'Model');
23
 
24
require_once dirname(dirname(__FILE__)) . DS . 'models.php';
25
 
26
/**
27
 * TreeBehaviorNumberTest class
28
 *
29
 * @package       Cake.Test.Case.Model.Behavior
30
 */
31
class TreeBehaviorNumberTest extends CakeTestCase {
32
 
33
/**
34
 * Whether backup global state for each test method or not
35
 *
36
 * @var bool
37
 */
38
	public $backupGlobals = false;
39
 
40
/**
41
 * settings property
42
 *
43
 * @var array
44
 */
45
	public $settings = array(
46
		'modelClass' => 'NumberTree',
47
		'leftField' => 'lft',
48
		'rightField' => 'rght',
49
		'parentField' => 'parent_id',
50
		'level' => 'level'
51
	);
52
 
53
/**
54
 * fixtures property
55
 *
56
 * @var array
57
 */
58
	public $fixtures = array('core.number_tree', 'core.person');
59
 
60
/**
61
 * testInitialize method
62
 *
63
 * @return void
64
 */
65
	public function testInitialize() {
66
		extract($this->settings);
67
		$this->Tree = new $modelClass();
68
		$this->Tree->initialize(2, 2);
69
 
70
		$result = $this->Tree->find('count');
71
		$this->assertEquals(7, $result);
72
 
73
		$validTree = $this->Tree->verify();
74
		$this->assertTrue($validTree);
75
	}
76
 
77
/**
78
 * testDetectInvalidLeft method
79
 *
80
 * @return void
81
 */
82
	public function testDetectInvalidLeft() {
83
		extract($this->settings);
84
		$this->Tree = new $modelClass();
85
		$this->Tree->initialize(2, 2);
86
 
87
		$result = $this->Tree->findByName('1.1');
88
 
89
		$save[$modelClass]['id'] = $result[$modelClass]['id'];
90
		$save[$modelClass][$leftField] = 0;
91
 
92
		$this->Tree->create();
93
		$this->Tree->save($save);
94
		$result = $this->Tree->verify();
95
		$this->assertNotSame(true, $result);
96
 
97
		$result = $this->Tree->recover();
98
		$this->assertTrue($result);
99
 
100
		$result = $this->Tree->verify();
101
		$this->assertTrue($result);
102
	}
103
 
104
/**
105
 * testDetectInvalidRight method
106
 *
107
 * @return void
108
 */
109
	public function testDetectInvalidRight() {
110
		extract($this->settings);
111
		$this->Tree = new $modelClass();
112
		$this->Tree->initialize(2, 2);
113
 
114
		$result = $this->Tree->findByName('1.1');
115
 
116
		$save[$modelClass]['id'] = $result[$modelClass]['id'];
117
		$save[$modelClass][$rightField] = 0;
118
 
119
		$this->Tree->create();
120
		$this->Tree->save($save);
121
		$result = $this->Tree->verify();
122
		$this->assertNotSame(true, $result);
123
 
124
		$result = $this->Tree->recover();
125
		$this->assertTrue($result);
126
 
127
		$result = $this->Tree->verify();
128
		$this->assertTrue($result);
129
	}
130
 
131
/**
132
 * testDetectInvalidParent method
133
 *
134
 * @return void
135
 */
136
	public function testDetectInvalidParent() {
137
		extract($this->settings);
138
		$this->Tree = new $modelClass();
139
		$this->Tree->initialize(2, 2);
140
 
141
		$result = $this->Tree->findByName('1.1');
142
 
143
		// Bypass behavior and any other logic
144
		$this->Tree->updateAll(array($parentField => null), array('id' => $result[$modelClass]['id']));
145
 
146
		$result = $this->Tree->verify();
147
		$this->assertNotSame(true, $result);
148
 
149
		$result = $this->Tree->recover();
150
		$this->assertTrue($result);
151
 
152
		$result = $this->Tree->verify();
153
		$this->assertTrue($result);
154
	}
155
 
156
/**
157
 * testDetectNoneExistentParent method
158
 *
159
 * @return void
160
 */
161
	public function testDetectNoneExistentParent() {
162
		extract($this->settings);
163
		$this->Tree = new $modelClass();
164
		$this->Tree->initialize(2, 2);
165
 
166
		$result = $this->Tree->findByName('1.1');
167
		$this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
168
 
169
		$result = $this->Tree->verify();
170
		$this->assertNotSame(true, $result);
171
 
172
		$result = $this->Tree->recover('MPTT');
173
		$this->assertTrue($result);
174
 
175
		$result = $this->Tree->verify();
176
		$this->assertTrue($result);
177
	}
178
 
179
/**
180
 * testRecoverUsingParentMode method
181
 *
182
 * @return void
183
 */
184
	public function testRecoverUsingParentMode() {
185
		extract($this->settings);
186
		$this->Tree = new $modelClass();
187
		$this->Tree->Behaviors->disable('Tree');
188
 
189
		$this->Tree->create();
190
		$this->Tree->save(array('name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0));
191
		$node1 = $this->Tree->id;
192
 
193
		$this->Tree->create();
194
		$this->Tree->save(array('name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0));
195
		$node11 = $this->Tree->id;
196
 
197
		$this->Tree->create();
198
		$this->Tree->save(array('name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0));
199
		$node12 = $this->Tree->id;
200
 
201
		$this->Tree->create();
202
		$this->Tree->save(array('name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0));
203
 
204
		$this->Tree->create();
205
		$this->Tree->save(array('name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0));
206
 
207
		$this->Tree->Behaviors->enable('Tree');
208
 
209
		$result = $this->Tree->verify();
210
		$this->assertNotSame(true, $result);
211
 
212
		$result = $this->Tree->recover();
213
		$this->assertTrue($result);
214
 
215
		$result = $this->Tree->verify();
216
		$this->assertTrue($result);
217
 
218
		$result = $this->Tree->find('first', array(
219
			'fields' => array('name', $parentField, $leftField, $rightField),
220
			'conditions' => array('name' => 'Main'),
221
			'recursive' => -1
222
		));
223
		$expected = array(
224
			$modelClass => array(
225
				'name' => 'Main',
226
				$parentField => null,
227
				$leftField => 1,
228
				$rightField => 10
229
			)
230
		);
231
		$this->assertEquals($expected, $result);
232
	}
233
 
234
/**
235
 * testRecoverUsingParentModeAndDelete method
236
 *
237
 * @return void
238
 */
239
	public function testRecoverUsingParentModeAndDelete() {
240
		extract($this->settings);
241
		$this->Tree = new $modelClass();
242
		$this->Tree->Behaviors->disable('Tree');
243
 
244
		$this->Tree->create();
245
		$this->Tree->save(array('name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0));
246
		$node1 = $this->Tree->id;
247
 
248
		$this->Tree->create();
249
		$this->Tree->save(array('name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0));
250
		$node11 = $this->Tree->id;
251
 
252
		$this->Tree->create();
253
		$this->Tree->save(array('name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0));
254
		$node12 = $this->Tree->id;
255
 
256
		$this->Tree->create();
257
		$this->Tree->save(array('name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0));
258
 
259
		$this->Tree->create();
260
		$this->Tree->save(array('name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0));
261
 
262
		$this->Tree->create();
263
		$this->Tree->save(array('name' => 'Lost', $parentField => 9, $leftField => 0, $rightField => 0));
264
 
265
		$this->Tree->Behaviors->enable('Tree');
266
 
267
		$this->Tree->bindModel(array('belongsTo' => array('Parent' => array(
268
			'className' => $this->Tree->name,
269
			'foreignKey' => $parentField
270
		))));
271
		$this->Tree->bindModel(array('hasMany' => array('Child' => array(
272
			'className' => $this->Tree->name,
273
			'foreignKey' => $parentField
274
		))));
275
 
276
		$result = $this->Tree->verify();
277
		$this->assertNotSame(true, $result);
278
 
279
		$count = $this->Tree->find('count');
280
		$this->assertEquals(6, $count);
281
 
282
		$result = $this->Tree->recover('parent', 'delete');
283
		$this->assertTrue($result);
284
 
285
		$result = $this->Tree->verify();
286
		$this->assertTrue($result);
287
 
288
		$count = $this->Tree->find('count');
289
		$this->assertEquals(5, $count);
290
 
291
		$result = $this->Tree->find('first', array(
292
			'fields' => array('name', $parentField, $leftField, $rightField),
293
			'conditions' => array('name' => 'Main'),
294
			'recursive' => -1
295
		));
296
		$expected = array(
297
			$modelClass => array(
298
				'name' => 'Main',
299
				$parentField => null,
300
				$leftField => 1,
301
				$rightField => 10
302
			)
303
		);
304
		$this->assertEquals($expected, $result);
305
	}
306
 
307
/**
308
 * testRecoverFromMissingParent method
309
 *
310
 * @return void
311
 */
312
	public function testRecoverFromMissingParent() {
313
		extract($this->settings);
314
		$this->Tree = new $modelClass();
315
		$this->Tree->initialize(2, 2);
316
 
317
		$result = $this->Tree->findByName('1.1');
318
		$this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
319
 
320
		$result = $this->Tree->verify();
321
		$this->assertNotSame(true, $result);
322
 
323
		$result = $this->Tree->recover();
324
		$this->assertTrue($result);
325
 
326
		$result = $this->Tree->verify();
327
		$this->assertTrue($result);
328
	}
329
 
330
/**
331
 * testDetectInvalidParents method
332
 *
333
 * @return void
334
 */
335
	public function testDetectInvalidParents() {
336
		extract($this->settings);
337
		$this->Tree = new $modelClass();
338
		$this->Tree->initialize(2, 2);
339
 
340
		$this->Tree->updateAll(array($parentField => null));
341
 
342
		$result = $this->Tree->verify();
343
		$this->assertNotSame(true, $result);
344
 
345
		$result = $this->Tree->recover();
346
		$this->assertTrue($result);
347
 
348
		$result = $this->Tree->verify();
349
		$this->assertTrue($result);
350
	}
351
 
352
/**
353
 * testDetectInvalidLftsRghts method
354
 *
355
 * @return void
356
 */
357
	public function testDetectInvalidLftsRghts() {
358
		extract($this->settings);
359
		$this->Tree = new $modelClass();
360
		$this->Tree->initialize(2, 2);
361
 
362
		$this->Tree->updateAll(array($leftField => 0, $rightField => 0));
363
 
364
		$result = $this->Tree->verify();
365
		$this->assertNotSame(true, $result);
366
 
367
		$this->Tree->recover();
368
 
369
		$result = $this->Tree->verify();
370
		$this->assertTrue($result);
371
	}
372
 
373
/**
374
 * Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
375
 *
376
 * @return void
377
 */
378
	public function testDetectEqualLftsRghts() {
379
		extract($this->settings);
380
		$this->Tree = new $modelClass();
381
		$this->Tree->initialize(1, 3);
382
 
383
		$result = $this->Tree->findByName('1.1');
384
		$this->Tree->updateAll(array($rightField => $result[$modelClass][$leftField]), array('id' => $result[$modelClass]['id']));
385
		$this->Tree->updateAll(array($leftField => $this->Tree->escapeField($leftField) . ' -1'),
386
			array($leftField . ' >' => $result[$modelClass][$leftField]));
387
		$this->Tree->updateAll(array($rightField => $this->Tree->escapeField($rightField) . ' -1'),
388
			array($rightField . ' >' => $result[$modelClass][$leftField]));
389
 
390
		$result = $this->Tree->verify();
391
		$this->assertNotSame(true, $result);
392
 
393
		$result = $this->Tree->recover();
394
		$this->assertTrue($result);
395
 
396
		$result = $this->Tree->verify();
397
		$this->assertTrue($result);
398
	}
399
 
400
/**
401
 * testAddOrphan method
402
 *
403
 * @return void
404
 */
405
	public function testAddOrphan() {
406
		extract($this->settings);
407
		$this->Tree = new $modelClass();
408
		$this->Tree->initialize(2, 2);
409
 
410
		$this->Tree->create();
411
		$this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
412
		$result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
413
		$expected = array($modelClass => array('name' => 'testAddOrphan', $parentField => null));
414
		$this->assertEquals($expected, $result);
415
 
416
		$validTree = $this->Tree->verify();
417
		$this->assertTrue($validTree);
418
	}
419
 
420
/**
421
 * testAddMiddle method
422
 *
423
 * @return void
424
 */
425
	public function testAddMiddle() {
426
		extract($this->settings);
427
		$this->Tree = new $modelClass();
428
		$this->Tree->initialize(2, 2);
429
 
430
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
431
		$initialCount = $this->Tree->find('count');
432
 
433
		$this->Tree->create();
434
		$result = $this->Tree->save(array($modelClass => array('name' => 'testAddMiddle', $parentField => $data[$modelClass]['id'])));
435
		$expected = array_merge(array($modelClass => array('name' => 'testAddMiddle', $parentField => '2')), $result);
436
		$this->assertSame($expected, $result);
437
 
438
		$laterCount = $this->Tree->find('count');
439
		$this->assertEquals($initialCount + 1, $laterCount);
440
 
441
		$children = $this->Tree->children($data[$modelClass]['id'], true, array('name'));
442
		$expected = array(array($modelClass => array('name' => '1.1.1')),
443
			array($modelClass => array('name' => '1.1.2')),
444
			array($modelClass => array('name' => 'testAddMiddle')));
445
		$this->assertSame($expected, $children);
446
 
447
		$validTree = $this->Tree->verify();
448
		$this->assertTrue($validTree);
449
	}
450
 
451
/**
452
 * testAddWithPreSpecifiedId method
453
 *
454
 * @return void
455
 */
456
	public function testAddWithPreSpecifiedId() {
457
		extract($this->settings);
458
		$this->Tree = new $modelClass();
459
		$this->Tree->initialize(2, 2);
460
 
461
		$data = $this->Tree->find('first', array(
462
			'fields' => array('id'),
463
			'conditions' => array($modelClass . '.name' => '1.1')
464
		));
465
 
466
		$this->Tree->create();
467
		$result = $this->Tree->save(array($modelClass => array(
468
			'id' => 100,
469
			'name' => 'testAddMiddle',
470
			$parentField => $data[$modelClass]['id'])
471
		));
472
		$expected = array_merge(
473
			array($modelClass => array('id' => 100, 'name' => 'testAddMiddle', $parentField => '2')),
474
			$result
475
		);
476
		$this->assertSame($expected, $result);
477
 
478
		$this->assertTrue($this->Tree->verify());
479
	}
480
 
481
/**
482
 * testAddInvalid method
483
 *
484
 * @return void
485
 */
486
	public function testAddInvalid() {
487
		extract($this->settings);
488
		$this->Tree = new $modelClass();
489
		$this->Tree->initialize(2, 2);
490
		$this->Tree->id = null;
491
 
492
		$initialCount = $this->Tree->find('count');
493
		//$this->expectError('Trying to save a node under a none-existant node in TreeBehavior::beforeSave');
494
 
495
		$this->Tree->create();
496
		$saveSuccess = $this->Tree->save(array($modelClass => array('name' => 'testAddInvalid', $parentField => 99999)));
497
		$this->assertFalse($saveSuccess);
498
 
499
		$laterCount = $this->Tree->find('count');
500
		$this->assertSame($initialCount, $laterCount);
501
 
502
		$validTree = $this->Tree->verify();
503
		$this->assertTrue($validTree);
504
	}
505
 
506
/**
507
 * testAddNotIndexedByModel method
508
 *
509
 * @return void
510
 */
511
	public function testAddNotIndexedByModel() {
512
		extract($this->settings);
513
		$this->Tree = new $modelClass();
514
		$this->Tree->initialize(2, 2);
515
 
516
		$this->Tree->create();
517
		$this->Tree->save(array('name' => 'testAddNotIndexed', $parentField => null));
518
		$result = $this->Tree->find('first', array('fields' => array('name', $parentField), 'order' => $modelClass . '.' . $leftField . ' desc'));
519
		$expected = array($modelClass => array('name' => 'testAddNotIndexed', $parentField => null));
520
		$this->assertEquals($expected, $result);
521
 
522
		$validTree = $this->Tree->verify();
523
		$this->assertTrue($validTree);
524
	}
525
 
526
/**
527
 * testMovePromote method
528
 *
529
 * @return void
530
 */
531
	public function testMovePromote() {
532
		extract($this->settings);
533
		$this->Tree = new $modelClass();
534
		$this->Tree->initialize(2, 2);
535
		$this->Tree->id = null;
536
 
537
		$parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
538
		$parentId = $parent[$modelClass]['id'];
539
 
540
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
541
		$this->Tree->id = $data[$modelClass]['id'];
542
		$this->Tree->saveField($parentField, $parentId);
543
		$direct = $this->Tree->children($parentId, true, array('id', 'name', $parentField, $leftField, $rightField));
544
		$expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
545
			array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
546
			array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
547
		$this->assertEquals($expected, $direct);
548
		$validTree = $this->Tree->verify();
549
		$this->assertTrue($validTree);
550
	}
551
 
552
/**
553
 * testGetLevel method
554
 *
555
 * @return void
556
 */
557
	public function testGetLevel() {
558
		extract($this->settings);
559
		$this->Tree = new $modelClass();
560
		$this->Tree->initialize(2, 2);
561
		$this->Tree->id = null;
562
 
563
		$result = $this->Tree->getLevel(5);
564
		$this->assertEquals(1, $result);
565
 
566
		$result = $this->Tree->getLevel(3);
567
		$this->assertEquals(2, $result);
568
 
569
		$this->assertFalse($this->Tree->getLevel(999));
570
	}
571
 
572
/**
573
 * testMoveWithWhitelist method
574
 *
575
 * @return void
576
 */
577
	public function testMoveWithWhitelist() {
578
		extract($this->settings);
579
		$this->Tree = new $modelClass();
580
		$this->Tree->initialize(2, 2);
581
		$this->Tree->id = null;
582
 
583
		$parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
584
		$parentId = $parent[$modelClass]['id'];
585
 
586
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
587
		$this->Tree->id = $data[$modelClass]['id'];
588
		$this->Tree->whitelist = array($parentField, 'name', 'description');
589
		$this->Tree->saveField($parentField, $parentId);
590
 
591
		$result = $this->Tree->children($parentId, true, array('id', 'name', $parentField, $leftField, $rightField));
592
		$expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 5)),
593
			array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 6, $rightField => 11)),
594
			array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 1, $leftField => 12, $rightField => 13)));
595
		$this->assertEquals($expected, $result);
596
		$this->assertTrue($this->Tree->verify());
597
	}
598
 
599
/**
600
 * testInsertWithWhitelist method
601
 *
602
 * @return void
603
 */
604
	public function testInsertWithWhitelist() {
605
		extract($this->settings);
606
		$this->Tree = new $modelClass();
607
		$this->Tree->initialize(2, 2);
608
 
609
		$this->Tree->whitelist = array('name', $parentField);
610
		$this->Tree->create();
611
		$this->Tree->save(array($modelClass => array('name' => 'testAddOrphan', $parentField => null)));
612
		$result = $this->Tree->findByName('testAddOrphan', array('name', $parentField, $leftField, $rightField));
613
		$expected = array('name' => 'testAddOrphan', $parentField => null, $leftField => '15', $rightField => 16);
614
		$this->assertEquals($expected, $result[$modelClass]);
615
		$this->assertTrue($this->Tree->verify());
616
	}
617
 
618
/**
619
 * testMoveBefore method
620
 *
621
 * @return void
622
 */
623
	public function testMoveBefore() {
624
		extract($this->settings);
625
		$this->Tree = new $modelClass();
626
		$this->Tree->initialize(2, 2);
627
		$this->Tree->id = null;
628
 
629
		$parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
630
		$parentId = $parent[$modelClass]['id'];
631
 
632
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
633
		$this->Tree->id = $data[$modelClass]['id'];
634
		$this->Tree->saveField($parentField, $parentId);
635
 
636
		$result = $this->Tree->children($parentId, true, array('name'));
637
		$expected = array(array($modelClass => array('name' => '1.1.1')),
638
			array($modelClass => array('name' => '1.1.2')),
639
			array($modelClass => array('name' => '1.2')));
640
		$this->assertEquals($expected, $result);
641
 
642
		$validTree = $this->Tree->verify();
643
		$this->assertTrue($validTree);
644
	}
645
 
646
/**
647
 * testMoveAfter method
648
 *
649
 * @return void
650
 */
651
	public function testMoveAfter() {
652
		extract($this->settings);
653
		$this->Tree = new $modelClass();
654
		$this->Tree->initialize(2, 2);
655
		$this->Tree->id = null;
656
 
657
		$parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
658
		$parentId = $parent[$modelClass]['id'];
659
 
660
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
661
		$this->Tree->id = $data[$modelClass]['id'];
662
		$this->Tree->saveField($parentField, $parentId);
663
 
664
		$result = $this->Tree->children($parentId, true, array('name'));
665
		$expected = array(array($modelClass => array('name' => '1.2.1')),
666
			array($modelClass => array('name' => '1.2.2')),
667
			array($modelClass => array('name' => '1.1')));
668
		$this->assertEquals($expected, $result);
669
 
670
		$validTree = $this->Tree->verify();
671
		$this->assertTrue($validTree);
672
	}
673
 
674
/**
675
 * testMoveDemoteInvalid method
676
 *
677
 * @return void
678
 */
679
	public function testMoveDemoteInvalid() {
680
		extract($this->settings);
681
		$this->Tree = new $modelClass();
682
		$this->Tree->initialize(2, 2);
683
		$this->Tree->id = null;
684
 
685
		$parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
686
		$parentId = $parent[$modelClass]['id'];
687
 
688
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
689
 
690
		$expected = $this->Tree->find('all');
691
		$before = $this->Tree->read(null, $data[$modelClass]['id']);
692
 
693
		$this->Tree->id = $parentId;
694
		$this->Tree->saveField($parentField, $data[$modelClass]['id']);
695
 
696
		$results = $this->Tree->find('all');
697
		$after = $this->Tree->read(null, $data[$modelClass]['id']);
698
 
699
		$this->assertEquals($expected, $results);
700
		$this->assertEquals($before, $after);
701
 
702
		$validTree = $this->Tree->verify();
703
		$this->assertTrue($validTree);
704
	}
705
 
706
/**
707
 * testMoveInvalid method
708
 *
709
 * @return void
710
 */
711
	public function testMoveInvalid() {
712
		extract($this->settings);
713
		$this->Tree = new $modelClass();
714
		$this->Tree->initialize(2, 2);
715
		$this->Tree->id = null;
716
 
717
		$initialCount = $this->Tree->find('count');
718
		$data = $this->Tree->findByName('1.1');
719
 
720
		$this->Tree->id = $data[$modelClass]['id'];
721
		$this->Tree->saveField($parentField, 999999);
722
 
723
		$laterCount = $this->Tree->find('count');
724
		$this->assertSame($initialCount, $laterCount);
725
 
726
		$validTree = $this->Tree->verify();
727
		$this->assertTrue($validTree);
728
	}
729
 
730
/**
731
 * testMoveSelfInvalid method
732
 *
733
 * @return void
734
 */
735
	public function testMoveSelfInvalid() {
736
		extract($this->settings);
737
		$this->Tree = new $modelClass();
738
		$this->Tree->initialize(2, 2);
739
		$this->Tree->id = null;
740
 
741
		$initialCount = $this->Tree->find('count');
742
		$data = $this->Tree->findByName('1.1');
743
 
744
		$this->Tree->id = $data[$modelClass]['id'];
745
		$saveSuccess = $this->Tree->saveField($parentField, $this->Tree->id);
746
 
747
		$this->assertFalse($saveSuccess);
748
		$laterCount = $this->Tree->find('count');
749
		$this->assertSame($initialCount, $laterCount);
750
 
751
		$validTree = $this->Tree->verify();
752
		$this->assertTrue($validTree);
753
	}
754
 
755
/**
756
 * testMoveUpSuccess method
757
 *
758
 * @return void
759
 */
760
	public function testMoveUpSuccess() {
761
		extract($this->settings);
762
		$this->Tree = new $modelClass();
763
		$this->Tree->initialize(2, 2);
764
 
765
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2')));
766
		$this->Tree->moveUp($data[$modelClass]['id']);
767
 
768
		$parent = $this->Tree->findByName('1. Root', array('id'));
769
		$this->Tree->id = $parent[$modelClass]['id'];
770
		$result = $this->Tree->children(null, true, array('name'));
771
		$expected = array(array($modelClass => array('name' => '1.2')),
772
			array($modelClass => array('name' => '1.1')));
773
		$this->assertSame($expected, $result);
774
	}
775
 
776
/**
777
 * testMoveUpFail method
778
 *
779
 * @return void
780
 */
781
	public function testMoveUpFail() {
782
		extract($this->settings);
783
		$this->Tree = new $modelClass();
784
		$this->Tree->initialize(2, 2);
785
 
786
		$data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.1')));
787
 
788
		$this->Tree->moveUp($data[$modelClass]['id']);
789
 
790
		$parent = $this->Tree->findByName('1. Root', array('id'));
791
		$this->Tree->id = $parent[$modelClass]['id'];
792
		$result = $this->Tree->children(null, true, array('name'));
793
		$expected = array(array($modelClass => array('name' => '1.1')),
794
			array($modelClass => array('name' => '1.2')));
795
		$this->assertSame($expected, $result);
796
	}
797
 
798
/**
799
 * testMoveUp2 method
800
 *
801
 * @return void
802
 */
803
	public function testMoveUp2() {
804
		extract($this->settings);
805
		$this->Tree = new $modelClass();
806
		$this->Tree->initialize(1, 10);
807
 
808
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
809
		$this->Tree->moveUp($data[$modelClass]['id'], 2);
810
 
811
		$parent = $this->Tree->findByName('1. Root', array('id'));
812
		$this->Tree->id = $parent[$modelClass]['id'];
813
		$result = $this->Tree->children(null, true, array('name'));
814
		$expected = array(
815
			array($modelClass => array('name' => '1.1')),
816
			array($modelClass => array('name' => '1.2')),
817
			array($modelClass => array('name' => '1.5')),
818
			array($modelClass => array('name' => '1.3')),
819
			array($modelClass => array('name' => '1.4')),
820
			array($modelClass => array('name' => '1.6')),
821
			array($modelClass => array('name' => '1.7')),
822
			array($modelClass => array('name' => '1.8')),
823
			array($modelClass => array('name' => '1.9')),
824
			array($modelClass => array('name' => '1.10')));
825
		$this->assertSame($expected, $result);
826
	}
827
 
828
/**
829
 * testMoveUpFirst method
830
 *
831
 * @return void
832
 */
833
	public function testMoveUpFirst() {
834
		extract($this->settings);
835
		$this->Tree = new $modelClass();
836
		$this->Tree->initialize(1, 10);
837
 
838
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
839
		$this->Tree->moveUp($data[$modelClass]['id'], true);
840
 
841
		$parent = $this->Tree->findByName('1. Root', array('id'));
842
		$this->Tree->id = $parent[$modelClass]['id'];
843
		$result = $this->Tree->children(null, true, array('name'));
844
		$expected = array(
845
			array($modelClass => array('name' => '1.5')),
846
			array($modelClass => array('name' => '1.1')),
847
			array($modelClass => array('name' => '1.2')),
848
			array($modelClass => array('name' => '1.3')),
849
			array($modelClass => array('name' => '1.4')),
850
			array($modelClass => array('name' => '1.6')),
851
			array($modelClass => array('name' => '1.7')),
852
			array($modelClass => array('name' => '1.8')),
853
			array($modelClass => array('name' => '1.9')),
854
			array($modelClass => array('name' => '1.10')));
855
		$this->assertSame($expected, $result);
856
	}
857
 
858
/**
859
 * testMoveDownSuccess method
860
 *
861
 * @return void
862
 */
863
	public function testMoveDownSuccess() {
864
		extract($this->settings);
865
		$this->Tree = new $modelClass();
866
		$this->Tree->initialize(2, 2);
867
 
868
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
869
		$this->Tree->moveDown($data[$modelClass]['id']);
870
 
871
		$parent = $this->Tree->findByName('1. Root', array('id'));
872
		$this->Tree->id = $parent[$modelClass]['id'];
873
		$result = $this->Tree->children(null, true, array('name'));
874
		$expected = array(array($modelClass => array('name' => '1.2')),
875
			array($modelClass => array('name' => '1.1')));
876
		$this->assertSame($expected, $result);
877
	}
878
 
879
/**
880
 * testMoveDownFail method
881
 *
882
 * @return void
883
 */
884
	public function testMoveDownFail() {
885
		extract($this->settings);
886
		$this->Tree = new $modelClass();
887
		$this->Tree->initialize(2, 2);
888
 
889
		$data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2')));
890
		$this->Tree->moveDown($data[$modelClass]['id']);
891
 
892
		$parent = $this->Tree->findByName('1. Root', array('id'));
893
		$this->Tree->id = $parent[$modelClass]['id'];
894
		$result = $this->Tree->children(null, true, array('name'));
895
		$expected = array(array($modelClass => array('name' => '1.1')),
896
			array($modelClass => array('name' => '1.2')));
897
		$this->assertSame($expected, $result);
898
	}
899
 
900
/**
901
 * testMoveDownLast method
902
 *
903
 * @return void
904
 */
905
	public function testMoveDownLast() {
906
		extract($this->settings);
907
		$this->Tree = new $modelClass();
908
		$this->Tree->initialize(1, 10);
909
 
910
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
911
		$this->Tree->moveDown($data[$modelClass]['id'], true);
912
 
913
		$parent = $this->Tree->findByName('1. Root', array('id'));
914
		$this->Tree->id = $parent[$modelClass]['id'];
915
		$result = $this->Tree->children(null, true, array('name'));
916
		$expected = array(
917
			array($modelClass => array('name' => '1.1')),
918
			array($modelClass => array('name' => '1.2')),
919
			array($modelClass => array('name' => '1.3')),
920
			array($modelClass => array('name' => '1.4')),
921
			array($modelClass => array('name' => '1.6')),
922
			array($modelClass => array('name' => '1.7')),
923
			array($modelClass => array('name' => '1.8')),
924
			array($modelClass => array('name' => '1.9')),
925
			array($modelClass => array('name' => '1.10')),
926
			array($modelClass => array('name' => '1.5')));
927
		$this->assertSame($expected, $result);
928
	}
929
 
930
/**
931
 * testMoveDown2 method
932
 *
933
 * @return void
934
 */
935
	public function testMoveDown2() {
936
		extract($this->settings);
937
		$this->Tree = new $modelClass();
938
		$this->Tree->initialize(1, 10);
939
 
940
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
941
		$this->Tree->moveDown($data[$modelClass]['id'], 2);
942
 
943
		$parent = $this->Tree->findByName('1. Root', array('id'));
944
		$this->Tree->id = $parent[$modelClass]['id'];
945
		$result = $this->Tree->children(null, true, array('name'));
946
		$expected = array(
947
			array($modelClass => array('name' => '1.1')),
948
			array($modelClass => array('name' => '1.2')),
949
			array($modelClass => array('name' => '1.3')),
950
			array($modelClass => array('name' => '1.4')),
951
			array($modelClass => array('name' => '1.6')),
952
			array($modelClass => array('name' => '1.7')),
953
			array($modelClass => array('name' => '1.5')),
954
			array($modelClass => array('name' => '1.8')),
955
			array($modelClass => array('name' => '1.9')),
956
			array($modelClass => array('name' => '1.10')));
957
		$this->assertSame($expected, $result);
958
	}
959
 
960
/**
961
 * testSaveNoMove method
962
 *
963
 * @return void
964
 */
965
	public function testSaveNoMove() {
966
		extract($this->settings);
967
		$this->Tree = new $modelClass();
968
		$this->Tree->initialize(1, 10);
969
 
970
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.5')));
971
		$this->Tree->id = $data[$modelClass]['id'];
972
		$this->Tree->saveField('name', 'renamed');
973
		$parent = $this->Tree->findByName('1. Root', array('id'));
974
		$this->Tree->id = $parent[$modelClass]['id'];
975
		$result = $this->Tree->children(null, true, array('name'));
976
		$expected = array(
977
			array($modelClass => array('name' => '1.1')),
978
			array($modelClass => array('name' => '1.2')),
979
			array($modelClass => array('name' => '1.3')),
980
			array($modelClass => array('name' => '1.4')),
981
			array($modelClass => array('name' => 'renamed')),
982
			array($modelClass => array('name' => '1.6')),
983
			array($modelClass => array('name' => '1.7')),
984
			array($modelClass => array('name' => '1.8')),
985
			array($modelClass => array('name' => '1.9')),
986
			array($modelClass => array('name' => '1.10')));
987
		$this->assertSame($expected, $result);
988
	}
989
 
990
/**
991
 * testMoveToRootAndMoveUp method
992
 *
993
 * @return void
994
 */
995
	public function testMoveToRootAndMoveUp() {
996
		extract($this->settings);
997
		$this->Tree = new $modelClass();
998
		$this->Tree->initialize(1, 1);
999
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
1000
		$this->Tree->id = $data[$modelClass]['id'];
1001
		$this->Tree->save(array($parentField => null));
1002
 
1003
		$result = $this->Tree->verify();
1004
		$this->assertTrue($result);
1005
 
1006
		$this->Tree->moveUp();
1007
 
1008
		$result = $this->Tree->find('all', array('fields' => 'name', 'order' => $modelClass . '.' . $leftField . ' ASC'));
1009
		$expected = array(array($modelClass => array('name' => '1.1')),
1010
			array($modelClass => array('name' => '1. Root')));
1011
		$this->assertSame($expected, $result);
1012
	}
1013
 
1014
/**
1015
 * testDelete method
1016
 *
1017
 * @return void
1018
 */
1019
	public function testDelete() {
1020
		extract($this->settings);
1021
		$this->Tree = new $modelClass();
1022
		$this->Tree->initialize(2, 2);
1023
 
1024
		$initialCount = $this->Tree->find('count');
1025
		$result = $this->Tree->findByName('1.1.1');
1026
 
1027
		$return = $this->Tree->delete($result[$modelClass]['id']);
1028
		$this->assertEquals(true, $return);
1029
 
1030
		$laterCount = $this->Tree->find('count');
1031
		$this->assertEquals($initialCount - 1, $laterCount);
1032
 
1033
		$validTree = $this->Tree->verify();
1034
		$this->assertTrue($validTree);
1035
 
1036
		$initialCount = $this->Tree->find('count');
1037
		$result = $this->Tree->findByName('1.1');
1038
 
1039
		$return = $this->Tree->delete($result[$modelClass]['id']);
1040
		$this->assertEquals(true, $return);
1041
 
1042
		$laterCount = $this->Tree->find('count');
1043
		$this->assertEquals($initialCount - 2, $laterCount);
1044
 
1045
		$validTree = $this->Tree->verify();
1046
		$this->assertTrue($validTree);
1047
	}
1048
 
1049
/**
1050
 * Test deleting a record that doesn't exist.
1051
 *
1052
 * @return void
1053
 */
1054
	public function testDeleteDoesNotExist() {
1055
		extract($this->settings);
1056
		$this->Tree = new $modelClass();
1057
		$this->Tree->initialize(2, 2);
1058
		$this->Tree->delete(99999);
1059
	}
1060
 
1061
/**
1062
 * testRemove method
1063
 *
1064
 * @return void
1065
 */
1066
	public function testRemove() {
1067
		extract($this->settings);
1068
		$this->Tree = new $modelClass();
1069
		$this->Tree->initialize(2, 2);
1070
		$initialCount = $this->Tree->find('count');
1071
		$result = $this->Tree->findByName('1.1');
1072
 
1073
		$this->Tree->removeFromTree($result[$modelClass]['id']);
1074
 
1075
		$laterCount = $this->Tree->find('count');
1076
		$this->assertEquals($initialCount, $laterCount);
1077
 
1078
		$children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'));
1079
		$expected = array(array($modelClass => array('name' => '1.1.1')),
1080
			array($modelClass => array('name' => '1.1.2')),
1081
			array($modelClass => array('name' => '1.2')));
1082
		$this->assertEquals($expected, $children);
1083
 
1084
		$topNodes = $this->Tree->children(false, true, array('name'));
1085
		$expected = array(array($modelClass => array('name' => '1. Root')),
1086
			array($modelClass => array('name' => '1.1')));
1087
		$this->assertEquals($expected, $topNodes);
1088
 
1089
		$validTree = $this->Tree->verify();
1090
		$this->assertTrue($validTree);
1091
	}
1092
 
1093
/**
1094
 * testRemoveLastTopParent method
1095
 *
1096
 * @return void
1097
 */
1098
	public function testRemoveLastTopParent() {
1099
		extract($this->settings);
1100
		$this->Tree = new $modelClass();
1101
		$this->Tree->initialize(2, 2);
1102
 
1103
		$initialCount = $this->Tree->find('count');
1104
		$initialTopNodes = $this->Tree->childCount(false);
1105
 
1106
		$result = $this->Tree->findByName('1. Root');
1107
		$this->Tree->removeFromTree($result[$modelClass]['id']);
1108
 
1109
		$laterCount = $this->Tree->find('count');
1110
		$laterTopNodes = $this->Tree->childCount(false);
1111
 
1112
		$this->assertEquals($initialCount, $laterCount);
1113
		$this->assertEquals($initialTopNodes, $laterTopNodes);
1114
 
1115
		$topNodes = $this->Tree->children(false, true, array('name'));
1116
		$expected = array(array($modelClass => array('name' => '1.1')),
1117
			array($modelClass => array('name' => '1.2')),
1118
			array($modelClass => array('name' => '1. Root')));
1119
 
1120
		$this->assertEquals($expected, $topNodes);
1121
 
1122
		$validTree = $this->Tree->verify();
1123
		$this->assertTrue($validTree);
1124
	}
1125
 
1126
/**
1127
 * testRemoveNoChildren method
1128
 *
1129
 * @return void
1130
 */
1131
	public function testRemoveNoChildren() {
1132
		extract($this->settings);
1133
		$this->Tree = new $modelClass();
1134
		$this->Tree->initialize(2, 2);
1135
		$initialCount = $this->Tree->find('count');
1136
 
1137
		$result = $this->Tree->findByName('1.1.1');
1138
		$this->Tree->removeFromTree($result[$modelClass]['id']);
1139
 
1140
		$laterCount = $this->Tree->find('count');
1141
		$this->assertEquals($initialCount, $laterCount);
1142
 
1143
		$nodes = $this->Tree->find('list', array('order' => $leftField));
1144
		$expected = array(
1145
			1 => '1. Root',
1146
			2 => '1.1',
1147
			4 => '1.1.2',
1148
			5 => '1.2',
1149
			6 => '1.2.1',
1150
			7 => '1.2.2',
1151
			3 => '1.1.1',
1152
		);
1153
 
1154
		$this->assertEquals($expected, $nodes);
1155
 
1156
		$validTree = $this->Tree->verify();
1157
		$this->assertTrue($validTree);
1158
	}
1159
 
1160
/**
1161
 * testRemoveAndDelete method
1162
 *
1163
 * @return void
1164
 */
1165
	public function testRemoveAndDelete() {
1166
		extract($this->settings);
1167
		$this->Tree = new $modelClass();
1168
		$this->Tree->initialize(2, 2);
1169
 
1170
		$initialCount = $this->Tree->find('count');
1171
		$result = $this->Tree->findByName('1.1');
1172
 
1173
		$this->Tree->removeFromTree($result[$modelClass]['id'], true);
1174
 
1175
		$laterCount = $this->Tree->find('count');
1176
		$this->assertEquals($initialCount - 1, $laterCount);
1177
 
1178
		$children = $this->Tree->children($result[$modelClass][$parentField], true, array('name'), $leftField . ' asc');
1179
		$expected = array(
1180
			array($modelClass => array('name' => '1.1.1')),
1181
			array($modelClass => array('name' => '1.1.2')),
1182
			array($modelClass => array('name' => '1.2'))
1183
		);
1184
		$this->assertEquals($expected, $children);
1185
 
1186
		$topNodes = $this->Tree->children(false, true, array('name'));
1187
		$expected = array(array($modelClass => array('name' => '1. Root')));
1188
		$this->assertEquals($expected, $topNodes);
1189
 
1190
		$validTree = $this->Tree->verify();
1191
		$this->assertTrue($validTree);
1192
	}
1193
 
1194
/**
1195
 * testRemoveAndDeleteNoChildren method
1196
 *
1197
 * @return void
1198
 */
1199
	public function testRemoveAndDeleteNoChildren() {
1200
		extract($this->settings);
1201
		$this->Tree = new $modelClass();
1202
		$this->Tree->initialize(2, 2);
1203
		$initialCount = $this->Tree->find('count');
1204
 
1205
		$result = $this->Tree->findByName('1.1.1');
1206
		$this->Tree->removeFromTree($result[$modelClass]['id'], true);
1207
 
1208
		$laterCount = $this->Tree->find('count');
1209
		$this->assertEquals($initialCount - 1, $laterCount);
1210
 
1211
		$nodes = $this->Tree->find('list', array('order' => $leftField));
1212
		$expected = array(
1213
			1 => '1. Root',
1214
			2 => '1.1',
1215
			4 => '1.1.2',
1216
			5 => '1.2',
1217
			6 => '1.2.1',
1218
			7 => '1.2.2',
1219
		);
1220
		$this->assertEquals($expected, $nodes);
1221
 
1222
		$validTree = $this->Tree->verify();
1223
		$this->assertTrue($validTree);
1224
	}
1225
 
1226
/**
1227
 * testChildren method
1228
 *
1229
 * @return void
1230
 */
1231
	public function testChildren() {
1232
		extract($this->settings);
1233
		$this->Tree = new $modelClass();
1234
		$this->Tree->initialize(2, 2);
1235
 
1236
		$data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
1237
		$this->Tree->id = $data[$modelClass]['id'];
1238
 
1239
		$direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
1240
		$expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
1241
			array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
1242
		$this->assertEquals($expected, $direct);
1243
 
1244
		$total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
1245
		$expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
1246
			array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
1247
			array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
1248
			array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
1249
			array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
1250
			array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12)));
1251
		$this->assertEquals($expected, $total);
1252
 
1253
		$this->assertEquals(array(), $this->Tree->children(10000));
1254
	}
1255
 
1256
/**
1257
 * testCountChildren method
1258
 *
1259
 * @return void
1260
 */
1261
	public function testCountChildren() {
1262
		extract($this->settings);
1263
		$this->Tree = new $modelClass();
1264
		$this->Tree->initialize(2, 2);
1265
 
1266
		$data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
1267
		$this->Tree->id = $data[$modelClass]['id'];
1268
 
1269
		$direct = $this->Tree->childCount(null, true);
1270
		$this->assertEquals(2, $direct);
1271
 
1272
		$total = $this->Tree->childCount();
1273
		$this->assertEquals(6, $total);
1274
 
1275
		$this->Tree->read(null, $data[$modelClass]['id']);
1276
		$id = $this->Tree->field('id', array($modelClass . '.name' => '1.2'));
1277
		$total = $this->Tree->childCount($id);
1278
		$this->assertEquals(2, $total);
1279
	}
1280
 
1281
/**
1282
 * testGetParentNode method
1283
 *
1284
 * @return void
1285
 */
1286
	public function testGetParentNode() {
1287
		extract($this->settings);
1288
		$this->Tree = new $modelClass();
1289
		$this->Tree->initialize(2, 2);
1290
 
1291
		$data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
1292
		$this->Tree->id = $data[$modelClass]['id'];
1293
 
1294
		$result = $this->Tree->getParentNode(null, array('name'));
1295
		$expected = array($modelClass => array('name' => '1.2'));
1296
		$this->assertSame($expected, $result);
1297
	}
1298
 
1299
/**
1300
 * testGetPath method
1301
 *
1302
 * @return void
1303
 */
1304
	public function testGetPath() {
1305
		extract($this->settings);
1306
		$this->Tree = new $modelClass();
1307
		$this->Tree->initialize(2, 2);
1308
 
1309
		$data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1.2.2')));
1310
		$this->Tree->id = $data[$modelClass]['id'];
1311
 
1312
		$result = $this->Tree->getPath(null, array('name'));
1313
		$expected = array(array($modelClass => array('name' => '1. Root')),
1314
			array($modelClass => array('name' => '1.2')),
1315
			array($modelClass => array('name' => '1.2.2')));
1316
		$this->assertSame($expected, $result);
1317
	}
1318
 
1319
/**
1320
 * testNoAmbiguousColumn method
1321
 *
1322
 * @return void
1323
 */
1324
	public function testNoAmbiguousColumn() {
1325
		extract($this->settings);
1326
		$this->Tree = new $modelClass();
1327
		$this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
1328
			array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
1329
		$this->Tree->initialize(2, 2);
1330
 
1331
		$data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
1332
		$this->Tree->id = $data[$modelClass]['id'];
1333
 
1334
		$direct = $this->Tree->children(null, true, array('id', 'name', $parentField, $leftField, $rightField));
1335
		$expected = array(array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
1336
			array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)));
1337
		$this->assertEquals($expected, $direct);
1338
 
1339
		$total = $this->Tree->children(null, null, array('id', 'name', $parentField, $leftField, $rightField));
1340
		$expected = array(
1341
			array($modelClass => array('id' => 2, 'name' => '1.1', $parentField => 1, $leftField => 2, $rightField => 7)),
1342
			array($modelClass => array('id' => 3, 'name' => '1.1.1', $parentField => 2, $leftField => 3, $rightField => 4)),
1343
			array($modelClass => array('id' => 4, 'name' => '1.1.2', $parentField => 2, $leftField => 5, $rightField => 6)),
1344
			array($modelClass => array('id' => 5, 'name' => '1.2', $parentField => 1, $leftField => 8, $rightField => 13)),
1345
			array($modelClass => array('id' => 6, 'name' => '1.2.1', $parentField => 5, $leftField => 9, $rightField => 10)),
1346
			array($modelClass => array('id' => 7, 'name' => '1.2.2', $parentField => 5, $leftField => 11, $rightField => 12))
1347
		);
1348
		$this->assertEquals($expected, $total);
1349
	}
1350
 
1351
/**
1352
 * testReorderTree method
1353
 *
1354
 * @return void
1355
 */
1356
	public function testReorderTree() {
1357
		extract($this->settings);
1358
		$this->Tree = new $modelClass();
1359
		$this->Tree->initialize(3, 3);
1360
		$nodes = $this->Tree->find('list', array('order' => $leftField));
1361
 
1362
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1')));
1363
		$this->Tree->moveDown($data[$modelClass]['id']);
1364
 
1365
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.2.1')));
1366
		$this->Tree->moveDown($data[$modelClass]['id']);
1367
 
1368
		$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.3.2.2')));
1369
		$this->Tree->moveDown($data[$modelClass]['id']);
1370
 
1371
		$unsortedNodes = $this->Tree->find('list', array('order' => $leftField));
1372
		$this->assertEquals($nodes, $unsortedNodes);
1373
		$this->assertNotEquals(array_keys($nodes), array_keys($unsortedNodes));
1374
 
1375
		$this->Tree->reorder();
1376
		$sortedNodes = $this->Tree->find('list', array('order' => $leftField));
1377
		$this->assertSame($nodes, $sortedNodes);
1378
	}
1379
 
1380
/**
1381
 * test reordering large-ish trees with cacheQueries = true.
1382
 * This caused infinite loops when moving down elements as stale data is returned
1383
 * from the memory cache
1384
 *
1385
 * @return void
1386
 */
1387
	public function testReorderBigTreeWithQueryCaching() {
1388
		extract($this->settings);
1389
		$this->Tree = new $modelClass();
1390
		$this->Tree->initialize(2, 10);
1391
 
1392
		$original = $this->Tree->cacheQueries;
1393
		$this->Tree->cacheQueries = true;
1394
		$this->Tree->reorder(array('field' => 'name', 'direction' => 'DESC'));
1395
		$this->assertTrue($this->Tree->cacheQueries, 'cacheQueries was not restored after reorder(). %s');
1396
		$this->Tree->cacheQueries = $original;
1397
	}
1398
 
1399
/**
1400
 * testGenerateTreeListWithSelfJoin method
1401
 *
1402
 * @return void
1403
 */
1404
	public function testGenerateTreeListWithSelfJoin() {
1405
		extract($this->settings);
1406
		$this->Tree = new $modelClass();
1407
		$this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
1408
			array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
1409
		$this->Tree->initialize(2, 2);
1410
 
1411
		$result = $this->Tree->generateTreeList();
1412
		$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');
1413
		$this->assertSame($expected, $result);
1414
	}
1415
 
1416
/**
1417
 * Test the formatting options of generateTreeList()
1418
 *
1419
 * @return void
1420
 */
1421
	public function testGenerateTreeListFormatting() {
1422
		extract($this->settings);
1423
		$this->Tree = new $modelClass();
1424
		$this->Tree->initialize(2, 2);
1425
 
1426
		$result = $this->Tree->generateTreeList(
1427
			null,
1428
			"{n}.$modelClass.id",
1429
			array('%s - %s', "{n}.$modelClass.id", "{n}.$modelClass.name")
1430
		);
1431
		$this->assertEquals('1 - 1. Root', $result[1]);
1432
		$this->assertEquals('_2 - 1.1', $result[2]);
1433
		$this->assertEquals('__3 - 1.1.1', $result[3]);
1434
	}
1435
 
1436
/**
1437
 * Test the formatting options of formatTreeList()
1438
 *
1439
 * @return void
1440
 */
1441
	public function testFormatTreeList() {
1442
		extract($this->settings);
1443
		$this->Tree = new $modelClass();
1444
		$this->Tree->initialize(2, 2);
1445
 
1446
		$options = array('order' => array('lft' => 'asc'));
1447
		$records = $this->Tree->find('all', $options);
1448
 
1449
		$options = array(
1450
			'keyPath' => "{n}.$modelClass.id",
1451
			'valuePath' => array('%s - %s', "{n}.$modelClass.id", "{n}.$modelClass.name"),
1452
			'spacer' => '--');
1453
		$result = $this->Tree->formatTreeList($records, $options);
1454
		$this->assertEquals('1 - 1. Root', $result[1]);
1455
		$this->assertEquals('--2 - 1.1', $result[2]);
1456
		$this->assertEquals('----3 - 1.1.1', $result[3]);
1457
	}
1458
 
1459
/**
1460
 * testArraySyntax method
1461
 *
1462
 * @return void
1463
 */
1464
	public function testArraySyntax() {
1465
		extract($this->settings);
1466
		$this->Tree = new $modelClass();
1467
		$this->Tree->initialize(3, 3);
1468
		$this->assertSame($this->Tree->childCount(2), $this->Tree->childCount(array('id' => 2)));
1469
		$this->assertSame($this->Tree->getParentNode(2), $this->Tree->getParentNode(array('id' => 2)));
1470
		$this->assertSame($this->Tree->getPath(4), $this->Tree->getPath(array('id' => 4)));
1471
	}
1472
 
1473
/**
1474
 * testFindThreaded method
1475
 *
1476
 * @return void
1477
 */
1478
	public function testFindThreaded() {
1479
		$Model = new Person();
1480
		$Model->recursive = -1;
1481
		$Model->Behaviors->load('Tree', array('parent' => 'mother_id'));
1482
 
1483
		$result = $Model->find('threaded');
1484
		$expected = array(
1485
			array(
1486
				'Person' => array(
1487
					'id' => '4',
1488
					'name' => 'mother - grand mother',
1489
					'mother_id' => '0',
1490
					'father_id' => '0'
1491
				),
1492
				'children' => array(
1493
					array(
1494
						'Person' => array(
1495
							'id' => '2',
1496
							'name' => 'mother',
1497
							'mother_id' => '4',
1498
							'father_id' => '5'
1499
						),
1500
						'children' => array(
1501
							array(
1502
								'Person' => array(
1503
									'id' => '1',
1504
									'name' => 'person',
1505
									'mother_id' => '2',
1506
									'father_id' => '3'
1507
								),
1508
								'children' => array()
1509
							)
1510
						)
1511
					)
1512
				)
1513
			),
1514
			array(
1515
				'Person' => array(
1516
					'id' => '5',
1517
					'name' => 'mother - grand father',
1518
					'mother_id' => '0',
1519
					'father_id' => '0'
1520
				),
1521
				'children' => array()
1522
			),
1523
			array(
1524
				'Person' => array(
1525
					'id' => '6',
1526
					'name' => 'father - grand mother',
1527
					'mother_id' => '0',
1528
					'father_id' => '0'
1529
				),
1530
				'children' => array(
1531
					array(
1532
						'Person' => array(
1533
							'id' => '3',
1534
							'name' => 'father',
1535
							'mother_id' => '6',
1536
							'father_id' => '7'
1537
						),
1538
						'children' => array()
1539
					)
1540
				)
1541
			),
1542
			array(
1543
				'Person' => array(
1544
					'id' => '7',
1545
					'name' => 'father - grand father',
1546
					'mother_id' => '0',
1547
					'father_id' => '0'
1548
				),
1549
				'children' => array()
1550
			)
1551
		);
1552
		$this->assertEquals($expected, $result);
1553
	}
1554
 
1555
	public function testLevel() {
1556
		extract($this->settings);
1557
		$this->Tree = new $modelClass();
1558
		$this->Tree->Behaviors->attach('Tree', array('level' => 'level'));
1559
		$this->Tree->initialize(2, 2);
1560
 
1561
		$result = $this->Tree->findByName('1. Root');
1562
		$this->assertEquals(0, $result[$modelClass][$level]);
1563
 
1564
		$result = $this->Tree->findByName('1.1');
1565
		$this->assertEquals(1, $result[$modelClass][$level]);
1566
 
1567
		$result = $this->Tree->findByName('1.2.2');
1568
		$this->assertEquals(2, $result[$modelClass][$level]);
1569
 
1570
		$result = $this->Tree->findByName('1.2.1');
1571
		$this->assertEquals(2, $result[$modelClass][$level]);
1572
 
1573
		// Save with parent_id not set
1574
		$this->Tree->save(array('id' => $result[$modelClass]['id'], 'name' => 'foo'));
1575
		$result = $this->Tree->findByName('foo');
1576
		$this->assertEquals(2, $result[$modelClass][$level]);
1577
 
1578
		// Save with parent_id not changed
1579
		$this->Tree->save(array(
1580
			'id' => $result[$modelClass]['id'],
1581
			'parent_id' => $result[$modelClass]['parent_id'],
1582
			'name' => 'foo2'
1583
		));
1584
		$result = $this->Tree->findByName('foo2');
1585
		$this->assertEquals(2, $result[$modelClass][$level]);
1586
 
1587
		// Save with parent_id changed
1588
		$result = $this->Tree->findByName('1.1');
1589
		$this->Tree->save(array(
1590
			'id' => $result[$modelClass]['id'],
1591
			'parent_id' => ''
1592
		));
1593
		$result = $this->Tree->findByName('1.1');
1594
		$this->assertEquals(0, $result[$modelClass][$level]);
1595
 
1596
		$result = $this->Tree->findByName('1.1.2');
1597
		$this->assertEquals(1, $result[$modelClass][$level]);
1598
 
1599
		$parent = $this->Tree->findByName('1.1.2');
1600
		$result = $this->Tree->findByName('1.2');
1601
		$this->Tree->save(array(
1602
			'id' => $result[$modelClass]['id'],
1603
			'parent_id' => $parent[$modelClass]['id']
1604
		));
1605
		$result = $this->Tree->findByName('1.2');
1606
		$this->assertEquals(2, $result[$modelClass][$level]);
1607
 
1608
		$result = $this->Tree->findByName('1.2.2');
1609
		$this->assertEquals(3, $result[$modelClass][$level]);
1610
	}
1611
}