Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
16591 anikendra 1
<?php
2
/**
3
 * TreeBehaviorScopedTest file
4
 *
5
 * A tree test using scope
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
 * TreeBehaviorScopedTest class
28
 *
29
 * @package       Cake.Test.Case.Model.Behavior
30
 */
31
class TreeBehaviorScopedTest 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' => 'FlagTree',
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.flag_tree', 'core.ad', 'core.campaign', 'core.translate', 'core.number_tree_two');
58
 
59
/**
60
 * testStringScope method
61
 *
62
 * @return void
63
 */
64
	public function testStringScope() {
65
		$this->Tree = new FlagTree();
66
		$this->Tree->order = null;
67
		$this->Tree->initialize(2, 3);
68
 
69
		$this->Tree->id = 1;
70
		$this->Tree->saveField('flag', 1);
71
		$this->Tree->id = 2;
72
		$this->Tree->saveField('flag', 1);
73
 
74
		$result = $this->Tree->children();
75
		$expected = array(
76
			array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
77
			array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
78
			array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
79
		);
80
		$this->assertEquals($expected, $result);
81
 
82
		$this->Tree->Behaviors->load('Tree', array('scope' => 'FlagTree.flag = 1'));
83
		$this->assertEquals(array(), $this->Tree->children());
84
 
85
		$this->Tree->id = 1;
86
		$this->Tree->Behaviors->load('Tree', array('scope' => 'FlagTree.flag = 1'));
87
 
88
		$result = $this->Tree->children();
89
		$expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
90
		$this->assertEquals($expected, $result);
91
 
92
		$this->assertTrue($this->Tree->delete());
93
		$this->assertEquals(11, $this->Tree->find('count'));
94
	}
95
 
96
/**
97
 * testArrayScope method
98
 *
99
 * @return void
100
 */
101
	public function testArrayScope() {
102
		$this->Tree = new FlagTree();
103
		$this->Tree->order = null;
104
		$this->Tree->initialize(2, 3);
105
 
106
		$this->Tree->id = 1;
107
		$this->Tree->saveField('flag', 1);
108
		$this->Tree->id = 2;
109
		$this->Tree->saveField('flag', 1);
110
 
111
		$result = $this->Tree->children();
112
		$expected = array(
113
			array('FlagTree' => array('id' => '3', 'name' => '1.1.1', 'parent_id' => '2', 'lft' => '3', 'rght' => '4', 'flag' => '0')),
114
			array('FlagTree' => array('id' => '4', 'name' => '1.1.2', 'parent_id' => '2', 'lft' => '5', 'rght' => '6', 'flag' => '0')),
115
			array('FlagTree' => array('id' => '5', 'name' => '1.1.3', 'parent_id' => '2', 'lft' => '7', 'rght' => '8', 'flag' => '0'))
116
		);
117
		$this->assertEquals($expected, $result);
118
 
119
		$this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
120
		$this->assertEquals(array(), $this->Tree->children());
121
 
122
		$this->Tree->id = 1;
123
		$this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
124
 
125
		$result = $this->Tree->children();
126
		$expected = array(array('FlagTree' => array('id' => '2', 'name' => '1.1', 'parent_id' => '1', 'lft' => '2', 'rght' => '9', 'flag' => '1')));
127
		$this->assertEquals($expected, $result);
128
 
129
		$this->assertTrue($this->Tree->delete());
130
		$this->assertEquals(11, $this->Tree->find('count'));
131
	}
132
 
133
/**
134
 * testSaveWithParentAndInvalidScope method
135
 *
136
 * Attempting to save an invalid data should not trigger an `Undefined offset`
137
 * error
138
 *
139
 * @return void
140
 */
141
	public function testSaveWithParentAndInvalidScope() {
142
		$this->Tree = new FlagTree();
143
		$this->Tree->order = null;
144
		$data = $this->Tree->create(array(
145
			'name' => 'Flag',
146
		));
147
		$tree = $this->Tree->save($data);
148
		$this->Tree->Behaviors->load('Tree', array(
149
			'scope' => array('FlagTree.flag' => 100)
150
		));
151
		$tree['FlagTree']['parent_id'] = 1;
152
		$result = $this->Tree->save($tree);
153
		$this->assertFalse($result);
154
	}
