pictcode / lib / Cake / Test / Case / Model / BehaviorCollectionTest.php @ 0b1b8047
履歴 | 表示 | アノテート | ダウンロード (38.111 KB)
1 | 635eef61 | spyder1211 | <?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 bool $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 bool $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 bool
|
||
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 bool $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 bool $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 bool $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 | * @return void
|
||
480 | */
|
||
481 | public function testLoadDisabled() { |
||
482 | $Apple = new Apple(); |
||
483 | $this->assertSame(array(), $Apple->Behaviors->loaded()); |
||
484 | |||
485 | $Apple->Behaviors->load('Translate', array('enabled' => false)); |
||
486 | $this->assertTrue($Apple->Behaviors->loaded('Translate')); |
||
487 | $this->assertFalse($Apple->Behaviors->enabled('Translate')); |
||
488 | } |
||
489 | |||
490 | /**
|
||
491 | * Tests loading aliased behaviors
|
||
492 | *
|
||
493 | * @return void
|
||
494 | */
|
||
495 | public function testLoadAlias() { |
||
496 | $Apple = new Apple(); |
||
497 | $this->assertSame(array(), $Apple->Behaviors->loaded()); |
||
498 | |||
499 | $Apple->Behaviors->load('Test', array('className' => 'TestAlias', 'somesetting' => true)); |
||
500 | $this->assertSame(array('Test'), $Apple->Behaviors->loaded()); |
||
501 | $this->assertInstanceOf('TestAliasBehavior', $Apple->Behaviors->Test); |
||
502 | $this->assertTrue($Apple->Behaviors->Test->settings['Apple']['somesetting']); |
||
503 | |||
504 | $this->assertEquals('working', $Apple->Behaviors->Test->testMethod($Apple, true)); |
||
505 | $this->assertEquals('working', $Apple->testMethod(true)); |
||
506 | $this->assertEquals('working', $Apple->Behaviors->dispatchMethod($Apple, 'testMethod')); |
||
507 | |||
508 | App::build(array('Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS))); |
||
509 | CakePlugin::load('TestPlugin'); |
||
510 | $this->assertTrue($Apple->Behaviors->load('SomeOther', array('className' => 'TestPlugin.TestPluginPersisterOne'))); |
||
511 | $this->assertInstanceOf('TestPluginPersisterOneBehavior', $Apple->Behaviors->SomeOther); |
||
512 | |||
513 | $result = $Apple->Behaviors->loaded(); |
||
514 | $this->assertEquals(array('Test', 'SomeOther'), $result, 'loaded() results are wrong.'); |
||
515 | App::build();
|
||
516 | CakePlugin::unload();
|
||
517 | } |
||
518 | |||
519 | /**
|
||
520 | * testBehaviorBinding method
|
||
521 | *
|
||
522 | * @return void
|
||
523 | */
|
||
524 | public function testBehaviorBinding() { |
||
525 | $Apple = new Apple(); |
||
526 | $this->assertSame(array(), $Apple->Behaviors->loaded()); |
||
527 | |||
528 | $Apple->Behaviors->load('Test', array('key' => 'value')); |
||
529 | $this->assertSame(array('Test'), $Apple->Behaviors->loaded()); |
||
530 | $this->assertEquals('testbehavior', strtolower(get_class($Apple->Behaviors->Test))); |
||
531 | $expected = array('beforeFind' => 'on', 'afterFind' => 'off', 'key' => 'value'); |
||
532 | $this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']); |
||
533 | $this->assertEquals(array('priority', 'Apple'), array_keys($Apple->Behaviors->Test->settings)); |
||
534 | |||
535 | $this->assertSame($Apple->Sample->Behaviors->loaded(), array()); |
||
536 | $Apple->Sample->Behaviors->load('Test', array('key2' => 'value2')); |
||
537 | $this->assertSame($Apple->Sample->Behaviors->loaded(), array('Test')); |
||
538 | $this->assertEquals(array('beforeFind' => 'on', 'afterFind' => 'off', 'key2' => 'value2'), $Apple->Sample->Behaviors->Test->settings['Sample']); |
||
539 | |||
540 | $this->assertEquals(array('priority', 'Apple', 'Sample'), array_keys($Apple->Behaviors->Test->settings)); |
||
541 | $this->assertSame(
|
||
542 | $Apple->Sample->Behaviors->Test->settings, |
||
543 | $Apple->Behaviors->Test->settings |
||
544 | ); |
||
545 | $this->assertNotSame($Apple->Behaviors->Test->settings['Apple'], $Apple->Sample->Behaviors->Test->settings['Sample']); |
||
546 | |||
547 | $Apple->Behaviors->load('Test', array('key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off')); |
||
548 | $Apple->Sample->Behaviors->load('Test', array('key' => 'value', 'key3' => 'value3', 'beforeFind' => 'off')); |
||
549 | $this->assertEquals(array('beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'value', 'key2' => 'value2', 'key3' => 'value3'), $Apple->Behaviors->Test->settings['Apple']); |
||
550 | $this->assertEquals($Apple->Behaviors->Test->settings['Apple'], $Apple->Sample->Behaviors->Test->settings['Sample']); |
||
551 | |||
552 | $this->assertFalse(isset($Apple->Child->Behaviors->Test)); |
||
553 | $Apple->Child->Behaviors->load('Test', array('key' => 'value', 'key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off')); |
||
554 | $this->assertEquals($Apple->Child->Behaviors->Test->settings['Child'], $Apple->Sample->Behaviors->Test->settings['Sample']); |
||
555 | |||
556 | $this->assertFalse(isset($Apple->Parent->Behaviors->Test)); |
||
557 | $Apple->Parent->Behaviors->load('Test', array('key' => 'value', 'key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off')); |
||
558 | $this->assertEquals($Apple->Parent->Behaviors->Test->settings['Parent'], $Apple->Sample->Behaviors->Test->settings['Sample']); |
||
559 | |||
560 | $Apple->Parent->Behaviors->load('Test', array('key' => 'value', 'key2' => 'value', 'key3' => 'value', 'beforeFind' => 'off')); |
||
561 | $this->assertNotEquals($Apple->Parent->Behaviors->Test->settings['Parent'], $Apple->Sample->Behaviors->Test->settings['Sample']); |
||
562 | |||
563 | $Apple->Behaviors->load('Plugin.Test', array('key' => 'new value')); |
||
564 | $expected = array( |
||
565 | 'beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'new value', |
||
566 | 'key2' => 'value2', 'key3' => 'value3' |
||
567 | ); |
||
568 | $this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']); |
||
569 | |||
570 | $current = $Apple->Behaviors->Test->settings['Apple']; |
||
571 | $expected = array_merge($current, array('mangle' => 'trigger mangled')); |
||
572 | $Apple->Behaviors->load('Test', array('mangle' => 'trigger')); |
||
573 | $this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']); |
||
574 | |||
575 | $Apple->Behaviors->load('Test'); |
||
576 | $expected = array_merge($current, array('mangle' => 'trigger mangled mangled')); |
||
577 | |||
578 | $this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']); |
||
579 | $Apple->Behaviors->load('Test', array('mangle' => 'trigger')); |
||
580 | $expected = array_merge($current, array('mangle' => 'trigger mangled')); |
||
581 | $this->assertEquals($expected, $Apple->Behaviors->Test->settings['Apple']); |
||
582 | } |
||
583 | |||
584 | /**
|
||
585 | * test that attach()/detach() works with plugin.banana
|
||
586 | *
|
||
587 | * @return void
|
||
588 | */
|
||
589 | public function testDetachWithPluginNames() { |
||
590 | $Apple = new Apple(); |
||
591 | $Apple->Behaviors->load('Plugin.Test'); |
||
592 | $this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior'); |
||
593 | $this->assertEquals(array('Test'), $Apple->Behaviors->loaded()); |
||
594 | |||
595 | $Apple->Behaviors->unload('Plugin.Test'); |
||
596 | $this->assertEquals(array(), $Apple->Behaviors->loaded()); |
||
597 | |||
598 | $Apple->Behaviors->load('Plugin.Test'); |
||
599 | $this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior'); |
||
600 | $this->assertEquals(array('Test'), $Apple->Behaviors->loaded()); |
||
601 | |||
602 | $Apple->Behaviors->unload('Test'); |
||
603 | $this->assertEquals(array(), $Apple->Behaviors->loaded()); |
||
604 | } |
||
605 | |||
606 | /**
|
||
607 | * test that attaching a non existent Behavior triggers a cake error.
|
||
608 | *
|
||
609 | * @expectedException MissingBehaviorException
|
||
610 | * @return void
|
||
611 | */
|
||
612 | public function testInvalidBehaviorCausingCakeError() { |
||
613 | $Apple = new Apple(); |
||
614 | $Apple->Behaviors->load('NoSuchBehavior'); |
||
615 | } |
||
616 | |||
617 | /**
|
||
618 | * testBehaviorToggling method
|
||
619 | *
|
||
620 | * @return void
|
||
621 | */
|
||
622 | public function testBehaviorToggling() { |
||
623 | $Apple = new Apple(); |
||
624 | $this->assertSame($Apple->Behaviors->enabled(), array()); |
||
625 | |||
626 | $Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value'))); |
||
627 | $this->assertSame($Apple->Behaviors->enabled(), array('Test')); |
||
628 | |||
629 | $Apple->Behaviors->disable('Test'); |
||
630 | $this->assertSame(array('Test'), $Apple->Behaviors->loaded()); |
||
631 | $this->assertSame($Apple->Behaviors->enabled(), array()); |
||
632 | |||
633 | $Apple->Sample->Behaviors->load('Test'); |
||
634 | $this->assertTrue($Apple->Sample->Behaviors->enabled('Test')); |
||
635 | $this->assertSame($Apple->Behaviors->enabled(), array()); |
||
636 | |||
637 | $Apple->Behaviors->enable('Test'); |
||
638 | $this->assertTrue($Apple->Behaviors->loaded('Test')); |
||
639 | $this->assertSame($Apple->Behaviors->enabled(), array('Test')); |
||
640 | |||
641 | $Apple->Behaviors->disable('Test'); |
||
642 | $this->assertSame($Apple->Behaviors->enabled(), array()); |
||
643 | $Apple->Behaviors->load('Test', array('enabled' => true)); |
||
644 | $this->assertSame($Apple->Behaviors->enabled(), array('Test')); |
||
645 | $Apple->Behaviors->load('Test', array('enabled' => false)); |
||
646 | $this->assertSame($Apple->Behaviors->enabled(), array()); |
||
647 | $Apple->Behaviors->unload('Test'); |
||
648 | $this->assertSame($Apple->Behaviors->enabled(), array()); |
||
649 | } |
||
650 | |||
651 | /**
|
||
652 | * testBehaviorFindCallbacks method
|
||
653 | *
|
||
654 | * @return void
|
||
655 | */
|
||
656 | public function testBehaviorFindCallbacks() { |
||
657 | $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.'); |
||
658 | |||
659 | $Apple = new Apple(); |
||
660 | $expected = $Apple->find('all'); |
||
661 | |||
662 | $Apple->Behaviors->load('Test'); |
||
663 | $this->assertNull($Apple->find('all')); |
||
664 | |||
665 | $Apple->Behaviors->load('Test', array('beforeFind' => 'off')); |
||
666 | $this->assertSame($expected, $Apple->find('all')); |
||
667 | |||
668 | $Apple->Behaviors->load('Test', array('beforeFind' => 'test')); |
||
669 | $this->assertSame($expected, $Apple->find('all')); |
||
670 | |||
671 | $Apple->Behaviors->load('Test', array('beforeFind' => 'modify')); |
||
672 | $expected2 = array( |
||
673 | array('Apple' => array('id' => '1', 'name' => 'Red Apple 1', 'mytime' => '22:57:17')), |
||
674 | array('Apple' => array('id' => '2', 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')), |
||
675 | array('Apple' => array('id' => '3', 'name' => 'green blue', 'mytime' => '22:57:17')) |
||
676 | ); |
||
677 | $result = $Apple->find('all', array('conditions' => array('Apple.id <' => '4'))); |
||
678 | $this->assertEquals($expected2, $result); |
||
679 | |||
680 | $Apple->Behaviors->disable('Test'); |
||
681 | $result = $Apple->find('all'); |
||
682 | $this->assertEquals($expected, $result); |
||
683 | |||
684 | $Apple->Behaviors->load('Test', array('beforeFind' => 'off', 'afterFind' => 'on')); |
||
685 | $this->assertSame($Apple->find('all'), array()); |
||
686 | |||
687 | $Apple->Behaviors->load('Test', array('afterFind' => 'off')); |
||
688 | $this->assertEquals($expected, $Apple->find('all')); |
||
689 | |||
690 | $Apple->Behaviors->load('Test', array('afterFind' => 'test')); |
||
691 | $this->assertEquals($expected, $Apple->find('all')); |
||
692 | |||
693 | $Apple->Behaviors->load('Test', array('afterFind' => 'test2')); |
||
694 | $this->assertEquals($expected, $Apple->find('all')); |
||
695 | |||
696 | $Apple->Behaviors->load('Test', array('afterFind' => 'modify')); |
||
697 | $expected = array( |
||
698 | 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'), |
||
699 | 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'), |
||
700 | 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'), |
||
701 | 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'), |
||
702 | 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'), |
||
703 | 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'), |
||
704 | 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') |
||
705 | ); |
||
706 | $this->assertEquals($expected, $Apple->find('all')); |
||
707 | } |
||
708 | |||
709 | /**
|
||
710 | * testBehaviorHasManyFindCallbacks method
|
||
711 | *
|
||
712 | * @return void
|
||
713 | */
|
||
714 | public function testBehaviorHasManyFindCallbacks() { |
||
715 | $Apple = new Apple(); |
||
716 | $Apple->unbindModel(array('hasOne' => array('Sample'), 'belongsTo' => array('Parent')), false); |
||
717 | $expected = $Apple->find('all'); |
||
718 | |||
719 | $Apple->unbindModel(array('hasMany' => array('Child'))); |
||
720 | $wellBehaved = $Apple->find('all'); |
||
721 | $Apple->Child->Behaviors->load('Test', array('afterFind' => 'modify')); |
||
722 | $Apple->unbindModel(array('hasMany' => array('Child'))); |
||
723 | $this->assertSame($Apple->find('all'), $wellBehaved); |
||
724 | |||
725 | $Apple->Child->Behaviors->load('Test', array('before' => 'off')); |
||
726 | $this->assertSame($expected, $Apple->find('all')); |
||
727 | |||
728 | $Apple->Child->Behaviors->load('Test', array('before' => 'test')); |
||
729 | $this->assertSame($expected, $Apple->find('all')); |
||
730 | |||
731 | $Apple->Child->Behaviors->load('Test', array('before' => 'modify')); |
||
732 | $result = $Apple->find('all', array('fields' => array('Apple.id'), 'conditions' => array('Apple.id <' => '4'))); |
||
733 | |||
734 | $Apple->Child->Behaviors->disable('Test'); |
||
735 | $result = $Apple->find('all'); |
||
736 | $this->assertEquals($expected, $result); |
||
737 | |||
738 | $Apple->Child->Behaviors->load('Test', array('before' => 'off', 'after' => 'on')); |
||
739 | |||
740 | $Apple->Child->Behaviors->load('Test', array('after' => 'off')); |
||
741 | $this->assertEquals($expected, $Apple->find('all')); |
||
742 | |||
743 | $Apple->Child->Behaviors->load('Test', array('after' => 'test')); |
||
744 | $this->assertEquals($expected, $Apple->find('all')); |
||
745 | |||
746 | $Apple->Child->Behaviors->load('Test', array('after' => 'test2')); |
||
747 | $this->assertEquals($expected, $Apple->find('all')); |
||
748 | } |
||
749 | |||
750 | /**
|
||
751 | * testBehaviorHasOneFindCallbacks method
|
||
752 | *
|
||
753 | * @return void
|
||
754 | */
|
||
755 | public function testBehaviorHasOneFindCallbacks() { |
||
756 | $Apple = new Apple(); |
||
757 | $Apple->unbindModel(array('hasMany' => array('Child'), 'belongsTo' => array('Parent')), false); |
||
758 | $expected = $Apple->find('all'); |
||
759 | |||
760 | $Apple->unbindModel(array('hasOne' => array('Sample'))); |
||
761 | $wellBehaved = $Apple->find('all'); |
||
762 | $Apple->Sample->Behaviors->load('Test'); |
||
763 | $Apple->unbindModel(array('hasOne' => array('Sample'))); |
||
764 | $this->assertSame($Apple->find('all'), $wellBehaved); |
||
765 | |||
766 | $Apple->Sample->Behaviors->load('Test', array('before' => 'off')); |
||
767 | $this->assertSame($expected, $Apple->find('all')); |
||
768 | |||
769 | $Apple->Sample->Behaviors->load('Test', array('before' => 'test')); |
||
770 | $this->assertSame($expected, $Apple->find('all')); |
||
771 | |||
772 | $Apple->Sample->Behaviors->disable('Test'); |
||
773 | $result = $Apple->find('all'); |
||
774 | $this->assertEquals($expected, $result); |
||
775 | |||
776 | $Apple->Sample->Behaviors->load('Test', array('after' => 'off')); |
||
777 | $this->assertEquals($expected, $Apple->find('all')); |
||
778 | |||
779 | $Apple->Sample->Behaviors->load('Test', array('after' => 'test')); |
||
780 | $this->assertEquals($expected, $Apple->find('all')); |
||
781 | |||
782 | $Apple->Sample->Behaviors->load('Test', array('after' => 'test2')); |
||
783 | $this->assertEquals($expected, $Apple->find('all')); |
||
784 | } |
||
785 | |||
786 | /**
|
||
787 | * testBehaviorBelongsToFindCallbacks method
|
||
788 | *
|
||
789 | * @return void
|
||
790 | */
|
||
791 | public function testBehaviorBelongsToFindCallbacks() { |
||
792 | $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.'); |
||
793 | |||
794 | $conditions = array('order' => 'Apple.id ASC'); |
||
795 | $Apple = new Apple(); |
||
796 | $Apple->unbindModel(array('hasMany' => array('Child'), 'hasOne' => array('Sample')), false); |
||
797 | $expected = $Apple->find('all', $conditions); |
||
798 | |||
799 | $Apple->unbindModel(array('belongsTo' => array('Parent'))); |
||
800 | $wellBehaved = $Apple->find('all', $conditions); |
||
801 | $Apple->Parent->Behaviors->load('Test'); |
||
802 | $Apple->unbindModel(array('belongsTo' => array('Parent'))); |
||
803 | $this->assertSame($Apple->find('all', $conditions), $wellBehaved); |
||
804 | |||
805 | $Apple->Parent->Behaviors->load('Test', array('before' => 'off')); |
||
806 | $this->assertSame($expected, $Apple->find('all', $conditions)); |
||
807 | |||
808 | $Apple->Parent->Behaviors->load('Test', array('before' => 'test')); |
||
809 | $this->assertSame($expected, $Apple->find('all', $conditions)); |
||
810 | |||
811 | $Apple->Parent->Behaviors->load('Test', array('before' => 'modify')); |
||
812 | $expected2 = array( |
||
813 | array(
|
||
814 | 'Apple' => array('id' => 1), |
||
815 | 'Parent' => array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')), |
||
816 | array(
|
||
817 | 'Apple' => array('id' => 2), |
||
818 | 'Parent' => array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17')), |
||
819 | array(
|
||
820 | 'Apple' => array('id' => 3), |
||
821 | 'Parent' => array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')) |
||
822 | ); |
||
823 | $result2 = $Apple->find('all', array( |
||
824 | 'fields' => array('Apple.id', 'Parent.id', 'Parent.name', 'Parent.mytime'), |
||
825 | 'conditions' => array('Apple.id <' => '4'), |
||
826 | 'order' => 'Apple.id ASC', |
||
827 | )); |
||
828 | $this->assertEquals($expected2, $result2); |
||
829 | |||
830 | $Apple->Parent->Behaviors->disable('Test'); |
||
831 | $result = $Apple->find('all', $conditions); |
||
832 | $this->assertEquals($expected, $result); |
||
833 | |||
834 | $Apple->Parent->Behaviors->load('Test', array('after' => 'off')); |
||
835 | $this->assertEquals($expected, $Apple->find('all', $conditions)); |
||
836 | |||
837 | $Apple->Parent->Behaviors->load('Test', array('after' => 'test')); |
||
838 | $this->assertEquals($expected, $Apple->find('all', $conditions)); |
||
839 | |||
840 | $Apple->Parent->Behaviors->load('Test', array('after' => 'test2')); |
||
841 | $this->assertEquals($expected, $Apple->find('all', $conditions)); |
||
842 | } |
||
843 | |||
844 | /**
|
||
845 | * testBehaviorSaveCallbacks method
|
||
846 | *
|
||
847 | * @return void
|
||
848 | */
|
||
849 | public function testBehaviorSaveCallbacks() { |
||
850 | $Sample = new Sample(); |
||
851 | $record = array('Sample' => array('apple_id' => 6, 'name' => 'sample99')); |
||
852 | |||
853 | $Sample->Behaviors->load('Test', array('beforeSave' => 'on')); |
||
854 | $Sample->create();
|
||
855 | $this->assertSame(false, $Sample->save($record)); |
||
856 | |||
857 | $Sample->Behaviors->load('Test', array('beforeSave' => 'off')); |
||
858 | $Sample->create();
|
||
859 | $result = $Sample->save($record); |
||
860 | $expected = $record; |
||
861 | $expected['Sample']['id'] = $Sample->id; |
||
862 | $this->assertSame($expected, $result); |
||
863 | |||
864 | $Sample->Behaviors->load('Test', array('beforeSave' => 'test')); |
||
865 | $Sample->create();
|
||
866 | $result = $Sample->save($record); |
||
867 | $expected = $record; |
||
868 | $expected['Sample']['id'] = $Sample->id; |
||
869 | $this->assertSame($expected, $result); |
||
870 | |||
871 | $Sample->Behaviors->load('Test', array('beforeSave' => 'modify')); |
||
872 | $expected = Hash::insert($record, 'Sample.name', 'sample99 modified before'); |
||
873 | $Sample->create();
|
||
874 | $result = $Sample->save($record); |
||
875 | $expected['Sample']['id'] = $Sample->id; |
||
876 | $this->assertSame($expected, $result); |
||
877 | |||
878 | $Sample->Behaviors->disable('Test'); |
||
879 | $this->assertSame($record, $Sample->save($record)); |
||
880 | |||
881 | $Sample->Behaviors->load('Test', array('beforeSave' => 'off', 'afterSave' => 'on')); |
||
882 | $expected = Hash::merge($record, array('Sample' => array('aftersave' => 'modified after on create'))); |
||
883 | $Sample->create();
|
||
884 | $result = $Sample->save($record); |
||
885 | $expected['Sample']['id'] = $Sample->id; |
||
886 | $this->assertEquals($expected, $result); |
||
887 | |||
888 | $Sample->Behaviors->load('Test', array('beforeSave' => 'modify', 'afterSave' => 'modify')); |
||
889 | $expected = Hash::merge($record, array('Sample' => array('name' => 'sample99 modified before modified after on create'))); |
||
890 | $Sample->create();
|
||
891 | $result = $Sample->save($record); |
||
892 | $expected['Sample']['id'] = $Sample->id; |
||
893 | $this->assertSame($expected, $result); |
||
894 | |||
895 | $Sample->Behaviors->load('Test', array('beforeSave' => 'off', 'afterSave' => 'test')); |
||
896 | $Sample->create();
|
||
897 | $expected = $record; |
||
898 | unset($expected['Sample']['name']); |
||
899 | $result = $Sample->save($record); |
||
900 | $expected['Sample']['id'] = $Sample->id; |
||
901 | $this->assertSame($expected, $result); |
||
902 | |||
903 | $Sample->Behaviors->load('Test', array('afterSave' => 'test2')); |
||
904 | $Sample->create();
|
||
905 | $expected = $record; |
||
906 | $result = $Sample->save($record); |
||
907 | $expected['Sample']['id'] = $Sample->id; |
||
908 | $this->assertSame($expected, $result); |
||
909 | |||
910 | $Sample->Behaviors->load('Test', array('beforeFind' => 'off', 'afterFind' => 'off')); |
||
911 | $Sample->recursive = -1; |
||
912 | $record2 = $Sample->read(null, 1); |
||
913 | |||
914 | $Sample->Behaviors->load('Test', array('afterSave' => 'on')); |
||
915 | $expected = Hash::merge($record2, array('Sample' => array('aftersave' => 'modified after'))); |
||
916 | $Sample->create();
|
||
917 | $this->assertSame($expected, $Sample->save($record2)); |
||
918 | |||
919 | $Sample->Behaviors->load('Test', array('afterSave' => 'modify')); |
||
920 | $expected = Hash::merge($record2, array('Sample' => array('name' => 'sample1 modified after'))); |
||
921 | $Sample->create();
|
||
922 | $this->assertSame($expected, $Sample->save($record2)); |
||
923 | } |
||
924 | |||
925 | /**
|
||
926 | * testBehaviorDeleteCallbacks method
|
||
927 | *
|
||
928 | * @return void
|
||
929 | */
|
||
930 | public function testBehaviorDeleteCallbacks() { |
||
931 | $Apple = new Apple(); |
||
932 | |||
933 | $Apple->Behaviors->load('Test', array('beforeFind' => 'off', 'beforeDelete' => 'off')); |
||
934 | $this->assertTrue($Apple->delete(6)); |
||
935 | |||
936 | $Apple->Behaviors->load('Test', array('beforeDelete' => 'on')); |
||
937 | $this->assertFalse($Apple->delete(4)); |
||
938 | |||
939 | $Apple->Behaviors->load('Test', array('beforeDelete' => 'test2')); |
||
940 | |||
941 | ob_start(); |
||
942 | $results = $Apple->delete(4); |
||
943 | $this->assertSame(trim(ob_get_clean()), 'beforeDelete success (cascading)'); |
||
944 | $this->assertTrue($results); |
||
945 | |||
946 | ob_start(); |
||
947 | $results = $Apple->delete(3, false); |
||
948 | $this->assertSame(trim(ob_get_clean()), 'beforeDelete success'); |
||
949 | $this->assertTrue($results); |
||
950 | |||
951 | $Apple->Behaviors->load('Test', array('beforeDelete' => 'off', 'afterDelete' => 'on')); |
||
952 | ob_start(); |
||
953 | $results = $Apple->delete(2, false); |
||
954 | $this->assertSame(trim(ob_get_clean()), 'afterDelete success'); |
||
955 | $this->assertTrue($results); |
||
956 | } |
||
957 | |||
958 | /**
|
||
959 | * testBehaviorOnErrorCallback method
|
||
960 | *
|
||
961 | * @return void
|
||
962 | */
|
||
963 | public function testBehaviorOnErrorCallback() { |
||
964 | $Apple = new Apple(); |
||
965 | |||
966 | $Apple->Behaviors->load('Test', array('beforeFind' => 'off', 'onError' => 'on')); |
||
967 | ob_start(); |
||
968 | $Apple->Behaviors->Test->onError($Apple, ''); |
||
969 | $this->assertSame(trim(ob_get_clean()), 'onError trigger success'); |
||
970 | } |
||
971 | |||
972 | /**
|
||
973 | * testBehaviorValidateCallback method
|
||
974 | *
|
||
975 | * @return void
|
||
976 | */
|
||
977 | public function testBehaviorValidateCallback() { |
||
978 | $Apple = new Apple(); |
||
979 | |||
980 | $Apple->Behaviors->load('Test'); |
||
981 | $this->assertTrue($Apple->validates()); |
||
982 | |||
983 | $Apple->Behaviors->load('Test', array('validate' => 'on')); |
||
984 | $this->assertFalse($Apple->validates()); |
||
985 | $this->assertSame($Apple->validationErrors, array('name' => array(true))); |
||
986 | |||
987 | $Apple->Behaviors->load('Test', array('validate' => 'stop')); |
||
988 | $this->assertFalse($Apple->validates()); |
||
989 | $this->assertSame($Apple->validationErrors, array('name' => array(true, true))); |
||
990 | |||
991 | $Apple->Behaviors->load('Test', array('validate' => 'whitelist')); |
||
992 | $Apple->validates();
|
||
993 | $this->assertSame($Apple->whitelist, array()); |
||
994 | |||
995 | $Apple->whitelist = array('unknown'); |
||
996 | $Apple->validates();
|
||
997 | $this->assertSame($Apple->whitelist, array('unknown', 'name')); |
||
998 | } |
||
999 | |||
1000 | /**
|
||
1001 | * testBehaviorValidateAfterCallback method
|
||
1002 | *
|
||
1003 | * @return void
|
||
1004 | */
|
||
1005 | public function testBehaviorValidateAfterCallback() { |
||
1006 | $Apple = new Apple(); |
||
1007 | |||
1008 | $Apple->Behaviors->load('Test'); |
||
1009 | $this->assertTrue($Apple->validates()); |
||
1010 | |||
1011 | $Apple->Behaviors->load('Test', array('afterValidate' => 'on')); |
||
1012 | $this->assertTrue($Apple->validates()); |
||
1013 | $this->assertSame($Apple->validationErrors, array()); |
||
1014 | |||
1015 | $Apple->Behaviors->load('Test', array('afterValidate' => 'test')); |
||
1016 | $Apple->data = array('bar'); |
||
1017 | $Apple->validates();
|
||
1018 | $this->assertEquals(array('foo'), $Apple->data); |
||
1019 | } |
||
1020 | |||
1021 | /**
|
||
1022 | * testBehaviorValidateMethods method
|
||
1023 | *
|
||
1024 | * @return void
|
||
1025 | */
|
||
1026 | public function testBehaviorValidateMethods() { |
||
1027 | $Apple = new Apple(); |
||
1028 | $Apple->Behaviors->load('Test'); |
||
1029 | $Apple->validate['color'] = 'validateField'; |
||
1030 | |||
1031 | $result = $Apple->save(array('name' => 'Genetically Modified Apple', 'color' => 'Orange')); |
||
1032 | $this->assertEquals(array('name', 'color', 'modified', 'created', 'id'), array_keys($result['Apple'])); |
||
1033 | |||
1034 | $Apple->create();
|
||
1035 | $result = $Apple->save(array('name' => 'Regular Apple', 'color' => 'Red')); |
||
1036 | $this->assertFalse($result); |
||
1037 | } |
||
1038 | |||
1039 | /**
|
||
1040 | * testBehaviorMethodDispatching method
|
||
1041 | *
|
||
1042 | * @return void
|
||
1043 | */
|
||
1044 | public function testBehaviorMethodDispatching() { |
||
1045 | $Apple = new Apple(); |
||
1046 | $Apple->Behaviors->load('Test'); |
||
1047 | |||
1048 | $expected = 'working'; |
||
1049 | $this->assertEquals($expected, $Apple->testMethod()); |
||
1050 | $this->assertEquals($expected, $Apple->Behaviors->dispatchMethod($Apple, 'testMethod')); |
||
1051 | |||
1052 | $result = $Apple->Behaviors->dispatchMethod($Apple, 'wtf'); |
||
1053 | $this->assertEquals(array('unhandled'), $result); |
||
1054 | |||
1055 | $result = $Apple->{'look for the remote'}('in the couch'); |
||
1056 | $expected = "Item.name = 'the remote' AND Location.name = 'the couch'"; |
||
1057 | $this->assertEquals($expected, $result); |
||
1058 | |||
1059 | $result = $Apple->{'look for THE REMOTE'}('in the couch'); |
||
1060 | $expected = "Item.name = 'THE REMOTE' AND Location.name = 'the couch'"; |
||
1061 | $this->assertEquals($expected, $result, 'Mapped method was lowercased.'); |
||
1062 | } |
||
1063 | |||
1064 | /**
|
||
1065 | * testBehaviorMethodDispatchingWithData method
|
||
1066 | *
|
||
1067 | * @return void
|
||
1068 | */
|
||
1069 | public function testBehaviorMethodDispatchingWithData() { |
||
1070 | $Apple = new Apple(); |
||
1071 | $Apple->Behaviors->load('Test'); |
||
1072 | |||
1073 | $Apple->set('field', 'value'); |
||
1074 | $this->assertTrue($Apple->testData()); |
||
1075 | $this->assertTrue($Apple->data['Apple']['field_2']); |
||
1076 | |||
1077 | $this->assertTrue($Apple->testData('one', 'two', 'three', 'four', 'five', 'six')); |
||
1078 | } |
||
1079 | |||
1080 | /**
|
||
1081 | * undocumented function
|
||
1082 | *
|
||
1083 | * @return void
|
||
1084 | */
|
||
1085 | public function testBindModelCallsInBehaviors() { |
||
1086 | // hasMany
|
||
1087 | $Article = new Article(); |
||
1088 | $Article->unbindModel(array('hasMany' => array('Comment'))); |
||
1089 | $result = $Article->find('first'); |
||
1090 | $this->assertFalse(array_key_exists('Comment', $result)); |
||
1091 | |||
1092 | $Article->Behaviors->load('Test4'); |
||
1093 | $result = $Article->find('first'); |
||
1094 | $this->assertTrue(array_key_exists('Comment', $result)); |
||
1095 | |||
1096 | // belongsTo
|
||
1097 | $Article->unbindModel(array('belongsTo' => array('User'))); |
||
1098 | $result = $Article->find('first'); |
||
1099 | $this->assertFalse(array_key_exists('User', $result)); |
||
1100 | |||
1101 | $Article->Behaviors->load('Test5'); |
||
1102 | $result = $Article->find('first'); |
||
1103 | $this->assertTrue(array_key_exists('User', $result)); |
||
1104 | |||
1105 | // hasAndBelongsToMany
|
||
1106 | $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag'))); |
||
1107 | $result = $Article->find('first'); |
||
1108 | $this->assertFalse(array_key_exists('Tag', $result)); |
||
1109 | |||
1110 | $Article->Behaviors->load('Test6'); |
||
1111 | $result = $Article->find('first'); |
||
1112 | $this->assertTrue(array_key_exists('Comment', $result)); |
||
1113 | |||
1114 | // hasOne
|
||
1115 | $Comment = new Comment(); |
||
1116 | $Comment->unbindModel(array('hasOne' => array('Attachment'))); |
||
1117 | $result = $Comment->find('first'); |
||
1118 | $this->assertFalse(array_key_exists('Attachment', $result)); |
||
1119 | |||
1120 | $Comment->Behaviors->load('Test7'); |
||
1121 | $result = $Comment->find('first'); |
||
1122 | $this->assertTrue(array_key_exists('Attachment', $result)); |
||
1123 | } |
||
1124 | |||
1125 | /**
|
||
1126 | * Test attach and detaching
|
||
1127 | *
|
||
1128 | * @return void
|
||
1129 | */
|
||
1130 | public function testBehaviorAttachAndDetach() { |
||
1131 | $Sample = new Sample(); |
||
1132 | $Sample->actsAs = array('Test3' => array('bar'), 'Test2' => array('foo', 'bar')); |
||
1133 | $Sample->Behaviors->init($Sample->alias, $Sample->actsAs); |
||
1134 | $Sample->Behaviors->load('Test2'); |
||
1135 | $Sample->Behaviors->unload('Test3'); |
||
1136 | |||
1137 | $Sample->Behaviors->trigger('beforeTest', array(&$Sample)); |
||
1138 | } |
||
1139 | |||
1140 | /**
|
||
1141 | * test that hasMethod works with basic functions.
|
||
1142 | *
|
||
1143 | * @return void
|
||
1144 | */
|
||
1145 | public function testHasMethodBasic() { |
||
1146 | new Sample(); |
||
1147 | $Collection = new BehaviorCollection(); |
||
1148 | $Collection->init('Sample', array('Test', 'Test2')); |
||
1149 | |||
1150 | $this->assertTrue($Collection->hasMethod('testMethod')); |
||
1151 | $this->assertTrue($Collection->hasMethod('resolveMethod')); |
||
1152 | |||
1153 | $this->assertFalse($Collection->hasMethod('No method')); |
||
1154 | } |
||
1155 | |||
1156 | /**
|
||
1157 | * test that hasMethod works with mapped methods.
|
||
1158 | *
|
||
1159 | * @return void
|
||
1160 | */
|
||
1161 | public function testHasMethodMappedMethods() { |
||
1162 | new Sample(); |
||
1163 | $Collection = new BehaviorCollection(); |
||
1164 | $Collection->init('Sample', array('Test', 'Test2')); |
||
1165 | |||
1166 | $this->assertTrue($Collection->hasMethod('look for the remote in the couch')); |
||
1167 | $this->assertTrue($Collection->hasMethod('mappingRobotOnTheRoof')); |
||
1168 | } |
||
1169 | |||
1170 | /**
|
||
1171 | * test hasMethod returning a 'callback'
|
||
1172 | *
|
||
1173 | * @return void
|
||
1174 | */
|
||
1175 | public function testHasMethodAsCallback() { |
||
1176 | new Sample(); |
||
1177 | $Collection = new BehaviorCollection(); |
||
1178 | $Collection->init('Sample', array('Test', 'Test2')); |
||
1179 | |||
1180 | $result = $Collection->hasMethod('testMethod', true); |
||
1181 | $expected = array('Test', 'testMethod'); |
||
1182 | $this->assertEquals($expected, $result); |
||
1183 | |||
1184 | $result = $Collection->hasMethod('resolveMethod', true); |
||
1185 | $expected = array('Test2', 'resolveMethod'); |
||
1186 | $this->assertEquals($expected, $result); |
||
1187 | |||
1188 | $result = $Collection->hasMethod('mappingRobotOnTheRoof', true); |
||
1189 | $expected = array('Test2', 'mapped', 'mappingRobotOnTheRoof'); |
||
1190 | $this->assertEquals($expected, $result); |
||
1191 | } |
||
1192 | |||
1193 | /**
|
||
1194 | * Test that behavior priority
|
||
1195 | *
|
||
1196 | * @return void
|
||
1197 | */
|
||
1198 | public function testBehaviorOrderCallbacks() { |
||
1199 | $model = ClassRegistry::init('Orangutan'); |
||
1200 | $model->Behaviors->init('Orangutan', array( |
||
1201 | 'Second' => array('priority' => 9), |
||
1202 | 'Third',
|
||
1203 | 'First' => array('priority' => 8), |
||
1204 | )); |
||
1205 | |||
1206 | $this->assertEmpty($model->called); |
||
1207 | |||
1208 | $model->find('first'); |
||
1209 | $expected = array( |
||
1210 | 'FirstBehavior',
|
||
1211 | 'SecondBehavior',
|
||
1212 | 'ThirdBehavior',
|
||
1213 | ); |
||
1214 | $this->assertEquals($expected, $model->called); |
||
1215 | |||
1216 | $model->called = array(); |
||
1217 | $model->Behaviors->load('Third', array('priority' => 1)); |
||
1218 | |||
1219 | $model->find('first'); |
||
1220 | $expected = array( |
||
1221 | 'ThirdBehavior',
|
||
1222 | 'FirstBehavior',
|
||
1223 | 'SecondBehavior'
|
||
1224 | ); |
||
1225 | $this->assertEquals($expected, $model->called); |
||
1226 | |||
1227 | $model->called = array(); |
||
1228 | $model->Behaviors->load('First'); |
||
1229 | |||
1230 | $model->find('first'); |
||
1231 | $expected = array( |
||
1232 | 'ThirdBehavior',
|
||
1233 | 'SecondBehavior',
|
||
1234 | 'FirstBehavior'
|
||
1235 | ); |
||
1236 | $this->assertEquals($expected, $model->called); |
||
1237 | |||
1238 | $model->called = array(); |
||
1239 | $model->Behaviors->unload('Third'); |
||
1240 | |||
1241 | $model->find('first'); |
||
1242 | $expected = array( |
||
1243 | 'SecondBehavior',
|
||
1244 | 'FirstBehavior'
|
||
1245 | ); |
||
1246 | $this->assertEquals($expected, $model->called); |
||
1247 | |||
1248 | $model->called = array(); |
||
1249 | $model->Behaviors->disable('Second'); |
||
1250 | |||
1251 | $model->find('first'); |
||
1252 | $expected = array( |
||
1253 | 'FirstBehavior'
|
||
1254 | ); |
||
1255 | $this->assertEquals($expected, $model->called); |
||
1256 | |||
1257 | $model->called = array(); |
||
1258 | $model->Behaviors->enable('Second'); |
||
1259 | |||
1260 | $model->find('first'); |
||
1261 | $expected = array( |
||
1262 | 'SecondBehavior',
|
||
1263 | 'FirstBehavior'
|
||
1264 | ); |
||
1265 | $this->assertEquals($expected, $model->called); |
||
1266 | } |
||
1267 | |||
1268 | } |