BehaviorCollectionTest.php 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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->attached());
  403. $Apple->Behaviors->load('Translate', array('enabled' => false));
  404. $this->assertTrue($Apple->Behaviors->attached('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->attached());
  413. $Apple->Behaviors->load('Test', array('className' => 'TestAlias', 'somesetting' => true));
  414. $this->assertSame(array('Test'), $Apple->Behaviors->attached());
  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->attached();
  425. $this->assertEquals(array('Test', 'SomeOther'), $result, 'attached() 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->attached());
  437. $Apple->Behaviors->attach('Test', array('key' => 'value'));
  438. $this->assertSame(array('Test'), $Apple->Behaviors->attached());
  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->attached(), array());
  444. $Apple->Sample->Behaviors->attach('Test', array('key2' => 'value2'));
  445. $this->assertSame($Apple->Sample->Behaviors->attached(), 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->attached());
  492. $Apple->Behaviors->detach('Plugin.Test');
  493. $this->assertEquals(array(), $Apple->Behaviors->attached());
  494. $Apple->Behaviors->attach('Plugin.Test');
  495. $this->assertTrue(isset($Apple->Behaviors->Test), 'Missing behavior');
  496. $this->assertEquals(array('Test'), $Apple->Behaviors->attached());
  497. $Apple->Behaviors->detach('Test');
  498. $this->assertEquals(array(), $Apple->Behaviors->attached());
  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. $expected = $Apple->find('all');
  518. $this->assertSame($Apple->Behaviors->enabled(), array());
  519. $Apple->Behaviors->init('Apple', array('Test' => array('key' => 'value')));
  520. $this->assertSame($Apple->Behaviors->enabled(), array('Test'));
  521. $Apple->Behaviors->disable('Test');
  522. $this->assertSame(array('Test'), $Apple->Behaviors->attached());
  523. $this->assertSame($Apple->Behaviors->enabled(), array());
  524. $Apple->Sample->Behaviors->attach('Test');
  525. $this->assertSame($Apple->Sample->Behaviors->enabled('Test'), true);
  526. $this->assertSame($Apple->Behaviors->enabled(), array());
  527. $Apple->Behaviors->enable('Test');
  528. $this->assertSame($Apple->Behaviors->attached('Test'), true);
  529. $this->assertSame($Apple->Behaviors->enabled(), array('Test'));
  530. $Apple->Behaviors->disable('Test');
  531. $this->assertSame($Apple->Behaviors->enabled(), array());
  532. $Apple->Behaviors->attach('Test', array('enabled' => true));
  533. $this->assertSame($Apple->Behaviors->enabled(), array('Test'));
  534. $Apple->Behaviors->attach('Test', array('enabled' => false));
  535. $this->assertSame($Apple->Behaviors->enabled(), array());
  536. $Apple->Behaviors->detach('Test');
  537. $this->assertSame($Apple->Behaviors->enabled(), array());
  538. }
  539. /**
  540. * testBehaviorFindCallbacks method
  541. *
  542. * @return void
  543. */
  544. public function testBehaviorFindCallbacks() {
  545. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  546. $Apple = new Apple();
  547. $expected = $Apple->find('all');
  548. $Apple->Behaviors->attach('Test');
  549. $this->assertSame($Apple->find('all'), null);
  550. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off'));
  551. $this->assertSame($expected, $Apple->find('all'));
  552. $Apple->Behaviors->attach('Test', array('beforeFind' => 'test'));
  553. $this->assertSame($expected, $Apple->find('all'));
  554. $Apple->Behaviors->attach('Test', array('beforeFind' => 'modify'));
  555. $expected2 = array(
  556. array('Apple' => array('id' => '1', 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
  557. array('Apple' => array('id' => '2', 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
  558. array('Apple' => array('id' => '3', 'name' => 'green blue', 'mytime' => '22:57:17'))
  559. );
  560. $result = $Apple->find('all', array('conditions' => array('Apple.id <' => '4')));
  561. $this->assertEquals($expected2, $result);
  562. $Apple->Behaviors->disable('Test');
  563. $result = $Apple->find('all');
  564. $this->assertEquals($expected, $result);
  565. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'on'));
  566. $this->assertSame($Apple->find('all'), array());
  567. $Apple->Behaviors->attach('Test', array('afterFind' => 'off'));
  568. $this->assertEquals($expected, $Apple->find('all'));
  569. $Apple->Behaviors->attach('Test', array('afterFind' => 'test'));
  570. $this->assertEquals($expected, $Apple->find('all'));
  571. $Apple->Behaviors->attach('Test', array('afterFind' => 'test2'));
  572. $this->assertEquals($expected, $Apple->find('all'));
  573. $Apple->Behaviors->attach('Test', array('afterFind' => 'modify'));
  574. $expected = array(
  575. 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'),
  576. 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'),
  577. 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'),
  578. 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'),
  579. 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'),
  580. 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'),
  581. 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')
  582. );
  583. $this->assertEquals($expected, $Apple->find('all'));
  584. }
  585. /**
  586. * testBehaviorHasManyFindCallbacks method
  587. *
  588. * @return void
  589. */
  590. public function testBehaviorHasManyFindCallbacks() {
  591. $Apple = new Apple();
  592. $Apple->unbindModel(array('hasOne' => array('Sample'), 'belongsTo' => array('Parent')), false);
  593. $expected = $Apple->find('all');
  594. $Apple->unbindModel(array('hasMany' => array('Child')));
  595. $wellBehaved = $Apple->find('all');
  596. $Apple->Child->Behaviors->attach('Test', array('afterFind' => 'modify'));
  597. $Apple->unbindModel(array('hasMany' => array('Child')));
  598. $this->assertSame($Apple->find('all'), $wellBehaved);
  599. $Apple->Child->Behaviors->attach('Test', array('before' => 'off'));
  600. $this->assertSame($expected, $Apple->find('all'));
  601. $Apple->Child->Behaviors->attach('Test', array('before' => 'test'));
  602. $this->assertSame($expected, $Apple->find('all'));
  603. $expected2 = array(
  604. array(
  605. 'Apple' => array('id' => 1),
  606. 'Child' => array(
  607. array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))),
  608. array(
  609. 'Apple' => array('id' => 2),
  610. 'Child' => array(
  611. array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17'),
  612. array('id' => 3, 'name' => 'green blue', 'mytime' => '22:57:17'),
  613. array('id' => 4, 'name' => 'Test Name', 'mytime' => '22:57:17'))),
  614. array(
  615. 'Apple' => array('id' => 3),
  616. 'Child' => array())
  617. );
  618. $Apple->Child->Behaviors->attach('Test', array('before' => 'modify'));
  619. $result = $Apple->find('all', array('fields' => array('Apple.id'), 'conditions' => array('Apple.id <' => '4')));
  620. $Apple->Child->Behaviors->disable('Test');
  621. $result = $Apple->find('all');
  622. $this->assertEquals($expected, $result);
  623. $Apple->Child->Behaviors->attach('Test', array('before' => 'off', 'after' => 'on'));
  624. $Apple->Child->Behaviors->attach('Test', array('after' => 'off'));
  625. $this->assertEquals($expected, $Apple->find('all'));
  626. $Apple->Child->Behaviors->attach('Test', array('after' => 'test'));
  627. $this->assertEquals($expected, $Apple->find('all'));
  628. $Apple->Child->Behaviors->attach('Test', array('after' => 'test2'));
  629. $this->assertEquals($expected, $Apple->find('all'));
  630. }
  631. /**
  632. * testBehaviorHasOneFindCallbacks method
  633. *
  634. * @return void
  635. */
  636. public function testBehaviorHasOneFindCallbacks() {
  637. $Apple = new Apple();
  638. $Apple->unbindModel(array('hasMany' => array('Child'), 'belongsTo' => array('Parent')), false);
  639. $expected = $Apple->find('all');
  640. $Apple->unbindModel(array('hasOne' => array('Sample')));
  641. $wellBehaved = $Apple->find('all');
  642. $Apple->Sample->Behaviors->attach('Test');
  643. $Apple->unbindModel(array('hasOne' => array('Sample')));
  644. $this->assertSame($Apple->find('all'), $wellBehaved);
  645. $Apple->Sample->Behaviors->attach('Test', array('before' => 'off'));
  646. $this->assertSame($expected, $Apple->find('all'));
  647. $Apple->Sample->Behaviors->attach('Test', array('before' => 'test'));
  648. $this->assertSame($expected, $Apple->find('all'));
  649. $Apple->Sample->Behaviors->disable('Test');
  650. $result = $Apple->find('all');
  651. $this->assertEquals($expected, $result);
  652. $Apple->Sample->Behaviors->attach('Test', array('after' => 'off'));
  653. $this->assertEquals($expected, $Apple->find('all'));
  654. $Apple->Sample->Behaviors->attach('Test', array('after' => 'test'));
  655. $this->assertEquals($expected, $Apple->find('all'));
  656. $Apple->Sample->Behaviors->attach('Test', array('after' => 'test2'));
  657. $this->assertEquals($expected, $Apple->find('all'));
  658. }
  659. /**
  660. * testBehaviorBelongsToFindCallbacks method
  661. *
  662. * @return void
  663. */
  664. public function testBehaviorBelongsToFindCallbacks() {
  665. $this->skipIf($this->db instanceof Sqlserver, 'This test is not compatible with SQL Server.');
  666. $Apple = new Apple();
  667. $Apple->unbindModel(array('hasMany' => array('Child'), 'hasOne' => array('Sample')), false);
  668. $expected = $Apple->find('all');
  669. $Apple->unbindModel(array('belongsTo' => array('Parent')));
  670. $wellBehaved = $Apple->find('all');
  671. $Apple->Parent->Behaviors->attach('Test');
  672. $Apple->unbindModel(array('belongsTo' => array('Parent')));
  673. $this->assertSame($Apple->find('all'), $wellBehaved);
  674. $Apple->Parent->Behaviors->attach('Test', array('before' => 'off'));
  675. $this->assertSame($expected, $Apple->find('all'));
  676. $Apple->Parent->Behaviors->attach('Test', array('before' => 'test'));
  677. $this->assertSame($expected, $Apple->find('all'));
  678. $Apple->Parent->Behaviors->attach('Test', array('before' => 'modify'));
  679. $expected2 = array(
  680. array(
  681. 'Apple' => array('id' => 1),
  682. 'Parent' => array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17')),
  683. array(
  684. 'Apple' => array('id' => 2),
  685. 'Parent' => array('id' => 1, 'name' => 'Red Apple 1', 'mytime' => '22:57:17')),
  686. array(
  687. 'Apple' => array('id' => 3),
  688. 'Parent' => array('id' => 2, 'name' => 'Bright Red Apple', 'mytime' => '22:57:17'))
  689. );
  690. $result2 = $Apple->find('all', array(
  691. 'fields' => array('Apple.id', 'Parent.id', 'Parent.name', 'Parent.mytime'),
  692. 'conditions' => array('Apple.id <' => '4')
  693. ));
  694. $this->assertEquals($expected2, $result2);
  695. $Apple->Parent->Behaviors->disable('Test');
  696. $result = $Apple->find('all');
  697. $this->assertEquals($expected, $result);
  698. $Apple->Parent->Behaviors->attach('Test', array('after' => 'off'));
  699. $this->assertEquals($expected, $Apple->find('all'));
  700. $Apple->Parent->Behaviors->attach('Test', array('after' => 'test'));
  701. $this->assertEquals($expected, $Apple->find('all'));
  702. $Apple->Parent->Behaviors->attach('Test', array('after' => 'test2'));
  703. $this->assertEquals($expected, $Apple->find('all'));
  704. }
  705. /**
  706. * testBehaviorSaveCallbacks method
  707. *
  708. * @return void
  709. */
  710. public function testBehaviorSaveCallbacks() {
  711. $Sample = new Sample();
  712. $record = array('Sample' => array('apple_id' => 6, 'name' => 'sample99'));
  713. $Sample->Behaviors->attach('Test', array('beforeSave' => 'on'));
  714. $Sample->create();
  715. $this->assertSame(false, $Sample->save($record));
  716. $Sample->Behaviors->attach('Test', array('beforeSave' => 'off'));
  717. $Sample->create();
  718. $result = $Sample->save($record);
  719. $expected = $record;
  720. $expected['Sample']['id'] = $Sample->id;
  721. $this->assertSame($expected, $result);
  722. $Sample->Behaviors->attach('Test', array('beforeSave' => 'test'));
  723. $Sample->create();
  724. $result = $Sample->save($record);
  725. $expected = $record;
  726. $expected['Sample']['id'] = $Sample->id;
  727. $this->assertSame($expected, $result);
  728. $Sample->Behaviors->attach('Test', array('beforeSave' => 'modify'));
  729. $expected = Hash::insert($record, 'Sample.name', 'sample99 modified before');
  730. $Sample->create();
  731. $result = $Sample->save($record);
  732. $expected['Sample']['id'] = $Sample->id;
  733. $this->assertSame($expected, $result);
  734. $Sample->Behaviors->disable('Test');
  735. $this->assertSame($record, $Sample->save($record));
  736. $Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'on'));
  737. $expected = Hash::merge($record, array('Sample' => array('aftersave' => 'modified after on create')));
  738. $Sample->create();
  739. $result = $Sample->save($record);
  740. $expected['Sample']['id'] = $Sample->id;
  741. $this->assertEquals($expected, $result);
  742. $Sample->Behaviors->attach('Test', array('beforeSave' => 'modify', 'afterSave' => 'modify'));
  743. $expected = Hash::merge($record, array('Sample' => array('name' => 'sample99 modified before modified after on create')));
  744. $Sample->create();
  745. $result = $Sample->save($record);
  746. $expected['Sample']['id'] = $Sample->id;
  747. $this->assertSame($expected, $result);
  748. $Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'test'));
  749. $Sample->create();
  750. $expected = $record;
  751. unset($expected['Sample']['name']);
  752. $result = $Sample->save($record);
  753. $expected['Sample']['id'] = $Sample->id;
  754. $this->assertSame($expected, $result);
  755. $Sample->Behaviors->attach('Test', array('afterSave' => 'test2'));
  756. $Sample->create();
  757. $expected = $record;
  758. $result = $Sample->save($record);
  759. $expected['Sample']['id'] = $Sample->id;
  760. $this->assertSame($expected, $result);
  761. $Sample->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'off'));
  762. $Sample->recursive = -1;
  763. $record2 = $Sample->read(null, 1);
  764. $Sample->Behaviors->attach('Test', array('afterSave' => 'on'));
  765. $expected = Hash::merge($record2, array('Sample' => array('aftersave' => 'modified after')));
  766. $Sample->create();
  767. $this->assertSame($expected, $Sample->save($record2));
  768. $Sample->Behaviors->attach('Test', array('afterSave' => 'modify'));
  769. $expected = Hash::merge($record2, array('Sample' => array('name' => 'sample1 modified after')));
  770. $Sample->create();
  771. $this->assertSame($expected, $Sample->save($record2));
  772. }
  773. /**
  774. * testBehaviorDeleteCallbacks method
  775. *
  776. * @return void
  777. */
  778. public function testBehaviorDeleteCallbacks() {
  779. $Apple = new Apple();
  780. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'beforeDelete' => 'off'));
  781. $this->assertSame($Apple->delete(6), true);
  782. $Apple->Behaviors->attach('Test', array('beforeDelete' => 'on'));
  783. $this->assertSame($Apple->delete(4), false);
  784. $Apple->Behaviors->attach('Test', array('beforeDelete' => 'test2'));
  785. ob_start();
  786. $results = $Apple->delete(4);
  787. $this->assertSame(trim(ob_get_clean()), 'beforeDelete success (cascading)');
  788. $this->assertSame($results, true);
  789. ob_start();
  790. $results = $Apple->delete(3, false);
  791. $this->assertSame(trim(ob_get_clean()), 'beforeDelete success');
  792. $this->assertSame($results, true);
  793. $Apple->Behaviors->attach('Test', array('beforeDelete' => 'off', 'afterDelete' => 'on'));
  794. ob_start();
  795. $results = $Apple->delete(2, false);
  796. $this->assertSame(trim(ob_get_clean()), 'afterDelete success');
  797. $this->assertSame($results, true);
  798. }
  799. /**
  800. * testBehaviorOnErrorCallback method
  801. *
  802. * @return void
  803. */
  804. public function testBehaviorOnErrorCallback() {
  805. $Apple = new Apple();
  806. $Apple->Behaviors->attach('Test', array('beforeFind' => 'off', 'onError' => 'on'));
  807. ob_start();
  808. $Apple->Behaviors->Test->onError($Apple, '');
  809. $this->assertSame(trim(ob_get_clean()), 'onError trigger success');
  810. }
  811. /**
  812. * testBehaviorValidateCallback method
  813. *
  814. * @return void
  815. */
  816. public function testBehaviorValidateCallback() {
  817. $Apple = new Apple();
  818. $Apple->Behaviors->attach('Test');
  819. $this->assertSame($Apple->validates(), true);
  820. $Apple->Behaviors->attach('Test', array('validate' => 'on'));
  821. $this->assertSame($Apple->validates(), false);
  822. $this->assertSame($Apple->validationErrors, array('name' => array(true)));
  823. $Apple->Behaviors->attach('Test', array('validate' => 'stop'));
  824. $this->assertSame($Apple->validates(), false);
  825. $this->assertSame($Apple->validationErrors, array('name' => array(true, true)));
  826. $Apple->Behaviors->attach('Test', array('validate' => 'whitelist'));
  827. $Apple->validates();
  828. $this->assertSame($Apple->whitelist, array());
  829. $Apple->whitelist = array('unknown');
  830. $Apple->validates();
  831. $this->assertSame($Apple->whitelist, array('unknown', 'name'));
  832. }
  833. /**
  834. * testBehaviorValidateAfterCallback method
  835. *
  836. * @return void
  837. */
  838. public function testBehaviorValidateAfterCallback() {
  839. $Apple = new Apple();
  840. $Apple->Behaviors->attach('Test');
  841. $this->assertSame($Apple->validates(), true);
  842. $Apple->Behaviors->attach('Test', array('afterValidate' => 'on'));
  843. $this->assertSame($Apple->validates(), true);
  844. $this->assertSame($Apple->validationErrors, array());
  845. $Apple->Behaviors->attach('Test', array('afterValidate' => 'test'));
  846. $Apple->data = array('bar');
  847. $Apple->validates();
  848. $this->assertEquals(array('foo'), $Apple->data);
  849. }
  850. /**
  851. * testBehaviorValidateMethods method
  852. *
  853. * @return void
  854. */
  855. public function testBehaviorValidateMethods() {
  856. $Apple = new Apple();
  857. $Apple->Behaviors->attach('Test');
  858. $Apple->validate['color'] = 'validateField';
  859. $result = $Apple->save(array('name' => 'Genetically Modified Apple', 'color' => 'Orange'));
  860. $this->assertEquals(array('name', 'color', 'modified', 'created', 'id'), array_keys($result['Apple']));
  861. $Apple->create();
  862. $result = $Apple->save(array('name' => 'Regular Apple', 'color' => 'Red'));
  863. $this->assertFalse($result);
  864. }
  865. /**
  866. * testBehaviorMethodDispatching method
  867. *
  868. * @return void
  869. */
  870. public function testBehaviorMethodDispatching() {
  871. $Apple = new Apple();
  872. $Apple->Behaviors->attach('Test');
  873. $expected = 'working';
  874. $this->assertEquals($expected, $Apple->testMethod());
  875. $this->assertEquals($expected, $Apple->Behaviors->dispatchMethod($Apple, 'testMethod'));
  876. $result = $Apple->Behaviors->dispatchMethod($Apple, 'wtf');
  877. $this->assertEquals(array('unhandled'), $result);
  878. $result = $Apple->{'look for the remote'}('in the couch');
  879. $expected = "Item.name = 'the remote' AND Location.name = 'the couch'";
  880. $this->assertEquals($expected, $result);
  881. $result = $Apple->{'look for THE REMOTE'}('in the couch');
  882. $expected = "Item.name = 'THE REMOTE' AND Location.name = 'the couch'";
  883. $this->assertEquals($expected, $result, 'Mapped method was lowercased.');
  884. }
  885. /**
  886. * testBehaviorMethodDispatchingWithData method
  887. *
  888. * @return void
  889. */
  890. public function testBehaviorMethodDispatchingWithData() {
  891. $Apple = new Apple();
  892. $Apple->Behaviors->attach('Test');
  893. $Apple->set('field', 'value');
  894. $this->assertTrue($Apple->testData());
  895. $this->assertTrue($Apple->data['Apple']['field_2']);
  896. $this->assertTrue($Apple->testData('one', 'two', 'three', 'four', 'five', 'six'));
  897. }
  898. /**
  899. * undocumented function
  900. *
  901. * @return void
  902. */
  903. public function testBindModelCallsInBehaviors() {
  904. // hasMany
  905. $Article = new Article();
  906. $Article->unbindModel(array('hasMany' => array('Comment')));
  907. $result = $Article->find('first');
  908. $this->assertFalse(array_key_exists('Comment', $result));
  909. $Article->Behaviors->attach('Test4');
  910. $result = $Article->find('first');
  911. $this->assertTrue(array_key_exists('Comment', $result));
  912. // belongsTo
  913. $Article->unbindModel(array('belongsTo' => array('User')));
  914. $result = $Article->find('first');
  915. $this->assertFalse(array_key_exists('User', $result));
  916. $Article->Behaviors->attach('Test5');
  917. $result = $Article->find('first');
  918. $this->assertTrue(array_key_exists('User', $result));
  919. // hasAndBelongsToMany
  920. $Article->unbindModel(array('hasAndBelongsToMany' => array('Tag')));
  921. $result = $Article->find('first');
  922. $this->assertFalse(array_key_exists('Tag', $result));
  923. $Article->Behaviors->attach('Test6');
  924. $result = $Article->find('first');
  925. $this->assertTrue(array_key_exists('Comment', $result));
  926. // hasOne
  927. $Comment = new Comment();
  928. $Comment->unbindModel(array('hasOne' => array('Attachment')));
  929. $result = $Comment->find('first');
  930. $this->assertFalse(array_key_exists('Attachment', $result));
  931. $Comment->Behaviors->attach('Test7');
  932. $result = $Comment->find('first');
  933. $this->assertTrue(array_key_exists('Attachment', $result));
  934. }
  935. /**
  936. * Test attach and detaching
  937. *
  938. * @return void
  939. */
  940. public function testBehaviorAttachAndDetach() {
  941. $Sample = new Sample();
  942. $Sample->actsAs = array('Test3' => array('bar'), 'Test2' => array('foo', 'bar'));
  943. $Sample->Behaviors->init($Sample->alias, $Sample->actsAs);
  944. $Sample->Behaviors->attach('Test2');
  945. $Sample->Behaviors->detach('Test3');
  946. $Sample->Behaviors->trigger('beforeTest', array(&$Sample));
  947. }
  948. /**
  949. * test that hasMethod works with basic functions.
  950. *
  951. * @return void
  952. */
  953. public function testHasMethodBasic() {
  954. $Sample = new Sample();
  955. $Collection = new BehaviorCollection();
  956. $Collection->init('Sample', array('Test', 'Test2'));
  957. $this->assertTrue($Collection->hasMethod('testMethod'));
  958. $this->assertTrue($Collection->hasMethod('resolveMethod'));
  959. $this->assertFalse($Collection->hasMethod('No method'));
  960. }
  961. /**
  962. * test that hasMethod works with mapped methods.
  963. *
  964. * @return void
  965. */
  966. public function testHasMethodMappedMethods() {
  967. $Sample = new Sample();
  968. $Collection = new BehaviorCollection();
  969. $Collection->init('Sample', array('Test', 'Test2'));
  970. $this->assertTrue($Collection->hasMethod('look for the remote in the couch'));
  971. $this->assertTrue($Collection->hasMethod('mappingRobotOnTheRoof'));
  972. }
  973. /**
  974. * test hasMethod returning a 'callback'
  975. *
  976. * @return void
  977. */
  978. public function testHasMethodAsCallback() {
  979. $Sample = new Sample();
  980. $Collection = new BehaviorCollection();
  981. $Collection->init('Sample', array('Test', 'Test2'));
  982. $result = $Collection->hasMethod('testMethod', true);
  983. $expected = array('Test', 'testMethod');
  984. $this->assertEquals($expected, $result);
  985. $result = $Collection->hasMethod('resolveMethod', true);
  986. $expected = array('Test2', 'resolveMethod');
  987. $this->assertEquals($expected, $result);
  988. $result = $Collection->hasMethod('mappingRobotOnTheRoof', true);
  989. $expected = array('Test2', 'mapped', 'mappingRobotOnTheRoof');
  990. $this->assertEquals($expected, $result);
  991. }
  992. }