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

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

https://bitbucket.org/webpolis/hurli
PHP | 1138 lines | 685 code | 141 blank | 312 comment | 41 complexity | 20b4901f5606b18a801aa41e6ba78a58 MD5 | raw file
  1. <?php
  2. /**
  3. * BehaviorTest file
  4. *
  5. * Long description for behavior.test.php
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @package cake
  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 attach()/detach() works with plugin.banana
  471. *
  472. * @return void
  473. */
  474. function testDetachWithPluginNames() {
  475. $Apple = new Apple();
  476. $Apple->Behaviors->attach('Plugin.Test');
  477. $this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
  478. $this->assertEqual($Apple->Behaviors->attached(), array('Test'));
  479. $Apple->Behaviors->detach('Plugin.Test');
  480. $this->assertEqual($Apple->Behaviors->attached(), array());
  481. $Apple->Behaviors->attach('Plugin.Test');
  482. $this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
  483. $this->assertEqual($Apple->Behaviors->attached(), array('Test'));
  484. $Apple->Behaviors->detach('Test');
  485. $this->assertEqual($Apple->Behaviors->attached(), array());
  486. }
  487. /**
  488. * test that attaching a non existant Behavior triggers a cake error.
  489. *
  490. * @return void
  491. */
  492. function testInvalidBehaviorCausingCakeError() {
  493. $Apple =& new Apple();
  494. $Apple->Behaviors =& new MockModelBehaviorCollection();
  495. $Apple->Behaviors->expectOnce('cakeError');
  496. $Apple->Behaviors->expectAt(0, 'cakeError', array('missingBehaviorFile', '*'));
  497. $this->assertFalse($Apple->Behaviors->attach('NoSuchBehavior'));
  498. }
  499. /**
  500. * testBehaviorToggling method
  501. *
  502. * @access public
  503. * @return void
  504. */
  505. function testBehaviorToggling() {
  506. $Apple = new Apple();
  507. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  508. $Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value')));
  509. $this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
  510. $Apple->Behaviors->disable('Test');
  511. $this->assertIdentical($Apple->Behaviors->attached(), array('Test'));
  512. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  513. $Apple->Sample->Behaviors->attach('Test');
  514. $this->assertIdentical($Apple->Sample->Behaviors->enabled('Test'), true);
  515. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  516. $Apple->Behaviors->enable('Test');
  517. $this->assertIdentical($Apple->Behaviors->attached('Test'), true);
  518. $this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
  519. $Apple->Behaviors->disable('Test');
  520. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  521. $Apple->Behaviors->attach('Test', array('enabled' => true));
  522. $this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
  523. $Apple->Behaviors->attach('Test', array('enabled' => false));
  524. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  525. $Apple->Behaviors->detach('Test');
  526. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  527. }
  528. /**
  529. * testBehaviorFindCallbacks method
  530. *
  531. * @access public
  532. * @return void
  533. */
  534. function testBehaviorFindCallbacks() {
  535. $Apple = new Apple();
  536. $expected = $Apple->find('all');
  537. $Apple->Behaviors->attach('Test');
  538. $this->assertIdentical($Apple->find('all'), null);
  539. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off'));
  540. $this->assertIdentical($Apple->find('all'), $expected);
  541. $Apple->Behaviors->attach('Test', array('beforeFind' => 'test'));
  542. $this->assertIdentical($Apple->find('all'), $expected);
  543. $Apple->Behaviors->attach('Test', array('beforeFind' => 'modify'));
  544. $expected2 = array(
  545. array('Apple' => array('id' => '1', 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
  546. array('Apple' => array('id' => '2', 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
  547. array('Apple' => array('id' => '3', 'name' => 'green blue', 'mytime' => '22:57:17'))
  548. );
  549. $result = $Apple->find('all', array('conditions' => array('Apple.id <' => '4')));
  550. $this->assertEqual($result, $expected2);
  551. $Apple->Behaviors->disable('Test');
  552. $result = $Apple->find('all');
  553. $this->assertEqual($result, $expected);
  554. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'on'));
  555. $this->assertIdentical($Apple->find('all'), array());
  556. $Apple->Behaviors->attach('Test', array('afterFind' => 'off'));
  557. $this->assertEqual($Apple->find('all'), $expected);
  558. $Apple->Behaviors->attach('Test', array('afterFind' => 'test'));
  559. $this->assertEqual($Apple->find('all'), $expected);
  560. $Apple->Behaviors->attach('Test', array('afterFind' => 'test2'));
  561. $this->assertEqual($Apple->find('all'), $expected);
  562. $Apple->Behaviors->attach('Test', array('afterFind' => 'modify'));
  563. $expected = array(
  564. 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'),
  565. 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'),
  566. 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'),
  567. 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'),
  568. 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'),
  569. 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'),
  570. 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')
  571. );
  572. $this->assertEqual($Apple->find('all'), $expected);
  573. }
  574. /**
  575. * testBehaviorHasManyFindCallbacks method
  576. *
  577. * @access public
  578. * @return void
  579. */
  580. function testBehaviorHasManyFindCallbacks() {
  581. $Apple = new Apple();
  582. $Apple->unbindModel(array('hasOne' => array('Sample'), 'belongsTo' => array('Parent')), false);
  583. $expected = $Apple->find('all');
  584. $Apple->unbindModel(array('hasMany' => array('Child')));
  585. $wellBehaved = $Apple->find('all');
  586. $Apple->Child->Behaviors->attach('Test', array('afterFind' => 'modify'));
  587. $Apple->unbindModel(array('hasMany' => array('Child')));
  588. $this->assertIdentical($Apple->find('all'), $wellBehaved);
  589. $Apple->Child->Behaviors->attach('Test', array('before' => 'off'));
  590. $this->assertIdentical($Apple->find('all'), $expected);
  591. $Apple->Child->Behaviors->attach('Test', array('before' => 'test'));
  592. $this->assertIdentical($Apple->find('all'), $expected);
  593. $expected2 = array(
  594. array(
  595. 'Apple' => array('id' => 1),
  596. 'Child' => array(
  597. array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))),
  598. array(
  599. 'Apple' => array('id' => 2),
  600. 'Child' => array(
  601. array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17'),
  602. array('id' => 3, 'name' => 'green blue', 'mytime' => '22:57:17'),
  603. array('id' => 4, 'name' => 'Test Name', 'mytime' => '22:57:17'))),
  604. array(
  605. 'Apple' => array('id' => 3),
  606. 'Child' => array())
  607. );
  608. $Apple->Child->Behaviors->attach('Test', array('before' => 'modify'));
  609. $result = $Apple->find('all', array('fields' => array('Apple.id'), 'conditions' => array('Apple.id <' => '4')));
  610. //$this->assertEqual($result, $expected2);
  611. $Apple->Child->Behaviors->disable('Test');
  612. $result = $Apple->find('all');
  613. $this->assertEqual($result, $expected);
  614. $Apple->Child->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on'));
  615. //$this->assertIdentical($Apple->find('all'), array());
  616. $Apple->Child->Behaviors->attach('Test', array('after' => 'off'));
  617. $this->assertEqual($Apple->find('all'), $expected);
  618. $Apple->Child->Behaviors->attach('Test', array('after' => 'test'));
  619. $this->assertEqual($Apple->find('all'), $expected);
  620. $Apple->Child->Behaviors->attach('Test', array('after' => 'test2'));
  621. $this->assertEqual($Apple->find('all'), $expected);
  622. $Apple->Child->Behaviors->attach('Test', array('after' => 'modify'));
  623. $expected = array(
  624. 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'),
  625. 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'),
  626. 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'),
  627. 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'),
  628. 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'),
  629. 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'),
  630. 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')
  631. );
  632. //$this->assertEqual($Apple->find('all'), $expected);
  633. }
  634. /**
  635. * testBehaviorHasOneFindCallbacks method
  636. *
  637. * @access public
  638. * @return void
  639. */
  640. function testBehaviorHasOneFindCallbacks() {
  641. $Apple = new Apple();
  642. $Apple->unbindModel(array('hasMany' => array('Child'), 'belongsTo' => array('Parent')), false);
  643. $expected = $Apple->find('all');
  644. $Apple->unbindModel(array('hasOne' => array('Sample')));
  645. $wellBehaved = $Apple->find('all');
  646. $Apple->Sample->Behaviors->attach('Test');
  647. $Apple->unbindModel(array('hasOne' => array('Sample')));
  648. $this->assertIdentical($Apple->find('all'), $wellBehaved);
  649. $Apple->Sample->Behaviors->attach('Test', array('before' => 'off'));
  650. $this->assertIdentical($Apple->find('all'), $expected);
  651. $Apple->Sample->Behaviors->attach('Test', array('before' => 'test'));
  652. $this->assertIdentical($Apple->find('all'), $expected);
  653. $Apple->Sample->Behaviors->attach('Test', array('before' => 'modify'));
  654. $expected2 = array(
  655. array(
  656. 'Apple' => array('id' => 1),
  657. 'Child' => array(
  658. array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))),
  659. array(
  660. 'Apple' => array('id' => 2),
  661. 'Child' => array(
  662. array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17'),
  663. array('id' => 3, 'name' => 'green blue', 'mytime' => '22:57:17'),
  664. array('id' => 4, 'name' => 'Test Name', 'mytime' => '22:57:17'))),
  665. array(
  666. 'Apple' => array('id' => 3),
  667. 'Child' => array())
  668. );
  669. $result = $Apple->find('all', array('fields' => array('Apple.id'), 'conditions' => array('Apple.id <' => '4')));
  670. //$this->assertEqual($result, $expected2);
  671. $Apple->Sample->Behaviors->disable('Test');
  672. $result = $Apple->find('all');
  673. $this->assertEqual($result, $expected);
  674. $Apple->Sample->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on'));
  675. //$this->assertIdentical($Apple->find('all'), array());
  676. $Apple->Sample->Behaviors->attach('Test', array('after' => 'off'));
  677. $this->assertEqual($Apple->find('all'), $expected);
  678. $Apple->Sample->Behaviors->attach('Test', array('after' => 'test'));
  679. $this->assertEqual($Apple->find('all'), $expected);
  680. $Apple->Sample->Behaviors->attach('Test', array('after' => 'test2'));
  681. $this->assertEqual($Apple->find('all'), $expected);
  682. $Apple->Sample->Behaviors->attach('Test', array('after' => 'modify'));
  683. $expected = array(
  684. 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'),
  685. 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'),
  686. 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'),
  687. 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'),
  688. 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'),
  689. 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'),
  690. 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')
  691. );
  692. //$this->assertEqual($Apple->find('all'), $expected);
  693. }
  694. /**
  695. * testBehaviorBelongsToFindCallbacks method
  696. *
  697. * @access public
  698. * @return void
  699. */
  700. function testBehaviorBelongsToFindCallbacks() {
  701. $Apple = new Apple();
  702. $Apple->unbindModel(array('hasMany' => array('Child'), 'hasOne' => array('Sample')), false);
  703. $expected = $Apple->find('all');
  704. $Apple->unbindModel(array('belongsTo' => array('Parent')));
  705. $wellBehaved = $Apple->find('all');
  706. $Apple->Parent->Behaviors->attach('Test');
  707. $Apple->unbindModel(array('belongsTo' => array('Parent')));
  708. $this->assertIdentical($Apple->find('all'), $wellBehaved);
  709. $Apple->Parent->Behaviors->attach('Test', array('before' => 'off'));
  710. $this->assertIdentical($Apple->find('all'), $expected);
  711. $Apple->Parent->Behaviors->attach('Test', array('before' => 'test'));
  712. $this->assertIdentical($Apple->find('all'), $expected);
  713. $Apple->Parent->Behaviors->attach('Test', array('before' => 'modify'));
  714. $expected2 = array(
  715. array(
  716. 'Apple' => array('id' => 1),
  717. 'Parent' => array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
  718. array(
  719. 'Apple' => array('id' => 2),
  720. 'Parent' => array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
  721. array(
  722. 'Apple' => array('id' => 3),
  723. 'Parent' => array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))
  724. );
  725. $result = $Apple->find('all', array(
  726. 'fields' => array('Apple.id', 'Parent.id', 'Parent.name', 'Parent.mytime'),
  727. 'conditions' => array('Apple.id <' => '4')
  728. ));
  729. $this->assertEqual($result, $expected2);
  730. $Apple->Parent->Behaviors->disable('Test');
  731. $result = $Apple->find('all');
  732. $this->assertEqual($result, $expected);
  733. $Apple->Parent->Behaviors->attach('Test', array('after' => 'off'));
  734. $this->assertEqual($Apple->find('all'), $expected);
  735. $Apple->Parent->Behaviors->attach('Test', array('after' => 'test'));
  736. $this->assertEqual($Apple->find('all'), $expected);
  737. $Apple->Parent->Behaviors->attach('Test', array('after' => 'test2'));
  738. $this->assertEqual($Apple->find('all'), $expected);
  739. $Apple->Parent->Behaviors->attach('Test', array('after' => 'modify'));
  740. $expected = array(
  741. 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'),
  742. 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'),
  743. 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'),
  744. 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'),
  745. 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'),
  746. 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'),
  747. 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')
  748. );
  749. //$this->assertEqual($Apple->find('all'), $expected);
  750. }
  751. /**
  752. * testBehaviorSaveCallbacks method
  753. *
  754. * @access public
  755. * @return void
  756. */
  757. function testBehaviorSaveCallbacks() {
  758. $Sample = new Sample();
  759. $record = array('Sample' => array('apple_id' => 6, 'name' => 'sample99'));
  760. $Sample->Behaviors->attach('Test', array('beforeSave' => 'on'));
  761. $Sample->create();
  762. $this->assertIdentical($Sample->save($record), false);
  763. $Sample->Behaviors->attach('Test', array('beforeSave' => 'off'));
  764. $Sample->create();
  765. $this->assertIdentical($Sample->save($record), $record);
  766. $Sample->Behaviors->attach('Test', array('beforeSave' => 'test'));
  767. $Sample->create();
  768. $this->assertIdentical($Sample->save($record), $record);
  769. $Sample->Behaviors->attach('Test', array('beforeSave' => 'modify'));
  770. $expected = Set::insert($record, 'Sample.name', 'sample99 modified before');
  771. $Sample->create();
  772. $this->assertIdentical($Sample->save($record), $expected);
  773. $Sample->Behaviors->disable('Test');
  774. $this->assertIdentical($Sample->save($record), $record);
  775. $Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'on'));
  776. $expected = Set::merge($record, array('Sample' => array('aftersave' => 'modified after on create')));
  777. $Sample->create();
  778. $this->assertIdentical($Sample->save($record), $expected);
  779. $Sample->Behaviors->attach('Test', array('beforeSave' => 'modify', 'afterSave' => 'modify'));
  780. $expected = Set::merge($record, array('Sample' => array('name' => 'sample99 modified before modified after on create')));
  781. $Sample->create();
  782. $this->assertIdentical($Sample->save($record), $expected);
  783. $Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'test'));
  784. $Sample->create();
  785. $this->assertIdentical($Sample->save($record), $record);
  786. $Sample->Behaviors->attach('Test', array('afterSave' => 'test2'));
  787. $Sample->create();
  788. $this->assertIdentical($Sample->save($record), $record);
  789. $Sample->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'off'));
  790. $Sample->recursive = -1;
  791. $record2 = $Sample->read(null, 1);
  792. $Sample->Behaviors->attach('Test', array('afterSave' => 'on'));
  793. $expected = Set::merge($record2, array('Sample' => array('aftersave' => 'modified after')));
  794. $Sample->create();
  795. $this->assertIdentical($Sample->save($record2), $expected);
  796. $Sample->Behaviors->attach('Test', array('afterSave' => 'modify'));
  797. $expected = Set::merge($record2, array('Sample' => array('name' => 'sample1 modified after')));
  798. $Sample->create();
  799. $this->assertIdentical($Sample->save($record2), $expected);
  800. }
  801. /**
  802. * testBehaviorDeleteCallbacks method
  803. *
  804. * @access public
  805. * @return void
  806. */
  807. function testBehaviorDeleteCallbacks() {
  808. $Apple = new Apple();
  809. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'beforeDelete' => 'off'));
  810. $this->assertIdentical($Apple->delete(6), true);
  811. $Apple->Behaviors->attach('Test', array('beforeDelete' => 'on'));
  812. $this->assertIdentical($Apple->delete(4), false);
  813. $Apple->Behaviors->attach('Test', array('beforeDelete' => 'test2'));
  814. if (ob_start()) {
  815. $results = $Apple->delete(4);
  816. $this->assertIdentical(trim(ob_get_clean()), 'beforeDelete success (cascading)');
  817. $this->assertIdentical($results, true);
  818. }
  819. if (ob_start()) {
  820. $results = $Apple->delete(3, false);
  821. $this->assertIdentical(trim(ob_get_clean()), 'beforeDelete success');
  822. $this->assertIdentical($results, true);
  823. }
  824. $Apple->Behaviors->attach('Test', array('beforeDelete' => 'off', 'afterDelete' => 'on'));
  825. if (ob_start()) {
  826. $results = $Apple->delete(2, false);
  827. $this->assertIdentical(trim(ob_get_clean()), 'afterDelete success');
  828. $this->assertIdentical($results, true);
  829. }
  830. }
  831. /**
  832. * testBehaviorOnErrorCallback method
  833. *
  834. * @access public
  835. * @return void
  836. */
  837. function testBehaviorOnErrorCallback() {
  838. $Apple = new Apple();
  839. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'onError' => 'on'));
  840. if (ob_start()) {
  841. $Apple->Behaviors->Test->onError($Apple);
  842. $this->assertIdentical(trim(ob_get_clean()), 'onError trigger success');
  843. }
  844. if (ob_start()) {
  845. $Apple->delete(99);
  846. //$this->assertIdentical(trim(ob_get_clean()), 'onError trigger success');
  847. }
  848. }
  849. /**
  850. * testBehaviorValidateCallback method
  851. *
  852. * @access public
  853. * @return void
  854. */
  855. function testBehaviorValidateCallback() {
  856. $Apple = new Apple();
  857. $Apple->Behaviors->attach('Test');
  858. $this->assertIdentical($Apple->validates(), true);
  859. $Apple->Behaviors->attach('Test', array('validate' => 'on'));
  860. $this->assertIdentical($Apple->validates(), false);
  861. $this->assertIdentical($Apple->validationErrors, array('name' => true));
  862. $Apple->Behaviors->attach('Test', array('validate' => 'stop'));
  863. $this->assertIdentical($Apple->validates(), false);
  864. $this->assertIdentical($Apple->validationErrors, array('name' => true));
  865. $Apple->Behaviors->attach('Test', array('validate' => 'whitelist'));
  866. $Apple->validates();
  867. $this->assertIdentical($Apple->whitelist, array());
  868. $Apple->whitelist = array('unknown');
  869. $Apple->validates();
  870. $this->assertIdentical($Apple->whitelist, array('unknown', 'name'));
  871. }
  872. /**
  873. * testBehaviorValidateMethods method
  874. *
  875. * @access public
  876. * @return void
  877. */
  878. function testBehaviorValidateMethods() {
  879. $Apple = new Apple();
  880. $Apple->Behaviors->attach('Test');
  881. $Apple->validate['color'] = 'validateField';
  882. $result = $Apple->save(array('name' => 'Genetically Modified Apple', 'color' => 'Orange'));
  883. $this->assertEqual(array_keys($result['Apple']), array('name', 'color', 'modified', 'created'));
  884. $Apple->create();
  885. $result = $Apple->save(array('name' => 'Regular Apple', 'color' => 'Red'));
  886. $this->assertFalse($result);
  887. }
  888. /**
  889. * testBehaviorMethodDispatching method
  890. *
  891. * @access public
  892. * @return void
  893. */
  894. function testBehaviorMethodDispatching() {
  895. $Apple = new Apple();
  896. $Apple->Behaviors->attach('Test');
  897. $expected = 'working';
  898. $this->assertEqual($Apple->testMethod(), $expected);
  899. $this->assertEqual($Apple->Behaviors->dispatchMethod($Apple, 'testMethod'), $expected);
  900. $result = $Apple->Behaviors->dispatchMethod($Apple, 'wtf');
  901. $this->assertEqual($result, array('unhandled'));
  902. $result = $Apple->{'look for the remote'}('in the couch');
  903. $expected = "Item.name = 'the remote' AND Location.name = 'the couch'";
  904. $this->assertEqual($result, $expected);
  905. }
  906. /**
  907. * testBehaviorMethodDispatchingWithData method
  908. *
  909. * @access public
  910. * @return void
  911. */
  912. function testBehaviorMethodDispatchingWithData() {
  913. $Apple = new Apple();
  914. $Apple->Behaviors->attach('Test');
  915. $Apple->set('field', 'value');
  916. $this->assertTrue($Apple->testData());
  917. $this->assertTrue($Apple->data['Apple']['field_2']);
  918. $this->assertTrue($Apple->testData('one', 'two', 'three', 'four', 'five', 'six'));
  919. }
  920. /**
  921. * testBehaviorTrigger method
  922. *
  923. * @access public
  924. * @return void
  925. */
  926. function testBehaviorTrigger() {
  927. $Apple =& new Apple();
  928. $Apple->Behaviors->attach('Test');
  929. $Apple->Behaviors->attach('Test2');
  930. $Apple->Behaviors->attach('Test3');
  931. $Apple->beforeTestResult = array();
  932. $Apple->Behaviors->trigger($Apple, 'beforeTest');
  933. $expected = array('testbehavior', 'test2behavior', 'test3behavior');
  934. $this->assertIdentical($Apple->beforeTestResult, $expected);
  935. $Apple->beforeTestResult = array();
  936. $Apple->Behaviors->trigger($Apple, 'beforeTest', array(), array('break' => true, 'breakOn' => 'test2behavior'));
  937. $expected = array('testbehavior', 'test2behavior');
  938. $this->assertIdentical($Apple->beforeTestResult, $expected);
  939. $Apple->beforeTestResult = array();
  940. $Apple->Behaviors->trigger($Apple, 'beforeTest', array(), array('break' => true, 'breakOn' => array('test2behavior', 'test3behavior')));
  941. $expected = array('testbehavior', 'test2behavior');
  942. $this->assertIdentical($Apple->beforeTestResult, $expected);
  943. }
  944. /**
  945. * undocumented function
  946. *
  947. * @return void
  948. * @access public
  949. */
  950. function testBindModelCallsInBehaviors() {
  951. $this->loadFixtures('Article', 'Comment');
  952. // hasMany
  953. $Article = new Article();
  954. $Article->unbindModel(array('hasMany' => array('Comment')));
  955. $result = $Article->find('first');
  956. $this->assertFalse(array_key_exists('Comment', $result));
  957. $Article->Behaviors->attach('Test4');
  958. $result = $Article->find('first');
  959. $this->assertTrue(array_key_exists('Comment', $result));
  960. // belongsTo
  961. $Article->unbindModel(array('belongsTo' => array('User')));
  962. $result = $Article->find('first');
  963. $this->assertFalse(array_key_exists('User', $result));
  964. $Article->Behaviors->attach('Test5');
  965. $result = $Article->find('first');
  966. $this->assertTrue(array_key_exists('User', $result));
  967. // hasAndBelongsToMany
  968. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
  969. $result = $Article->find('first');
  970. $this->assertFalse(array_key_exists('Tag', $result));
  971. $Article->Behaviors->attach('Test6');
  972. $result = $Article->find('first');
  973. $this->assertTrue(array_key_exists('Comment', $result));
  974. // hasOne
  975. $Comment = new Comment();
  976. $Comment->unbindModel(array('hasOne' => array('Attachment')));
  977. $result = $Comment->find('first');
  978. $this->assertFalse(array_key_exists('Attachment', $result));
  979. $Comment->Behaviors->attach('Test7');
  980. $result = $Comment->find('first');
  981. $this->assertTrue(array_key_exists('Attachment', $result));
  982. }
  983. /**
  984. * Test attach and detaching
  985. *
  986. * @access public
  987. * @return void
  988. */
  989. function testBehaviorAttachAndDetach() {
  990. $Sample =& new Sample();
  991. $Sample->actsAs = array('Test3' => array('bar'), 'Test2' => array('foo', 'bar'));
  992. $Sample->Behaviors->init($Sample->alias, $Sample->actsAs);
  993. $Sample->Behaviors->attach('Test2');
  994. $Sample->Behaviors->detach('Test3');
  995. $Sample->Behaviors->trigger($Sample, 'beforeTest');
  996. }
  997. }