155
 
156
/**
157
 * testMoveUpWithScope method
158
 *
159
 * @return void
160
 */
161
	public function testMoveUpWithScope() {
162
		$this->Ad = new Ad();
163
		$this->Ad->order = null;
164
		$this->Ad->Behaviors->load('Tree', array('scope' => 'Campaign'));
165
		$this->Ad->moveUp(6);
166
 
167
		$this->Ad->id = 4;
168
		$result = $this->Ad->children();
169
		$this->assertEquals(array(6, 5), Hash::extract($result, '{n}.Ad.id'));
170
		$this->assertEquals(array(2, 2), Hash::extract($result, '{n}.Campaign.id'));
171
	}
172
 
173
/**
174
 * testMoveDownWithScope method
175
 *
176
 * @return void
177
 */
178
	public function testMoveDownWithScope() {
179
		$this->Ad = new Ad();
180
		$this->Ad->order = null;
181
		$this->Ad->Behaviors->load('Tree', array('scope' => 'Campaign'));
182
		$this->Ad->moveDown(6);
183
 
184
		$this->Ad->id = 4;
185
		$result = $this->Ad->children();
186
		$this->assertEquals(array(5, 6), Hash::extract($result, '{n}.Ad.id'));
187
		$this->assertEquals(array(2, 2), Hash::extract($result, '{n}.Campaign.id'));
188
	}
189
 
190
/**
191
 * Tests the interaction (non-interference) between TreeBehavior and other behaviors with respect
192
 * to callback hooks
193
 *
194
 * @return void
195
 */
196
	public function testTranslatingTree() {
197
		$this->Tree = new FlagTree();
198
		$this->Tree->order = null;
199
		$this->Tree->cacheQueries = false;
200
		$this->Tree->Behaviors->load('Translate', array('title'));
201
 
202
		//Save
203
		$this->Tree->create();
204
		$this->Tree->locale = 'eng';
205
		$data = array('FlagTree' => array(
206
			'title' => 'name #1',
207
			'name' => 'test',
208
			'locale' => 'eng',
209
			'parent_id' => null,
210
		));
211
		$this->Tree->save($data);
212
		$result = $this->Tree->find('all');
213
		$expected = array(array('FlagTree' => array(
214
			'id' => 1,
215
			'title' => 'name #1',
216
			'name' => 'test',
217
			'parent_id' => null,
218
			'lft' => 1,
219
			'rght' => 2,
220
			'flag' => 0,
221
			'locale' => 'eng',
222
		)));
223
		$this->assertEquals($expected, $result);
224
 
225
		// update existing record, same locale
226
		$this->Tree->create();
227
		$data['FlagTree']['title'] = 'Named 2';
228
		$this->Tree->id = 1;
229
		$this->Tree->save($data);
230
		$result = $this->Tree->find('all');
231
		$expected = array(array('FlagTree' => array(
232
			'id' => 1,
233
			'title' => 'Named 2',
234
			'name' => 'test',
235
			'parent_id' => null,
236
			'lft' => 1,
237
			'rght' => 2,
238
			'flag' => 0,
239
			'locale' => 'eng',
240
		)));
241
		$this->assertEquals($expected, $result);
242
 
243
		// update different locale, same record
244
		$this->Tree->create();
245
		$this->Tree->locale = 'deu';
246
		$this->Tree->id = 1;
247
		$data = array('FlagTree' => array(
248
			'id' => 1,
249
			'parent_id' => null,
250
			'title' => 'namen #1',
251
			'name' => 'test',
252
			'locale' => 'deu',
253
		));
254
		$this->Tree->save($data);
255
 
256
		$this->Tree->locale = 'deu';
257
		$result = $this->Tree->find('all');
258
		$expected = array(
259
			array(
260
				'FlagTree' => array(
261
					'id' => 1,
262
					'title' => 'namen #1',
263
					'name' => 'test',
264
					'parent_id' => null,
265
					'lft' => 1,
266
					'rght' => 2,
267
					'flag' => 0,
268
					'locale' => 'deu',
269
				)
270
			)
271
		);
272
		$this->assertEquals($expected, $result);
273
 
274
		// Save with bindTranslation
275
		$this->Tree->locale = 'eng';
276
		$data = array(
277
			'title' => array('eng' => 'New title', 'spa' => 'Nuevo leyenda'),
278
			'name' => 'test',
279
			'parent_id' => null
280
		);
281
		$this->Tree->create($data);
282
		$this->Tree->save();
283
 
284
		$this->Tree->unbindTranslation();
285
		$translations = array('title' => 'Title');
286
		$this->Tree->bindTranslation($translations, false);
287
		$this->Tree->locale = array('eng', 'spa');
288
 
289
		$result = $this->Tree->read();
290
		$expected = array(
291
			'FlagTree' => array(
292
				'id' => 2,
293
				'parent_id' => null,
294
				'locale' => 'eng',
295
				'name' => 'test',
296
				'title' => 'New title',
297
				'flag' => 0,
298
				'lft' => 3,
299
				'rght' => 4
300
			),
301
			'Title' => array(
302
				array('id' => 21, 'locale' => 'eng', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'title', 'content' => 'New title'),
303
				array('id' => 22, 'locale' => 'spa', 'model' => 'FlagTree', 'foreign_key' => 2, 'field' => 'title', 'content' => 'Nuevo leyenda')
304
			),
305
		);
306
		$this->assertEquals($expected, $result);
307
	}
