BehaviorCollectionTest.php 41 KB

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