~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/tests/cases/libs/model/model_behavior.test.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * BehaviorTest file
 
4
 *
 
5
 * Long description for behavior.test.php
 
6
 *
 
7
 * PHP versions 4 and 5
 
8
 *
 
9
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
10
 * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
11
 *
 
12
 * Licensed under The MIT License
 
13
 * Redistributions of files must retain the above copyright notice.
 
14
 *
 
15
 * @copyright     CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
16
 * @link          http://cakephp.org CakePHP(tm) Project
 
17
 * @package       cake
 
18
 * @subpackage    cake.tests.cases.libs.model
 
19
 * @since         1.2
 
20
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 
21
 */
 
22
App::import('Model', 'AppModel');
 
23
require_once dirname(__FILE__) . DS . 'models.php';
 
24
 
 
25
Mock::generatePartial('BehaviorCollection', 'MockModelBehaviorCollection', array('cakeError', '_stop'));
 
26
 
 
27
/**
 
28
 * TestBehavior class
 
29
 *
 
30
 * @package       cake
 
31
 * @subpackage    cake.tests.cases.libs.model
 
32
 */
 
33
class TestBehavior extends ModelBehavior {
 
34
 
 
35
/**
 
36
 * mapMethods property
 
37
 *
 
38
 * @var array
 
39
 * @access public
 
40
 */
 
41
        var $mapMethods = array('/test(\w+)/' => 'testMethod', '/look for\s+(.+)/' => 'speakEnglish');
 
42
 
 
43
/**
 
44
 * setup method
 
45
 *
 
46
 * @param mixed $model
 
47
 * @param array $config
 
48
 * @access public
 
49
 * @return void
 
50
 */
 
51
        function setup(&$model, $config = array()) {
 
52
                parent::setup($model, $config);
 
53
                if (isset($config['mangle'])) {
 
54
                        $config['mangle'] .= ' mangled';
 
55
                }
 
56
                $this->settings[$model->alias] = array_merge(array('beforeFind' => 'on', 'afterFind' => 'off'), $config);
 
57
        }
 
58
 
 
59
/**
 
60
 * beforeFind method
 
61
 *
 
62
 * @param mixed $model
 
63
 * @param mixed $query
 
64
 * @access public
 
65
 * @return void
 
66
 */
 
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);
 
71
                }
 
72
                switch ($settings['beforeFind']) {
 
73
                        case 'on':
 
74
                                return false;
 
75
                        break;
 
76
                        case 'test':
 
77
                                return null;
 
78
                        break;
 
79
                        case 'modify':
 
80
                                $query['fields'] = array($model->alias . '.id', $model->alias . '.name', $model->alias . '.mytime');
 
81
                                $query['recursive'] = -1;
 
82
                                return $query;
 
83
                        break;
 
84
                }
 
85
        }
 
86
 
 
87
/**
 
88
 * afterFind method
 
89
 *
 
90
 * @param mixed $model
 
91
 * @param mixed $results
 
92
 * @param mixed $primary
 
93
 * @access public
 
94
 * @return void
 
95
 */
 
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);
 
100
                }
 
101
                switch ($settings['afterFind']) {
 
102
                        case 'on':
 
103
                                return array();
 
104
                        break;
 
105
                        case 'test':
 
106
                                return true;
 
107
                        break;
 
108
                        case 'test2':
 
109
                                return null;
 
110
                        break;
 
111
                        case 'modify':
 
112
                                return Set::extract($results, "{n}.{$model->alias}");
 
113
                        break;
 
114
                }
 
115
        }
 
116
 
 
117
/**
 
118
 * beforeSave method
 
119
 *
 
120
 * @param mixed $model
 
121
 * @access public
 
122
 * @return void
 
123
 */
 
124
        function beforeSave(&$model) {
 
125
                $settings = $this->settings[$model->alias];
 
126
                if (!isset($settings['beforeSave']) || $settings['beforeSave'] == 'off') {
 
127
                        return parent::beforeSave($model);
 
128
                }
 
129
                switch ($settings['beforeSave']) {
 
130
                        case 'on':
 
131
                                return false;
 
132
                        break;
 
133
                        case 'test':
 
134
                                return null;
 
135
                        break;
 
136
                        case 'modify':
 
137
                                $model->data[$model->alias]['name'] .= ' modified before';
 
138
                                return true;
 
139
                        break;
 
140
                }
 
141
        }
 
142
 
 
143
/**
 
144
 * afterSave method
 
145
 *
 
146
 * @param mixed $model
 
147
 * @param mixed $created
 
148
 * @access public
 
149
 * @return void
 
150
 */
 
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);
 
155
                }
 
156
                $string = 'modified after';
 
157
                if ($created) {
 
158
                        $string .= ' on create';
 
159
                }
 