308
 
309
/**
310
 * testGenerateTreeListWithSelfJoin method
311
 *
312
 * @return void
313
 */
314
	public function testAliasesWithScopeInTwoTreeAssociations() {
315
		extract($this->settings);
316
		$this->Tree = new $modelClass();
317
		$this->Tree->order = null;
318
		$this->Tree->initialize(2, 2);
319
 
320
		$this->TreeTwo = new NumberTreeTwo();
321
		$this->TreeTwo->order = null;
322
 
323
		$record = $this->Tree->find('first');
324
 
325
		$this->Tree->bindModel(array(
326
			'hasMany' => array(
327
				'SecondTree' => array(
328
					'className' => 'NumberTreeTwo',
329
					'foreignKey' => 'number_tree_id'
330
				)
331
			)
332
		));
333
		$this->TreeTwo->bindModel(array(
334
			'belongsTo' => array(
335
				'FirstTree' => array(
336
					'className' => $modelClass,
337
					'foreignKey' => 'number_tree_id'
338
				)
339
			)
340
		));
341
		$this->TreeTwo->Behaviors->load('Tree', array(
342
			'scope' => 'FirstTree'
343
		));
344
 
345
		$data = array(
346
			'NumberTreeTwo' => array(
347
				'name' => 'First',
348
				'number_tree_id' => $record['FlagTree']['id']
349
			)
350
		);
351
		$this->TreeTwo->create();
352
		$result = $this->TreeTwo->save($data);
353
		$this->assertFalse(empty($result));
354
 
355
		$result = $this->TreeTwo->find('first');
356
		$expected = array('NumberTreeTwo' => array(
357
			'id' => 1,
358
			'name' => 'First',
359
			'number_tree_id' => $record['FlagTree']['id'],
360
			'parent_id' => null,
361
			'lft' => 1,
362
			'rght' => 2
363
		));
364
		$this->assertEquals($expected, $result);
365
	}
366
 
367
/**
368
 * testGenerateTreeListWithScope method
369
 *
370
 * @return void
371
 */
