5
* Long description for behavior.test.php
9
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
12
* Licensed under The MIT License
13
* Redistributions of files must retain the above copyright notice.
15
* @copyright CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
16
* @link http://cakephp.org CakePHP(tm) Project
18
* @subpackage cake.tests.cases.libs.model
20
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
22
App::import('Model', 'AppModel');
23
require_once dirname(__FILE__) . DS . 'models.php';
25
Mock::generatePartial('BehaviorCollection', 'MockModelBehaviorCollection', array('cakeError', '_stop'));
31
* @subpackage cake.tests.cases.libs.model
33
class TestBehavior extends ModelBehavior {
41
var $mapMethods = array('/test(\w+)/' => 'testMethod', '/look for\s+(.+)/' => 'speakEnglish');
47
* @param array $config
51
function setup(&$model, $config = array()) {
52
parent::setup($model, $config);
53
if (isset($config['mangle'])) {
54
$config['mangle'] .= ' mangled';
56
$this->settings[$model->alias] = array_merge(array('beforeFind' => 'on', 'afterFind' => 'off'), $config);
67
function beforeFind(&$model, $query) {
68
$settings = $this->settings[$model->alias];
69
if (!isset($settings['beforeFind']) || $settings['beforeFind'] == 'off') {
70
return parent::beforeFind($model, $query);
72
switch ($settings['beforeFind']) {
80
$query['fields'] = array($model->alias . '.id', $model->alias . '.name', $model->alias . '.mytime');
81
$query['recursive'] = -1;
91
* @param mixed $results
92
* @param mixed $primary
96
function afterFind(&$model, $results, $primary) {
97
$settings = $this->settings[$model->alias];
98
if (!isset($settings['afterFind']) || $settings['afterFind'] == 'off') {
99
return parent::afterFind($model, $results, $primary);
101
switch ($settings['afterFind']) {
112
return Set::extract($results, "{n}.{$model->alias}");
120
* @param mixed $model
124
function beforeSave(&$model) {
125
$settings = $this->settings[$model->alias];
126
if (!isset($settings['beforeSave']) || $settings['beforeSave'] == 'off') {
127
return parent::beforeSave($model);
129
switch ($settings['beforeSave']) {
137
$model->data[$model->alias]['name'] .= ' modified before';
146
* @param mixed $model
147
* @param mixed $created
151
function afterSave(&$model, $created) {
152
$settings = $this->settings[$model->alias];
153
if (!isset($settings['afterSave']) || $settings['afterSave'] == 'off') {
154
return parent::afterSave($model, $created);
156
$string = 'modified after';
158
$string .= ' on create';
160
switch ($settings['afterSave']) {
162
$model->data[$model->alias]['aftersave'] = $string;
165
unset($model->data[$model->alias]['name']);
171
$model->data[$model->alias]['name'] .= ' ' . $string;
177
* beforeValidate method
179
* @param mixed $model
183
function beforeValidate(&$model) {
184
$settings = $this->settings[$model->alias];
185
if (!isset($settings['validate']) || $settings['validate'] == 'off') {
186
return parent::beforeValidate($model);
188
switch ($settings['validate']) {
190
$model->invalidate('name');
197
$this->_addToWhitelist($model, array('name'));
201
$model->invalidate('name');
208
* beforeDelete method
210
* @param mixed $model
211
* @param bool $cascade
215
function beforeDelete(&$model, $cascade = true) {
216
$settings =& $this->settings[$model->alias];
217
if (!isset($settings['beforeDelete']) || $settings['beforeDelete'] == 'off') {
218
return parent::beforeDelete($model, $cascade);
220
switch ($settings['beforeDelete']) {
228
echo 'beforeDelete success';
230
echo ' (cascading) ';
239
* @param mixed $model
243
function afterDelete(&$model) {
244
$settings =& $this->settings[$model->alias];
245
if (!isset($settings['afterDelete']) || $settings['afterDelete'] == 'off') {
246
return parent::afterDelete($model);
248
switch ($settings['afterDelete']) {
250
echo 'afterDelete success';
258
* @param mixed $model
262
function onError(&$model) {
263
$settings = $this->settings[$model->alias];
264
if (!isset($settings['onError']) || $settings['onError'] == 'off') {
265
return parent::onError($model, $cascade);
267
echo "onError trigger success";
272
* @param mixed $model
276
function beforeTest(&$model) {
277
$model->beforeTestResult[] = strtolower(get_class($this));
278
return strtolower(get_class($this));
284
* @param mixed $model
289
function testMethod(&$model, $param = true) {
290
if ($param === true) {
298
* @param mixed $model
302
function testData(&$model) {
303
if (!isset($model->data['Apple']['field'])) {
306
$model->data['Apple']['field_2'] = true;
311
* validateField method
313
* @param mixed $model
314
* @param mixed $field
318
function validateField(&$model, $field) {
319
return current($field) === 'Orange';
323
* speakEnglish method
325
* @param mixed $model
326
* @param mixed $method
327
* @param mixed $query
331
function speakEnglish(&$model, $method, $query) {
332
$method = preg_replace('/look for\s+/', 'Item.name = \'', $method);
333
$query = preg_replace('/^in\s+/', 'Location.name = \'', $query);
334
return $method . '\' AND ' . $query . '\'';
339
* Test2Behavior class
342
* @subpackage cake.tests.cases.libs.model
344
class Test2Behavior extends TestBehavior{
348
* Test3Behavior class
351
* @subpackage cake.tests.cases.libs.model
353
class Test3Behavior extends TestBehavior{
357
* Test4Behavior class
360
* @subpackage cake.tests.cases.libs.model
362
class Test4Behavior extends ModelBehavior{
363
function setup(&$model, $config = null) {
365
array('hasMany' => array('Comment'))
371
* Test5Behavior class
374
* @subpackage cake.tests.cases.libs.model
376
class Test5Behavior extends ModelBehavior{
377
function setup(&$model, $config = null) {
379
array('belongsTo' => array('User'))
385
* Test6Behavior class
388
* @subpackage cake.tests.cases.libs.model
390
class Test6Behavior extends ModelBehavior{
391
function setup(&$model, $config = null) {
393
array('hasAndBelongsToMany' => array('Tag'))
399
* Test7Behavior class
402
* @subpackage cake.tests.cases.libs.model
404
class Test7Behavior extends ModelBehavior{
405
function setup(&$model, $config = null) {
407
array('hasOne' => array('Attachment'))
416
* @subpackage cake.tests.cases.libs.model
418
class BehaviorTest extends CakeTestCase {
426
var $fixtures = array(
427
'core.apple', 'core.sample', 'core.article', 'core.user', 'core.comment',
428
'core.attachment', 'core.tag', 'core.articles_tag'
438
ClassRegistry::flush();
442
* testBehaviorBinding method
447
function testBehaviorBinding() {
448
$Apple = new Apple();
449
$this->assertIdentical($Apple->Behaviors->attached(), array());
451
$Apple->Behaviors->attach('Test', array('key' => 'value'));
452
$this->assertIdentical($Apple->Behaviors->attached(), array('Test'));
453
$this->assertEqual(strtolower(get_class($Apple->Behaviors->Test)), 'testbehavior');
454
$expected = array('beforeFind' => 'on', 'afterFind' => 'off', 'key' => 'value');
455
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
456
$this->assertEqual(array_keys($Apple->Behaviors->Test->settings), array('Apple'));
458
$this->assertIdentical($Apple->Sample->Behaviors->attached(), array());
459
$Apple->Sample->Behaviors->attach('Test', array('key2' => 'value2'));
460
$this->assertIdentical($Apple->Sample->Behaviors->attached(), array('Test'));
461
$this->assertEqual($Apple->Sample->Behaviors->Test->settings['Sample'], array('beforeFind' => 'on', 'afterFind' => 'off', 'key2' => 'value2'));
463
$this->assertEqual(array_keys($Apple->Behaviors->Test->settings), array('Apple', 'Sample'));
464
$this->assertIdentical(
465
$Apple->Sample->Behaviors->Test->settings,
466
$Apple->Behaviors->Test->settings
468
$this->assertNotIdentical($Apple->Behaviors->Test->settings['Apple'], $Apple->Sample->Behaviors->Test->settings['Sample']);
470
$Apple->Behaviors->attach('Test', array('key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off'));
471
$Apple->Sample->Behaviors->attach('Test', array('key' => 'value', 'key3' => 'value3', 'beforeFind' => 'off'));
472
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], array('beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'value', 'key2' => 'value2', 'key3' => 'value3'));
473
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $Apple->Sample->Behaviors->Test->settings['Sample']);
475
$this->assertFalse(isset($Apple->Child->Behaviors->Test));
476
$Apple->Child->Behaviors->attach('Test', array('key' => 'value', 'key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off'));
477
$this->assertEqual($Apple->Child->Behaviors->Test->settings['Child'], $Apple->Sample->Behaviors->Test->settings['Sample']);
479
$this->assertFalse(isset($Apple->Parent->Behaviors->Test));
480
$Apple->Parent->Behaviors->attach('Test', array('key' => 'value', 'key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off'));
481
$this->assertEqual($Apple->Parent->Behaviors->Test->settings['Parent'], $Apple->Sample->Behaviors->Test->settings['Sample']);
483
$Apple->Parent->Behaviors->attach('Test', array('key' => 'value', 'key2' => 'value', 'key3' => 'value', 'beforeFind' => 'off'));
484
$this->assertNotEqual($Apple->Parent->Behaviors->Test->settings['Parent'], $Apple->Sample->Behaviors->Test->settings['Sample']);
486
$Apple->Behaviors->attach('Plugin.Test', array('key' => 'new value'));
488
'beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'new value',
489
'key2' => 'value2', 'key3' => 'value3'
491
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
493
$current = $Apple->Behaviors->Test->settings['Apple'];
494
$expected = array_merge($current, array('mangle' => 'trigger mangled'));
495
$Apple->Behaviors->attach('Test', array('mangle' => 'trigger'));
496
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
498
$Apple->Behaviors->attach('Test');
499
$expected = array_merge($current, array('mangle' => 'trigger mangled mangled'));
501
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
502
$Apple->Behaviors->attach('Test', array('mangle' => 'trigger'));
503
$expected = array_merge($current, array('mangle' => 'trigger mangled'));
504
$this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
508
* test that attach()/detach() works with plugin.banana
512
function testDetachWithPluginNames() {
513
$Apple = new Apple();
514
$Apple->Behaviors->attach('Plugin.Test');
515
$this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
516
$this->assertEqual($Apple->Behaviors->attached(), array('Test'));
518
$Apple->Behaviors->detach('Plugin.Test');
519
$this->assertEqual($Apple->Behaviors->attached(), array());
521
$Apple->Behaviors->attach('Plugin.Test');
522
$this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
523
$this->assertEqual($Apple->Behaviors->attached(), array('Test'));
525
$Apple->Behaviors->detach('Test');
526
$this->assertEqual($Apple->Behaviors->attached(), array());
530
* test that attaching a non existant Behavior triggers a cake error.
534
function testInvalidBehaviorCausingCakeError() {
535
$Apple =& new Apple();
536
$Apple->Behaviors =& new MockModelBehaviorCollection();
537
$Apple->Behaviors->expectOnce('cakeError');
538
$Apple->Behaviors->expectAt(0, 'cakeError', array('missingBehaviorFile', '*'));
539
$this->assertFalse($Apple->Behaviors->attach('NoSuchBehavior'));
543
* testBehaviorToggling method
548
function testBehaviorToggling() {
549
$Apple = new Apple();
550
$this->assertIdentical($Apple->Behaviors->enabled(), array());
552
$Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value')));
553
$this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
555
$Apple->Behaviors->disable('Test');
556
$this->assertIdentical($Apple->Behaviors->attached(), array('Test'));
557
$this->assertIdentical($Apple->Behaviors->enabled(), array());
559
$Apple->Sample->Behaviors->attach('Test');
560
$this->assertIdentical($Apple->Sample->Behaviors->enabled('Test'), true);
561
$this->assertIdentical($Apple->Behaviors->enabled(), array());
563
$Apple->Behaviors->enable('Test');
564
$this->assertIdentical($Apple->Behaviors->attached('Test'), true);
565
$this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
567
$Apple->Behaviors->disable('Test');
568
$this->assertIdentical($Apple->Behaviors->enabled(), array());
569
$Apple->Behaviors->attach('Test', array('enabled' => true));
570
$this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
571
$Apple->Behaviors->attach('Test', array('enabled' => false));
572
$this->assertIdentical($Apple->Behaviors->enabled(), array());
573
$Apple->Behaviors->detach('Test');
574
$this->assertIdentical($Apple->Behaviors->enabled(), array());
578
* testBehaviorFindCallbacks method
583
function testBehaviorFindCallbacks() {
584
$Apple = new Apple();
585
$expected = $Apple->find('all');
587
$Apple->Behaviors->attach('Test');
588
$this->assertIdentical($Apple->find('all'), null);
590
$Apple->Behaviors->attach('Test', array('beforeFind' => 'off'));
591
$this->assertIdentical($Apple->find('all'), $expected);
593
$Apple->Behaviors->attach('Test', array('beforeFind' => 'test'));
594
$this->assertIdentical($Apple->find('all'), $expected);
596
$Apple->Behaviors->attach('Test', array('beforeFind' => 'modify'));
598
array('Apple' => array('id' => '1', 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
599
array('Apple' => array('id' => '2', 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
600
array('Apple' => array('id' => '3', 'name' => 'green blue', 'mytime' => '22:57:17'))
602
$result = $Apple->find('all', array('conditions' => array('Apple.id <' => '4')));
603
$this->assertEqual($result, $expected2);
605
$Apple->Behaviors->disable('Test');
606
$result = $Apple->find('all');
607
$this->assertEqual($result, $expected);
609
$Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'on'));
610
$this->assertIdentical($Apple->find('all'), array());
612
$Apple->Behaviors->attach('Test', array('afterFind' => 'off'));
613
$this->assertEqual($Apple->find('all'), $expected);
615
$Apple->Behaviors->attach('Test', array('afterFind' => 'test'));
616
$this->assertEqual($Apple->find('all'), $expected);
618
$Apple->Behaviors->attach('Test', array('afterFind' => 'test2'));
619
$this->assertEqual($Apple->find('all'), $expected);
621
$Apple->Behaviors->attach('Test', array('afterFind' => 'modify'));
623
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'),
624
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'),
625
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'),
626
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'),
627
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'),
628
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'),
629
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')
631
$this->assertEqual($Apple->find('all'), $expected);
635
* testBehaviorHasManyFindCallbacks method
640
function testBehaviorHasManyFindCallbacks() {
641
$Apple = new Apple();
642
$Apple->unbindModel(array('hasOne' => array('Sample'), 'belongsTo' => array('Parent')), false);
643
$expected = $Apple->find('all');
645
$Apple->unbindModel(array('hasMany' => array('Child')));
646
$wellBehaved = $Apple->find('all');
647
$Apple->Child->Behaviors->attach('Test', array('afterFind' => 'modify'));
648
$Apple->unbindModel(array('hasMany' => array('Child')));
649
$this->assertIdentical($Apple->find('all'), $wellBehaved);
651
$Apple->Child->Behaviors->attach('Test', array('before' => 'off'));
652
$this->assertIdentical($Apple->find('all'), $expected);
654
$Apple->Child->Behaviors->attach('Test', array('before' => 'test'));
655
$this->assertIdentical($Apple->find('all'), $expected);
659
'Apple' => array('id' => 1),
661
array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))),
663
'Apple' => array('id' => 2),
665
array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17'),
666
array('id' => 3, 'name' => 'green blue', 'mytime' => '22:57:17'),
667
array('id' => 4, 'name' => 'Test Name', 'mytime' => '22:57:17'))),
669
'Apple' => array('id' => 3),
673
$Apple->Child->Behaviors->attach('Test', array('before' => 'modify'));
674
$result = $Apple->find('all', array('fields' => array('Apple.id'), 'conditions' => array('Apple.id <' => '4')));
675
//$this->assertEqual($result, $expected2);
677
$Apple->Child->Behaviors->disable('Test');
678
$result = $Apple->find('all');
679
$this->assertEqual($result, $expected);
681
$Apple->Child->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on'));
682
//$this->assertIdentical($Apple->find('all'), array());
684
$Apple->Child->Behaviors->attach('Test', array('after' => 'off'));
685
$this->assertEqual($Apple->find('all'), $expected);
687
$Apple->Child->Behaviors->attach('Test', array('after' => 'test'));
688
$this->assertEqual($Apple->find('all'), $expected);
690
$Apple->Child->Behaviors->attach('Test', array('after' => 'test2'));
691
$this->assertEqual($Apple->find('all'), $expected);
693
$Apple->Child->Behaviors->attach('Test', array('after' => 'modify'));
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')
703
//$this->assertEqual($Apple->find('all'), $expected);
707
* testBehaviorHasOneFindCallbacks method
712
function testBehaviorHasOneFindCallbacks() {
713
$Apple = new Apple();
714
$Apple->unbindModel(array('hasMany' => array('Child'), 'belongsTo' => array('Parent')), false);
715
$expected = $Apple->find('all');
717
$Apple->unbindModel(array('hasOne' => array('Sample')));
718
$wellBehaved = $Apple->find('all');
719
$Apple->Sample->Behaviors->attach('Test');
720
$Apple->unbindModel(array('hasOne' => array('Sample')));
721
$this->assertIdentical($Apple->find('all'), $wellBehaved);
723
$Apple->Sample->Behaviors->attach('Test', array('before' => 'off'));
724
$this->assertIdentical($Apple->find('all'), $expected);
726
$Apple->Sample->Behaviors->attach('Test', array('before' => 'test'));
727
$this->assertIdentical($Apple->find('all'), $expected);
729
$Apple->Sample->Behaviors->attach('Test', array('before' => 'modify'));
732
'Apple' => array('id' => 1),
734
array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))),
736
'Apple' => array('id' => 2),
738
array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17'),
739
array('id' => 3, 'name' => 'green blue', 'mytime' => '22:57:17'),
740
array('id' => 4, 'name' => 'Test Name', 'mytime' => '22:57:17'))),
742
'Apple' => array('id' => 3),
745
$result = $Apple->find('all', array('fields' => array('Apple.id'), 'conditions' => array('Apple.id <' => '4')));
746
//$this->assertEqual($result, $expected2);
748
$Apple->Sample->Behaviors->disable('Test');
749
$result = $Apple->find('all');
750
$this->assertEqual($result, $expected);
752
$Apple->Sample->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on'));
753
//$this->assertIdentical($Apple->find('all'), array());
755
$Apple->Sample->Behaviors->attach('Test', array('after' => 'off'));
756
$this->assertEqual($Apple->find('all'), $expected);
758
$Apple->Sample->Behaviors->attach('Test', array('after' => 'test'));
759
$this->assertEqual($Apple->find('all'), $expected);
761
$Apple->Sample->Behaviors->attach('Test', array('after' => 'test2'));
762
$this->assertEqual($Apple->find('all'), $expected);
764
$Apple->Sample->Behaviors->attach('Test', array('after' => 'modify'));
766
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'),
767
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'),
768
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'),
769
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'),
770
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'),
771
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'),
772
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')
774
//$this->assertEqual($Apple->find('all'), $expected);
777
* testBehaviorBelongsToFindCallbacks method
782
function testBehaviorBelongsToFindCallbacks() {
783
$Apple = new Apple();
784
$Apple->unbindModel(array('hasMany' => array('Child'), 'hasOne' => array('Sample')), false);
785
$expected = $Apple->find('all');
787
$Apple->unbindModel(array('belongsTo' => array('Parent')));
788
$wellBehaved = $Apple->find('all');
789
$Apple->Parent->Behaviors->attach('Test');
790
$Apple->unbindModel(array('belongsTo' => array('Parent')));
791
$this->assertIdentical($Apple->find('all'), $wellBehaved);
793
$Apple->Parent->Behaviors->attach('Test', array('before' => 'off'));
794
$this->assertIdentical($Apple->find('all'), $expected);
796
$Apple->Parent->Behaviors->attach('Test', array('before' => 'test'));
797
$this->assertIdentical($Apple->find('all'), $expected);
799
$Apple->Parent->Behaviors->attach('Test', array('before' => 'modify'));
802
'Apple' => array('id' => 1),
803
'Parent' => array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
805
'Apple' => array('id' => 2),
806
'Parent' => array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
808
'Apple' => array('id' => 3),
809
'Parent' => array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))
811
$result = $Apple->find('all', array(
812
'fields' => array('Apple.id', 'Parent.id', 'Parent.name', 'Parent.mytime'),
813
'conditions' => array('Apple.id <' => '4')
815
$this->assertEqual($result, $expected2);
817
$Apple->Parent->Behaviors->disable('Test');
818
$result = $Apple->find('all');
819
$this->assertEqual($result, $expected);
821
$Apple->Parent->Behaviors->attach('Test', array('after' => 'off'));
822
$this->assertEqual($Apple->find('all'), $expected);
824
$Apple->Parent->Behaviors->attach('Test', array('after' => 'test'));
825
$this->assertEqual($Apple->find('all'), $expected);
827
$Apple->Parent->Behaviors->attach('Test', array('after' => 'test2'));
828
$this->assertEqual($Apple->find('all'), $expected);
830
$Apple->Parent->Behaviors->attach('Test', array('after' => 'modify'));
832
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'),
833
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'),
834
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'),
835
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'),
836
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'),
837
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'),
838
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')
840
//$this->assertEqual($Apple->find('all'), $expected);
844
* testBehaviorSaveCallbacks method
849
function testBehaviorSaveCallbacks() {
850
$Sample = new Sample();
851
$record = array('Sample' => array('apple_id' => 6, 'name' => 'sample99'));
853
$Sample->Behaviors->attach('Test', array('beforeSave' => 'on'));
855
$this->assertIdentical($Sample->save($record), false);
857
$Sample->Behaviors->attach('Test', array('beforeSave' => 'off'));
859
$this->assertIdentical($Sample->save($record), $record);
861
$Sample->Behaviors->attach('Test', array('beforeSave' => 'test'));
863
$this->assertIdentical($Sample->save($record), $record);
865
$Sample->Behaviors->attach('Test', array('beforeSave' => 'modify'));
866
$expected = Set::insert($record, 'Sample.name', 'sample99 modified before');
868
$this->assertIdentical($Sample->save($record), $expected);
870
$Sample->Behaviors->disable('Test');
871
$this->assertIdentical($Sample->save($record), $record);
873
$Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'on'));
874
$expected = Set::merge($record, array('Sample' => array('aftersave' => 'modified after on create')));
876
$this->assertIdentical($Sample->save($record), $expected);
878
$Sample->Behaviors->attach('Test', array('beforeSave' => 'modify', 'afterSave' => 'modify'));
879
$expected = Set::merge($record, array('Sample' => array('name' => 'sample99 modified before modified after on create')));
881
$this->assertIdentical($Sample->save($record), $expected);
883
$Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'test'));
885
$this->assertIdentical($Sample->save($record), $record);
887
$Sample->Behaviors->attach('Test', array('afterSave' => 'test2'));
889
$this->assertIdentical($Sample->save($record), $record);
891
$Sample->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'off'));
892
$Sample->recursive = -1;
893
$record2 = $Sample->read(null, 1);
895
$Sample->Behaviors->attach('Test', array('afterSave' => 'on'));
896
$expected = Set::merge($record2, array('Sample' => array('aftersave' => 'modified after')));
898
$this->assertIdentical($Sample->save($record2), $expected);
900
$Sample->Behaviors->attach('Test', array('afterSave' => 'modify'));
901
$expected = Set::merge($record2, array('Sample' => array('name' => 'sample1 modified after')));
903
$this->assertIdentical($Sample->save($record2), $expected);
907
* testBehaviorDeleteCallbacks method
912
function testBehaviorDeleteCallbacks() {
913
$Apple = new Apple();
915
$Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'beforeDelete' => 'off'));
916
$this->assertIdentical($Apple->delete(6), true);
918
$Apple->Behaviors->attach('Test', array('beforeDelete' => 'on'));
919
$this->assertIdentical($Apple->delete(4), false);
921
$Apple->Behaviors->attach('Test', array('beforeDelete' => 'test2'));
923
$results = $Apple->delete(4);
924
$this->assertIdentical(trim(ob_get_clean()), 'beforeDelete success (cascading)');
925
$this->assertIdentical($results, true);
928
$results = $Apple->delete(3, false);
929
$this->assertIdentical(trim(ob_get_clean()), 'beforeDelete success');
930
$this->assertIdentical($results, true);
933
$Apple->Behaviors->attach('Test', array('beforeDelete' => 'off', 'afterDelete' => 'on'));
935
$results = $Apple->delete(2, false);
936
$this->assertIdentical(trim(ob_get_clean()), 'afterDelete success');
937
$this->assertIdentical($results, true);
941
* testBehaviorOnErrorCallback method
946
function testBehaviorOnErrorCallback() {
947
$Apple = new Apple();
949
$Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'onError' => 'on'));
951
$Apple->Behaviors->Test->onError($Apple);
952
$this->assertIdentical(trim(ob_get_clean()), 'onError trigger success');
957
//$this->assertIdentical(trim(ob_get_clean()), 'onError trigger success');
961
* testBehaviorValidateCallback method
966
function testBehaviorValidateCallback() {
967
$Apple = new Apple();
969
$Apple->Behaviors->attach('Test');
970
$this->assertIdentical($Apple->validates(), true);
972
$Apple->Behaviors->attach('Test', array('validate' => 'on'));
973
$this->assertIdentical($Apple->validates(), false);
974
$this->assertIdentical($Apple->validationErrors, array('name' => true));
976
$Apple->Behaviors->attach('Test', array('validate' => 'stop'));
977
$this->assertIdentical($Apple->validates(), false);
978
$this->assertIdentical($Apple->validationErrors, array('name' => true));
980
$Apple->Behaviors->attach('Test', array('validate' => 'whitelist'));
982
$this->assertIdentical($Apple->whitelist, array());
984
$Apple->whitelist = array('unknown');
986
$this->assertIdentical($Apple->whitelist, array('unknown', 'name'));
990
* testBehaviorValidateMethods method
995
function testBehaviorValidateMethods() {
996
$Apple = new Apple();
997
$Apple->Behaviors->attach('Test');
998
$Apple->validate['color'] = 'validateField';
1000
$result = $Apple->save(array('name' => 'Genetically Modified Apple', 'color' => 'Orange'));
1001
$this->assertEqual(array_keys($result['Apple']), array('name', 'color', 'modified', 'created'));
1004
$result = $Apple->save(array('name' => 'Regular Apple', 'color' => 'Red'));
1005
$this->assertFalse($result);
1009
* testBehaviorMethodDispatching method
1014
function testBehaviorMethodDispatching() {
1015
$Apple = new Apple();
1016
$Apple->Behaviors->attach('Test');
1018
$expected = 'working';
1019
$this->assertEqual($Apple->testMethod(), $expected);
1020
$this->assertEqual($Apple->Behaviors->dispatchMethod($Apple, 'testMethod'), $expected);
1022
$result = $Apple->Behaviors->dispatchMethod($Apple, 'wtf');
1023
$this->assertEqual($result, array('unhandled'));
1025
$result = $Apple->{'look for the remote'}('in the couch');
1026
$expected = "Item.name = 'the remote' AND Location.name = 'the couch'";
1027
$this->assertEqual($result, $expected);
1031
* testBehaviorMethodDispatchingWithData method
1036
function testBehaviorMethodDispatchingWithData() {
1037
$Apple = new Apple();
1038
$Apple->Behaviors->attach('Test');
1040
$Apple->set('field', 'value');
1041
$this->assertTrue($Apple->testData());
1042
$this->assertTrue($Apple->data['Apple']['field_2']);
1044
$this->assertTrue($Apple->testData('one', 'two', 'three', 'four', 'five', 'six'));
1048
* testBehaviorTrigger method
1053
function testBehaviorTrigger() {
1054
$Apple =& new Apple();
1055
$Apple->Behaviors->attach('Test');
1056
$Apple->Behaviors->attach('Test2');
1057
$Apple->Behaviors->attach('Test3');
1059
$Apple->beforeTestResult = array();
1060
$Apple->Behaviors->trigger($Apple, 'beforeTest');
1061
$expected = array('testbehavior', 'test2behavior', 'test3behavior');
1062
$this->assertIdentical($Apple->beforeTestResult, $expected);
1064
$Apple->beforeTestResult = array();
1065
$Apple->Behaviors->trigger($Apple, 'beforeTest', array(), array('break' => true, 'breakOn' => 'test2behavior'));
1066
$expected = array('testbehavior', 'test2behavior');
1067
$this->assertIdentical($Apple->beforeTestResult, $expected);
1069
$Apple->beforeTestResult = array();
1070
$Apple->Behaviors->trigger($Apple, 'beforeTest', array(), array('break' => true, 'breakOn' => array('test2behavior', 'test3behavior')));
1071
$expected = array('testbehavior', 'test2behavior');
1072
$this->assertIdentical($Apple->beforeTestResult, $expected);
1076
* undocumented function
1081
function testBindModelCallsInBehaviors() {
1082
$this->loadFixtures('Article', 'Comment');
1085
$Article = new Article();
1086
$Article->unbindModel(array('hasMany' => array('Comment')));
1087
$result = $Article->find('first');
1088
$this->assertFalse(array_key_exists('Comment', $result));
1090
$Article->Behaviors->attach('Test4');
1091
$result = $Article->find('first');
1092
$this->assertTrue(array_key_exists('Comment', $result));
1095
$Article->unbindModel(array('belongsTo' => array('User')));
1096
$result = $Article->find('first');
1097
$this->assertFalse(array_key_exists('User', $result));
1099
$Article->Behaviors->attach('Test5');
1100
$result = $Article->find('first');
1101
$this->assertTrue(array_key_exists('User', $result));
1103
// hasAndBelongsToMany
1104
$Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
1105
$result = $Article->find('first');
1106
$this->assertFalse(array_key_exists('Tag', $result));
1108
$Article->Behaviors->attach('Test6');
1109
$result = $Article->find('first');
1110
$this->assertTrue(array_key_exists('Comment', $result));
1113
$Comment = new Comment();
1114
$Comment->unbindModel(array('hasOne' => array('Attachment')));
1115
$result = $Comment->find('first');
1116
$this->assertFalse(array_key_exists('Attachment', $result));
1118
$Comment->Behaviors->attach('Test7');
1119
$result = $Comment->find('first');
1120
$this->assertTrue(array_key_exists('Attachment', $result));
1124
* Test attach and detaching
1129
function testBehaviorAttachAndDetach() {
1130
$Sample =& new Sample();
1131
$Sample->actsAs = array('Test3' => array('bar'), 'Test2' => array('foo', 'bar'));
1132
$Sample->Behaviors->init($Sample->alias, $Sample->actsAs);
1133
$Sample->Behaviors->attach('Test2');
1134
$Sample->Behaviors->detach('Test3');
1136
$Sample->Behaviors->trigger($Sample, 'beforeTest');