BehaviorCollectionTest.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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(LIBS . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)));
  424. $this->assertTrue($Apple->Behaviors->load('SomeOther', array('className' => 'TestPlugin.TestPluginPersisterOne')));
  425. $this->assertInstanceOf('TestPluginPersisterOneBehavior', $Apple->Behaviors->SomeOther);
  426. $result = $Apple->Behaviors->attached();
  427. $this->assertEquals(array('Test', 'SomeOther'), $result, 'attached() results are wrong.');
  428. App::build();
  429. }
  430. /**
  431. * testBehaviorBinding method
  432. *
  433. * @access public
  434. * @return void
  435. */
  436. function testBehaviorBinding() {
  437. $Apple = new Apple();
  438. $this->assertIdentical($Apple->Behaviors->attached(), array());
  439. $Apple->Behaviors->attach('Test', array('key' => 'value'));
  440. $this->assertIdentical($Apple->Behaviors->attached(), array('Test'));
  441. $this->assertEqual(strtolower(get_class($Apple->Behaviors->Test)), 'testbehavior');
  442. $expected = array('beforeFind' => 'on', 'afterFind' => 'off', 'key' => 'value');
  443. $this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
  444. $this->assertEqual(array_keys($Apple->Behaviors->Test->settings), array('Apple'));
  445. $this->assertIdentical($Apple->Sample->Behaviors->attached(), array());
  446. $Apple->Sample->Behaviors->attach('Test', array('key2' => 'value2'));
  447. $this->assertIdentical($Apple->Sample->Behaviors->attached(), array('Test'));
  448. $this->assertEqual($Apple->Sample->Behaviors->Test->settings['Sample'], array('beforeFind' => 'on', 'afterFind' => 'off', 'key2' => 'value2'));
  449. $this->assertEqual(array_keys($Apple->Behaviors->Test->settings), array('Apple', 'Sample'));
  450. $this->assertIdentical(
  451. $Apple->Sample->Behaviors->Test->settings,
  452. $Apple->Behaviors->Test->settings
  453. );
  454. $this->assertNotIdentical($Apple->Behaviors->Test->settings['Apple'], $Apple->Sample->Behaviors->Test->settings['Sample']);
  455. $Apple->Behaviors->attach('Test', array('key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off'));
  456. $Apple->Sample->Behaviors->attach('Test', array('key' => 'value', 'key3' => 'value3', 'beforeFind' => 'off'));
  457. $this->assertEqual($Apple->Behaviors->Test->settings['Apple'], array('beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'value', 'key2' => 'value2', 'key3' => 'value3'));
  458. $this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $Apple->Sample->Behaviors->Test->settings['Sample']);
  459. $this->assertFalse(isset($Apple->Child->Behaviors->Test));
  460. $Apple->Child->Behaviors->attach('Test', array('key' => 'value', 'key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off'));
  461. $this->assertEqual($Apple->Child->Behaviors->Test->settings['Child'], $Apple->Sample->Behaviors->Test->settings['Sample']);
  462. $this->assertFalse(isset($Apple->Parent->Behaviors->Test));
  463. $Apple->Parent->Behaviors->attach('Test', array('key' => 'value', 'key2' => 'value2', 'key3' => 'value3', 'beforeFind' => 'off'));
  464. $this->assertEqual($Apple->Parent->Behaviors->Test->settings['Parent'], $Apple->Sample->Behaviors->Test->settings['Sample']);
  465. $Apple->Parent->Behaviors->attach('Test', array('key' => 'value', 'key2' => 'value', 'key3' => 'value', 'beforeFind' => 'off'));
  466. $this->assertNotEqual($Apple->Parent->Behaviors->Test->settings['Parent'], $Apple->Sample->Behaviors->Test->settings['Sample']);
  467. $Apple->Behaviors->attach('Plugin.Test', array('key' => 'new value'));
  468. $expected = array(
  469. 'beforeFind' => 'off', 'afterFind' => 'off', 'key' => 'new value',
  470. 'key2' => 'value2', 'key3' => 'value3'
  471. );
  472. $this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
  473. $current = $Apple->Behaviors->Test->settings['Apple'];
  474. $expected = array_merge($current, array('mangle' => 'trigger mangled'));
  475. $Apple->Behaviors->attach('Test', array('mangle' => 'trigger'));
  476. $this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
  477. $Apple->Behaviors->attach('Test');
  478. $expected = array_merge($current, array('mangle' => 'trigger mangled mangled'));
  479. $this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
  480. $Apple->Behaviors->attach('Test', array('mangle' => 'trigger'));
  481. $expected = array_merge($current, array('mangle' => 'trigger mangled'));
  482. $this->assertEqual($Apple->Behaviors->Test->settings['Apple'], $expected);
  483. }
  484. /**
  485. * test that attach()/detach() works with plugin.banana
  486. *
  487. * @return void
  488. */
  489. function testDetachWithPluginNames() {
  490. $Apple = new Apple();
  491. $Apple->Behaviors->attach('Plugin.Test');
  492. $this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
  493. $this->assertEqual($Apple->Behaviors->attached(), array('Test'));
  494. $Apple->Behaviors->detach('Plugin.Test');
  495. $this->assertEqual($Apple->Behaviors->attached(), array());
  496. $Apple->Behaviors->attach('Plugin.Test');
  497. $this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
  498. $this->assertEqual($Apple->Behaviors->attached(), array('Test'));
  499. $Apple->Behaviors->detach('Test');
  500. $this->assertEqual($Apple->Behaviors->attached(), array());
  501. }
  502. /**
  503. * test that attaching a non existant Behavior triggers a cake error.
  504. *
  505. * @expectedException MissingBehaviorClassException
  506. * @return void
  507. */
  508. function testInvalidBehaviorCausingCakeError() {
  509. $Apple = new Apple();
  510. $Apple->Behaviors->attach('NoSuchBehavior');
  511. }
  512. /**
  513. * testBehaviorToggling method
  514. *
  515. * @access public
  516. * @return void
  517. */
  518. function testBehaviorToggling() {
  519. $Apple = new Apple();
  520. $expected = $Apple->find('all');
  521. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  522. $Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value')));
  523. $this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
  524. $Apple->Behaviors->disable('Test');
  525. $this->assertIdentical($Apple->Behaviors->attached(), array('Test'));
  526. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  527. $Apple->Sample->Behaviors->attach('Test');
  528. $this->assertIdentical($Apple->Sample->Behaviors->enabled('Test'), true);
  529. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  530. $Apple->Behaviors->enable('Test');
  531. $this->assertIdentical($Apple->Behaviors->attached('Test'), true);
  532. $this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
  533. $Apple->Behaviors->disable('Test');
  534. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  535. $Apple->Behaviors->attach('Test', array('enabled' => true));
  536. $this->assertIdentical($Apple->Behaviors->enabled(), array('Test'));
  537. $Apple->Behaviors->attach('Test', array('enabled' => false));
  538. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  539. $Apple->Behaviors->detach('Test');
  540. $this->assertIdentical($Apple->Behaviors->enabled(), array());
  541. }
  542. /**
  543. * testBehaviorFindCallbacks method
  544. *
  545. * @access public
  546. * @return void
  547. */
  548. function testBehaviorFindCallbacks() {
  549. $Apple = new Apple();
  550. $expected = $Apple->find('all');
  551. $Apple->Behaviors->attach('Test');
  552. $this->assertIdentical($Apple->find('all'), null);
  553. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off'));
  554. $this->assertIdentical($Apple->find('all'), $expected);
  555. $Apple->Behaviors->attach('Test', array('beforeFind' => 'test'));
  556. $this->assertIdentical($Apple->find('all'), $expected);
  557. $Apple->Behaviors->attach('Test', array('beforeFind' => 'modify'));
  558. $expected2 = array(
  559. array('Apple' => array('id' => '1', 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
  560. array('Apple' => array('id' => '2', 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
  561. array('Apple' => array('id' => '3', 'name' => 'green blue', 'mytime' => '22:57:17'))
  562. );
  563. $result = $Apple->find('all', array('conditions' => array('Apple.id <' => '4')));
  564. $this->assertEqual($result, $expected2);
  565. $Apple->Behaviors->disable('Test');
  566. $result = $Apple->find('all');
  567. $this->assertEqual($result, $expected);
  568. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'on'));
  569. $this->assertIdentical($Apple->find('all'), array());
  570. $Apple->Behaviors->attach('Test', array('afterFind' => 'off'));
  571. $this->assertEqual($Apple->find('all'), $expected);
  572. $Apple->Behaviors->attach('Test', array('afterFind' => 'test'));
  573. $this->assertEqual($Apple->find('all'), $expected);
  574. $Apple->Behaviors->attach('Test', array('afterFind' => 'test2'));
  575. $this->assertEqual($Apple->find('all'), $expected);
  576. $Apple->Behaviors->attach('Test', array('afterFind' => 'modify'));
  577. $expected = array(
  578. 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'),
  579. 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'),
  580. 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'),
  581. 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'),
  582. 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'),
  583. 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'),
  584. 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')
  585. );
  586. $this->assertEqual($Apple->find('all'), $expected);
  587. }
  588. /**
  589. * testBehaviorHasManyFindCallbacks method
  590. *
  591. * @access public
  592. * @return void
  593. */
  594. function testBehaviorHasManyFindCallbacks() {
  595. $Apple = new Apple();
  596. $Apple->unbindModel(array('hasOne' => array('Sample'), 'belongsTo' => array('Parent')), false);
  597. $expected = $Apple->find('all');
  598. $Apple->unbindModel(array('hasMany' => array('Child')));
  599. $wellBehaved = $Apple->find('all');
  600. $Apple->Child->Behaviors->attach('Test', array('afterFind' => 'modify'));
  601. $Apple->unbindModel(array('hasMany' => array('Child')));
  602. $this->assertIdentical($Apple->find('all'), $wellBehaved);
  603. $Apple->Child->Behaviors->attach('Test', array('before' => 'off'));
  604. $this->assertIdentical($Apple->find('all'), $expected);
  605. $Apple->Child->Behaviors->attach('Test', array('before' => 'test'));
  606. $this->assertIdentical($Apple->find('all'), $expected);
  607. $expected2 = array(
  608. array(
  609. 'Apple' => array('id' => 1),
  610. 'Child' => array(
  611. array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))),
  612. array(
  613. 'Apple' => array('id' => 2),
  614. 'Child' => array(
  615. array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17'),
  616. array('id' => 3, 'name' => 'green blue', 'mytime' => '22:57:17'),
  617. array('id' => 4, 'name' => 'Test Name', 'mytime' => '22:57:17'))),
  618. array(
  619. 'Apple' => array('id' => 3),
  620. 'Child' => array())
  621. );
  622. $Apple->Child->Behaviors->attach('Test', array('before' => 'modify'));
  623. $result = $Apple->find('all', array('fields' => array('Apple.id'), 'conditions' => array('Apple.id <' => '4')));
  624. //$this->assertEqual($result, $expected2);
  625. $Apple->Child->Behaviors->disable('Test');
  626. $result = $Apple->find('all');
  627. $this->assertEqual($result, $expected);
  628. $Apple->Child->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on'));
  629. //$this->assertIdentical($Apple->find('all'), array());
  630. $Apple->Child->Behaviors->attach('Test', array('after' => 'off'));
  631. $this->assertEqual($Apple->find('all'), $expected);
  632. $Apple->Child->Behaviors->attach('Test', array('after' => 'test'));
  633. $this->assertEqual($Apple->find('all'), $expected);
  634. $Apple->Child->Behaviors->attach('Test', array('after' => 'test2'));
  635. $this->assertEqual($Apple->find('all'), $expected);
  636. $Apple->Child->Behaviors->attach('Test', array('after' => 'modify'));
  637. $expected = array(
  638. 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'),
  639. 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'),
  640. 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'),
  641. 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'),
  642. 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'),
  643. 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'),
  644. 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')
  645. );
  646. //$this->assertEqual($Apple->find('all'), $expected);
  647. }
  648. /**
  649. * testBehaviorHasOneFindCallbacks method
  650. *
  651. * @access public
  652. * @return void
  653. */
  654. function testBehaviorHasOneFindCallbacks() {
  655. $Apple = new Apple();
  656. $Apple->unbindModel(array('hasMany' => array('Child'), 'belongsTo' => array('Parent')), false);
  657. $expected = $Apple->find('all');
  658. $Apple->unbindModel(array('hasOne' => array('Sample')));
  659. $wellBehaved = $Apple->find('all');
  660. $Apple->Sample->Behaviors->attach('Test');
  661. $Apple->unbindModel(array('hasOne' => array('Sample')));
  662. $this->assertIdentical($Apple->find('all'), $wellBehaved);
  663. $Apple->Sample->Behaviors->attach('Test', array('before' => 'off'));
  664. $this->assertIdentical($Apple->find('all'), $expected);
  665. $Apple->Sample->Behaviors->attach('Test', array('before' => 'test'));
  666. $this->assertIdentical($Apple->find('all'), $expected);
  667. $Apple->Sample->Behaviors->attach('Test', array('before' => 'modify'));
  668. $expected2 = array(
  669. array(
  670. 'Apple' => array('id' => 1),
  671. 'Child' => array(
  672. array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))),
  673. array(
  674. 'Apple' => array('id' => 2),
  675. 'Child' => array(
  676. array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17'),
  677. array('id' => 3, 'name' => 'green blue', 'mytime' => '22:57:17'),
  678. array('id' => 4, 'name' => 'Test Name', 'mytime' => '22:57:17'))),
  679. array(
  680. 'Apple' => array('id' => 3),
  681. 'Child' => array())
  682. );
  683. $result = $Apple->find('all', array('fields' => array('Apple.id'), 'conditions' => array('Apple.id <' => '4')));
  684. //$this->assertEqual($result, $expected2);
  685. $Apple->Sample->Behaviors->disable('Test');
  686. $result = $Apple->find('all');
  687. $this->assertEqual($result, $expected);
  688. $Apple->Sample->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on'));
  689. //$this->assertIdentical($Apple->find('all'), array());
  690. $Apple->Sample->Behaviors->attach('Test', array('after' => 'off'));
  691. $this->assertEqual($Apple->find('all'), $expected);
  692. $Apple->Sample->Behaviors->attach('Test', array('after' => 'test'));
  693. $this->assertEqual($Apple->find('all'), $expected);
  694. $Apple->Sample->Behaviors->attach('Test', array('after' => 'test2'));
  695. $this->assertEqual($Apple->find('all'), $expected);
  696. $Apple->Sample->Behaviors->attach('Test', array('after' => 'modify'));
  697. $expected = array(
  698. 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'),
  699. 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'),
  700. 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'),
  701. 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'),
  702. 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'),
  703. 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'),
  704. 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')
  705. );
  706. //$this->assertEqual($Apple->find('all'), $expected);
  707. }
  708. /**
  709. * testBehaviorBelongsToFindCallbacks method
  710. *
  711. * @access public
  712. * @return void
  713. */
  714. function testBehaviorBelongsToFindCallbacks() {
  715. $Apple = new Apple();
  716. $Apple->unbindModel(array('hasMany' => array('Child'), 'hasOne' => array('Sample')), false);
  717. $expected = $Apple->find('all');
  718. $Apple->unbindModel(array('belongsTo' => array('Parent')));
  719. $wellBehaved = $Apple->find('all');
  720. $Apple->Parent->Behaviors->attach('Test');
  721. $Apple->unbindModel(array('belongsTo' => array('Parent')));
  722. $this->assertIdentical($Apple->find('all'), $wellBehaved);
  723. $Apple->Parent->Behaviors->attach('Test', array('before' => 'off'));
  724. $this->assertIdentical($Apple->find('all'), $expected);
  725. $Apple->Parent->Behaviors->attach('Test', array('before' => 'test'));
  726. $this->assertIdentical($Apple->find('all'), $expected);
  727. $Apple->Parent->Behaviors->attach('Test', array('before' => 'modify'));
  728. $expected2 = array(
  729. array(
  730. 'Apple' => array('id' => 1),
  731. 'Parent' => array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
  732. array(
  733. 'Apple' => array('id' => 2),
  734. 'Parent' => array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
  735. array(
  736. 'Apple' => array('id' => 3),
  737. 'Parent' => array('id' => 2,'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))
  738. );
  739. $result = $Apple->find('all', array(
  740. 'fields' => array('Apple.id', 'Parent.id', 'Parent.name', 'Parent.mytime'),
  741. 'conditions' => array('Apple.id <' => '4')
  742. ));
  743. $this->assertEqual($result, $expected2);
  744. $Apple->Parent->Behaviors->disable('Test');
  745. $result = $Apple->find('all');
  746. $this->assertEqual($result, $expected);
  747. $Apple->Parent->Behaviors->attach('Test', array('after' => 'off'));
  748. $this->assertEqual($Apple->find('all'), $expected);
  749. $Apple->Parent->Behaviors->attach('Test', array('after' => 'test'));
  750. $this->assertEqual($Apple->find('all'), $expected);
  751. $Apple->Parent->Behaviors->attach('Test', array('after' => 'test2'));
  752. $this->assertEqual($Apple->find('all'), $expected);
  753. $Apple->Parent->Behaviors->attach('Test', array('after' => 'modify'));
  754. $expected = array(
  755. 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'),
  756. 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'),
  757. 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'),
  758. 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'),
  759. 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'),
  760. 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'),
  761. 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')
  762. );
  763. //$this->assertEqual($Apple->find('all'), $expected);
  764. }
  765. /**
  766. * testBehaviorSaveCallbacks method
  767. *
  768. * @access public
  769. * @return void
  770. */
  771. function testBehaviorSaveCallbacks() {
  772. $Sample = new Sample();
  773. $record = array('Sample' => array('apple_id' => 6, 'name' => 'sample99'));
  774. $Sample->Behaviors->attach('Test', array('beforeSave' => 'on'));
  775. $Sample->create();
  776. $this->assertIdentical($Sample->save($record), false);
  777. $Sample->Behaviors->attach('Test', array('beforeSave' => 'off'));
  778. $Sample->create();
  779. $this->assertIdentical($Sample->save($record), $record);
  780. $Sample->Behaviors->attach('Test', array('beforeSave' => 'test'));
  781. $Sample->create();
  782. $this->assertIdentical($Sample->save($record), $record);
  783. $Sample->Behaviors->attach('Test', array('beforeSave' => 'modify'));
  784. $expected = Set::insert($record, 'Sample.name', 'sample99 modified before');
  785. $Sample->create();
  786. $this->assertIdentical($Sample->save($record), $expected);
  787. $Sample->Behaviors->disable('Test');
  788. $this->assertIdentical($Sample->save($record), $record);
  789. $Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'on'));
  790. $expected = Set::merge($record, array('Sample' => array('aftersave' => 'modified after on create')));
  791. $Sample->create();
  792. $this->assertIdentical($Sample->save($record), $expected);
  793. $Sample->Behaviors->attach('Test', array('beforeSave' => 'modify', 'afterSave' => 'modify'));
  794. $expected = Set::merge($record, array('Sample' => array('name' => 'sample99 modified before modified after on create')));
  795. $Sample->create();
  796. $this->assertIdentical($Sample->save($record), $expected);
  797. $Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'test'));
  798. $Sample->create();
  799. $this->assertIdentical($Sample->save($record), $record);
  800. $Sample->Behaviors->attach('Test', array('afterSave' => 'test2'));
  801. $Sample->create();
  802. $this->assertIdentical($Sample->save($record), $record);
  803. $Sample->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'off'));
  804. $Sample->recursive = -1;
  805. $record2 = $Sample->read(null, 1);
  806. $Sample->Behaviors->attach('Test', array('afterSave' => 'on'));
  807. $expected = Set::merge($record2, array('Sample' => array('aftersave' => 'modified after')));
  808. $Sample->create();
  809. $this->assertIdentical($Sample->save($record2), $expected);
  810. $Sample->Behaviors->attach('Test', array('afterSave' => 'modify'));
  811. $expected = Set::merge($record2, array('Sample' => array('name' => 'sample1 modified after')));
  812. $Sample->create();
  813. $this->assertIdentical($Sample->save($record2), $expected);
  814. }
  815. /**
  816. * testBehaviorDeleteCallbacks method
  817. *
  818. * @access public
  819. * @return void
  820. */
  821. function testBehaviorDeleteCallbacks() {
  822. $Apple = new Apple();
  823. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'beforeDelete' => 'off'));
  824. $this->assertIdentical($Apple->delete(6), true);
  825. $Apple->Behaviors->attach('Test', array('beforeDelete' => 'on'));
  826. $this->assertIdentical($Apple->delete(4), false);
  827. $Apple->Behaviors->attach('Test', array('beforeDelete' => 'test2'));
  828. ob_start();
  829. $results = $Apple->delete(4);
  830. $this->assertIdentical(trim(ob_get_clean()), 'beforeDelete success (cascading)');
  831. $this->assertIdentical($results, true);
  832. ob_start();
  833. $results = $Apple->delete(3, false);
  834. $this->assertIdentical(trim(ob_get_clean()), 'beforeDelete success');
  835. $this->assertIdentical($results, true);
  836. $Apple->Behaviors->attach('Test', array('beforeDelete' => 'off', 'afterDelete' => 'on'));
  837. ob_start();
  838. $results = $Apple->delete(2, false);
  839. $this->assertIdentical(trim(ob_get_clean()), 'afterDelete success');
  840. $this->assertIdentical($results, true);
  841. }
  842. /**
  843. * testBehaviorOnErrorCallback method
  844. *
  845. * @access public
  846. * @return void
  847. */
  848. function testBehaviorOnErrorCallback() {
  849. $Apple = new Apple();
  850. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'onError' => 'on'));
  851. ob_start();
  852. $Apple->Behaviors->Test->onError($Apple, '');
  853. $this->assertIdentical(trim(ob_get_clean()), 'onError trigger success');
  854. }
  855. /**
  856. * testBehaviorValidateCallback method
  857. *
  858. * @access public
  859. * @return void
  860. */
  861. function testBehaviorValidateCallback() {
  862. $Apple = new Apple();
  863. $Apple->Behaviors->attach('Test');
  864. $this->assertIdentical($Apple->validates(), true);
  865. $Apple->Behaviors->attach('Test', array('validate' => 'on'));
  866. $this->assertIdentical($Apple->validates(), false);
  867. $this->assertIdentical($Apple->validationErrors, array('name' => true));
  868. $Apple->Behaviors->attach('Test', array('validate' => 'stop'));
  869. $this->assertIdentical($Apple->validates(), false);
  870. $this->assertIdentical($Apple->validationErrors, array('name' => true));
  871. $Apple->Behaviors->attach('Test', array('validate' => 'whitelist'));
  872. $Apple->validates();
  873. $this->assertIdentical($Apple->whitelist, array());
  874. $Apple->whitelist = array('unknown');
  875. $Apple->validates();
  876. $this->assertIdentical($Apple->whitelist, array('unknown', 'name'));
  877. }
  878. /**
  879. * testBehaviorValidateMethods method
  880. *
  881. * @access public
  882. * @return void
  883. */
  884. function testBehaviorValidateMethods() {
  885. $Apple = new Apple();
  886. $Apple->Behaviors->attach('Test');
  887. $Apple->validate['color'] = 'validateField';
  888. $result = $Apple->save(array('name' => 'Genetically Modified Apple', 'color' => 'Orange'));
  889. $this->assertEqual(array_keys($result['Apple']), array('name', 'color', 'modified', 'created'));
  890. $Apple->create();
  891. $result = $Apple->save(array('name' => 'Regular Apple', 'color' => 'Red'));
  892. $this->assertFalse($result);
  893. }
  894. /**
  895. * testBehaviorMethodDispatching method
  896. *
  897. * @access public
  898. * @return void
  899. */
  900. function testBehaviorMethodDispatching() {
  901. $Apple = new Apple();
  902. $Apple->Behaviors->attach('Test');
  903. $expected = 'working';
  904. $this->assertEqual($Apple->testMethod(), $expected);
  905. $this->assertEqual($Apple->Behaviors->dispatchMethod($Apple, 'testMethod'), $expected);
  906. $result = $Apple->Behaviors->dispatchMethod($Apple, 'wtf');
  907. $this->assertEqual($result, array('unhandled'));
  908. $result = $Apple->{'look for the remote'}('in the couch');
  909. $expected = "Item.name = 'the remote' AND Location.name = 'the couch'";
  910. $this->assertEqual($result, $expected);
  911. $result = $Apple->{'look for THE REMOTE'}('in the couch');
  912. $expected = "Item.name = 'THE REMOTE' AND Location.name = 'the couch'";
  913. $this->assertEqual($result, $expected, 'Mapped method was lowercased.');
  914. }
  915. /**
  916. * testBehaviorMethodDispatchingWithData method
  917. *
  918. * @access public
  919. * @return void
  920. */
  921. function testBehaviorMethodDispatchingWithData() {
  922. $Apple = new Apple();
  923. $Apple->Behaviors->attach('Test');
  924. $Apple->set('field', 'value');
  925. $this->assertTrue($Apple->testData());
  926. $this->assertTrue($Apple->data['Apple']['field_2']);
  927. $this->assertTrue($Apple->testData('one', 'two', 'three', 'four', 'five', 'six'));
  928. }
  929. /**
  930. * undocumented function
  931. *
  932. * @return void
  933. */
  934. public function testBindModelCallsInBehaviors() {
  935. $this->loadFixtures('Article', 'Comment');
  936. // hasMany
  937. $Article = new Article();
  938. $Article->unbindModel(array('hasMany' => array('Comment')));
  939. $result = $Article->find('first');
  940. $this->assertFalse(array_key_exists('Comment', $result));
  941. $Article->Behaviors->attach('Test4');
  942. $result = $Article->find('first');
  943. $this->assertTrue(array_key_exists('Comment', $result));
  944. // belongsTo
  945. $Article->unbindModel(array('belongsTo' => array('User')));
  946. $result = $Article->find('first');
  947. $this->assertFalse(array_key_exists('User', $result));
  948. $Article->Behaviors->attach('Test5');
  949. $result = $Article->find('first');
  950. $this->assertTrue(array_key_exists('User', $result));
  951. // hasAndBelongsToMany
  952. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
  953. $result = $Article->find('first');
  954. $this->assertFalse(array_key_exists('Tag', $result));
  955. $Article->Behaviors->attach('Test6');
  956. $result = $Article->find('first');
  957. $this->assertTrue(array_key_exists('Comment', $result));
  958. // hasOne
  959. $Comment = new Comment();
  960. $Comment->unbindModel(array('hasOne' => array('Attachment')));
  961. $result = $Comment->find('first');
  962. $this->assertFalse(array_key_exists('Attachment', $result));
  963. $Comment->Behaviors->attach('Test7');
  964. $result = $Comment->find('first');
  965. $this->assertTrue(array_key_exists('Attachment', $result));
  966. }
  967. /**
  968. * Test attach and detaching
  969. *
  970. * @access public
  971. * @return void
  972. */
  973. function testBehaviorAttachAndDetach() {
  974. $Sample = new Sample();
  975. $Sample->actsAs = array('Test3' => array('bar'), 'Test2' => array('foo', 'bar'));
  976. $Sample->Behaviors->init($Sample->alias, $Sample->actsAs);
  977. $Sample->Behaviors->attach('Test2');
  978. $Sample->Behaviors->detach('Test3');
  979. $Sample->Behaviors->trigger('beforeTest', array(&$Sample));
  980. }
  981. /**
  982. * test that hasMethod works with basic functions.
  983. *
  984. * @return void
  985. */
  986. function testHasMethodBasic() {
  987. $Sample = new Sample();
  988. $Collection = new BehaviorCollection();
  989. $Collection->init('Sample', array('Test', 'Test2'));
  990. $this->assertTrue($Collection->hasMethod('testMethod'));
  991. $this->assertTrue($Collection->hasMethod('resolveMethod'));
  992. $this->assertFalse($Collection->hasMethod('No method'));
  993. }
  994. /**
  995. * test that hasMethod works with mapped methods.
  996. *
  997. * @return void
  998. */
  999. function testHasMethodMappedMethods() {
  1000. $Sample = new Sample();
  1001. $Collection = new BehaviorCollection();
  1002. $Collection->init('Sample', array('Test', 'Test2'));
  1003. $this->assertTrue($Collection->hasMethod('look for the remote in the couch'));
  1004. $this->assertTrue($Collection->hasMethod('mappingRobotOnTheRoof'));
  1005. }
  1006. /**
  1007. * test hasMethod returrning a 'callback'
  1008. *
  1009. * @return void
  1010. */
  1011. function testHasMethodAsCallback() {
  1012. $Sample = new Sample();
  1013. $Collection = new BehaviorCollection();
  1014. $Collection->init('Sample', array('Test', 'Test2'));
  1015. $result = $Collection->hasMethod('testMethod', true);
  1016. $expected = array('Test', 'testMethod');
  1017. $this->assertEquals($expected, $result);
  1018. $result = $Collection->hasMethod('resolveMethod', true);
  1019. $expected = array('Test2', 'resolveMethod');
  1020. $this->assertEquals($expected, $result);
  1021. $result = $Collection->hasMethod('mappingRobotOnTheRoof', true);
  1022. $expected = array('Test2', 'mapped', 'mappingRobotOnTheRoof');
  1023. $this->assertEquals($expected, $result);
  1024. }
  1025. }