BehaviorCollectionTest.php 36 KB

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