160
                switch ($settings['afterSave']) {
 
161
                        case 'on':
 
162
                                $model->data[$model->alias]['aftersave'] = $string;
 
163
                        break;
 
164
                        case 'test':
 
165
                                unset($model->data[$model->alias]['name']);
 
166
                        break;
 
167
                        case 'test2':
 
168
                                return false;
 
169
                        break;
 
170
                        case 'modify':
 
171
                                $model->data[$model->alias]['name'] .= ' ' . $string;
 
172
                        break;
 
173
                }
 
174
        }
 
175
 
 
176
/**
 
177
 * beforeValidate method
 
178
 *
 
179
 * @param mixed $model
 
180
 * @access public
 
181
 * @return void
 
182
 */
 
183
        function beforeValidate(&$model) {
 
184
                $settings = $this->settings[$model->alias];
 
185
                if (!isset($settings['validate']) || $settings['validate'] == 'off') {
 
186
                        return parent::beforeValidate($model);
 
187
                }
 
188
                switch ($settings['validate']) {
 
189
                        case 'on':
 
190
                                $model->invalidate('name');
 
191
                                return true;
 
192
                        break;
 
193
                        case 'test':
 
194
                                return null;
 
195
                        break;
 
196
                        case 'whitelist':
 
197
                                $this->_addToWhitelist($model, array('name'));
 
198
                                return true;
 
199
                        break;
 
200
                        case 'stop':
 
201
                                $model->invalidate('name');
 
202
                                return false;
 
203
                        break;
 
204
                }
 
205
        }
 
206
 
 
207
/**
 
208
 * beforeDelete method
 
209
 *
 
210
 * @param mixed $model
 
211
 * @param bool $cascade
 
212
 * @access public
 
213
 * @return void
 
214
 */
 
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);
 
219
                }
 
220
                switch ($settings['beforeDelete']) {
 
221
                        case 'on':
 
222
                                return false;
 
223
                        break;
 
224
                        case 'test':
 
225
                                return null;
 
226
                        break;
 
227
                        case 'test2':
 
228
                                echo 'beforeDelete success';
 
229
                                if ($cascade) {
 
230
                                        echo ' (cascading) ';
 
231
                                }
 
232
                        break;
 
233
                }
 
234
        }
 
235
 
 
236
/**
 
237
 * afterDelete method
 
238
 *
 
239
 * @param mixed $model
 
240
 * @access public
 
241
 * @return void
 
242
 */
 
243
        function afterDelete(&$model) {
 
244
                $settings =& $this->settings[$model->alias];
 
245
                if (!isset($settings['afterDelete']) || $settings['afterDelete'] == 'off') {
 
246
                        return parent::afterDelete($model);
 
247
                }
 
248
                switch ($settings['afterDelete']) {
 
249
                        case 'on':
 
250
                                echo 'afterDelete success';
 
251
                        break;
 
252
                }
 
253
        }
 
254
 
 
255
/**
 
256
 * onError method
 
257
 *
 
258
 * @param mixed $model
 
259
 * @access public
 
260
 * @return void
 
261
 */
 
262
        function onError(&$model) {
 
263
                $settings = $this->settings[$model->alias];
 
264
                if (!isset($settings['onError']) || $settings['onError'] == 'off') {
 
265
                        return parent::onError($model, $cascade);
 
266
                }
 
267
                echo "onError trigger success";
 
268
        }
 
269
/**
 
270
 * beforeTest method
 
271
 *
 
272
 * @param mixed $model
 
273
 * @access public
 
274
 * @return void
 
275
 */
 
276
        function beforeTest(&$model) {
 
277
                $model->beforeTestResult[] = strtolower(get_class($this));
 
278
                return strtolower(get_class($this));
 
279
        }
 
280
 
 
281
/**
 
282
 * testMethod method
 
283
 *
 
284
 * @param mixed $model
 
285
 * @param bool $param
 
286
 * @access public
 
287
 * @return void
 
288
 */
 
289
        function testMethod(&$model, $param = true) {
 
290
                if ($param === true) {
 
291
                        return 'working';
 
292
                }
 
293
        }
 
294
 
 
295
/**
 
296
 * testData method
 
297
 *
 
298
 * @param mixed $model
 
299
 * @access public
 
300
 * @return void
 
301
 */
 
302
        function testData(&$model) {
 
303
                if (!isset($model->data['Apple']['field'])) {
 
304
                        return false;
 
305
                }
 
306
                $model->data['Apple']['field_2'] = true;
 
307
                return true;
 
308
        }
 
309
 
 
310
/**
 
311
 * validateField method
 
312
 *
 
313
 * @param mixed $model
 
314
 * @param mixed $field
 
315
 * @access public
 
316
 * @return void
 
317
 */
 
318
        function validateField(&$model, $field) {
 
319
                return current($field) === 'Orange';
 
320
        }
 
