BehaviorCollectionTest.php 38 KB

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