PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/cases/libs/model/model_behavior.test.php

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