BehaviorCollectionTest.php 38 KB

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