BehaviorCollectionTest.php 38 KB

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