372
	public function testGenerateTreeListWithScope() {
373
		extract($this->settings);
374
		$this->Tree = new $modelClass();
375
		$this->Tree->order = null;
376
		$this->Tree->initialize(2, 3);
377
 
378
		$this->Tree->id = 1;
379
		$this->Tree->saveField('flag', 1);
380
		$this->Tree->id = 2;
381
		$this->Tree->saveField('flag', 1);
382
 
383
		$this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
384
 
385
		$result = $this->Tree->generateTreeList();
386
		$expected = array(
387
			1 => '1. Root',
388
			2 => '_1.1'
389
		);
390
		$this->assertEquals($expected, $result);
391
 
392
		// As string.
393
		$this->Tree->Behaviors->load('Tree', array('scope' => 'FlagTree.flag = 1'));
394
 
395
		$result = $this->Tree->generateTreeList();
396
		$this->assertEquals($expected, $result);
397
 
398
		// Merging conditions.
399
		$result = $this->Tree->generateTreeList(array('FlagTree.id >' => 1));
400
		$expected = array(
401
			2 => '1.1'
402
		);
403
		$this->assertEquals($expected, $result);
404
	}
405
 
406
/**
407
 * testRecoverUsingParentMode method
408
 *
409
 * @return void
410
 */
411
	public function testRecoverUsingParentMode() {
412
		extract($this->settings);
413
		$this->Tree = new $modelClass();
414
		$this->Tree->order = null;
415
		$this->Tree->initialize(2, 3);
416
 
417
		$this->Tree->Behaviors->load('Tree', array('scope' => 'FlagTree.flag = 1'));
418
		$this->Tree->Behaviors->disable('Tree');
419
 
420
		$this->Tree->create();
421
		$this->Tree->save(array('name' => 'Main', $parentField => null, $leftField => 0, $rightField => 0, 'flag' => 1));
422
		$node1 = $this->Tree->id;
423
 
424
		$this->Tree->create();
425
		$this->Tree->save(array('name' => 'About Us', $parentField => $node1, $leftField => 0, $rightField => 0, 'flag' => 1));
426
		$node11 = $this->Tree->id;
427
 
428
		$this->Tree->create();
429
		$this->Tree->save(array('name' => 'Programs', $parentField => $node1, $leftField => 0, $rightField => 0, 'flag' => 1));
430
		$node12 = $this->Tree->id;
431
 
432
		$this->Tree->create();
433
		$this->Tree->save(array('name' => 'Mission and History', $parentField => $node11, $leftField => 0, $rightField => 0, 'flag' => 1));
434
 
435
		$this->Tree->create();
436
		$this->Tree->save(array('name' => 'Overview', $parentField => $node12, $leftField => 0, $rightField => 0, 'flag' => 1));
437
 
438
		$this->Tree->Behaviors->enable('Tree');
439
 
440
		$result = $this->Tree->verify();
441
		$this->assertNotSame(true, $result);
442
 
443
		$result = $this->Tree->recover();
444
		$this->assertTrue($result);
445
 
446
		$result = $this->Tree->verify();
447
		$this->assertTrue($result);
448
 
449
		$result = $this->Tree->find('first', array(
450
			'fields' => array('name', $parentField, $leftField, $rightField, 'flag'),
451
			'conditions' => array('name' => 'Main'),
452
			'recursive' => -1
453
		));
454
		$expected = array(
455
			$modelClass => array(
456
				'name' => 'Main',
457
				$parentField => null,
458
				$leftField => 1,
459
				$rightField => 10,
460
				'flag' => 1
461
			)
462
		);
463
		$this->assertEquals($expected, $result);
464
	}
465
 
466
/**
467
 * testRecoverFromMissingParent method
468
 *
469
 * @return void
470
 */
