BehaviorCollectionTest.php 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  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($Sample->save($record), false);
  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($result, $expected);
  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($result, $expected);
  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($result, $expected);
  787. $Sample->Behaviors->disable('Test');
  788. $this->assertSame($Sample->save($record), $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($result, $expected);
  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($result, $expected);
  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($result, $expected);
  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($Sample->save($record2), $expected);
  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($Sample->save($record2), $expected);
  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. $this->loadFixtures('Article', 'Comment');
  940. // hasMany
  941. $Article = new Article();
  942. $Article->unbindModel(array('hasMany' => array('Comment')));
  943. $result = $Article->find('first');
  944. $this->assertFalse(array_key_exists('Comment', $result));
  945. $Article->Behaviors->attach('Test4');
  946. $result = $Article->find('first');
  947. $this->assertTrue(array_key_exists('Comment', $result));
  948. // belongsTo
  949. $Article->unbindModel(array('belongsTo' => array('User')));
  950. $result = $Article->find('first');
  951. $this->assertFalse(array_key_exists('User', $result));
  952. $Article->Behaviors->attach('Test5');
  953. $result = $Article->find('first');
  954. $this->assertTrue(array_key_exists('User', $result));
  955. // hasAndBelongsToMany
  956. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
  957. $result = $Article->find('first');
  958. $this->assertFalse(array_key_exists('Tag', $result));
  959. $Article->Behaviors->attach('Test6');
  960. $result = $Article->find('first');
  961. $this->assertTrue(array_key_exists('Comment', $result));
  962. // hasOne
  963. $Comment = new Comment();
  964. $Comment->unbindModel(array('hasOne' => array('Attachment')));
  965. $result = $Comment->find('first');
  966. $this->assertFalse(array_key_exists('Attachment', $result));
  967. $Comment->Behaviors->attach('Test7');
  968. $result = $Comment->find('first');
  969. $this->assertTrue(array_key_exists('Attachment', $result));
  970. }
  971. /**
  972. * Test attach and detaching
  973. *
  974. * @return void
  975. */
  976. public function testBehaviorAttachAndDetach() {
  977. $Sample = new Sample();
  978. $Sample->actsAs = array('Test3' => array('bar'), 'Test2' => array('foo', 'bar'));
  979. $Sample->Behaviors->init($Sample->alias, $Sample->actsAs);
  980. $Sample->Behaviors->attach('Test2');
  981. $Sample->Behaviors->detach('Test3');
  982. $Sample->Behaviors->trigger('beforeTest', array(&$Sample));
  983. }
  984. /**
  985. * test that hasMethod works with basic functions.
  986. *
  987. * @return void
  988. */
  989. public function testHasMethodBasic() {
  990. $Sample = new Sample();
  991. $Collection = new BehaviorCollection();
  992. $Collection->init('Sample', array('Test', 'Test2'));
  993. $this->assertTrue($Collection->hasMethod('testMethod'));
  994. $this->assertTrue($Collection->hasMethod('resolveMethod'));
  995. $this->assertFalse($Collection->hasMethod('No method'));
  996. }
  997. /**
  998. * test that hasMethod works with mapped methods.
  999. *
  1000. * @return void
  1001. */
  1002. public function testHasMethodMappedMethods() {
  1003. $Sample = new Sample();
  1004. $Collection = new BehaviorCollection();
  1005. $Collection->init('Sample', array('Test', 'Test2'));
  1006. $this->assertTrue($Collection->hasMethod('look for the remote in the couch'));
  1007. $this->assertTrue($Collection->hasMethod('mappingRobotOnTheRoof'));
  1008. }
  1009. /**
  1010. * test hasMethod returrning a 'callback'
  1011. *
  1012. * @return void
  1013. */
  1014. public function testHasMethodAsCallback() {
  1015. $Sample = new Sample();
  1016. $Collection = new BehaviorCollection();
  1017. $Collection->init('Sample', array('Test', 'Test2'));
  1018. $result = $Collection->hasMethod('testMethod', true);
  1019. $expected = array('Test', 'testMethod');
  1020. $this->assertEquals($expected, $result);
  1021. $result = $Collection->hasMethod('resolveMethod', true);
  1022. $expected = array('Test2', 'resolveMethod');
  1023. $this->assertEquals($expected, $result);
  1024. $result = $Collection->hasMethod('mappingRobotOnTheRoof', true);
  1025. $expected = array('Test2', 'mapped', 'mappingRobotOnTheRoof');
  1026. $this->assertEquals($expected, $result);
  1027. }
  1028. }