321
 
 
322
/**
 
323
 * speakEnglish method
 
324
 *
 
325
 * @param mixed $model
 
326
 * @param mixed $method
 
327
 * @param mixed $query
 
328
 * @access public
 
329
 * @return void
 
330
 */
 
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 . '\'';
 
335
        }
 
336
}
 
337
 
 
338
/**
 
339
 * Test2Behavior class
 
340
 *
 
341
 * @package       cake
 
342
 * @subpackage    cake.tests.cases.libs.model
 
343
 */
 
344
class Test2Behavior extends TestBehavior{
 
345
}
 
346
 
 
347
/**
 
348
 * Test3Behavior class
 
349
 *
 
350
 * @package       cake
 
351
 * @subpackage    cake.tests.cases.libs.model
 
352
 */
 
353
class Test3Behavior extends TestBehavior{
 
354
}
 
355
 
 
356
/**
 
357
 * Test4Behavior class
 
358
 *
 
359
 * @package       cake
 
360
 * @subpackage    cake.tests.cases.libs.model
 
361
 */
 
362
class Test4Behavior extends ModelBehavior{
 
363
        function setup(&$model, $config = null) {
 
364
                $model->bindModel(
 
365
                        array('hasMany' => array('Comment'))
 
366
                );
 
367
        }
 
368
}
 
369
 
 
370
/**
 
371
 * Test5Behavior class
 
372
 *
 
373
 * @package       cake
 
374
 * @subpackage    cake.tests.cases.libs.model
 
375
 */
 
376
class Test5Behavior extends ModelBehavior{
 
377
        function setup(&$model, $config = null) {
 
378
                $model->bindModel(
 
379
                        array('belongsTo' => array('User'))
 
380
                );
 
381
        }
 
382
}
 
383
 
 
384
/**
 
385
 * Test6Behavior class
 
386
 *
 
387
 * @package       cake
 
388
 * @subpackage    cake.tests.cases.libs.model
 
389
 */
 
390
class Test6Behavior extends ModelBehavior{
 
391
        function setup(&$model, $config = null) {
 
392
                $model->bindModel(
 
393
                        array('hasAndBelongsToMany' => array('Tag'))
 
394
                );
 
395
        }
 
396
}
 
397
 
 
398
/**
 
399
 * Test7Behavior class
 
400
 *
 
401
 * @package       cake
 
402
 * @subpackage    cake.tests.cases.libs.model
 
403
 */
 
404
class Test7Behavior extends ModelBehavior{
 
405
        function setup(&$model, $config = null) {
 
406
                $model->bindModel(
 
407
                        array('hasOne' => array('Attachment'))
 
408
                );
 
409
        }
 
410
}
 
411
 
 
412
/**
 
413
 * BehaviorTest class
 
414
 *
 
415
 * @package       cake
 
416
 * @subpackage    cake.tests.cases.libs.model
 
417
 */
 
418
class BehaviorTest extends CakeTestCase {
 
419
 
 
420
/**
 
421
 * fixtures property
 
422
 *
 
423
 * @var array
 
424
 * @access public
 
425
 */
 
426
        var $fixtures = array(
 
427
                'core.apple', 'core.sample', 'core.article', 'core.user', 'core.comment',
 
428
                'core.attachment', 'core.tag', 'core.articles_tag'
 
429
        );
 
430
 
 
431
/**
 
432
 * tearDown method
 
433
 *
 
434
 * @access public
 
435
 * @return void
 
436
 */
 
437
        function endTest() {
 
438
                ClassRegistry::flush();
 
439
        }
 
440
 
 
441
/**
 
442
 * testBehaviorBinding method
 
443
 *
 
444
 * @access public
 
445
 * @return void
 
446
 */
 
447
        function testBehaviorBinding() {
 
448
                $Apple = new Apple();
 
449
                $this->assertIdentical($Apple->Behaviors->attached(), array());
 
450
 
 
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'));
 
457
 
 
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'));
 
462
 
 
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
 
467
                );
 
468
                $this->assertNotIdentical($Apple->Behaviors->Test->settings['Apple'], $Apple->Sample->Behaviors->Test->settings['Sample']);
 
469
 
 
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']);
 
474
 
 
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']);
 
478
 
 
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']);
 
482
 
 
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']);
 
485
 
 
486
                $Apple->Behaviors->attach('Plugin.Test', array('key' => 'new value'));
 
487
                $expected = array(
 
488
                        'beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'new value',
 
489
                        'key2' => 'value2', 'key3' => 'value3'
 
490
                );
 
491
                $this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
 
492
 
 
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);
 
497
 
 
498
                $Apple->Behaviors->attach('Test');
 
499
                $expected = array_merge($current, array('mangle' => 'trigger mangled mangled'));
 
500
 
 
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);
 
505
        }
 
506
 
 
507
/**
 
508
 * test that attach()/detach() works with plugin.banana
 
509
 *
 
510
 * @return void
 
511
 */
 
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'));
 
