PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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