BehaviorCollectionTest.php 41 KB

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