517
 
 
518
                $Apple->Behaviors->detach('Plugin.Test');
 
519
                $this->assertEqual($Apple->Behaviors->attached(), array());
 
520
 
 
521
                $Apple->Behaviors->attach('Plugin.Test');
 
522
                $this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
 
523
                $this->assertEqual($Apple->Behaviors->attached(), array('Test'));
 
524
 
 
525
                $Apple->Behaviors->detach('Test');
 
526
                $this->assertEqual($Apple->Behaviors->attached(), array());
 
527
        }
 
528
 
 
529
/**
 
530
 * test that attaching a non existant Behavior triggers a cake error.
 
531
 *
 
532
 * @return void
 
533
 */
 
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'));
 
540
        }
 
541
 
 
542
/**
 
543
 * testBehaviorToggling method
 
544
 *
 
545
 * @access public
 
546
 * @return void
 
547
 */
 
548
        function testBehaviorToggling() {
 
549
                $Apple = new Apple();
 
550
                $this->assertIdentical($Apple->Behaviors->enabled(), array());
 
551
 
 
552
                $Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value')));
 
553
                $this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
 
554
 
 
555
                $Apple->Behaviors->disable('Test');
 
556
                $this->assertIdentical($Apple->Behaviors->attached(), array('Test'));
 
557
                $this->assertIdentical($Apple->Behaviors->enabled(), array());
 
558
 
 
559
                $Apple->Sample->Behaviors->attach('Test');
 
560
                $this->assertIdentical($Apple->Sample->Behaviors->enabled('Test'), true);
 
561
                $this->assertIdentical($Apple->Behaviors->enabled(), array());
 
562
 
 
563
                $Apple->Behaviors->enable('Test');
 
564
                $this->assertIdentical($Apple->Behaviors->attached('Test'), true);
 
565
                $this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
 
566
 
 
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());
 
575
        }
 
576
 
 
577
/**
 
578
 * testBehaviorFindCallbacks method
 
579
 *
 
580
 * @access public
 
581
 * @return void
 
582
 */
 
583
        function testBehaviorFindCallbacks() {
 
584
                $Apple = new Apple();
 
585
                $expected = $Apple->find('all');
 
586
 
 
587
                $Apple->Behaviors->attach('Test');
 
588
                $this->assertIdentical($Apple->find('all'), null);
 
589
 
 
590
                $Apple->Behaviors->attach('Test', array('beforeFind' => 'off'));
 
591
                $this->assertIdentical($Apple->find('all'), $expected);
 
592
 
 
593
                $Apple->Behaviors->attach('Test', array('beforeFind' => 'test'));
 
594
                $this->assertIdentical($Apple->find('all'), $expected);
 
595
 
 
596
                $Apple->Behaviors->attach('Test', array('beforeFind' => 'modify'));
 
597
                $expected2 = array(
 
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'))
 
601
                );
 
602
                $result = $Apple->find('all', array('conditions' => array('Apple.id <' => '4')));
 
603
                $this->assertEqual($result, $expected2);
 
604
 
 
605
                $Apple->Behaviors->disable('Test');
 
606
                $result = $Apple->find('all');
 
607
                $this->assertEqual($result, $expected);
 
608
 
 
609
                $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'on'));
 
610
                $this->assertIdentical($Apple->find('all'), array());
 
611
 
 
612
                $Apple->Behaviors->attach('Test', array('afterFind' => 'off'));
 
613
                $this->assertEqual($Apple->find('all'), $expected);
 
614
 
 
615
                $Apple->Behaviors->attach('Test', array('afterFind' => 'test'));
 
616
                $this->assertEqual($Apple->find('all'), $expected);
 
617
 
 
618
                $Apple->Behaviors->attach('Test', array('afterFind' => 'test2'));
 
619
                $this->assertEqual($Apple->find('all'), $expected);
 
620
 
 
621
                $Apple->Behaviors->attach('Test', array('afterFind' => 'modify'));
 
622
                $expected = array(
 
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')
 
630
                );
 
631
                $this->assertEqual($Apple->find('all'), $expected);
 
632
        }
 
633
 
 
634
/**
 
635
 * testBehaviorHasManyFindCallbacks method
 
636
 *
 
637
 * @access public
 
638
 * @return void
 
639
 */
 
640
        function testBehaviorHasManyFindCallbacks() {
 
641
                $Apple = new Apple();
 
642
                $Apple->unbindModel(array('hasOne' => array('Sample'), 'belongsTo' => array('Parent')), false);
 
643
                $expected = $Apple->find('all');
 
644
 
 
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);
 
650
 
 
651
                $Apple->Child->Behaviors->attach('Test', array('before' => 'off'));
 
652
                $this->assertIdentical($Apple->find('all'), $expected);
 
653
 
 
654
                $Apple->Child->Behaviors->attach('Test', array('before' => 'test'));
 
