| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* BehaviorTest file
|
|
|
4 |
*
|
|
|
5 |
* Long description for behavior.test.php
|
|
|
6 |
*
|
|
|
7 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
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://cakephp.org CakePHP(tm) Project
|
|
|
16 |
* @package Cake.Test.Case.Model
|
|
|
17 |
* @since 1.2
|
|
|
18 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
19 |
*/
|
|
|
20 |
|
|
|
21 |
App::uses('AppModel', 'Model');
|
|
|
22 |
|
|
|
23 |
require_once dirname(__FILE__) . DS . 'models.php';
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* TestBehavior class
|
|
|
27 |
*
|
|
|
28 |
* @package Cake.Test.Case.Model
|
|
|
29 |
*/
|
|
|
30 |
class TestBehavior extends ModelBehavior {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* mapMethods property
|
|
|
34 |
*
|
|
|
35 |
* @var array
|
|
|
36 |
*/
|
|
|
37 |
public $mapMethods = array('/test(\w+)/' => 'testMethod', '/look for\s+(.+)/' => 'speakEnglish');
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* setup method
|
|
|
41 |
*
|
|
|
42 |
* @param Model $model
|
|
|
43 |
* @param array $config
|
|
|
44 |
* @return void
|
|
|
45 |
*/
|
|
|
46 |
public function setup(Model $model, $config = array()) {
|
|
|
47 |
parent::setup($model, $config);
|
|
|
48 |
if (isset($config['mangle'])) {
|
|
|
49 |
$config['mangle'] .= ' mangled';
|
|
|
50 |
}
|
|
|
51 |
$this->settings[$model->alias] = array_merge(array('beforeFind' => 'on', 'afterFind' => 'off'), $config);
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* beforeFind method
|
|
|
56 |
*
|
|
|
57 |
* @param Model $model
|
|
|
58 |
* @param array $query
|
|
|
59 |
* @return void
|
|
|
60 |
*/
|
|
|
61 |
public function beforeFind(Model $model, $query) {
|
|
|
62 |
$settings = $this->settings[$model->alias];
|
|
|
63 |
if (!isset($settings['beforeFind']) || $settings['beforeFind'] === 'off') {
|
|
|
64 |
return parent::beforeFind($model, $query);
|
|
|
65 |
}
|
|
|
66 |
switch ($settings['beforeFind']) {
|
|
|
67 |
case 'on':
|
|
|
68 |
return false;
|
|
|
69 |
case 'test':
|
|
|
70 |
return null;
|
|
|
71 |
case 'modify':
|
|
|
72 |
$query['fields'] = array($model->alias . '.id', $model->alias . '.name', $model->alias . '.mytime');
|
|
|
73 |
$query['recursive'] = -1;
|
|
|
74 |
return $query;
|
|
|
75 |
}
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* afterFind method
|
|
|
80 |
*
|
|
|
81 |
* @param Model $model
|
|
|
82 |
* @param array $results
|
|
|
83 |
* @param boolean $primary
|
|
|
84 |
* @return void
|
|
|
85 |
*/
|
|
|
86 |
public function afterFind(Model $model, $results, $primary = false) {
|
|
|
87 |
$settings = $this->settings[$model->alias];
|
|
|
88 |
if (!isset($settings['afterFind']) || $settings['afterFind'] === 'off') {
|
|
|
89 |
return parent::afterFind($model, $results, $primary);
|
|
|
90 |
}
|
|
|
91 |
switch ($settings['afterFind']) {
|
|
|
92 |
case 'on':
|
|
|
93 |
return array();
|
|
|
94 |
case 'test':
|
|
|
95 |
return true;
|
|
|
96 |
case 'test2':
|
|
|
97 |
return null;
|
|
|
98 |
case 'modify':
|
|
|
99 |
return Hash::extract($results, "{n}.{$model->alias}");
|
|
|
100 |
}
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
/**
|
|
|
104 |
* beforeSave method
|
|
|
105 |
*
|
|
|
106 |
* @param Model $model Model using this behavior
|
|
|
107 |
* @param array $options Options passed from Model::save().
|
|
|
108 |
* @return mixed False if the operation should abort. Any other result will continue.
|
|
|
109 |
* @see Model::save()
|
|
|
110 |
*/
|
|
|
111 |
public function beforeSave(Model $model, $options = array()) {
|
|
|
112 |
$settings = $this->settings[$model->alias];
|
|
|
113 |
if (!isset($settings['beforeSave']) || $settings['beforeSave'] === 'off') {
|
|
|
114 |
return parent::beforeSave($model, $options);
|
|
|
115 |
}
|
|
|
116 |
switch ($settings['beforeSave']) {
|
|
|
117 |
case 'on':
|
|
|
118 |
return false;
|
|
|
119 |
case 'test':
|
|
|
120 |
return true;
|
|
|
121 |
case 'modify':
|
|
|
122 |
$model->data[$model->alias]['name'] .= ' modified before';
|
|
|
123 |
return true;
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* afterSave method
|
|
|
129 |
*
|
|
|
130 |
* @param Model $model
|
|
|
131 |
* @param boolean $created
|
|
|
132 |
* @param array $options Options passed from Model::save().
|
|
|
133 |
* @return void
|
|
|
134 |
*/
|
|
|
135 |
public function afterSave(Model $model, $created, $options = array()) {
|
|
|
136 |
$settings = $this->settings[$model->alias];
|
|
|
137 |
if (!isset($settings['afterSave']) || $settings['afterSave'] === 'off') {
|
|
|
138 |
return parent::afterSave($model, $created, $options);
|
|
|
139 |
}
|
|
|
140 |
$string = 'modified after';
|
|
|
141 |
if ($created) {
|
|
|
142 |
$string .= ' on create';
|
|
|
143 |
}
|
|
|
144 |
switch ($settings['afterSave']) {
|
|
|
145 |
case 'on':
|
|
|
146 |
$model->data[$model->alias]['aftersave'] = $string;
|
|
|
147 |
break;
|
|
|
148 |
case 'test':
|
|
|
149 |
unset($model->data[$model->alias]['name']);
|
|
|
150 |
break;
|
|
|
151 |
case 'test2':
|
|
|
152 |
return false;
|
|
|
153 |
case 'modify':
|
|
|
154 |
$model->data[$model->alias]['name'] .= ' ' . $string;
|
|
|
155 |
break;
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* beforeValidate Callback
|
|
|
161 |
*
|
|
|
162 |
* @param Model $Model Model invalidFields was called on.
|
|
|
163 |
* @param array $options Options passed from Model::save().
|
|
|
164 |
* @return boolean
|
|
|
165 |
* @see Model::save()
|
|
|
166 |
*/
|
|
|
167 |
public function beforeValidate(Model $model, $options = array()) {
|
|
|
168 |
$settings = $this->settings[$model->alias];
|
|
|
169 |
if (!isset($settings['validate']) || $settings['validate'] === 'off') {
|
|
|
170 |
return parent::beforeValidate($model, $options);
|
|
|
171 |
}
|
|
|
172 |
switch ($settings['validate']) {
|
|
|
173 |
case 'on':
|
|
|
174 |
$model->invalidate('name');
|
|
|
175 |
return true;
|
|
|
176 |
case 'test':
|
|
|
177 |
return null;
|
|
|
178 |
case 'whitelist':
|
|
|
179 |
$this->_addToWhitelist($model, array('name'));
|
|
|
180 |
return true;
|
|
|
181 |
case 'stop':
|
|
|
182 |
$model->invalidate('name');
|
|
|
183 |
return false;
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/**
|
|
|
188 |
* afterValidate method
|
|
|
189 |
*
|
|
|
190 |
* @param Model $model
|
|
|
191 |
* @param boolean $cascade
|
|
|
192 |
* @return void
|
|
|
193 |
*/
|
|
|
194 |
public function afterValidate(Model $model) {
|
|
|
195 |
$settings = $this->settings[$model->alias];
|
|
|
196 |
if (!isset($settings['afterValidate']) || $settings['afterValidate'] === 'off') {
|
|
|
197 |
return parent::afterValidate($model);
|
|
|
198 |
}
|
|
|
199 |
switch ($settings['afterValidate']) {
|
|
|
200 |
case 'on':
|
|
|
201 |
return false;
|
|
|
202 |
case 'test':
|
|
|
203 |
$model->data = array('foo');
|
|
|
204 |
return true;
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
|
|
|
208 |
/**
|
|
|
209 |
* beforeDelete method
|
|
|
210 |
*
|
|
|
211 |
* @param Model $model
|
|
|
212 |
* @param boolean $cascade
|
|
|
213 |
* @return void
|
|
|
214 |
*/
|
|
|
215 |
public function beforeDelete(Model $model, $cascade = true) {
|
|
|
216 |
$settings = $this->settings[$model->alias];
|
|
|
217 |
if (!isset($settings['beforeDelete']) || $settings['beforeDelete'] === 'off') {
|
|
|
218 |
return parent::beforeDelete($model, $cascade);
|
|
|
219 |
}
|
|
|
220 |
switch ($settings['beforeDelete']) {
|
|
|
221 |
case 'on':
|
|
|
222 |
return false;
|
|
|
223 |
case 'test':
|
|
|
224 |
return null;
|
|
|
225 |
case 'test2':
|
|
|
226 |
echo 'beforeDelete success';
|
|
|
227 |
if ($cascade) {
|
|
|
228 |
echo ' (cascading) ';
|
|
|
229 |
}
|
|
|
230 |
return true;
|
|
|
231 |
}
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
/**
|
|
|
235 |
* afterDelete method
|
|
|
236 |
*
|
|
|
237 |
* @param Model $model
|
|
|
238 |
* @return void
|
|
|
239 |
*/
|
|
|
240 |
public function afterDelete(Model $model) {
|
|
|
241 |
$settings = $this->settings[$model->alias];
|
|
|
242 |
if (!isset($settings['afterDelete']) || $settings['afterDelete'] === 'off') {
|
|
|
243 |
return parent::afterDelete($model);
|
|
|
244 |
}
|
|
|
245 |
switch ($settings['afterDelete']) {
|
|
|
246 |
case 'on':
|
|
|
247 |
echo 'afterDelete success';
|
|
|
248 |
break;
|
|
|
249 |
}
|
|
|
250 |
}
|
|
|
251 |
|
|
|
252 |
/**
|
|
|
253 |
* onError method
|
|
|
254 |
*
|
|
|
255 |
* @param Model $model
|
|
|
256 |
* @return void
|
|
|
257 |
*/
|
|
|
258 |
public function onError(Model $model, $error) {
|
|
|
259 |
$settings = $this->settings[$model->alias];
|
|
|
260 |
if (!isset($settings['onError']) || $settings['onError'] === 'off') {
|
|
|
261 |
return parent::onError($model, $error);
|
|
|
262 |
}
|
|
|
263 |
echo "onError trigger success";
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
/**
|
|
|
267 |
* beforeTest method
|
|
|
268 |
*
|
|
|
269 |
* @param Model $model
|
|
|
270 |
* @return void
|
|
|
271 |
*/
|
|
|
272 |
public function beforeTest(Model $model) {
|
|
|
273 |
if (!isset($model->beforeTestResult)) {
|
|
|
274 |
$model->beforeTestResult = array();
|
|
|
275 |
}
|
|
|
276 |
$model->beforeTestResult[] = strtolower(get_class($this));
|
|
|
277 |
return strtolower(get_class($this));
|
|
|
278 |
}
|
|
|
279 |
|
|
|
280 |
/**
|
|
|
281 |
* testMethod method
|
|
|
282 |
*
|
|
|
283 |
* @param Model $model
|
|
|
284 |
* @param boolean $param
|
|
|
285 |
* @return void
|
|
|
286 |
*/
|
|
|
287 |
public function testMethod(Model $model, $param = true) {
|
|
|
288 |
if ($param === true) {
|
|
|
289 |
return 'working';
|
|
|
290 |
}
|
|
|
291 |
}
|
|
|
292 |
|
|
|
293 |
/**
|
|
|
294 |
* testData method
|
|
|
295 |
*
|
|
|
296 |
* @param Model $model
|
|
|
297 |
* @return void
|
|
|
298 |
*/
|
|
|
299 |
public function testData(Model $model) {
|
|
|
300 |
if (!isset($model->data['Apple']['field'])) {
|
|
|
301 |
return false;
|
|
|
302 |
}
|
|
|
303 |
$model->data['Apple']['field_2'] = true;
|
|
|
304 |
return true;
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
/**
|
|
|
308 |
* validateField method
|
|
|
309 |
*
|
|
|
310 |
* @param Model $model
|
|
|
311 |
* @param string|array $field
|
|
|
312 |
* @return void
|
|
|
313 |
*/
|
|
|
314 |
public function validateField(Model $model, $field) {
|
|
|
315 |
return current($field) === 'Orange';
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
/**
|
|
|
319 |
* speakEnglish method
|
|
|
320 |
*
|
|
|
321 |
* @param Model $model
|
|
|
322 |
* @param string $method
|
|
|
323 |
* @param string $query
|
|
|
324 |
* @return void
|
|
|
325 |
*/
|
|
|
326 |
public function speakEnglish(Model $model, $method, $query) {
|
|
|
327 |
$method = preg_replace('/look for\s+/', 'Item.name = \'', $method);
|
|
|
328 |
$query = preg_replace('/^in\s+/', 'Location.name = \'', $query);
|
|
|
329 |
return $method . '\' AND ' . $query . '\'';
|
|
|
330 |
}
|
|
|
331 |
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
/**
|
|
|
335 |
* Test2Behavior class
|
|
|
336 |
*
|
|
|
337 |
* @package Cake.Test.Case.Model
|
|
|
338 |
*/
|
|
|
339 |
class Test2Behavior extends TestBehavior {
|
|
|
340 |
|
|
|
341 |
public $mapMethods = array('/mappingRobot(\w+)/' => 'mapped');
|
|
|
342 |
|
|
|
343 |
public function resolveMethod(Model $model, $stuff) {
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
public function mapped(Model $model, $method, $query) {
|
|
|
347 |
}
|
|
|
348 |
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
/**
|
|
|
352 |
* Test3Behavior class
|
|
|
353 |
*
|
|
|
354 |
* @package Cake.Test.Case.Model
|
|
|
355 |
*/
|
|
|
356 |
class Test3Behavior extends TestBehavior {
|
|
|
357 |
}
|
|
|
358 |
|
|
|
359 |
/**
|
|
|
360 |
* Test4Behavior class
|
|
|
361 |
*
|
|
|
362 |
* @package Cake.Test.Case.Model
|
|
|
363 |
*/
|
|
|
364 |
class Test4Behavior extends ModelBehavior {
|
|
|
365 |
|
|
|
366 |
public function setup(Model $model, $config = null) {
|
|
|
367 |
$model->bindModel(
|
|
|
368 |
array('hasMany' => array('Comment'))
|
|
|
369 |
);
|
|
|
370 |
}
|
|
|
371 |
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
/**
|
|
|
375 |
* Test5Behavior class
|
|
|
376 |
*
|
|
|
377 |
* @package Cake.Test.Case.Model
|
|
|
378 |
*/
|
|
|
379 |
class Test5Behavior extends ModelBehavior {
|
|
|
380 |
|
|
|
381 |
public function setup(Model $model, $config = null) {
|
|
|
382 |
$model->bindModel(
|
|
|
383 |
array('belongsTo' => array('User'))
|
|
|
384 |
);
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
/**
|
|
|
390 |
* Test6Behavior class
|
|
|
391 |
*
|
|
|
392 |
* @package Cake.Test.Case.Model
|
|
|
393 |
*/
|
|
|
394 |
class Test6Behavior extends ModelBehavior {
|
|
|
395 |
|
|
|
396 |
public function setup(Model $model, $config = null) {
|
|
|
397 |
$model->bindModel(
|
|
|
398 |
array('hasAndBelongsToMany' => array('Tag'))
|
|
|
399 |
);
|
|
|
400 |
}
|
|
|
401 |
|
|
|
402 |
}
|
|
|
403 |
|
|
|
404 |
/**
|
|
|
405 |
* Test7Behavior class
|
|
|
406 |
*
|
|
|
407 |
* @package Cake.Test.Case.Model
|
|
|
408 |
*/
|
|
|
409 |
class Test7Behavior extends ModelBehavior {
|
|
|
410 |
|
|
|
411 |
public function setup(Model $model, $config = null) {
|
|
|
412 |
$model->bindModel(
|
|
|
413 |
array('hasOne' => array('Attachment'))
|
|
|
414 |
);
|
|
|
415 |
}
|
|
|
416 |
|
|
|
417 |
}
|
|
|
418 |
|
|
|
419 |
/**
|
|
|
420 |
* Extended TestBehavior
|
|
|
421 |
*/
|
|
|
422 |
class TestAliasBehavior extends TestBehavior {
|
|
|
423 |
}
|
|
|
424 |
|
|
|
425 |
/**
|
|
|
426 |
* FirstBehavior
|
|
|
427 |
*/
|
|
|
428 |
class FirstBehavior extends ModelBehavior {
|
|
|
429 |
|
|
|
430 |
public function beforeFind(Model $model, $query = array()) {
|
|
|
431 |
$model->called[] = get_class($this);
|
|
|
432 |
return $query;
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
/**
|
|
|
438 |
* SecondBehavior
|
|
|
439 |
*/
|
|
|
440 |
class SecondBehavior extends FirstBehavior {
|
|
|
441 |
}
|
|
|
442 |
|
|
|
443 |
/**
|
|
|
444 |
* ThirdBehavior
|
|
|
445 |
*/
|
|
|
446 |
class ThirdBehavior extends FirstBehavior {
|
|
|
447 |
}
|
|
|
448 |
|
|
|
449 |
/**
|
|
|
450 |
* Orangutan Model
|
|
|
451 |
*/
|
|
|
452 |
class Orangutan extends Monkey {
|
|
|
453 |
|
|
|
454 |
public $called = array();
|
|
|
455 |
|
|
|
456 |
}
|
|
|
457 |
|
|
|
458 |
/**
|
|
|
459 |
* BehaviorCollection class
|
|
|
460 |
*
|
|
|
461 |
* @package Cake.Test.Case.Model
|
|
|
462 |
*/
|
|
|
463 |
class BehaviorCollectionTest extends CakeTestCase {
|
|
|
464 |
|
|
|
465 |
/**
|
|
|
466 |
* fixtures property
|
|
|
467 |
*
|
|
|
468 |
* @var array
|
|
|
469 |
*/
|
|
|
470 |
public $fixtures = array(
|
|
|
471 |
'core.apple', 'core.sample', 'core.article', 'core.user', 'core.comment',
|
|
|
472 |
'core.attachment', 'core.tag', 'core.articles_tag', 'core.translate',
|
|
|
473 |
'core.device'
|
|
|
474 |
);
|
|
|
475 |
|
|
|
476 |
/**
|
|
|
477 |
* Test load() with enabled => false
|
|
|
478 |
*
|
|
|
479 |
*/
|
|
|
480 |
public function testLoadDisabled() {
|
|
|
481 |
$Apple = new Apple();
|
|
|
482 |
$this->assertSame(array(), $Apple->Behaviors->loaded());
|
|
|
483 |
|
|
|
484 |
$Apple->Behaviors->load('Translate', array('enabled' => false));
|
|
|
485 |
$this->assertTrue($Apple->Behaviors->loaded('Translate'));
|
|
|
486 |
$this->assertFalse($Apple->Behaviors->enabled('Translate'));
|
|
|
487 |
}
|
|
|
488 |
|
|
|
489 |
/**
|
|
|
490 |
* Tests loading aliased behaviors
|
|
|
491 |
*/
|
|
|
492 |
public function testLoadAlias() {
|
|
|
493 |
$Apple = new Apple();
|
|
|
494 |
$this->assertSame(array(), $Apple->Behaviors->loaded());
|
|
|
495 |
|
|
|
496 |
$Apple->Behaviors->load('Test', array('className' => 'TestAlias', 'somesetting' => true));
|
|
|
497 |
$this->assertSame(array('Test'), $Apple->Behaviors->loaded());
|
|
|
498 |
$this->assertInstanceOf('TestAliasBehavior', $Apple->Behaviors->Test);
|
|
|
499 |
$this->assertTrue($Apple->Behaviors->Test->settings['Apple']['somesetting']);
|
|
|
500 |
|
|
|
501 |
$this->assertEquals('working', $Apple->Behaviors->Test->testMethod($Apple, true));
|
|
|
502 |
$this->assertEquals('working', $Apple->testMethod(true));
|
|
|
503 |
$this->assertEquals('working', $Apple->Behaviors->dispatchMethod($Apple, 'testMethod'));
|
|
|
504 |
|
|
|
505 |
App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)));
|
|
|
506 |
CakePlugin::load('TestPlugin');
|
|
|
507 |
$this->assertTrue($Apple->Behaviors->load('SomeOther', array('className' => 'TestPlugin.TestPluginPersisterOne')));
|
|
|
508 |
$this->assertInstanceOf('TestPluginPersisterOneBehavior', $Apple->Behaviors->SomeOther);
|
|
|
509 |
|
|
|
510 |
$result = $Apple->Behaviors->loaded();
|
|
|
511 |
$this->assertEquals(array('Test', 'SomeOther'), $result, 'loaded() results are wrong.');
|
|
|
512 |
App::build();
|
|
|
513 |
CakePlugin::unload();
|
|
|
514 |
}
|
|
|
515 |
|
|
|
516 |
/**
|
|
|
517 |
* testBehaviorBinding method
|
|
|
518 |
*
|
|
|
519 |
* @return void
|
|
|
520 |
*/
|
|
|
521 |
public function testBehaviorBinding() {
|
|
|
522 |
$Apple = new Apple();
|
|
|
523 |
$this->assertSame(array(), $Apple->Behaviors->loaded());
|
|
|
524 |
|
|
|
525 |
$Apple->Behaviors->load('Test', array('key' => 'value'));
|
|
|
526 |
$this->assertSame(array('Test'), $Apple->Behaviors->loaded());
|
|
|
527 |
$this->assertEquals('testbehavior', strtolower(get_class($Apple->Behaviors->Test)));
|
|
|
528 |
$expected = array('beforeFind' => 'on', 'afterFind' => 'off', 'key' => 'value');
|
|
|
529 |
$this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']);
|
|
|
530 |
$this->assertEquals(array('priority', 'Apple'), array_keys($Apple->Behaviors->Test->settings));
|
|
|
531 |
|
|
|
532 |
$this->assertSame($Apple->Sample->Behaviors->loaded(), array());
|
|
|
533 |
$Apple->Sample->Behaviors->load('Test', array('key2' => 'value2'));
|
|
|
534 |
$this->assertSame($Apple->Sample->Behaviors->loaded(), array('Test'));
|
|
|
535 |
$this->assertEquals(array('beforeFind' => 'on', 'afterFind' => 'off', 'key2' => 'value2'), $Apple->Sample->Behaviors->Test->settings['Sample']);
|
|
|
536 |
|
|
|
537 |
$this->assertEquals(array('priority', 'Apple', 'Sample'), array_keys($Apple->Behaviors->Test->settings));
|
|
|
538 |
$this->assertSame(
|
|
|
539 |
$Apple->Sample->Behaviors->Test->settings,
|
|
|
540 |
$Apple->Behaviors->Test->settings
|
|
|
541 |
);
|
|
|
542 |
$this->assertNotSame($Apple->Behaviors->Test->settings['Apple'], $Apple->Sample->Behaviors->Test->settings['Sample']);
|
|
|
543 |
|
|
|
544 |
$Apple->Behaviors->load('Test', array('key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off'));
|
|
|
545 |
$Apple->Sample->Behaviors->load('Test', array('key' => 'value', 'key3' => 'value3', 'beforeFind' => 'off'));
|
|
|
546 |
$this->assertEquals(array('beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'value', 'key2' => 'value2', 'key3' => 'value3'), $Apple->Behaviors->Test->settings['Apple']);
|
|
|
547 |
$this->assertEquals($Apple->Behaviors->Test->settings['Apple'], $Apple->Sample->Behaviors->Test->settings['Sample']);
|
|
|
548 |
|
|
|
549 |
$this->assertFalse(isset($Apple->Child->Behaviors->Test));
|
|
|
550 |
$Apple->Child->Behaviors->load('Test', array('key' => 'value', 'key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off'));
|
|
|
551 |
$this->assertEquals($Apple->Child->Behaviors->Test->settings['Child'], $Apple->Sample->Behaviors->Test->settings['Sample']);
|
|
|
552 |
|
|
|
553 |
$this->assertFalse(isset($Apple->Parent->Behaviors->Test));
|
|
|
554 |
$Apple->Parent->Behaviors->load('Test', array('key' => 'value', 'key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off'));
|
|
|
555 |
$this->assertEquals($Apple->Parent->Behaviors->Test->settings['Parent'], $Apple->Sample->Behaviors->Test->settings['Sample']);
|
|
|
556 |
|
|
|
557 |
$Apple->Parent->Behaviors->load('Test', array('key' => 'value', 'key2' => 'value', 'key3' => 'value', 'beforeFind' => 'off'));
|
|
|
558 |
$this->assertNotEquals($Apple->Parent->Behaviors->Test->settings['Parent'], $Apple->Sample->Behaviors->Test->settings['Sample']);
|
|
|
559 |
|
|
|
560 |
$Apple->Behaviors->load('Plugin.Test', array('key' => 'new value'));
|
|
|
561 |
$expected = array(
|
|
|
562 |
'beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'new value',
|
|
|
563 |
'key2' => 'value2', 'key3' => 'value3'
|
|
|
564 |
);
|
|
|
565 |
$this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']);
|
|
|
566 |
|
|
|
567 |
$current = $Apple->Behaviors->Test->settings['Apple'];
|
|
|
568 |
$expected = array_merge($current, array('mangle' => 'trigger mangled'));
|
|
|
569 |
$Apple->Behaviors->load('Test', array('mangle' => 'trigger'));
|
|
|
570 |
$this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']);
|
|
|
571 |
|
|
|
572 |
$Apple->Behaviors->load('Test');
|
|
|
573 |
$expected = array_merge($current, array('mangle' => 'trigger mangled mangled'));
|
|
|
574 |
|
|
|
575 |
$this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']);
|
|
|
576 |
$Apple->Behaviors->load('Test', array('mangle' => 'trigger'));
|
|
|
577 |
$expected = array_merge($current, array('mangle' => 'trigger mangled'));
|
|
|
578 |
$this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']);
|
|
|
579 |
}
|
|
|
580 |
|
|
|
581 |
/**
|
|
|
582 |
* test that attach()/detach() works with plugin.banana
|
|
|
583 |
*
|
|
|
584 |
* @return void
|
|
|
585 |
*/
|
|
|
586 |
public function testDetachWithPluginNames() {
|
|
|
587 |
$Apple = new Apple();
|
|
|
588 |
$Apple->Behaviors->load('Plugin.Test');
|
|
|
589 |
$this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
|
|
|
590 |
$this->assertEquals(array('Test'), $Apple->Behaviors->loaded());
|
|
|
591 |
|
|
|
592 |
$Apple->Behaviors->unload('Plugin.Test');
|
|
|
593 |
$this->assertEquals(array(), $Apple->Behaviors->loaded());
|
|
|
594 |
|
|
|
595 |
$Apple->Behaviors->load('Plugin.Test');
|
|
|
596 |
$this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
|
|
|
597 |
$this->assertEquals(array('Test'), $Apple->Behaviors->loaded());
|
|
|
598 |
|
|
|
599 |
$Apple->Behaviors->unload('Test');
|
|
|
600 |
$this->assertEquals(array(), $Apple->Behaviors->loaded());
|
|
|
601 |
}
|
|
|
602 |
|
|
|
603 |
/**
|
|
|
604 |
* test that attaching a non existent Behavior triggers a cake error.
|
|
|
605 |
*
|
|
|
606 |
* @expectedException MissingBehaviorException
|
|
|
607 |
* @return void
|
|
|
608 |
*/
|
|
|
609 |
public function testInvalidBehaviorCausingCakeError() {
|
|
|
610 |
$Apple = new Apple();
|
|
|
611 |
$Apple->Behaviors->load('NoSuchBehavior');
|
|
|
612 |
}
|
|
|
613 |
|
|
|
614 |
/**
|
|
|
615 |
* testBehaviorToggling method
|
|
|
616 |
*
|
|
|
617 |
* @return void
|
|
|
618 |
*/
|
|
|
619 |
public function testBehaviorToggling() {
|
|
|
620 |
$Apple = new Apple();
|
|
|
621 |
$this->assertSame($Apple->Behaviors->enabled(), array());
|
|
|
622 |
|
|
|
623 |
$Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value')));
|
|
|
624 |
$this->assertSame($Apple->Behaviors->enabled(), array('Test'));
|
|
|
625 |
|
|
|
626 |
$Apple->Behaviors->disable('Test');
|
|
|
627 |
$this->assertSame(array('Test'), $Apple->Behaviors->loaded());
|
|
|
628 |
$this->assertSame($Apple->Behaviors->enabled(), array());
|
|
|
629 |
|
|
|
630 |
$Apple->Sample->Behaviors->load('Test');
|
|
|
631 |
$this->assertTrue($Apple->Sample->Behaviors->enabled('Test'));
|
|
|
632 |
$this->assertSame($Apple->Behaviors->enabled(), array());
|
|
|
633 |
|
|
|
634 |
$Apple->Behaviors->enable('Test');
|
|
|
635 |
$this->assertTrue($Apple->Behaviors->loaded('Test'));
|
|
|
636 |
$this->assertSame($Apple->Behaviors->enabled(), array('Test'));
|
|
|
637 |
|
|
|
638 |
$Apple->Behaviors->disable('Test');
|
|
|
639 |
$this->assertSame($Apple->Behaviors->enabled(), array());
|
|
|
640 |
$Apple->Behaviors->load('Test', array('enabled' => true));
|
|
|
641 |
$this->assertSame($Apple->Behaviors->enabled(), array('Test'));
|
|
|
642 |
$Apple->Behaviors->load('Test', array('enabled' => false));
|
|
|
643 |
$this->assertSame($Apple->Behaviors->enabled(), array());
|
|
|
644 |
$Apple->Behaviors->unload('Test');
|
|
|
645 |
$this->assertSame($Apple->Behaviors->enabled(), array());
|
|
|
646 |
}
|
|
|
647 |
|
|
|
648 |
/**
|
|
|
649 |
* testBehaviorFindCallbacks method
|
|
|
650 |
*
|
|
|
651 |
* @return void
|
|
|
652 |
*/
|
|
|
653 |
public function testBehaviorFindCallbacks() {
|
|
|
654 |
$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
|
|
|
655 |
|
|
|
656 |
$Apple = new Apple();
|
|
|
657 |
$expected = $Apple->find('all');
|
|
|
658 |
|
|
|
659 |
$Apple->Behaviors->load('Test');
|
|
|
660 |
$this->assertNull($Apple->find('all'));
|
|
|
661 |
|
|
|
662 |
$Apple->Behaviors->load('Test', array('beforeFind' => 'off'));
|
|
|
663 |
$this->assertSame($expected, $Apple->find('all'));
|
|
|
664 |
|
|
|
665 |
$Apple->Behaviors->load('Test', array('beforeFind' => 'test'));
|
|
|
666 |
$this->assertSame($expected, $Apple->find('all'));
|
|
|
667 |
|
|
|
668 |
$Apple->Behaviors->load('Test', array('beforeFind' => 'modify'));
|
|
|
669 |
$expected2 = array(
|
|
|
670 |
array('Apple' => array('id' => '1', 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
|
|
|
671 |
array('Apple' => array('id' => '2', 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
|
|
|
672 |
array('Apple' => array('id' => '3', 'name' => 'green blue', 'mytime' => '22:57:17'))
|
|
|
673 |
);
|
|
|
674 |
$result = $Apple->find('all', array('conditions' => array('Apple.id <' => '4')));
|
|
|
675 |
$this->assertEquals($expected2, $result);
|
|
|
676 |
|
|
|
677 |
$Apple->Behaviors->disable('Test');
|
|
|
678 |
$result = $Apple->find('all');
|
|
|
679 |
$this->assertEquals($expected, $result);
|
|
|
680 |
|
|
|
681 |
$Apple->Behaviors->load('Test', array('beforeFind' => 'off', 'afterFind' => 'on'));
|
|
|
682 |
$this->assertSame($Apple->find('all'), array());
|
|
|
683 |
|
|
|
684 |
$Apple->Behaviors->load('Test', array('afterFind' => 'off'));
|
|
|
685 |
$this->assertEquals($expected, $Apple->find('all'));
|
|
|
686 |
|
|
|
687 |
$Apple->Behaviors->load('Test', array('afterFind' => 'test'));
|
|
|
688 |
$this->assertEquals($expected, $Apple->find('all'));
|
|
|
689 |
|
|
|
690 |
$Apple->Behaviors->load('Test', array('afterFind' => 'test2'));
|
|
|
691 |
$this->assertEquals($expected, $Apple->find('all'));
|
|
|
692 |
|
|
|
693 |
$Apple->Behaviors->load('Test', array('afterFind' => 'modify'));
|
|
|
694 |
$expected = array(
|
|
|
695 |
array('id' => '1', 'apple_id' => '2', 'color' => 'Red 1', 'name' => 'Red Apple 1', 'created' => '2006-11-22 10:38:58', 'date' => '1951-01-04', 'modified' => '2006-12-01 13:31:26', 'mytime' => '22:57:17'),
|
|
|
696 |
array('id' => '2', 'apple_id' => '1', 'color' => 'Bright Red 1', 'name' => 'Bright Red Apple', 'created' => '2006-11-22 10:43:13', 'date' => '2014-01-01', 'modified' => '2006-11-30 18:38:10', 'mytime' => '22:57:17'),
|
|
|
697 |
array('id' => '3', 'apple_id' => '2', 'color' => 'blue green', 'name' => 'green blue', 'created' => '2006-12-25 05:13:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:24', 'mytime' => '22:57:17'),
|
|
|
698 |
array('id' => '4', 'apple_id' => '2', 'color' => 'Blue Green', 'name' => 'Test Name', 'created' => '2006-12-25 05:23:36', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:23:36', 'mytime' => '22:57:17'),
|
|
|
699 |
array('id' => '5', 'apple_id' => '5', 'color' => 'Green', 'name' => 'Blue Green', 'created' => '2006-12-25 05:24:06', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:16', 'mytime' => '22:57:17'),
|
|
|
700 |
array('id' => '6', 'apple_id' => '4', 'color' => 'My new appleOrange', 'name' => 'My new apple', 'created' => '2006-12-25 05:29:39', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:29:39', 'mytime' => '22:57:17'),
|
|
|
701 |
array('id' => '7', 'apple_id' => '6', 'color' => 'Some wierd color', 'name' => 'Some odd color', 'created' => '2006-12-25 05:34:21', 'date' => '2006-12-25', 'modified' => '2006-12-25 05:34:21', 'mytime' => '22:57:17')
|
|
|
702 |
);
|
|
|
703 |
$this->assertEquals($expected, $Apple->find('all'));
|
|
|
704 |
}
|
|
|
705 |
|
|
|
706 |
/**
|
|
|
707 |
* testBehaviorHasManyFindCallbacks method
|
|
|
708 |
*
|
|
|
709 |
* @return void
|
|
|
710 |
*/
|
|
|
711 |
public function testBehaviorHasManyFindCallbacks() {
|
|
|
712 |
$Apple = new Apple();
|
|
|
713 |
$Apple->unbindModel(array('hasOne' => array('Sample'), 'belongsTo' => array('Parent')), false);
|
|
|
714 |
$expected = $Apple->find('all');
|
|
|
715 |
|
|
|
716 |
$Apple->unbindModel(array('hasMany' => array('Child')));
|
|
|
717 |
$wellBehaved = $Apple->find('all');
|
|
|
718 |
$Apple->Child->Behaviors->load('Test', array('afterFind' => 'modify'));
|
|
|
719 |
$Apple->unbindModel(array('hasMany' => array('Child')));
|
|
|
720 |
$this->assertSame($Apple->find('all'), $wellBehaved);
|
|
|
721 |
|
|
|
722 |
$Apple->Child->Behaviors->load('Test', array('before' => 'off'));
|
|
|
723 |
$this->assertSame($expected, $Apple->find('all'));
|
|
|
724 |
|
|
|
725 |
$Apple->Child->Behaviors->load('Test', array('before' => 'test'));
|
|
|
726 |
$this->assertSame($expected, $Apple->find('all'));
|
|
|
727 |
|
|
|
728 |
$Apple->Child->Behaviors->load('Test', array('before' => 'modify'));
|
|
|
729 |
$result = $Apple->find('all', array('fields' => array('Apple.id'), 'conditions' => array('Apple.id <' => '4')));
|
|
|
730 |
|
|
|
731 |
$Apple->Child->Behaviors->disable('Test');
|
|
|
732 |
$result = $Apple->find('all');
|
|
|
733 |
$this->assertEquals($expected, $result);
|
|
|
734 |
|
|
|
735 |
$Apple->Child->Behaviors->load('Test', array('before' => 'off', 'after' => 'on'));
|
|
|
736 |
|
|
|
737 |
$Apple->Child->Behaviors->load('Test', array('after' => 'off'));
|
|
|
738 |
$this->assertEquals($expected, $Apple->find('all'));
|
|
|
739 |
|
|
|
740 |
$Apple->Child->Behaviors->load('Test', array('after' => 'test'));
|
|
|
741 |
$this->assertEquals($expected, $Apple->find('all'));
|
|
|
742 |
|
|
|
743 |
$Apple->Child->Behaviors->load('Test', array('after' => 'test2'));
|
|
|
744 |
$this->assertEquals($expected, $Apple->find('all'));
|
|
|
745 |
}
|
|
|
746 |
|
|
|
747 |
/**
|
|
|
748 |
* testBehaviorHasOneFindCallbacks method
|
|
|
749 |
*
|
|
|
750 |
* @return void
|
|
|
751 |
*/
|
|
|
752 |
public function testBehaviorHasOneFindCallbacks() {
|
|
|
753 |
$Apple = new Apple();
|
|
|
754 |
$Apple->unbindModel(array('hasMany' => array('Child'), 'belongsTo' => array('Parent')), false);
|
|
|
755 |
$expected = $Apple->find('all');
|
|
|
756 |
|
|
|
757 |
$Apple->unbindModel(array('hasOne' => array('Sample')));
|
|
|
758 |
$wellBehaved = $Apple->find('all');
|
|
|
759 |
$Apple->Sample->Behaviors->load('Test');
|
|
|
760 |
$Apple->unbindModel(array('hasOne' => array('Sample')));
|
|
|
761 |
$this->assertSame($Apple->find('all'), $wellBehaved);
|
|
|
762 |
|
|
|
763 |
$Apple->Sample->Behaviors->load('Test', array('before' => 'off'));
|
|
|
764 |
$this->assertSame($expected, $Apple->find('all'));
|
|
|
765 |
|
|
|
766 |
$Apple->Sample->Behaviors->load('Test', array('before' => 'test'));
|
|
|
767 |
$this->assertSame($expected, $Apple->find('all'));
|
|
|
768 |
|
|
|
769 |
$Apple->Sample->Behaviors->disable('Test');
|
|
|
770 |
$result = $Apple->find('all');
|
|
|
771 |
$this->assertEquals($expected, $result);
|
|
|
772 |
|
|
|
773 |
$Apple->Sample->Behaviors->load('Test', array('after' => 'off'));
|
|
|
774 |
$this->assertEquals($expected, $Apple->find('all'));
|
|
|
775 |
|
|
|
776 |
$Apple->Sample->Behaviors->load('Test', array('after' => 'test'));
|
|
|
777 |
$this->assertEquals($expected, $Apple->find('all'));
|
|
|
778 |
|
|
|
779 |
$Apple->Sample->Behaviors->load('Test', array('after' => 'test2'));
|
|
|
780 |
$this->assertEquals($expected, $Apple->find('all'));
|
|
|
781 |
}
|
|
|
782 |
|
|
|
783 |
/**
|
|
|
784 |
* testBehaviorBelongsToFindCallbacks method
|
|
|
785 |
*
|
|
|
786 |
* @return void
|
|
|
787 |
*/
|
|
|
788 |
public function testBehaviorBelongsToFindCallbacks() {
|
|
|
789 |
$this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
|
|
|
790 |
|
|
|
791 |
$conditions = array('order' => 'Apple.id ASC');
|
|
|
792 |
$Apple = new Apple();
|
|
|
793 |
$Apple->unbindModel(array('hasMany' => array('Child'), 'hasOne' => array('Sample')), false);
|
|
|
794 |
$expected = $Apple->find('all', $conditions);
|
|
|
795 |
|
|
|
796 |
$Apple->unbindModel(array('belongsTo' => array('Parent')));
|
|
|
797 |
$wellBehaved = $Apple->find('all', $conditions);
|
|
|
798 |
$Apple->Parent->Behaviors->load('Test');
|
|
|
799 |
$Apple->unbindModel(array('belongsTo' => array('Parent')));
|
|
|
800 |
$this->assertSame($Apple->find('all', $conditions), $wellBehaved);
|
|
|
801 |
|
|
|
802 |
$Apple->Parent->Behaviors->load('Test', array('before' => 'off'));
|
|
|
803 |
$this->assertSame($expected, $Apple->find('all', $conditions));
|
|
|
804 |
|
|
|
805 |
$Apple->Parent->Behaviors->load('Test', array('before' => 'test'));
|
|
|
806 |
$this->assertSame($expected, $Apple->find('all', $conditions));
|
|
|
807 |
|
|
|
808 |
$Apple->Parent->Behaviors->load('Test', array('before' => 'modify'));
|
|
|
809 |
$expected2 = array(
|
|
|
810 |
array(
|
|
|
811 |
'Apple' => array('id' => 1),
|
|
|
812 |
'Parent' => array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
|
|
|
813 |
array(
|
|
|
814 |
'Apple' => array('id' => 2),
|
|
|
815 |
'Parent' => array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
|
|
|
816 |
array(
|
|
|
817 |
'Apple' => array('id' => 3),
|
|
|
818 |
'Parent' => array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))
|
|
|
819 |
);
|
|
|
820 |
$result2 = $Apple->find('all', array(
|
|
|
821 |
'fields' => array('Apple.id', 'Parent.id', 'Parent.name', 'Parent.mytime'),
|
|
|
822 |
'conditions' => array('Apple.id <' => '4'),
|
|
|
823 |
'order' => 'Apple.id ASC',
|
|
|
824 |
));
|
|
|
825 |
$this->assertEquals($expected2, $result2);
|
|
|
826 |
|
|
|
827 |
$Apple->Parent->Behaviors->disable('Test');
|
|
|
828 |
$result = $Apple->find('all', $conditions);
|
|
|
829 |
$this->assertEquals($expected, $result);
|
|
|
830 |
|
|
|
831 |
$Apple->Parent->Behaviors->load('Test', array('after' => 'off'));
|
|
|
832 |
$this->assertEquals($expected, $Apple->find('all', $conditions));
|
|
|
833 |
|
|
|
834 |
$Apple->Parent->Behaviors->load('Test', array('after' => 'test'));
|
|
|
835 |
$this->assertEquals($expected, $Apple->find('all', $conditions));
|
|
|
836 |
|
|
|
837 |
$Apple->Parent->Behaviors->load('Test', array('after' => 'test2'));
|
|
|
838 |
$this->assertEquals($expected, $Apple->find('all', $conditions));
|
|
|
839 |
}
|
|
|
840 |
|
|
|
841 |
/**
|
|
|
842 |
* testBehaviorSaveCallbacks method
|
|
|
843 |
*
|
|
|
844 |
* @return void
|
|
|
845 |
*/
|
|
|
846 |
public function testBehaviorSaveCallbacks() {
|
|
|
847 |
$Sample = new Sample();
|
|
|
848 |
$record = array('Sample' => array('apple_id' => 6, 'name' => 'sample99'));
|
|
|
849 |
|
|
|
850 |
$Sample->Behaviors->load('Test', array('beforeSave' => 'on'));
|
|
|
851 |
$Sample->create();
|
|
|
852 |
$this->assertSame(false, $Sample->save($record));
|
|
|
853 |
|
|
|
854 |
$Sample->Behaviors->load('Test', array('beforeSave' => 'off'));
|
|
|
855 |
$Sample->create();
|
|
|
856 |
$result = $Sample->save($record);
|
|
|
857 |
$expected = $record;
|
|
|
858 |
$expected['Sample']['id'] = $Sample->id;
|
|
|
859 |
$this->assertSame($expected, $result);
|
|
|
860 |
|
|
|
861 |
$Sample->Behaviors->load('Test', array('beforeSave' => 'test'));
|
|
|
862 |
$Sample->create();
|
|
|
863 |
$result = $Sample->save($record);
|
|
|
864 |
$expected = $record;
|
|
|
865 |
$expected['Sample']['id'] = $Sample->id;
|
|
|
866 |
$this->assertSame($expected, $result);
|
|
|
867 |
|
|
|
868 |
$Sample->Behaviors->load('Test', array('beforeSave' => 'modify'));
|
|
|
869 |
$expected = Hash::insert($record, 'Sample.name', 'sample99 modified before');
|
|
|
870 |
$Sample->create();
|
|
|
871 |
$result = $Sample->save($record);
|
|
|
872 |
$expected['Sample']['id'] = $Sample->id;
|
|
|
873 |
$this->assertSame($expected, $result);
|
|
|
874 |
|
|
|
875 |
$Sample->Behaviors->disable('Test');
|
|
|
876 |
$this->assertSame($record, $Sample->save($record));
|
|
|
877 |
|
|
|
878 |
$Sample->Behaviors->load('Test', array('beforeSave' => 'off', 'afterSave' => 'on'));
|
|
|
879 |
$expected = Hash::merge($record, array('Sample' => array('aftersave' => 'modified after on create')));
|
|
|
880 |
$Sample->create();
|
|
|
881 |
$result = $Sample->save($record);
|
|
|
882 |
$expected['Sample']['id'] = $Sample->id;
|
|
|
883 |
$this->assertEquals($expected, $result);
|
|
|
884 |
|
|
|
885 |
$Sample->Behaviors->load('Test', array('beforeSave' => 'modify', 'afterSave' => 'modify'));
|
|
|
886 |
$expected = Hash::merge($record, array('Sample' => array('name' => 'sample99 modified before modified after on create')));
|
|
|
887 |
$Sample->create();
|
|
|
888 |
$result = $Sample->save($record);
|
|
|
889 |
$expected['Sample']['id'] = $Sample->id;
|
|
|
890 |
$this->assertSame($expected, $result);
|
|
|
891 |
|
|
|
892 |
$Sample->Behaviors->load('Test', array('beforeSave' => 'off', 'afterSave' => 'test'));
|
|
|
893 |
$Sample->create();
|
|
|
894 |
$expected = $record;
|
|
|
895 |
unset($expected['Sample']['name']);
|
|
|
896 |
$result = $Sample->save($record);
|
|
|
897 |
$expected['Sample']['id'] = $Sample->id;
|
|
|
898 |
$this->assertSame($expected, $result);
|
|
|
899 |
|
|
|
900 |
$Sample->Behaviors->load('Test', array('afterSave' => 'test2'));
|
|
|
901 |
$Sample->create();
|
|
|
902 |
$expected = $record;
|
|
|
903 |
$result = $Sample->save($record);
|
|
|
904 |
$expected['Sample']['id'] = $Sample->id;
|
|
|
905 |
$this->assertSame($expected, $result);
|
|
|
906 |
|
|
|
907 |
$Sample->Behaviors->load('Test', array('beforeFind' => 'off', 'afterFind' => 'off'));
|
|
|
908 |
$Sample->recursive = -1;
|
|
|
909 |
$record2 = $Sample->read(null, 1);
|
|
|
910 |
|
|
|
911 |
$Sample->Behaviors->load('Test', array('afterSave' => 'on'));
|
|
|
912 |
$expected = Hash::merge($record2, array('Sample' => array('aftersave' => 'modified after')));
|
|
|
913 |
$Sample->create();
|
|
|
914 |
$this->assertSame($expected, $Sample->save($record2));
|
|
|
915 |
|
|
|
916 |
$Sample->Behaviors->load('Test', array('afterSave' => 'modify'));
|
|
|
917 |
$expected = Hash::merge($record2, array('Sample' => array('name' => 'sample1 modified after')));
|
|
|
918 |
$Sample->create();
|
|
|
919 |
$this->assertSame($expected, $Sample->save($record2));
|
|
|
920 |
}
|
|
|
921 |
|
|
|
922 |
/**
|
|
|
923 |
* testBehaviorDeleteCallbacks method
|
|
|
924 |
*
|
|
|
925 |
* @return void
|
|
|
926 |
*/
|
|
|
927 |
public function testBehaviorDeleteCallbacks() {
|
|
|
928 |
$Apple = new Apple();
|
|
|
929 |
|
|
|
930 |
$Apple->Behaviors->load('Test', array('beforeFind' => 'off', 'beforeDelete' => 'off'));
|
|
|
931 |
$this->assertTrue($Apple->delete(6));
|
|
|
932 |
|
|
|
933 |
$Apple->Behaviors->load('Test', array('beforeDelete' => 'on'));
|
|
|
934 |
$this->assertFalse($Apple->delete(4));
|
|
|
935 |
|
|
|
936 |
$Apple->Behaviors->load('Test', array('beforeDelete' => 'test2'));
|
|
|
937 |
|
|
|
938 |
ob_start();
|
|
|
939 |
$results = $Apple->delete(4);
|
|
|
940 |
$this->assertSame(trim(ob_get_clean()), 'beforeDelete success (cascading)');
|
|
|
941 |
$this->assertTrue($results);
|
|
|
942 |
|
|
|
943 |
ob_start();
|
|
|
944 |
$results = $Apple->delete(3, false);
|
|
|
945 |
$this->assertSame(trim(ob_get_clean()), 'beforeDelete success');
|
|
|
946 |
$this->assertTrue($results);
|
|
|
947 |
|
|
|
948 |
$Apple->Behaviors->load('Test', array('beforeDelete' => 'off', 'afterDelete' => 'on'));
|
|
|
949 |
ob_start();
|
|
|
950 |
$results = $Apple->delete(2, false);
|
|
|
951 |
$this->assertSame(trim(ob_get_clean()), 'afterDelete success');
|
|
|
952 |
$this->assertTrue($results);
|
|
|
953 |
}
|
|
|
954 |
|
|
|
955 |
/**
|
|
|
956 |
* testBehaviorOnErrorCallback method
|
|
|
957 |
*
|
|
|
958 |
* @return void
|
|
|
959 |
*/
|
|
|
960 |
public function testBehaviorOnErrorCallback() {
|
|
|
961 |
$Apple = new Apple();
|
|
|
962 |
|
|
|
963 |
$Apple->Behaviors->load('Test', array('beforeFind' => 'off', 'onError' => 'on'));
|
|
|
964 |
ob_start();
|
|
|
965 |
$Apple->Behaviors->Test->onError($Apple, '');
|
|
|
966 |
$this->assertSame(trim(ob_get_clean()), 'onError trigger success');
|
|
|
967 |
}
|
|
|
968 |
|
|
|
969 |
/**
|
|
|
970 |
* testBehaviorValidateCallback method
|
|
|
971 |
*
|
|
|
972 |
* @return void
|
|
|
973 |
*/
|
|
|
974 |
public function testBehaviorValidateCallback() {
|
|
|
975 |
$Apple = new Apple();
|
|
|
976 |
|
|
|
977 |
$Apple->Behaviors->load('Test');
|
|
|
978 |
$this->assertTrue($Apple->validates());
|
|
|
979 |
|
|
|
980 |
$Apple->Behaviors->load('Test', array('validate' => 'on'));
|
|
|
981 |
$this->assertFalse($Apple->validates());
|
|
|
982 |
$this->assertSame($Apple->validationErrors, array('name' => array(true)));
|
|
|
983 |
|
|
|
984 |
$Apple->Behaviors->load('Test', array('validate' => 'stop'));
|
|
|
985 |
$this->assertFalse($Apple->validates());
|
|
|
986 |
$this->assertSame($Apple->validationErrors, array('name' => array(true, true)));
|
|
|
987 |
|
|
|
988 |
$Apple->Behaviors->load('Test', array('validate' => 'whitelist'));
|
|
|
989 |
$Apple->validates();
|
|
|
990 |
$this->assertSame($Apple->whitelist, array());
|
|
|
991 |
|
|
|
992 |
$Apple->whitelist = array('unknown');
|
|
|
993 |
$Apple->validates();
|
|
|
994 |
$this->assertSame($Apple->whitelist, array('unknown', 'name'));
|
|
|
995 |
}
|
|
|
996 |
|
|
|
997 |
/**
|
|
|
998 |
* testBehaviorValidateAfterCallback method
|
|
|
999 |
*
|
|
|
1000 |
* @return void
|
|
|
1001 |
*/
|
|
|
1002 |
public function testBehaviorValidateAfterCallback() {
|
|
|
1003 |
$Apple = new Apple();
|
|
|
1004 |
|
|
|
1005 |
$Apple->Behaviors->load('Test');
|
|
|
1006 |
$this->assertTrue($Apple->validates());
|
|
|
1007 |
|
|
|
1008 |
$Apple->Behaviors->load('Test', array('afterValidate' => 'on'));
|
|
|
1009 |
$this->assertTrue($Apple->validates());
|
|
|
1010 |
$this->assertSame($Apple->validationErrors, array());
|
|
|
1011 |
|
|
|
1012 |
$Apple->Behaviors->load('Test', array('afterValidate' => 'test'));
|
|
|
1013 |
$Apple->data = array('bar');
|
|
|
1014 |
$Apple->validates();
|
|
|
1015 |
$this->assertEquals(array('foo'), $Apple->data);
|
|
|
1016 |
}
|
|
|
1017 |
|
|
|
1018 |
/**
|
|
|
1019 |
* testBehaviorValidateMethods method
|
|
|
1020 |
*
|
|
|
1021 |
* @return void
|
|
|
1022 |
*/
|
|
|
1023 |
public function testBehaviorValidateMethods() {
|
|
|
1024 |
$Apple = new Apple();
|
|
|
1025 |
$Apple->Behaviors->load('Test');
|
|
|
1026 |
$Apple->validate['color'] = 'validateField';
|
|
|
1027 |
|
|
|
1028 |
$result = $Apple->save(array('name' => 'Genetically Modified Apple', 'color' => 'Orange'));
|
|
|
1029 |
$this->assertEquals(array('name', 'color', 'modified', 'created', 'id'), array_keys($result['Apple']));
|
|
|
1030 |
|
|
|
1031 |
$Apple->create();
|
|
|
1032 |
$result = $Apple->save(array('name' => 'Regular Apple', 'color' => 'Red'));
|
|
|
1033 |
$this->assertFalse($result);
|
|
|
1034 |
}
|
|
|
1035 |
|
|
|
1036 |
/**
|
|
|
1037 |
* testBehaviorMethodDispatching method
|
|
|
1038 |
*
|
|
|
1039 |
* @return void
|
|
|
1040 |
*/
|
|
|
1041 |
public function testBehaviorMethodDispatching() {
|
|
|
1042 |
$Apple = new Apple();
|
|
|
1043 |
$Apple->Behaviors->load('Test');
|
|
|
1044 |
|
|
|
1045 |
$expected = 'working';
|
|
|
1046 |
$this->assertEquals($expected, $Apple->testMethod());
|
|
|
1047 |
$this->assertEquals($expected, $Apple->Behaviors->dispatchMethod($Apple, 'testMethod'));
|
|
|
1048 |
|
|
|
1049 |
$result = $Apple->Behaviors->dispatchMethod($Apple, 'wtf');
|
|
|
1050 |
$this->assertEquals(array('unhandled'), $result);
|
|
|
1051 |
|
|
|
1052 |
$result = $Apple->{'look for the remote'}('in the couch');
|
|
|
1053 |
$expected = "Item.name = 'the remote' AND Location.name = 'the couch'";
|
|
|
1054 |
$this->assertEquals($expected, $result);
|
|
|
1055 |
|
|
|
1056 |
$result = $Apple->{'look for THE REMOTE'}('in the couch');
|
|
|
1057 |
$expected = "Item.name = 'THE REMOTE' AND Location.name = 'the couch'";
|
|
|
1058 |
$this->assertEquals($expected, $result, 'Mapped method was lowercased.');
|
|
|
1059 |
}
|
|
|
1060 |
|
|
|
1061 |
/**
|
|
|
1062 |
* testBehaviorMethodDispatchingWithData method
|
|
|
1063 |
*
|
|
|
1064 |
* @return void
|
|
|
1065 |
*/
|
|
|
1066 |
public function testBehaviorMethodDispatchingWithData() {
|
|
|
1067 |
$Apple = new Apple();
|
|
|
1068 |
$Apple->Behaviors->load('Test');
|
|
|
1069 |
|
|
|
1070 |
$Apple->set('field', 'value');
|
|
|
1071 |
$this->assertTrue($Apple->testData());
|
|
|
1072 |
$this->assertTrue($Apple->data['Apple']['field_2']);
|
|
|
1073 |
|
|
|
1074 |
$this->assertTrue($Apple->testData('one', 'two', 'three', 'four', 'five', 'six'));
|
|
|
1075 |
}
|
|
|
1076 |
|
|
|
1077 |
/**
|
|
|
1078 |
* undocumented function
|
|
|
1079 |
*
|
|
|
1080 |
* @return void
|
|
|
1081 |
*/
|
|
|
1082 |
public function testBindModelCallsInBehaviors() {
|
|
|
1083 |
// hasMany
|
|
|
1084 |
$Article = new Article();
|
|
|
1085 |
$Article->unbindModel(array('hasMany' => array('Comment')));
|
|
|
1086 |
$result = $Article->find('first');
|
|
|
1087 |
$this->assertFalse(array_key_exists('Comment', $result));
|
|
|
1088 |
|
|
|
1089 |
$Article->Behaviors->load('Test4');
|
|
|
1090 |
$result = $Article->find('first');
|
|
|
1091 |
$this->assertTrue(array_key_exists('Comment', $result));
|
|
|
1092 |
|
|
|
1093 |
// belongsTo
|
|
|
1094 |
$Article->unbindModel(array('belongsTo' => array('User')));
|
|
|
1095 |
$result = $Article->find('first');
|
|
|
1096 |
$this->assertFalse(array_key_exists('User', $result));
|
|
|
1097 |
|
|
|
1098 |
$Article->Behaviors->load('Test5');
|
|
|
1099 |
$result = $Article->find('first');
|
|
|
1100 |
$this->assertTrue(array_key_exists('User', $result));
|
|
|
1101 |
|
|
|
1102 |
// hasAndBelongsToMany
|
|
|
1103 |
$Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
|
|
|
1104 |
$result = $Article->find('first');
|
|
|
1105 |
$this->assertFalse(array_key_exists('Tag', $result));
|
|
|
1106 |
|
|
|
1107 |
$Article->Behaviors->load('Test6');
|
|
|
1108 |
$result = $Article->find('first');
|
|
|
1109 |
$this->assertTrue(array_key_exists('Comment', $result));
|
|
|
1110 |
|
|
|
1111 |
// hasOne
|
|
|
1112 |
$Comment = new Comment();
|
|
|
1113 |
$Comment->unbindModel(array('hasOne' => array('Attachment')));
|
|
|
1114 |
$result = $Comment->find('first');
|
|
|
1115 |
$this->assertFalse(array_key_exists('Attachment', $result));
|
|
|
1116 |
|
|
|
1117 |
$Comment->Behaviors->load('Test7');
|
|
|
1118 |
$result = $Comment->find('first');
|
|
|
1119 |
$this->assertTrue(array_key_exists('Attachment', $result));
|
|
|
1120 |
}
|
|
|
1121 |
|
|
|
1122 |
/**
|
|
|
1123 |
* Test attach and detaching
|
|
|
1124 |
*
|
|
|
1125 |
* @return void
|
|
|
1126 |
*/
|
|
|
1127 |
public function testBehaviorAttachAndDetach() {
|
|
|
1128 |
$Sample = new Sample();
|
|
|
1129 |
$Sample->actsAs = array('Test3' => array('bar'), 'Test2' => array('foo', 'bar'));
|
|
|
1130 |
$Sample->Behaviors->init($Sample->alias, $Sample->actsAs);
|
|
|
1131 |
$Sample->Behaviors->load('Test2');
|
|
|
1132 |
$Sample->Behaviors->unload('Test3');
|
|
|
1133 |
|
|
|
1134 |
$Sample->Behaviors->trigger('beforeTest', array(&$Sample));
|
|
|
1135 |
}
|
|
|
1136 |
|
|
|
1137 |
/**
|
|
|
1138 |
* test that hasMethod works with basic functions.
|
|
|
1139 |
*
|
|
|
1140 |
* @return void
|
|
|
1141 |
*/
|
|
|
1142 |
public function testHasMethodBasic() {
|
|
|
1143 |
new Sample();
|
|
|
1144 |
$Collection = new BehaviorCollection();
|
|
|
1145 |
$Collection->init('Sample', array('Test', 'Test2'));
|
|
|
1146 |
|
|
|
1147 |
$this->assertTrue($Collection->hasMethod('testMethod'));
|
|
|
1148 |
$this->assertTrue($Collection->hasMethod('resolveMethod'));
|
|
|
1149 |
|
|
|
1150 |
$this->assertFalse($Collection->hasMethod('No method'));
|
|
|
1151 |
}
|
|
|
1152 |
|
|
|
1153 |
/**
|
|
|
1154 |
* test that hasMethod works with mapped methods.
|
|
|
1155 |
*
|
|
|
1156 |
* @return void
|
|
|
1157 |
*/
|
|
|
1158 |
public function testHasMethodMappedMethods() {
|
|
|
1159 |
new Sample();
|
|
|
1160 |
$Collection = new BehaviorCollection();
|
|
|
1161 |
$Collection->init('Sample', array('Test', 'Test2'));
|
|
|
1162 |
|
|
|
1163 |
$this->assertTrue($Collection->hasMethod('look for the remote in the couch'));
|
|
|
1164 |
$this->assertTrue($Collection->hasMethod('mappingRobotOnTheRoof'));
|
|
|
1165 |
}
|
|
|
1166 |
|
|
|
1167 |
/**
|
|
|
1168 |
* test hasMethod returning a 'callback'
|
|
|
1169 |
*
|
|
|
1170 |
* @return void
|
|
|
1171 |
*/
|
|
|
1172 |
public function testHasMethodAsCallback() {
|
|
|
1173 |
new Sample();
|
|
|
1174 |
$Collection = new BehaviorCollection();
|
|
|
1175 |
$Collection->init('Sample', array('Test', 'Test2'));
|
|
|
1176 |
|
|
|
1177 |
$result = $Collection->hasMethod('testMethod', true);
|
|
|
1178 |
$expected = array('Test', 'testMethod');
|
|
|
1179 |
$this->assertEquals($expected, $result);
|
|
|
1180 |
|
|
|
1181 |
$result = $Collection->hasMethod('resolveMethod', true);
|
|
|
1182 |
$expected = array('Test2', 'resolveMethod');
|
|
|
1183 |
$this->assertEquals($expected, $result);
|
|
|
1184 |
|
|
|
1185 |
$result = $Collection->hasMethod('mappingRobotOnTheRoof', true);
|
|
|
1186 |
$expected = array('Test2', 'mapped', 'mappingRobotOnTheRoof');
|
|
|
1187 |
$this->assertEquals($expected, $result);
|
|
|
1188 |
}
|
|
|
1189 |
|
|
|
1190 |
/**
|
|
|
1191 |
* Test that behavior priority
|
|
|
1192 |
*/
|
|
|
1193 |
public function testBehaviorOrderCallbacks() {
|
|
|
1194 |
$model = ClassRegistry::init('Orangutan');
|
|
|
1195 |
$model->Behaviors->init('Orangutan', array(
|
|
|
1196 |
'Second' => array('priority' => 9),
|
|
|
1197 |
'Third',
|
|
|
1198 |
'First' => array('priority' => 8),
|
|
|
1199 |
));
|
|
|
1200 |
|
|
|
1201 |
$this->assertEmpty($model->called);
|
|
|
1202 |
|
|
|
1203 |
$model->find('first');
|
|
|
1204 |
$expected = array(
|
|
|
1205 |
'FirstBehavior',
|
|
|
1206 |
'SecondBehavior',
|
|
|
1207 |
'ThirdBehavior',
|
|
|
1208 |
);
|
|
|
1209 |
$this->assertEquals($expected, $model->called);
|
|
|
1210 |
|
|
|
1211 |
$model->called = array();
|
|
|
1212 |
$model->Behaviors->load('Third', array('priority' => 1));
|
|
|
1213 |
|
|
|
1214 |
$model->find('first');
|
|
|
1215 |
$expected = array(
|
|
|
1216 |
'ThirdBehavior',
|
|
|
1217 |
'FirstBehavior',
|
|
|
1218 |
'SecondBehavior'
|
|
|
1219 |
);
|
|
|
1220 |
$this->assertEquals($expected, $model->called);
|
|
|
1221 |
|
|
|
1222 |
$model->called = array();
|
|
|
1223 |
$model->Behaviors->load('First');
|
|
|
1224 |
|
|
|
1225 |
$model->find('first');
|
|
|
1226 |
$expected = array(
|
|
|
1227 |
'ThirdBehavior',
|
|
|
1228 |
'SecondBehavior',
|
|
|
1229 |
'FirstBehavior'
|
|
|
1230 |
);
|
|
|
1231 |
$this->assertEquals($expected, $model->called);
|
|
|
1232 |
|
|
|
1233 |
$model->called = array();
|
|
|
1234 |
$model->Behaviors->unload('Third');
|
|
|
1235 |
|
|
|
1236 |
$model->find('first');
|
|
|
1237 |
$expected = array(
|
|
|
1238 |
'SecondBehavior',
|
|
|
1239 |
'FirstBehavior'
|
|
|
1240 |
);
|
|
|
1241 |
$this->assertEquals($expected, $model->called);
|
|
|
1242 |
|
|
|
1243 |
$model->called = array();
|
|
|
1244 |
$model->Behaviors->disable('Second');
|
|
|
1245 |
|
|
|
1246 |
$model->find('first');
|
|
|
1247 |
$expected = array(
|
|
|
1248 |
'FirstBehavior'
|
|
|
1249 |
);
|
|
|
1250 |
$this->assertEquals($expected, $model->called);
|
|
|
1251 |
|
|
|
1252 |
$model->called = array();
|
|
|
1253 |
$model->Behaviors->enable('Second');
|
|
|
1254 |
|
|
|
1255 |
$model->find('first');
|
|
|
1256 |
$expected = array(
|
|
|
1257 |
'SecondBehavior',
|
|
|
1258 |
'FirstBehavior'
|
|
|
1259 |
);
|
|
|
1260 |
$this->assertEquals($expected, $model->called);
|
|
|
1261 |
}
|
|
|
1262 |
|
|
|
1263 |
}
|