BehaviorCollectionTest.php 38 KB

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