655
                $this->assertIdentical($Apple->find('all'), $expected);
 
656
 
 
657
                $expected2 = array(
 
658
                        array(
 
659
                                'Apple' => array('id' => 1),
 
660
                                'Child' => array(
 
661
                                        array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))),
 
662
                        array(
 
663
                                'Apple' => array('id' => 2),
 
664
                                'Child' => array(
 
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'))),
 
668
                        array(
 
669
                                'Apple' => array('id' => 3),
 
670
                                'Child' => array())
 
671
                );
 
672
 
 
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);
 
676
 
 
677
                $Apple->Child->Behaviors->disable('Test');
 
678
                $result = $Apple->find('all');
 
679
                $this->assertEqual($result, $expected);
 
680
 
 
681
                $Apple->Child->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on'));
 
682
                //$this->assertIdentical($Apple->find('all'), array());
 
683
 
 
684
                $Apple->Child->Behaviors->attach('Test', array('after' => 'off'));
 
685
                $this->assertEqual($Apple->find('all'), $expected);
 
686
 
 
687
                $Apple->Child->Behaviors->attach('Test', array('after' => 'test'));
 
688
                $this->assertEqual($Apple->find('all'), $expected);
 
689
 
 
690
                $Apple->Child->Behaviors->attach('Test', array('after' => 'test2'));
 
691
                $this->assertEqual($Apple->find('all'), $expected);
 
