BehaviorCollectionTest.php 35 KB

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