/
TreeBehaviorTest.php
560 lines (497 loc) · 15.2 KB
/
TreeBehaviorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 3.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Behavior;
use Cake\Collection\Collection;
use Cake\Event\Event;
use Cake\Model\Behavior\TranslateBehavior;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
/**
* Translate behavior test case
*/
class TreeBehaviorTest extends TestCase {
/**
* fixtures
*
* @var array
*/
public $fixtures = [
'core.number_tree',
'core.menu_link_tree'
];
public function setUp() {
parent::setUp();
$this->table = TableRegistry::get('NumberTrees');
$this->table->addBehavior('Tree');
}
public function tearDown() {
parent::tearDown();
TableRegistry::clear();
}
/**
* Tests the find('path') method
*
* @return void
*/
public function testFindPath() {
$nodes = $this->table->find('path', ['for' => 9]);
$this->assertEquals([1, 6, 9], $nodes->extract('id')->toArray());
$nodes = $this->table->find('path', ['for' => 10]);
$this->assertEquals([1, 6, 10], $nodes->extract('id')->toArray());
$nodes = $this->table->find('path', ['for' => 5]);
$this->assertEquals([1, 2, 5], $nodes->extract('id')->toArray());
$nodes = $this->table->find('path', ['for' => 1]);
$this->assertEquals([1], $nodes->extract('id')->toArray());
// find path with scope
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$nodes = $table->find('path', ['for' => 5]);
$this->assertEquals([1, 3, 4, 5], $nodes->extract('id')->toArray());
}
/**
* Tests the childCount() method
*
* @return void
*/
public function testChildCount() {
// direct children for the root node
$countDirect = $this->table->childCount(1, true);
$this->assertEquals(2, $countDirect);
// counts all the children of root
$count = $this->table->childCount(1, false);
$this->assertEquals(9, $count);
// counts direct children
$count = $this->table->childCount(2, false);
$this->assertEquals(3, $count);
// count children for a middle-node
$count = $this->table->childCount(6, false);
$this->assertEquals(4, $count);
// count leaf children
$count = $this->table->childCount(10, false);
$this->assertEquals(0, $count);
// test scoping
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$count = $table->childCount(3, false);
$this->assertEquals(2, $count);
}
/**
* Tests the childCount() plus callable scoping
*
* @return void
*/
public function testCallableScoping() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', [
'scope' => function ($query) {
return $query->where(['menu' => 'main-menu']);
}
]);
$count = $table->childCount(1, false);
$this->assertEquals(4, $count);
}
/**
* Tests the find('children') method
*
* @return void
*/
public function testFindChildren() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
// root
$nodeIds = [];
$nodes = $table->find('children', ['for' => 1])->all();
$this->assertEquals([2, 3, 4, 5], $nodes->extract('id')->toArray());
// leaf
$nodeIds = [];
$nodes = $table->find('children', ['for' => 5])->all();
$this->assertEquals(0, count($nodes->extract('id')->toArray()));
// direct children
$nodes = $table->find('children', ['for' => 1, 'direct' => true])->all();
$this->assertEquals([2, 3], $nodes->extract('id')->toArray());
}
/**
* Tests that find('children') will throw an exception if the node was not found
*
* @expectedException \Cake\ORM\Error\RecordNotFoundException
* @return void
*/
public function testFindChildrenException() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$query = $table->find('children', ['for' => 500]);
}
/**
* Tests the moveUp() method
*
* @return void
*/
public function testMoveUp() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
// top level, wont move
$this->assertFalse($this->table->moveUp(1, 10));
// edge cases
$this->assertFalse($this->table->moveUp(1, 0));
$this->assertFalse($this->table->moveUp(1, -10));
// move inner node
$result = $table->moveUp(3, 1);
$nodes = $table->find('children', ['for' => 1])->all();
$this->assertEquals([3, 4, 5, 2], $nodes->extract('id')->toArray());
$this->assertTrue($result);
// move leaf
$this->assertFalse($table->moveUp(5, 1));
// move to first position
$table->moveUp(8, true);
$nodes = $table->find()
->select(['id'])
->where(function($exp) {
return $exp->isNull('parent_id');
})
->where(['menu' => 'main-menu'])
->order(['lft' => 'ASC'])
->all();
$this->assertEquals([8, 1, 6], $nodes->extract('id')->toArray());
}
/**
* Tests that moveUp() will throw an exception if the node was not found
*
* @expectedException \Cake\ORM\Error\RecordNotFoundException
* @return void
*/
public function testMoveUpException() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$table->moveUp(500, 1);
}
/**
* Tests the moveDown() method
*
* @return void
*/
public function testMoveDown() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
// latest node, wont move
$this->assertFalse($this->table->moveDown(8, 10));
// edge cases
$this->assertFalse($this->table->moveDown(8, 0));
$this->assertFalse($this->table->moveUp(8, -10));
// move inner node
$result = $table->moveDown(2, 1);
$nodes = $table->find('children', ['for' => 1])->all();
$this->assertEquals([3, 4, 5, 2], $nodes->extract('id')->toArray());
$this->assertTrue($result);
// move leaf
$this->assertFalse( $table->moveDown(5, 1));
// move to last position
$table->moveDown(1, true);
$nodes = $table->find()
->select(['id'])
->where(function($exp) {
return $exp->isNull('parent_id');
})
->where(['menu' => 'main-menu'])
->order(['lft' => 'ASC'])
->all();
$this->assertEquals([6, 8, 1], $nodes->extract('id')->toArray());
}
/**
* Tests that moveDown() will throw an exception if the node was not found
*
* @expectedException \Cake\ORM\Error\RecordNotFoundException
* @return void
*/
public function testMoveDownException() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$table->moveDown(500, 1);
}
/**
* Tests the recover function
*
* @return void
*/
public function testRecover() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$table->updateAll(['lft' => null, 'rght' => null], []);
$table->recover();
$result = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $result);
}
/**
* Tests the recover function with a custom scope
*
* @return void
*/
public function testRecoverScoped() {
$table = TableRegistry::get('MenuLinkTrees');
$table->addBehavior('Tree', ['scope' => ['menu' => 'main-menu']]);
$expected = $table->find()
->where(['menu' => 'main-menu'])
->order('lft')
->hydrate(false)
->toArray();
$expected2 = $table->find()
->where(['menu' => 'categories'])
->order('lft')
->hydrate(false)
->toArray();
$table->updateAll(['lft' => null, 'rght' => null], ['menu' => 'main-menu']);
$table->recover();
$result = $table->find()
->where(['menu' => 'main-menu'])
->order('lft')
->hydrate(false)
->toArray();
$this->assertEquals($expected, $result);
$result2 = $table->find()
->where(['menu' => 'categories'])
->order('lft')
->hydrate(false)
->toArray();
$this->assertEquals($expected2, $result2);
}
/**
* Tests adding a new orphan node
*
* @return void
*/
public function testAddOrphan() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = new Entity(
['name' => 'New Orphan', 'parent_id' => null],
['markNew' => true]
);
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertSame($entity, $table->save($entity));
$this->assertEquals(23, $entity->lft);
$this->assertEquals(24, $entity->rght);
$expected[] = $entity->toArray();
$results = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $results);
}
/**
* Tests that adding a child node as a decendant of one of the roots works
*
* @return void
*/
public function testAddMiddle() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = new Entity(
['name' => 'laptops', 'parent_id' => 1],
['markNew' => true]
);
$this->assertSame($entity, $table->save($entity));
$this->assertEquals(20, $entity->lft);
$this->assertEquals(21, $entity->rght);
$result = $table->find()->order('lft')->hydrate(false)->toArray();
$table->recover();
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $result);
}
/**
* Tests adding a leaf to the tree
*
* @return void
*/
public function testAddLeaf() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = new Entity(
['name' => 'laptops', 'parent_id' => 2],
['markNew' => true]
);
$this->assertSame($entity, $table->save($entity));
$this->assertEquals(9, $entity->lft);
$this->assertEquals(10, $entity->rght);
$results = $table->find()->order('lft')->hydrate(false)->toArray();
$table->recover();
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $results);
}
/**
* Tests moving a subtree to the right
*
* @return void
*/
public function testReParentSubTreeRight() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(2);
$entity->parent_id = 6;
$this->assertSame($entity, $table->save($entity));
$this->assertEquals(11, $entity->lft);
$this->assertEquals(18, $entity->rght);
$result = $table->find()->order('lft')->hydrate(false);
$expected = [1, 6, 7, 8, 9, 10, 2, 3, 4, 5, 11];
$this->assertEquals($expected, $result->extract('id')->toArray());
$numbers = [];
$result->each(function($v) use (&$numbers) {
$numbers[] = $v['lft'];
$numbers[] = $v['rght'];
});
sort($numbers);
$this->assertEquals(range(1, 22), $numbers);
}
/**
* Tests moving a subtree to the left
*
* @return void
*/
public function testReParentSubTreeLeft() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(6);
$entity->parent_id = 2;
$this->assertSame($entity, $table->save($entity));
$this->assertEquals(9, $entity->lft);
$this->assertEquals(18, $entity->rght);
$result = $table->find()->order('lft')->hydrate(false)->toArray();
$table->recover();
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $result);
}
/**
* Test moving a leaft to the left
*
* @return void
*/
public function testReParentLeafLeft() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(10);
$entity->parent_id = 2;
$this->assertSame($entity, $table->save($entity));
$this->assertEquals(9, $entity->lft);
$this->assertEquals(10, $entity->rght);
$result = $table->find()->order('lft')->hydrate(false)->toArray();
$table->recover();
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $result);
}
/**
* Test moving a leaft to the left
*
* @return void
*/
public function testReParentLeafRight() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(5);
$entity->parent_id = 6;
$this->assertSame($entity, $table->save($entity));
$this->assertEquals(17, $entity->lft);
$this->assertEquals(18, $entity->rght);
$result = $table->find()->order('lft')->hydrate(false);
$expected = [1, 2, 3, 4, 6, 7, 8, 9, 10, 5, 11];
$this->assertEquals($expected, $result->extract('id')->toArray());
$numbers = [];
$result->each(function($v) use (&$numbers) {
$numbers[] = $v['lft'];
$numbers[] = $v['rght'];
});
sort($numbers);
$this->assertEquals(range(1, 22), $numbers);
}
/**
* Tests moving a subtree as a new root
*
* @return void
*/
public function testRootingSubTree() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(2);
$entity->parent_id = null;
$this->assertSame($entity, $table->save($entity));
$this->assertEquals(15, $entity->lft);
$this->assertEquals(22, $entity->rght);
$result = $table->find()->order('lft')->hydrate(false);
$expected = [1, 6, 7, 8, 9, 10, 11, 2, 3, 4, 5];
$this->assertEquals($expected, $result->extract('id')->toArray());
$numbers = [];
$result->each(function($v) use (&$numbers) {
$numbers[] = $v['lft'];
$numbers[] = $v['rght'];
});
sort($numbers);
$this->assertEquals(range(1, 22), $numbers);
}
/**
* Tests that trying to create a cycle throws an exception
*
* @expectedException RuntimeException
* @expectedExceptionMessage Cannot use node "5" as parent for entity "2"
* @return void
*/
public function testReparentCycle() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(2);
$entity->parent_id = 5;
$table->save($entity);
}
/**
* Tests deleting a leaf in the tree
*
* @return void
*/
public function testDeleteLeaf() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(4);
$this->assertTrue($table->delete($entity));
$result = $table->find()->order('lft')->hydrate(false)->toArray();
$table->recover();
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $result);
}
/**
* Tests deleting a subtree
*
* @return void
*/
public function testDeleteSubTree() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(6);
$this->assertTrue($table->delete($entity));
$result = $table->find()->order('lft')->hydrate(false)->toArray();
$table->recover();
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $result);
}
/**
* Test deleting a root node
*
* @return void
*/
public function testDeleteRoot() {
$table = TableRegistry::get('NumberTrees');
$table->addBehavior('Tree');
$entity = $table->get(1);
$this->assertTrue($table->delete($entity));
$result = $table->find()->order('lft')->hydrate(false)->toArray();
$table->recover();
$expected = $table->find()->order('lft')->hydrate(false)->toArray();
$this->assertEquals($expected, $result);
}
}