BehaviorCollectionTest.php 38 KB

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