BehaviorCollectionTest.php 41 KB

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