PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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