692
 
 
693
                $Apple->Child->Behaviors->attach('Test', array('after' => '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->assertEqual($Apple->find('all'), $expected);
 
704
 
 
705
        }
 
706
        /**
 
707
 * testBehaviorHasOneFindCallbacks method
 
708
 *
 
709
 * @access public
 
710
 * @return void
 
711
 */
 
712
        function testBehaviorHasOneFindCallbacks() {
 
713
                $Apple = new Apple();
 
714
                $Apple->unbindModel(array('hasMany' => array('Child'), 'belongsTo' => array('Parent')), false);
 
715
                $expected = $Apple->find('all');
 
716
 
 
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);
 
722
 
 
723
                $Apple->Sample->Behaviors->attach('Test', array('before' => 'off'));
 
724
                $this->assertIdentical($Apple->find('all'), $expected);
 
725
 
 
726
                $Apple->Sample->Behaviors->attach('Test', array('before' => 'test'));
 
727
                $this->assertIdentical($Apple->find('all'), $expected);
 
728
 
 
729
                $Apple->Sample->Behaviors->attach('Test', array('before' => 'modify'));
 
730
                $expected2 = array(
 
731
                        array(
 
732
                                'Apple' => array('id' => 1),
 
733
                                'Child' => array(
 
734
                                        array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))),
 
735
                        array(
 
736
                                'Apple' => array('id' => 2),
 
737
                                'Child' => array(
 
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'))),
 
741
                        array(
 
742
                                'Apple' => array('id' => 3),
 
743
                                'Child' => array())
 
744
                );
 
745
                $result = $Apple->find('all', array('fields' => array('Apple.id'), 'conditions' => array('Apple.id <' => '4')));
 
746
                //$this->assertEqual($result, $expected2);
 
747
 
 
748
                $Apple->Sample->Behaviors->disable('Test');
 
749
                $result = $Apple->find('all');
 
750
                $this->assertEqual($result, $expected);
 
751
 
 
752
                $Apple->Sample->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on'));
 
753
                //$this->assertIdentical($Apple->find('all'), array());
 
754
 
 
755
                $Apple->Sample->Behaviors->attach('Test', array('after' => 'off'));
 
756
                $this->assertEqual($Apple->find('all'), $expected);
 
757
 
 
758
                $Apple->Sample->Behaviors->attach('Test', array('after' => 'test'));
 
759
                $this->assertEqual($Apple->find('all'), $expected);
 
760
 
 
761
                $Apple->Sample->Behaviors->attach('Test', array('after' => 'test2'));
 
762
                $this->assertEqual($Apple->find('all'), $expected);
 
763
 
 
764
                $Apple->Sample->Behaviors->attach('Test', array('after' => 'modify'));
 
765
                $expected = array(
 
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')
 
773
                );
 
774
                //$this->assertEqual($Apple->find('all'), $expected);
 
775
        }
 
776
        /**
 
777
 * testBehaviorBelongsToFindCallbacks method
 
778
 *
 
779
 * @access public
 
780
 * @return void
 
781
 */
 
782
        function testBehaviorBelongsToFindCallbacks() {
 
783
                $Apple = new Apple();
 
784
                $Apple->unbindModel(array('hasMany' => array('Child'), 'hasOne' => array('Sample')), false);
 
785
                $expected = $Apple->find('all');
 
786
 
 
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);
 
792
 
 
793
                $Apple->Parent->Behaviors->attach('Test', array('before' => 'off'));
 
794
                $this->assertIdentical($Apple->find('all'), $expected);
 
795
 
 
796
                $Apple->Parent->Behaviors->attach('Test', array('before' => 'test'));
 
797
                $this->assertIdentical($Apple->find('all'), $expected);
 
798
 
 
799
                $Apple->Parent->Behaviors->attach('Test', array('before' => 'modify'));
 
800
                $expected2 = array(
 
801
                        array(
 
802
                                'Apple' => array('id' => 1),
 
803
                                'Parent' => array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
 
804
                        array(
 
805
                                'Apple' => array('id' => 2),
 
806
                                'Parent' => array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
 
807
                        array(
 
808
                                'Apple' => array('id' => 3),
 
809
                                'Parent' => array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))
 
810
                );
 
811
                $result = $Apple->find('all', array(
 
812
                        'fields' => array('Apple.id', 'Parent.id', 'Parent.name', 'Parent.mytime'),
 
813
                        'conditions' => array('Apple.id <' => '4')
 
814
                ));
 
815
                $this->assertEqual($result, $expected2);
 
816
 
 
817
                $Apple->Parent->Behaviors->disable('Test');
 
818
                $result = $Apple->find('all');
 
819
                $this->assertEqual($result, $expected);
 
820
 
 
821
                $Apple->Parent->Behaviors->attach('Test', array('after' => 'off'));
 
822
                $this->assertEqual($Apple->find('all'), $expected);
 
823
 
 
824
                $Apple->Parent->Behaviors->attach('Test', array('after' => 'test'));
 
825
                $this->assertEqual($Apple->find('all'), $expected);
 
826
 
 
827
                $Apple->Parent->Behaviors->attach('Test', array('after' => 'test2'));
 
828
                $this->assertEqual($Apple->find('all'), $expected);
 
829
 
 
830
                $Apple->Parent->Behaviors->attach('Test', array('after' => 'modify'));
 
831
                $expected = array(
 
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')
 
839
                );
 
840
                //$this->assertEqual($Apple->find('all'), $expected);
 
841
        }
 
842
 
 
843
/**
 
844
 * testBehaviorSaveCallbacks method
 
845
 *
 
846
 * @access public
 
847
 * @return void
 
848
 */
 
849
        function testBehaviorSaveCallbacks() {
 
850
                $Sample = new Sample();
 
851
                $record = array('Sample' => array('apple_id' => 6, 'name' => 'sample99'));
 
852
 
 
853
                $Sample->Behaviors->attach('Test', array('beforeSave' => 'on'));
 
854
                $Sample->create();
 
855
                $this->assertIdentical($Sample->save($record), false);
 
856
 
 
857
                $Sample->Behaviors->attach('Test', array('beforeSave' => 'off'));
 
858
                $Sample->create();
 
859
                $this->assertIdentical($Sample->save($record), $record);
 
860
 
 
861
                $Sample->Behaviors->attach('Test', array('beforeSave' => 'test'));
 
862
                $Sample->create();
 
863
                $this->assertIdentical($Sample->save($record), $record);
 
864
 
 
865
                $Sample->Behaviors->attach('Test', array('beforeSave' => 'modify'));
 
866
                $expected = Set::insert($record, 'Sample.name', 'sample99 modified before');
 
867
                $Sample->create();
 
868
                $this->assertIdentical($Sample->save($record), $expected);
 
869
 
 
870
                $Sample->Behaviors->disable('Test');
 
871
                $this->assertIdentical($Sample->save($record), $record);
 
872
 
 
873
                $Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'on'));
 
874
                $expected = Set::merge($record, array('Sample' => array('aftersave' => 'modified after on create')));
 
875
                $Sample->create();
 
876
                $this->assertIdentical($Sample->save($record), $expected);
 
877
 
 
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')));
 
880
                $Sample->create();
 
881
                $this->assertIdentical($Sample->save($record), $expected);
 
882
 
 
883
                $Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'test'));
 
884
                $Sample->create();
 
885
                $this->assertIdentical($Sample->save($record), $record);
 
886
 
 
887
                $Sample->Behaviors->attach('Test', array('afterSave' => 'test2'));
 
888
                $Sample->create();
 
889
                $this->assertIdentical($Sample->save($record), $record);
 
890
 
 
891
                $Sample->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'off'));
 
892
                $Sample->recursive = -1;
 
893
                $record2 = $Sample->read(null, 1);
 
894
 
 
895
                $Sample->Behaviors->attach('Test', array('afterSave' => 'on'));
 
896
                $expected = Set::merge($record2, array('Sample' => array('aftersave' => 'modified after')));
 
897
                $Sample->create();
 
898
                $this->assertIdentical($Sample->save($record2), $expected);
 
899
 
 
900
                $Sample->Behaviors->attach('Test', array('afterSave' => 'modify'));
 
