Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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