PageRenderTime 73ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/Cake/Test/Case/Model/BehaviorCollectionTest.php

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