901
                $expected = Set::merge($record2, array('Sample' => array('name' => 'sample1 modified after')));
 
902
                $Sample->create();
 
903
                $this->assertIdentical($Sample->save($record2), $expected);
 
904
        }
 
905
 
 
906
/**
 
907
 * testBehaviorDeleteCallbacks method
 
908
 *
 
909
 * @access public
 
910
 * @return void
 
911
 */
 
912
        function testBehaviorDeleteCallbacks() {
 
913
                $Apple = new Apple();
 
914
 
 
915
                $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'beforeDelete' => 'off'));
 
916
                $this->assertIdentical($Apple->delete(6), true);
 
917
 
 
918
                $Apple->Behaviors->attach('Test', array('beforeDelete' => 'on'));
 
919
                $this->assertIdentical($Apple->delete(4), false);
 
920
 
 
921
                $Apple->Behaviors->attach('Test', array('beforeDelete' => 'test2'));
 
922
                if (ob_start()) {
 
923
                        $results = $Apple->delete(4);
 
924
                        $this->assertIdentical(trim(ob_get_clean()), 'beforeDelete success (cascading)');
 
925
                        $this->assertIdentical($results, true);
 
926
                }
 
927
                if (ob_start()) {
 
928
                        $results = $Apple->delete(3, false);
 
929
                        $this->assertIdentical(trim(ob_get_clean()), 'beforeDelete success');
 
930
                        $this->assertIdentical($results, true);
 
931
                }
 
932
 
 
933
                $Apple->Behaviors->attach('Test', array('beforeDelete' => 'off', 'afterDelete' => 'on'));
 
934
                if (ob_start()) {
 
935
                        $results = $Apple->delete(2, false);
 
936
                        $this->assertIdentical(trim(ob_get_clean()), 'afterDelete success');
 
937
                        $this->assertIdentical($results, true);
 
938
                }
 
939
        }
 
940
        /**
 
941
 * testBehaviorOnErrorCallback method
 
942
 *
 
943
 * @access public
 
944
 * @return void
 
945
 */
 
946
        function testBehaviorOnErrorCallback() {
 
947
                $Apple = new Apple();
 
948
 
 
949
                $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'onError' => 'on'));
 
950
                if (ob_start()) {
 
951
                        $Apple->Behaviors->Test->onError($Apple);
 
952
                        $this->assertIdentical(trim(ob_get_clean()), 'onError trigger success');
 
953
                }
 
954
 
 
955
                if (ob_start()) {
 
956
                        $Apple->delete(99);
 
957
                        //$this->assertIdentical(trim(ob_get_clean()), 'onError trigger success');
 
958
                }
 
959
        }
 
960
        /**
 
961
 * testBehaviorValidateCallback method
 
962
 *
 
963
 * @access public
 
964
 * @return void
 
965
 */
 
966
        function testBehaviorValidateCallback() {
 
967
                $Apple = new Apple();
 
968
 
 
969
                $Apple->Behaviors->attach('Test');
 
970
                $this->assertIdentical($Apple->validates(), true);
 
971
 
 
972
                $Apple->Behaviors->attach('Test', array('validate' => 'on'));
 
973
                $this->assertIdentical($Apple->validates(), false);
 
974
                $this->assertIdentical($Apple->validationErrors, array('name' => true));
 
975
 
 
976
                $Apple->Behaviors->attach('Test', array('validate' => 'stop'));
 
977
                $this->assertIdentical($Apple->validates(), false);
 
978
                $this->assertIdentical($Apple->validationErrors, array('name' => true));
 
979
 
 
980
                $Apple->Behaviors->attach('Test', array('validate' => 'whitelist'));
 
981
                $Apple->validates();
 
982
                $this->assertIdentical($Apple->whitelist, array());
 
983
 
 
984
                $Apple->whitelist = array('unknown');
 
985
                $Apple->validates();
 
986
                $this->assertIdentical($Apple->whitelist, array('unknown', 'name'));
 
987
        }
 
988
 
 
989
/**
 
990
 * testBehaviorValidateMethods method
 
991
 *
 
992
 * @access public
 
993
 * @return void
 
994
 */
 
995
        function testBehaviorValidateMethods() {
 
996
                $Apple = new Apple();
 
997
                $Apple->Behaviors->attach('Test');
 
998
                $Apple->validate['color'] = 'validateField';
 
999
 
 
1000
                $result = $Apple->save(array('name' => 'Genetically Modified Apple', 'color' => 'Orange'));
 
1001
                $this->assertEqual(array_keys($result['Apple']), array('name', 'color', 'modified', 'created'));
 
1002
 
 
1003
                $Apple->create();
 
1004
                $result = $Apple->save(array('name' => 'Regular Apple', 'color' => 'Red'));
 
1005
                $this->assertFalse($result);
 
1006
        }
 
