| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* TreeBehaviorUuidTest file
|
|
|
4 |
*
|
|
|
5 |
* Tree test using UUIDs
|
|
|
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 |
App::uses('String', 'Utility');
|
|
|
24 |
|
|
|
25 |
require_once dirname(dirname(__FILE__)) . DS . 'models.php';
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* TreeBehaviorUuidTest class
|
|
|
29 |
*
|
|
|
30 |
* @package Cake.Test.Case.Model.Behavior
|
|
|
31 |
*/
|
|
|
32 |
class TreeBehaviorUuidTest extends CakeTestCase {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Whether backup global state for each test method or not
|
|
|
36 |
*
|
|
|
37 |
* @var boolean
|
|
|
38 |
*/
|
|
|
39 |
public $backupGlobals = false;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* settings property
|
|
|
43 |
*
|
|
|
44 |
* @var array
|
|
|
45 |
*/
|
|
|
46 |
public $settings = array(
|
|
|
47 |
'modelClass' => 'UuidTree',
|
|
|
48 |
'leftField' => 'lft',
|
|
|
49 |
'rightField' => 'rght',
|
|
|
50 |
'parentField' => 'parent_id'
|
|
|
51 |
);
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* fixtures property
|
|
|
55 |
*
|
|
|
56 |
* @var array
|
|
|
57 |
*/
|
|
|
58 |
public $fixtures = array('core.uuid_tree');
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* testAddWithPreSpecifiedId method
|
|
|
62 |
*
|
|
|
63 |
* @return void
|
|
|
64 |
*/
|
|
|
65 |
public function testAddWithPreSpecifiedId() {
|
|
|
66 |
extract($this->settings);
|
|
|
67 |
$this->Tree = new $modelClass();
|
|
|
68 |
$this->Tree->order = null;
|
|
|
69 |
$this->Tree->initialize(2, 2);
|
|
|
70 |
|
|
|
71 |
$data = $this->Tree->find('first', array(
|
|
|
72 |
'fields' => array('id'),
|
|
|
73 |
'conditions' => array($modelClass . '.name' => '1.1')
|
|
|
74 |
));
|
|
|
75 |
|
|
|
76 |
$id = String::uuid();
|
|
|
77 |
$this->Tree->create();
|
|
|
78 |
$result = $this->Tree->save(array($modelClass => array(
|
|
|
79 |
'id' => $id,
|
|
|
80 |
'name' => 'testAddMiddle',
|
|
|
81 |
$parentField => $data[$modelClass]['id'])
|
|
|
82 |
));
|
|
|
83 |
$expected = array_merge(
|
|
|
84 |
array($modelClass => array('id' => $id, 'name' => 'testAddMiddle', $parentField => '2')),
|
|
|
85 |
$result
|
|
|
86 |
);
|
|
|
87 |
$this->assertSame($expected, $result);
|
|
|
88 |
|
|
|
89 |
$this->assertTrue($this->Tree->verify());
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* testMovePromote method
|
|
|
94 |
*
|
|
|
95 |
* @return void
|
|
|
96 |
*/
|
|
|
97 |
public function testMovePromote() {
|
|
|
98 |
extract($this->settings);
|
|
|
99 |
$this->Tree = new $modelClass();
|
|
|
100 |
$this->Tree->order = null;
|
|
|
101 |
$this->Tree->initialize(2, 2);
|
|
|
102 |
$this->Tree->id = null;
|
|
|
103 |
|
|
|
104 |
$parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
|
|
|
105 |
$parentId = $parent[$modelClass]['id'];
|
|
|
106 |
|
|
|
107 |
$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
|
|
|
108 |
$this->Tree->id = $data[$modelClass]['id'];
|
|
|
109 |
$this->Tree->saveField($parentField, $parentId);
|
|
|
110 |
$direct = $this->Tree->children($parentId, true, array('name', $leftField, $rightField));
|
|
|
111 |
$expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
|
|
|
112 |
array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
|
|
|
113 |
array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
|
|
|
114 |
$this->assertEquals($direct, $expects);
|
|
|
115 |
$validTree = $this->Tree->verify();
|
|
|
116 |
$this->assertTrue($validTree);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* testMoveWithWhitelist method
|
|
|
121 |
*
|
|
|
122 |
* @return void
|
|
|
123 |
*/
|
|
|
124 |
public function testMoveWithWhitelist() {
|
|
|
125 |
extract($this->settings);
|
|
|
126 |
$this->Tree = new $modelClass();
|
|
|
127 |
$this->Tree->order = null;
|
|
|
128 |
$this->Tree->initialize(2, 2);
|
|
|
129 |
$this->Tree->id = null;
|
|
|
130 |
|
|
|
131 |
$parent = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
|
|
|
132 |
$parentId = $parent[$modelClass]['id'];
|
|
|
133 |
|
|
|
134 |
$data = $this->Tree->find('first', array('fields' => array('id'), 'conditions' => array($modelClass . '.name' => '1.1.1')));
|
|
|
135 |
$this->Tree->id = $data[$modelClass]['id'];
|
|
|
136 |
$this->Tree->whitelist = array($parentField, 'name', 'description');
|
|
|
137 |
$this->Tree->saveField($parentField, $parentId);
|
|
|
138 |
|
|
|
139 |
$result = $this->Tree->children($parentId, true, array('name', $leftField, $rightField));
|
|
|
140 |
$expected = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 5)),
|
|
|
141 |
array($modelClass => array('name' => '1.2', $leftField => 6, $rightField => 11)),
|
|
|
142 |
array($modelClass => array('name' => '1.1.1', $leftField => 12, $rightField => 13)));
|
|
|
143 |
$this->assertEquals($expected, $result);
|
|
|
144 |
$this->assertTrue($this->Tree->verify());
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* testRemoveNoChildren method
|
|
|
149 |
*
|
|
|
150 |
* @return void
|
|
|
151 |
*/
|
|
|
152 |
public function testRemoveNoChildren() {
|
|
|
153 |
extract($this->settings);
|
|
|
154 |
$this->Tree = new $modelClass();
|
|
|
155 |
$this->Tree->order = null;
|
|
|
156 |
$this->Tree->initialize(2, 2);
|
|
|
157 |
$initialCount = $this->Tree->find('count');
|
|
|
158 |
|
|
|
159 |
$result = $this->Tree->findByName('1.1.1');
|
|
|
160 |
$this->Tree->removeFromTree($result[$modelClass]['id']);
|
|
|
161 |
|
|
|
162 |
$laterCount = $this->Tree->find('count');
|
|
|
163 |
$this->assertEquals($initialCount, $laterCount);
|
|
|
164 |
|
|
|
165 |
$nodes = $this->Tree->find('list', array('order' => $leftField));
|
|
|
166 |
$expects = array(
|
|
|
167 |
'1. Root',
|
|
|
168 |
'1.1',
|
|
|
169 |
'1.1.2',
|
|
|
170 |
'1.2',
|
|
|
171 |
'1.2.1',
|
|
|
172 |
'1.2.2',
|
|
|
173 |
'1.1.1',
|
|
|
174 |
);
|
|
|
175 |
|
|
|
176 |
$this->assertEquals(array_values($nodes), $expects);
|
|
|
177 |
|
|
|
178 |
$validTree = $this->Tree->verify();
|
|
|
179 |
$this->assertTrue($validTree);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
/**
|
|
|
183 |
* testRemoveAndDeleteNoChildren method
|
|
|
184 |
*
|
|
|
185 |
* @return void
|
|
|
186 |
*/
|
|
|
187 |
public function testRemoveAndDeleteNoChildren() {
|
|
|
188 |
extract($this->settings);
|
|
|
189 |
$this->Tree = new $modelClass();
|
|
|
190 |
$this->Tree->order = null;
|
|
|
191 |
$this->Tree->initialize(2, 2);
|
|
|
192 |
$initialCount = $this->Tree->find('count');
|
|
|
193 |
|
|
|
194 |
$result = $this->Tree->findByName('1.1.1');
|
|
|
195 |
$this->Tree->removeFromTree($result[$modelClass]['id'], true);
|
|
|
196 |
|
|
|
197 |
$laterCount = $this->Tree->find('count');
|
|
|
198 |
$this->assertEquals($initialCount - 1, $laterCount);
|
|
|
199 |
|
|
|
200 |
$nodes = $this->Tree->find('list', array('order' => $leftField));
|
|
|
201 |
$expects = array(
|
|
|
202 |
'1. Root',
|
|
|
203 |
'1.1',
|
|
|
204 |
'1.1.2',
|
|
|
205 |
'1.2',
|
|
|
206 |
'1.2.1',
|
|
|
207 |
'1.2.2',
|
|
|
208 |
);
|
|
|
209 |
$this->assertEquals(array_values($nodes), $expects);
|
|
|
210 |
|
|
|
211 |
$validTree = $this->Tree->verify();
|
|
|
212 |
$this->assertTrue($validTree);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
/**
|
|
|
216 |
* testChildren method
|
|
|
217 |
*
|
|
|
218 |
* @return void
|
|
|
219 |
*/
|
|
|
220 |
public function testChildren() {
|
|
|
221 |
extract($this->settings);
|
|
|
222 |
$this->Tree = new $modelClass();
|
|
|
223 |
$this->Tree->order = null;
|
|
|
224 |
$this->Tree->initialize(2, 2);
|
|
|
225 |
|
|
|
226 |
$data = $this->Tree->find('first', array('conditions' => array($modelClass . '.name' => '1. Root')));
|
|
|
227 |
$this->Tree->id = $data[$modelClass]['id'];
|
|
|
228 |
|
|
|
229 |
$direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
|
|
|
230 |
$expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
|
|
|
231 |
array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
|
|
|
232 |
$this->assertEquals($direct, $expects);
|
|
|
233 |
|
|
|
234 |
$total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
|
|
|
235 |
$expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
|
|
|
236 |
array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
|
|
|
237 |
array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
|
|
|
238 |
array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
|
|
|
239 |
array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
|
|
|
240 |
array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12)));
|
|
|
241 |
$this->assertEquals($total, $expects);
|
|
|
242 |
}
|
|
|
243 |
|
|
|
244 |
/**
|
|
|
245 |
* testNoAmbiguousColumn method
|
|
|
246 |
*
|
|
|
247 |
* @return void
|
|
|
248 |
*/
|
|
|
249 |
public function testNoAmbiguousColumn() {
|
|
|
250 |
extract($this->settings);
|
|
|
251 |
$this->Tree = new $modelClass();
|
|
|
252 |
$this->Tree->order = null;
|
|
|
253 |
$this->Tree->initialize(2, 2);
|
|
|
254 |
|
|
|
255 |
$this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
|
|
|
256 |
array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
|
|
|
257 |
|
|
|
258 |
$data = $this->Tree->find('first', array(
|
|
|
259 |
'conditions' => array($modelClass . '.name' => '1. Root'),
|
|
|
260 |
'recursive' => -1
|
|
|
261 |
));
|
|
|
262 |
$this->Tree->id = $data[$modelClass]['id'];
|
|
|
263 |
|
|
|
264 |
$direct = $this->Tree->children(null, true, array('name', $leftField, $rightField));
|
|
|
265 |
$expects = array(array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
|
|
|
266 |
array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)));
|
|
|
267 |
$this->assertEquals($direct, $expects);
|
|
|
268 |
|
|
|
269 |
$total = $this->Tree->children(null, null, array('name', $leftField, $rightField));
|
|
|
270 |
$expects = array(
|
|
|
271 |
array($modelClass => array('name' => '1.1', $leftField => 2, $rightField => 7)),
|
|
|
272 |
array($modelClass => array('name' => '1.1.1', $leftField => 3, $rightField => 4)),
|
|
|
273 |
array($modelClass => array('name' => '1.1.2', $leftField => 5, $rightField => 6)),
|
|
|
274 |
array($modelClass => array('name' => '1.2', $leftField => 8, $rightField => 13)),
|
|
|
275 |
array($modelClass => array('name' => '1.2.1', $leftField => 9, $rightField => 10)),
|
|
|
276 |
array($modelClass => array('name' => '1.2.2', $leftField => 11, $rightField => 12))
|
|
|
277 |
);
|
|
|
278 |
$this->assertEquals($total, $expects);
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
/**
|
|
|
282 |
* testGenerateTreeListWithSelfJoin method
|
|
|
283 |
*
|
|
|
284 |
* @return void
|
|
|
285 |
*/
|
|
|
286 |
public function testGenerateTreeListWithSelfJoin() {
|
|
|
287 |
extract($this->settings);
|
|
|
288 |
$this->Tree = new $modelClass();
|
|
|
289 |
$this->Tree->order = null;
|
|
|
290 |
$this->Tree->bindModel(array('belongsTo' => array('Dummy' =>
|
|
|
291 |
array('className' => $modelClass, 'foreignKey' => $parentField, 'conditions' => array('Dummy.id' => null)))), false);
|
|
|
292 |
$this->Tree->initialize(2, 2);
|
|
|
293 |
|
|
|
294 |
$result = $this->Tree->generateTreeList();
|
|
|
295 |
$expected = array('1. Root', '_1.1', '__1.1.1', '__1.1.2', '_1.2', '__1.2.1', '__1.2.2');
|
|
|
296 |
$this->assertSame(array_values($result), $expected);
|
|
|
297 |
}
|
|
|
298 |
}
|