BehaviorCollectionTest.php 38 KB

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