1007
 
 
1008
/**
 
1009
 * testBehaviorMethodDispatching method
 
1010
 *
 
1011
 * @access public
 
1012
 * @return void
 
1013
 */
 
1014
        function testBehaviorMethodDispatching() {
 
1015
                $Apple = new Apple();
 
1016
                $Apple->Behaviors->attach('Test');
 
1017
 
 
1018
                $expected = 'working';
 
1019
                $this->assertEqual($Apple->testMethod(), $expected);
 
1020
                $this->assertEqual($Apple->Behaviors->dispatchMethod($Apple, 'testMethod'), $expected);
 
1021
 
 
1022
                $result = $Apple->Behaviors->dispatchMethod($Apple, 'wtf');
 
1023
                $this->assertEqual($result, array('unhandled'));
 
1024
 
 
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);
 
1028
        }
 
1029
 
 
1030
/**
 
1031
 * testBehaviorMethodDispatchingWithData method
 
1032
 *
 
1033
 * @access public
 
1034
 * @return void
 
1035
 */
 
1036
        function testBehaviorMethodDispatchingWithData() {
 
1037
                $Apple = new Apple();
 
1038
                $Apple->Behaviors->attach('Test');
 
1039
 
 
1040
                $Apple->set('field', 'value');
 
1041
                $this->assertTrue($Apple->testData());
 
1042
                $this->assertTrue($Apple->data['Apple']['field_2']);
 
1043
 
 
1044
                $this->assertTrue($Apple->testData('one', 'two', 'three', 'four', 'five', 'six'));
 
1045
        }
 
1046
 
 
1047
/**
 
1048
 * testBehaviorTrigger method
 
1049
 *
 
1050
 * @access public
 
1051
 * @return void
 
1052
 */
 
1053
        function testBehaviorTrigger() {
 
1054
                $Apple =& new Apple();
 
1055
                $Apple->Behaviors->attach('Test');
 
1056
                $Apple->Behaviors->attach('Test2');
 
1057
                $Apple->Behaviors->attach('Test3');
 
1058
 
 
1059
                $Apple->beforeTestResult = array();
 
1060
                $Apple->Behaviors->trigger($Apple, 'beforeTest');
 
1061
                $expected = array('testbehavior', 'test2behavior', 'test3behavior');
 
1062
                $this->assertIdentical($Apple->beforeTestResult, $expected);
 
1063
 
 
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);
 
1068
 
 
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);
 
1073
        }
 
1074
 
 
1075
/**
 
1076
 * undocumented function
 
1077
 *
 
1078
 * @return void
 
1079
 * @access public
 
1080
 */
 
1081
        function testBindModelCallsInBehaviors() {
 
1082
                $this->loadFixtures('Article', 'Comment');
 
1083
 
 
1084
                // hasMany
 
1085
                $Article = new Article();
 
1086
                $Article->unbindModel(array('hasMany' => array('Comment')));
 
1087
                $result = $Article->find('first');
 
1088
                $this->assertFalse(array_key_exists('Comment', $result));
 
1089
 
 
1090
                $Article->Behaviors->attach('Test4');
 
1091
                $result = $Article->find('first');
 
1092
                $this->assertTrue(array_key_exists('Comment', $result));
 
1093
 
 
1094
                // belongsTo
 
1095
                $Article->unbindModel(array('belongsTo' => array('User')));
 
1096
                $result = $Article->find('first');
 
1097
                $this->assertFalse(array_key_exists('User', $result));
 
1098
 
 
1099
                $Article->Behaviors->attach('Test5');
 
1100
                $result = $Article->find('first');
 
1101
                $this->assertTrue(array_key_exists('User', $result));
 
1102
 
 
1103
                // hasAndBelongsToMany
 
1104
                $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
 
1105
                $result = $Article->find('first');
 
1106
                $this->assertFalse(array_key_exists('Tag', $result));
 
1107
 
 
1108
                $Article->Behaviors->attach('Test6');
 
1109
                $result = $Article->find('first');
 
1110
                $this->assertTrue(array_key_exists('Comment', $result));
 
1111
 
 
1112
                // hasOne
 
1113
                $Comment = new Comment();
 
1114
                $Comment->unbindModel(array('hasOne' => array('Attachment')));
 
1115
                $result = $Comment->find('first');
 
1116
                $this->assertFalse(array_key_exists('Attachment', $result));
 
1117
 
 
1118
                $Comment->Behaviors->attach('Test7');
 
1119
                $result = $Comment->find('first');
 
1120
                $this->assertTrue(array_key_exists('Attachment', $result));
 
1121
        }
 
1122
 
 
1123
/**
 
1124
 * Test attach and detaching
 
1125
 *
 
1126
 * @access public
 
1127
 * @return void
 
1128
 */
 
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');
 
1135
 
 
1136
                $Sample->Behaviors->trigger($Sample, 'beforeTest');
 
1137
        }
 
1138
}