Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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