BehaviorCollectionTest.php 36 KB

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