471
	public function testRecoverFromMissingParent() {
472
		extract($this->settings);
473
		$this->Tree = new $modelClass();
474
		$this->Tree->order = null;
475
		$this->Tree->initialize(2, 2);
476
 
477
		$this->Tree->id = 1;
478
		$this->Tree->saveField('flag', 1);
479
		$this->Tree->id = 2;
480
		$this->Tree->saveField('flag', 1);
481
 
482
		$this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
483
 
484
		$result = $this->Tree->findByName('1.1');
485
		$this->Tree->updateAll(array($parentField => 999999), array('id' => $result[$modelClass]['id']));
486
 
487
		$result = $this->Tree->verify();
488
		$this->assertNotSame(true, $result);
489
 
490
		$result = $this->Tree->recover();
491
		$this->assertTrue($result);
492
 
493
		$result = $this->Tree->verify();
494
		$this->assertTrue($result);
495
	}
496
 
497
/**
498
 * testDetectInvalidParents method
499
 *
500
 * @return void
501
 */
502
	public function testDetectInvalidParents() {
503
		extract($this->settings);
504
		$this->Tree = new $modelClass();
505
		$this->Tree->order = null;
506
		$this->Tree->initialize(2, 2);
507
 
508
		$this->Tree->id = 1;
509
		$this->Tree->saveField('flag', 1);
510
		$this->Tree->id = 2;
511
		$this->Tree->saveField('flag', 1);
512
 
513
		$this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
514
 
515
		$this->Tree->updateAll(array($parentField => null));
516
 
517
		$result = $this->Tree->verify();
518
		$this->assertNotSame(true, $result);
519
 
520
		$result = $this->Tree->recover();
521
		$this->assertTrue($result);
522
 
523
		$result = $this->Tree->verify();
524
		$this->assertTrue($result);
525
	}
526
 
527
/**
528
 * testDetectInvalidLftsRghts method
529
 *
530
 * @return void
531
 */
532
	public function testDetectInvalidLftsRghts() {
533
		extract($this->settings);
534
		$this->Tree = new $modelClass();
535
		$this->Tree->order = null;
536
		$this->Tree->initialize(2, 2);
537
 
538
		$this->Tree->id = 1;
539
		$this->Tree->saveField('flag', 1);
540
		$this->Tree->id = 2;
541
		$this->Tree->saveField('flag', 1);
542
 
543
		$this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
544
 
545
		$this->Tree->updateAll(array($leftField => 0, $rightField => 0));
546
 
547
		$result = $this->Tree->verify();
548
		$this->assertNotSame(true, $result);
549
 
550
		$this->Tree->recover();
551
 
552
		$result = $this->Tree->verify();
553
		$this->assertTrue($result);
554
	}
555
 
556
/**
557
 * Reproduces a situation where a single node has lft= rght, and all other lft and rght fields follow sequentially
558
 *
559
 * @return void
560
 */
561
	public function testDetectEqualLftsRghts() {
562
		extract($this->settings);
563
		$this->Tree = new $modelClass();
564
		$this->Tree->order = null;
565
		$this->Tree->initialize(1, 3);
566
 
567
		$this->Tree->id = 1;
568
		$this->Tree->saveField('flag', 1);
569
		$this->Tree->id = 2;
570
		$this->Tree->saveField('flag', 1);
571
 
572
		$this->Tree->Behaviors->load('Tree', array('scope' => array('FlagTree.flag' => 1)));
573
 
574
		$result = $this->Tree->findByName('1.1');
575
		$this->Tree->updateAll(array($rightField => $result[$modelClass][$leftField]), array('id' => $result[$modelClass]['id']));
576
		$this->Tree->updateAll(array($leftField => $this->Tree->escapeField($leftField) . ' -1'),
577
			array($leftField . ' >' => $result[$modelClass][$leftField]));
578
		$this->Tree->updateAll(array($rightField => $this->Tree->escapeField($rightField) . ' -1'),
579
			array($rightField . ' >' => $result[$modelClass][$leftField]));
580
 
581
		$result = $this->Tree->verify();
582
		$this->assertNotSame(true, $result);
583
 
584
		$result = $this->Tree->recover();
585
		$this->assertTrue($result);
586
 
587
		$result = $this->Tree->verify();
588
		$this->assertTrue($result);
589
	}
590
 
591
}