Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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