BehaviorCollectionTest.php 36 KB

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