ModelTaskTest.php 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. <?php
  2. /**
  3. * CakePHP : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP Project
  12. * @since 1.2.6
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell\Task;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Model\Model;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\Shell\Task\ModelTask;
  21. use Cake\Shell\Task\TemplateTask;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Utility\ClassRegistry;
  24. use Cake\Utility\Inflector;
  25. /**
  26. * ModelTaskTest class
  27. */
  28. class ModelTaskTest extends TestCase {
  29. /**
  30. * fixtures
  31. *
  32. * @var array
  33. */
  34. public $fixtures = array(
  35. 'core.bake_articles', 'core.bake_comments', 'core.bake_articles_bake_tags',
  36. 'core.bake_tags', 'core.users', 'core.category_threads', 'core.number_trees',
  37. 'core.counter_cache_users', 'core.counter_cache_posts',
  38. 'core.tags', 'core.articles_tags'
  39. );
  40. /**
  41. * setUp method
  42. *
  43. * @return void
  44. */
  45. public function setUp() {
  46. parent::setUp();
  47. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  48. $this->Task = $this->getMock('Cake\Shell\Task\ModelTask',
  49. array('in', 'err', 'createFile', '_stop', '_checkUnitTest'),
  50. array($io)
  51. );
  52. $this->Task->connection = 'test';
  53. $this->_setupOtherMocks();
  54. TableRegistry::clear();
  55. }
  56. /**
  57. * Setup a mock that has out mocked. Normally this is not used as it makes $this->at() really tricky.
  58. *
  59. * @return void
  60. */
  61. protected function _useMockedOut() {
  62. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  63. $this->Task = $this->getMock('Cake\Shell\Task\ModelTask',
  64. array('in', 'out', 'err', 'hr', 'createFile', '_stop', '_checkUnitTest'),
  65. array($io)
  66. );
  67. $this->_setupOtherMocks();
  68. }
  69. /**
  70. * sets up the rest of the dependencies for Model Task
  71. *
  72. * @return void
  73. */
  74. protected function _setupOtherMocks() {
  75. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  76. $this->Task->Fixture = $this->getMock('Cake\Shell\Task\FixtureTask', [], [$io]);
  77. $this->Task->Test = $this->getMock('Cake\Shell\Task\FixtureTask', [], [$io]);
  78. $this->Task->Template = new TemplateTask($io);
  79. $this->Task->Template->interactive = false;
  80. $this->Task->name = 'Model';
  81. }
  82. /**
  83. * tearDown method
  84. *
  85. * @return void
  86. */
  87. public function tearDown() {
  88. parent::tearDown();
  89. unset($this->Task);
  90. $this->fixtureManager->shutDown();
  91. }
  92. /**
  93. * Test that listAll uses the connection property
  94. *
  95. * @return void
  96. */
  97. public function testListAllConnection() {
  98. $this->_useMockedOut();
  99. $this->Task->connection = 'test';
  100. $result = $this->Task->listAll();
  101. $this->assertContains('bake_articles', $result);
  102. $this->assertContains('bake_articles_bake_tags', $result);
  103. $this->assertContains('bake_tags', $result);
  104. $this->assertContains('bake_comments', $result);
  105. $this->assertContains('category_threads', $result);
  106. }
  107. /**
  108. * Test getName() method.
  109. *
  110. * @return void
  111. */
  112. public function testGetTable() {
  113. $result = $this->Task->getTable('BakeArticle');
  114. $this->assertEquals('bake_articles', $result);
  115. $result = $this->Task->getTable('BakeArticles');
  116. $this->assertEquals('bake_articles', $result);
  117. $this->Task->params['table'] = 'bake_articles';
  118. $result = $this->Task->getTable('Article');
  119. $this->assertEquals('bake_articles', $result);
  120. }
  121. /**
  122. * Test getting the a table class.
  123. *
  124. * @return void
  125. */
  126. public function testGetTableObject() {
  127. $result = $this->Task->getTableObject('Article', 'bake_articles');
  128. $this->assertInstanceOf('Cake\ORM\Table', $result);
  129. $this->assertEquals('bake_articles', $result->table());
  130. $this->assertEquals('Article', $result->alias());
  131. }
  132. /**
  133. * Test getAssociations with off flag.
  134. *
  135. * @return void
  136. */
  137. public function testGetAssociationsNoFlag() {
  138. $this->Task->params['no-associations'] = true;
  139. $articles = TableRegistry::get('BakeArticle');
  140. $this->assertEquals([], $this->Task->getAssociations($articles));
  141. }
  142. /**
  143. * Test applying associations.
  144. *
  145. * @return void
  146. */
  147. public function testApplyAssociations() {
  148. $articles = TableRegistry::get('BakeArticles');
  149. $assocs = [
  150. 'belongsTo' => [
  151. [
  152. 'alias' => 'BakeUsers',
  153. 'foreignKey' => 'bake_user_id',
  154. ],
  155. ],
  156. 'hasMany' => [
  157. [
  158. 'alias' => 'BakeComments',
  159. 'foreignKey' => 'bake_article_id',
  160. ],
  161. ],
  162. 'belongsToMany' => [
  163. [
  164. 'alias' => 'BakeTags',
  165. 'foreignKey' => 'bake_article_id',
  166. 'joinTable' => 'bake_articles_bake_tags',
  167. 'targetForeignKey' => 'bake_tag_id',
  168. ],
  169. ],
  170. ];
  171. $original = $articles->associations()->keys();
  172. $this->assertEquals([], $original);
  173. $this->Task->applyAssociations($articles, $assocs);
  174. $new = $articles->associations()->keys();
  175. $expected = ['bakeusers', 'bakecomments', 'baketags'];
  176. $this->assertEquals($expected, $new);
  177. }
  178. /**
  179. * Test applying associations does nothing on a concrete class
  180. *
  181. * @return void
  182. */
  183. public function testApplyAssociationsConcreteClass() {
  184. Configure::write('App.namespace', 'TestApp');
  185. $articles = TableRegistry::get('Articles');
  186. $assocs = [
  187. 'belongsTo' => [
  188. [
  189. 'alias' => 'BakeUsers',
  190. 'foreignKey' => 'bake_user_id',
  191. ],
  192. ],
  193. 'hasMany' => [
  194. [
  195. 'alias' => 'BakeComments',
  196. 'foreignKey' => 'bake_article_id',
  197. ],
  198. ],
  199. 'belongsToMany' => [
  200. [
  201. 'alias' => 'BakeTags',
  202. 'foreignKey' => 'bake_article_id',
  203. 'joinTable' => 'bake_articles_bake_tags',
  204. 'targetForeignKey' => 'bake_tag_id',
  205. ],
  206. ],
  207. ];
  208. $original = $articles->associations()->keys();
  209. $this->Task->applyAssociations($articles, $assocs);
  210. $new = $articles->associations()->keys();
  211. $this->assertEquals($original, $new);
  212. }
  213. /**
  214. * Test getAssociations
  215. *
  216. * @return void
  217. */
  218. public function testGetAssociations() {
  219. $articles = TableRegistry::get('BakeArticles');
  220. $result = $this->Task->getAssociations($articles);
  221. $expected = [
  222. 'belongsTo' => [
  223. [
  224. 'alias' => 'BakeUsers',
  225. 'foreignKey' => 'bake_user_id'
  226. ],
  227. ],
  228. 'hasMany' => [
  229. [
  230. 'alias' => 'BakeComments',
  231. 'foreignKey' => 'bake_article_id',
  232. ],
  233. ],
  234. 'belongsToMany' => [
  235. [
  236. 'alias' => 'BakeTags',
  237. 'foreignKey' => 'bake_article_id',
  238. 'joinTable' => 'bake_articles_bake_tags',
  239. 'targetForeignKey' => 'bake_tag_id',
  240. ],
  241. ],
  242. ];
  243. $this->assertEquals($expected, $result);
  244. }
  245. /**
  246. * Test getAssociations in a plugin
  247. *
  248. * @return void
  249. */
  250. public function testGetAssociationsPlugin() {
  251. $articles = TableRegistry::get('BakeArticles');
  252. $this->Task->plugin = 'TestPlugin';
  253. $result = $this->Task->getAssociations($articles);
  254. $expected = [
  255. 'belongsTo' => [
  256. [
  257. 'alias' => 'BakeUsers',
  258. 'className' => 'TestPlugin.BakeUsers',
  259. 'foreignKey' => 'bake_user_id'
  260. ],
  261. ],
  262. 'hasMany' => [
  263. [
  264. 'alias' => 'BakeComments',
  265. 'className' => 'TestPlugin.BakeComments',
  266. 'foreignKey' => 'bake_article_id',
  267. ],
  268. ],
  269. 'belongsToMany' => [
  270. [
  271. 'alias' => 'BakeTags',
  272. 'className' => 'TestPlugin.BakeTags',
  273. 'foreignKey' => 'bake_article_id',
  274. 'joinTable' => 'bake_articles_bake_tags',
  275. 'targetForeignKey' => 'bake_tag_id',
  276. ],
  277. ],
  278. ];
  279. $this->assertEquals($expected, $result);
  280. }
  281. /**
  282. * test that belongsTo generation works.
  283. *
  284. * @return void
  285. */
  286. public function testBelongsToGeneration() {
  287. $model = TableRegistry::get('BakeComments');
  288. $result = $this->Task->findBelongsTo($model, []);
  289. $expected = [
  290. 'belongsTo' => [
  291. [
  292. 'alias' => 'BakeArticles',
  293. 'foreignKey' => 'bake_article_id'
  294. ],
  295. [
  296. 'alias' => 'BakeUsers',
  297. 'foreignKey' => 'bake_user_id'
  298. ],
  299. ]
  300. ];
  301. $this->assertEquals($expected, $result);
  302. $model = TableRegistry::get('CategoryThreads');
  303. $result = $this->Task->findBelongsTo($model, array());
  304. $expected = [
  305. 'belongsTo' => [
  306. [
  307. 'alias' => 'ParentCategoryThreads',
  308. 'className' => 'CategoryThreads',
  309. 'foreignKey' => 'parent_id'
  310. ],
  311. ]
  312. ];
  313. $this->assertEquals($expected, $result);
  314. $this->Task->plugin = 'Blog';
  315. $result = $this->Task->findBelongsTo($model, array());
  316. $expected = [
  317. 'belongsTo' => [
  318. [
  319. 'alias' => 'ParentCategoryThreads',
  320. 'className' => 'Blog.CategoryThreads',
  321. 'foreignKey' => 'parent_id'
  322. ],
  323. ]
  324. ];
  325. $this->assertEquals($expected, $result);
  326. }
  327. /**
  328. * Test that belongsTo generation works for models with composite
  329. * primary keys
  330. *
  331. * @return void
  332. */
  333. public function testBelongsToGenerationCompositeKey() {
  334. $model = TableRegistry::get('ArticlesTags');
  335. $result = $this->Task->findBelongsTo($model, []);
  336. $expected = [
  337. 'belongsTo' => [
  338. [
  339. 'alias' => 'Articles',
  340. 'foreignKey' => 'article_id'
  341. ],
  342. [
  343. 'alias' => 'Tags',
  344. 'foreignKey' => 'tag_id'
  345. ]
  346. ]
  347. ];
  348. $this->assertEquals($expected, $result);
  349. }
  350. /**
  351. * Test that belongsTo generation ignores _id mid-column
  352. *
  353. * @return void
  354. */
  355. public function testBelongsToGenerationIdMidColumn() {
  356. $model = TableRegistry::get('Articles');
  357. $model->schema([
  358. 'id' => ['type' => 'integer'],
  359. 'thing_id_field' => ['type' => 'integer'],
  360. ]);
  361. $result = $this->Task->findBelongsTo($model, []);
  362. $this->assertEquals([], $result);
  363. }
  364. /**
  365. * test that hasOne and/or hasMany relations are generated properly.
  366. *
  367. * @return void
  368. */
  369. public function testHasManyGeneration() {
  370. $this->Task->connection = 'test';
  371. $model = TableRegistry::get('BakeArticles');
  372. $result = $this->Task->findHasMany($model, []);
  373. $expected = [
  374. 'hasMany' => [
  375. [
  376. 'alias' => 'BakeComments',
  377. 'foreignKey' => 'bake_article_id',
  378. ],
  379. ],
  380. ];
  381. $this->assertEquals($expected, $result);
  382. $model = TableRegistry::get('CategoryThreads');
  383. $result = $this->Task->findHasMany($model, []);
  384. $expected = [
  385. 'hasMany' => [
  386. [
  387. 'alias' => 'ChildCategoryThreads',
  388. 'className' => 'CategoryThreads',
  389. 'foreignKey' => 'parent_id',
  390. ],
  391. ]
  392. ];
  393. $this->assertEquals($expected, $result);
  394. $this->Task->plugin = 'Blog';
  395. $result = $this->Task->findHasMany($model, array());
  396. $expected = [
  397. 'hasMany' => [
  398. [
  399. 'alias' => 'ChildCategoryThreads',
  400. 'className' => 'Blog.CategoryThreads',
  401. 'foreignKey' => 'parent_id'
  402. ],
  403. ]
  404. ];
  405. $this->assertEquals($expected, $result);
  406. }
  407. /**
  408. * Test that HABTM generation works
  409. *
  410. * @return void
  411. */
  412. public function testHasAndBelongsToManyGeneration() {
  413. $this->Task->connection = 'test';
  414. $model = TableRegistry::get('BakeArticles');
  415. $result = $this->Task->findBelongsToMany($model, []);
  416. $expected = [
  417. 'belongsToMany' => [
  418. [
  419. 'alias' => 'BakeTags',
  420. 'foreignKey' => 'bake_article_id',
  421. 'joinTable' => 'bake_articles_bake_tags',
  422. 'targetForeignKey' => 'bake_tag_id',
  423. ],
  424. ],
  425. ];
  426. $this->assertEquals($expected, $result);
  427. }
  428. /**
  429. * Test getting accessible fields.
  430. *
  431. * @return void
  432. */
  433. public function testGetFields() {
  434. $model = TableRegistry::get('BakeArticles');
  435. $result = $this->Task->getFields($model);
  436. $expected = [
  437. 'bake_user_id',
  438. 'title',
  439. 'body',
  440. 'published',
  441. ];
  442. $this->assertEquals($expected, $result);
  443. }
  444. /**
  445. * Test getting accessible fields includes associations.
  446. *
  447. * @return void
  448. */
  449. public function testGetFieldsAssociations() {
  450. $model = TableRegistry::get('BakeArticles');
  451. $model->belongsToMany('BakeTags');
  452. $model->belongsTo('BakeAuthors');
  453. $model->hasMany('BakeComments');
  454. $result = $this->Task->getFields($model);
  455. $this->assertContains('bake_tags', $result);
  456. $this->assertContains('bake_comments', $result);
  457. $this->assertContains('bake_author', $result);
  458. }
  459. /**
  460. * Test getting field with the no- option
  461. *
  462. * @return void
  463. */
  464. public function testGetFieldsDisabled() {
  465. $model = TableRegistry::get('BakeArticles');
  466. $this->Task->params['no-fields'] = true;
  467. $result = $this->Task->getFields($model);
  468. $this->assertEquals([], $result);
  469. }
  470. /**
  471. * Test getting field with a whitelist
  472. *
  473. * @return void
  474. */
  475. public function testGetFieldsWhiteList() {
  476. $model = TableRegistry::get('BakeArticles');
  477. $this->Task->params['fields'] = 'id, title , , body , created';
  478. $result = $this->Task->getFields($model);
  479. $expected = [
  480. 'id',
  481. 'title',
  482. 'body',
  483. 'created',
  484. ];
  485. $this->assertEquals($expected, $result);
  486. }
  487. /**
  488. * Test getting hidden fields.
  489. *
  490. * @return void
  491. */
  492. public function testGetHiddenFields() {
  493. $model = TableRegistry::get('Users');
  494. $result = $this->Task->getHiddenFields($model);
  495. $expected = [
  496. 'password',
  497. ];
  498. $this->assertEquals($expected, $result);
  499. }
  500. /**
  501. * Test getting hidden field with the no- option
  502. *
  503. * @return void
  504. */
  505. public function testGetHiddenFieldsDisabled() {
  506. $model = TableRegistry::get('Users');
  507. $this->Task->params['no-hidden'] = true;
  508. $result = $this->Task->getHiddenFields($model);
  509. $this->assertEquals([], $result);
  510. }
  511. /**
  512. * Test getting hidden field with a whitelist
  513. *
  514. * @return void
  515. */
  516. public function testGetHiddenFieldsWhiteList() {
  517. $model = TableRegistry::get('Users');
  518. $this->Task->params['hidden'] = 'id, title , , body , created';
  519. $result = $this->Task->getHiddenFields($model);
  520. $expected = [
  521. 'id',
  522. 'title',
  523. 'body',
  524. 'created',
  525. ];
  526. $this->assertEquals($expected, $result);
  527. }
  528. /**
  529. * Test getting primary key
  530. *
  531. * @return void
  532. */
  533. public function testGetPrimaryKey() {
  534. $model = TableRegistry::get('BakeArticles');
  535. $result = $this->Task->getPrimaryKey($model);
  536. $expected = ['id'];
  537. $this->assertEquals($expected, $result);
  538. $this->Task->params['primary-key'] = 'id, , account_id';
  539. $result = $this->Task->getPrimaryKey($model);
  540. $expected = ['id', 'account_id'];
  541. $this->assertEquals($expected, $result);
  542. }
  543. /**
  544. * test getting validation rules with the no-validation rule.
  545. *
  546. * @return void
  547. */
  548. public function testGetValidationDisabled() {
  549. $model = TableRegistry::get('BakeArticles');
  550. $this->Task->params['no-validation'] = true;
  551. $result = $this->Task->getValidation($model);
  552. $this->assertEquals([], $result);
  553. }
  554. /**
  555. * test getting validation rules.
  556. *
  557. * @return void
  558. */
  559. public function testGetValidation() {
  560. $model = TableRegistry::get('BakeArticles');
  561. $result = $this->Task->getValidation($model);
  562. $expected = [
  563. 'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  564. 'title' => ['valid' => ['rule' => false, 'allowEmpty' => false]],
  565. 'body' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  566. 'published' => ['valid' => ['rule' => 'boolean', 'allowEmpty' => true]],
  567. 'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
  568. ];
  569. $this->assertEquals($expected, $result);
  570. $model = TableRegistry::get('BakeComments');
  571. $result = $this->Task->getValidation($model);
  572. $expected = [
  573. 'bake_article_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  574. 'bake_user_id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => false]],
  575. 'comment' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  576. 'published' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  577. 'otherid' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']]
  578. ];
  579. $this->assertEquals($expected, $result);
  580. }
  581. /**
  582. * Tests that a username column will get a validateUnique rule applied
  583. *
  584. * @return void
  585. */
  586. public function testGetValidationWithUnique() {
  587. $model = TableRegistry::get('Users');
  588. $result = $this->Task->getValidation($model);
  589. $expected = [
  590. 'password' => ['valid' => ['rule' => false, 'allowEmpty' => true]],
  591. 'id' => ['valid' => ['rule' => 'numeric', 'allowEmpty' => 'create']],
  592. 'username' => [
  593. 'valid' => [
  594. 'rule' => false,
  595. 'allowEmpty' => true
  596. ],
  597. 'unique' => [
  598. 'rule' => 'validateUnique',
  599. 'provider' => 'table'
  600. ]
  601. ]
  602. ];
  603. $this->assertEquals($expected, $result);
  604. }
  605. /**
  606. * test non interactive doActsAs
  607. *
  608. * @return void
  609. */
  610. public function testGetBehaviors() {
  611. $model = TableRegistry::get('NumberTrees');
  612. $result = $this->Task->getBehaviors($model);
  613. $this->assertEquals(['Tree' => []], $result);
  614. $model = TableRegistry::get('BakeArticles');
  615. $result = $this->Task->getBehaviors($model);
  616. $this->assertEquals(['Timestamp' => []], $result);
  617. TableRegistry::clear();
  618. TableRegistry::get('Users', [
  619. 'table' => 'counter_cache_users'
  620. ]);
  621. $model = TableRegistry::get('Posts', [
  622. 'table' => 'counter_cache_posts'
  623. ]);
  624. $result = $this->Task->getBehaviors($model);
  625. $expected = [
  626. 'CounterCache' => ["'Users' => ['post_count']"]
  627. ];
  628. $this->assertEquals($expected, $result);
  629. }
  630. /**
  631. * Test getDisplayField() method.
  632. *
  633. * @return void
  634. */
  635. public function testGetDisplayField() {
  636. $model = TableRegistry::get('BakeArticles');
  637. $result = $this->Task->getDisplayField($model);
  638. $this->assertEquals('title', $result);
  639. $this->Task->params['display-field'] = 'custom';
  640. $result = $this->Task->getDisplayField($model);
  641. $this->assertEquals('custom', $result);
  642. }
  643. /**
  644. * Ensure that the fixture object is correctly called.
  645. *
  646. * @return void
  647. */
  648. public function testBakeFixture() {
  649. $this->Task->plugin = 'TestPlugin';
  650. $this->Task->Fixture->expects($this->at(0))
  651. ->method('bake')
  652. ->with('BakeArticle', 'bake_articles');
  653. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  654. $this->assertEquals($this->Task->plugin, $this->Task->Fixture->plugin);
  655. $this->assertEquals($this->Task->connection, $this->Task->Fixture->connection);
  656. $this->assertEquals($this->Task->interactive, $this->Task->Fixture->interactive);
  657. }
  658. /**
  659. * Ensure that the fixture baking can be disabled
  660. *
  661. * @return void
  662. */
  663. public function testBakeFixtureDisabled() {
  664. $this->Task->params['no-fixture'] = true;
  665. $this->Task->plugin = 'TestPlugin';
  666. $this->Task->Fixture->expects($this->never())
  667. ->method('bake');
  668. $this->Task->bakeFixture('BakeArticle', 'bake_articles');
  669. }
  670. /**
  671. * Ensure that the test object is correctly called.
  672. *
  673. * @return void
  674. */
  675. public function testBakeTest() {
  676. $this->Task->plugin = 'TestPlugin';
  677. $this->Task->Test->expects($this->at(0))
  678. ->method('bake')
  679. ->with('Table', 'BakeArticle');
  680. $this->Task->bakeTest('BakeArticle');
  681. $this->assertEquals($this->Task->plugin, $this->Task->Test->plugin);
  682. $this->assertEquals($this->Task->connection, $this->Task->Test->connection);
  683. $this->assertEquals($this->Task->interactive, $this->Task->Test->interactive);
  684. }
  685. /**
  686. * Ensure that test baking can be disabled.
  687. *
  688. * @return void
  689. */
  690. public function testBakeTestDisabled() {
  691. $this->Task->params['no-test'] = true;
  692. $this->Task->plugin = 'TestPlugin';
  693. $this->Task->Test->expects($this->never())
  694. ->method('bake');
  695. $this->Task->bakeTest('BakeArticle');
  696. }
  697. /**
  698. * test baking validation
  699. *
  700. * @return void
  701. */
  702. public function testBakeTableValidation() {
  703. $validation = [
  704. 'id' => [
  705. 'valid' => array(
  706. 'allowEmpty' => 'create',
  707. 'rule' => 'numeric',
  708. )
  709. ],
  710. 'name' => [
  711. 'valid' => [
  712. 'allowEmpty' => false,
  713. 'rule' => false,
  714. ]
  715. ],
  716. 'email' => [
  717. 'valid' => [
  718. 'allowEmpty' => true,
  719. 'rule' => 'email'
  720. ],
  721. 'unique' => [
  722. 'rule' => 'validateUnique',
  723. 'provider' => 'table'
  724. ]
  725. ]
  726. ];
  727. $model = TableRegistry::get('BakeArticles');
  728. $result = $this->Task->bakeTable($model, compact('validation'));
  729. $this->assertContains('namespace App\Model\Table;', $result);
  730. $this->assertContains('use Cake\ORM\Table;', $result);
  731. $this->assertContains('use Cake\Validation\Validator;', $result);
  732. $this->assertContains('class BakeArticlesTable extends Table {', $result);
  733. $this->assertContains('public function validationDefault(Validator $validator) {', $result);
  734. $this->assertContains("->add('id', 'valid', ['rule' => 'numeric'])", $result);
  735. $this->assertContains("->add('email', 'valid', ['rule' => 'email'])", $result);
  736. $this->assertContains(
  737. "->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table'])",
  738. $result);
  739. $this->assertContains("->allowEmpty('id', 'create')", $result);
  740. $this->assertContains("->allowEmpty('email')", $result);
  741. $this->assertContains("->validatePresence('name', 'create')", $result);
  742. }
  743. /**
  744. * test baking
  745. *
  746. * @return void
  747. */
  748. public function testBakeTableConfig() {
  749. $config = [
  750. 'table' => 'articles',
  751. 'primaryKey' => ['id'],
  752. 'displayField' => 'title',
  753. 'behaviors' => ['Timestamp' => ''],
  754. ];
  755. $model = TableRegistry::get('BakeArticles');
  756. $result = $this->Task->bakeTable($model, $config);
  757. $this->assertContains('public function initialize(array $config) {', $result);
  758. $this->assertContains("this->primaryKey('id');\n", $result);
  759. $this->assertContains("this->displayField('title');\n", $result);
  760. $this->assertContains("this->addBehavior('Timestamp');\n", $result);
  761. $this->assertContains("this->table('articles');\n", $result);
  762. $this->assertContains('use Cake\Validation\Validator;', $result);
  763. }
  764. /**
  765. * test baking relations
  766. *
  767. * @return void
  768. */
  769. public function testBakeTableRelations() {
  770. $associations = [
  771. 'belongsTo' => [
  772. [
  773. 'alias' => 'SomethingElse',
  774. 'foreignKey' => 'something_else_id',
  775. ],
  776. [
  777. 'alias' => 'BakeUser',
  778. 'foreignKey' => 'bake_user_id',
  779. ],
  780. ],
  781. 'hasMany' => [
  782. [
  783. 'alias' => 'BakeComment',
  784. 'foreignKey' => 'parent_id',
  785. ],
  786. ],
  787. 'belongsToMany' => [
  788. [
  789. 'alias' => 'BakeTag',
  790. 'foreignKey' => 'bake_article_id',
  791. 'joinTable' => 'bake_articles_bake_tags',
  792. 'targetForeignKey' => 'bake_tag_id',
  793. ],
  794. ]
  795. ];
  796. $model = TableRegistry::get('BakeArticles');
  797. $result = $this->Task->bakeTable($model, compact('associations'));
  798. $this->assertContains("this->hasMany('BakeComment', [", $result);
  799. $this->assertContains("this->belongsTo('SomethingElse', [", $result);
  800. $this->assertContains("this->belongsTo('BakeUser', [", $result);
  801. $this->assertContains("this->belongsToMany('BakeTag', [", $result);
  802. $this->assertContains("'joinTable' => 'bake_articles_bake_tags',", $result);
  803. }
  804. /**
  805. * test baking an entity class
  806. *
  807. * @return void
  808. */
  809. public function testBakeEntity() {
  810. $config = [
  811. 'fields' => []
  812. ];
  813. $model = TableRegistry::get('BakeArticles');
  814. $result = $this->Task->bakeEntity($model, $config);
  815. $this->assertContains('namespace App\Model\Entity;', $result);
  816. $this->assertContains('use Cake\ORM\Entity;', $result);
  817. $this->assertContains('class BakeArticle extends Entity {', $result);
  818. $this->assertNotContains('$_accessible', $result);
  819. }
  820. /**
  821. * test baking an entity class
  822. *
  823. * @return void
  824. */
  825. public function testBakeEntityFields() {
  826. $config = [
  827. 'fields' => ['title', 'body', 'published']
  828. ];
  829. $model = TableRegistry::get('BakeArticles');
  830. $result = $this->Task->bakeEntity($model, $config);
  831. $this->assertContains("protected \$_accessible = [", $result);
  832. $this->assertContains("'title' => true,", $result);
  833. $this->assertContains("'body' => true,", $result);
  834. $this->assertContains("'published' => true", $result);
  835. $this->assertNotContains("protected \$_hidden", $result);
  836. }
  837. /**
  838. * test baking an entity class sets hidden fields.
  839. *
  840. * @return void
  841. */
  842. public function testBakeEntityHidden() {
  843. $model = TableRegistry::get('BakeUsers');
  844. $config = [
  845. 'hidden' => ['password'],
  846. ];
  847. $result = $this->Task->bakeEntity($model, $config);
  848. $this->assertContains("protected \$_hidden = [", $result);
  849. $this->assertContains("'password'", $result);
  850. $this->assertNotContains("protected \$_accessible", $result);
  851. }
  852. /**
  853. * test bake() with a -plugin param
  854. *
  855. * @return void
  856. */
  857. public function testBakeTableWithPlugin() {
  858. $this->Task->plugin = 'ControllerTest';
  859. // fake plugin path
  860. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  861. $path = $this->_normalizePath(APP . 'Plugin/ControllerTest/src/Model/Table/BakeArticlesTable.php');
  862. $this->Task->expects($this->once())->method('createFile')
  863. ->with($path, $this->logicalAnd(
  864. $this->stringContains('namespace ControllerTest\\Model\\Table;'),
  865. $this->stringContains('use Cake\\ORM\\Table;'),
  866. $this->stringContains('class BakeArticlesTable extends Table {')
  867. ));
  868. $model = TableRegistry::get('BakeArticles');
  869. $this->Task->bakeTable($model);
  870. }
  871. /**
  872. * test bake() with a -plugin param
  873. *
  874. * @return void
  875. */
  876. public function testBakeEntityWithPlugin() {
  877. $this->Task->plugin = 'ControllerTest';
  878. // fake plugin path
  879. Plugin::load('ControllerTest', array('path' => APP . 'Plugin' . DS . 'ControllerTest' . DS));
  880. $path = APP . 'Plugin' . DS . 'ControllerTest' . DS . 'src' . DS . 'Model' . DS . 'Entity' . DS . 'BakeArticle.php';
  881. $path = $this->_normalizePath($path);
  882. $this->Task->expects($this->once())->method('createFile')
  883. ->with($path, $this->logicalAnd(
  884. $this->stringContains('namespace ControllerTest\\Model\\Entity;'),
  885. $this->stringContains('use Cake\\ORM\\Entity;'),
  886. $this->stringContains('class BakeArticle extends Entity {')
  887. ));
  888. $model = TableRegistry::get('BakeArticles');
  889. $this->Task->bakeEntity($model);
  890. }
  891. /**
  892. * test that execute with no args
  893. *
  894. * @return void
  895. */
  896. public function testMainNoArgs() {
  897. $this->_useMockedOut();
  898. $this->Task->connection = 'test';
  899. $this->Task->path = '/my/path/';
  900. $this->Task->expects($this->at(0))
  901. ->method('out')
  902. ->with($this->stringContains('Choose a model to bake from the following:'));
  903. $this->Task->main();
  904. }
  905. /**
  906. * test that execute passes runs bake depending with named model.
  907. *
  908. * @return void
  909. */
  910. public function testMainWithNamedModel() {
  911. $this->Task->connection = 'test';
  912. $tableFile = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  913. $this->Task->expects($this->at(0))
  914. ->method('createFile')
  915. ->with($tableFile, $this->stringContains('class BakeArticlesTable extends Table'));
  916. $entityFile = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  917. $this->Task->expects($this->at(1))
  918. ->method('createFile')
  919. ->with($entityFile, $this->stringContains('class BakeArticle extends Entity'));
  920. $this->Task->main('BakeArticles');
  921. }
  922. /**
  923. * data provider for testMainWithNamedModelVariations
  924. *
  925. * @return void
  926. */
  927. public static function nameVariations() {
  928. return array(
  929. array('BakeArticles'), array('BakeArticle'), array('bake_article'), array('bake_articles')
  930. );
  931. }
  932. /**
  933. * test that execute passes with different inflections of the same name.
  934. *
  935. * @dataProvider nameVariations
  936. * @return void
  937. */
  938. public function testMainWithNamedModelVariations($name) {
  939. $this->Task->connection = 'test';
  940. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  941. $this->Task->expects($this->at(0))
  942. ->method('createFile')
  943. ->with($filename, $this->stringContains('class BakeArticlesTable extends Table {'));
  944. $this->Task->main($name);
  945. }
  946. /**
  947. * test that execute runs all() when args[0] = all
  948. *
  949. * @return void
  950. */
  951. public function testMainIntoAll() {
  952. $count = count($this->Task->listAll());
  953. if ($count != count($this->fixtures)) {
  954. $this->markTestSkipped('Additional tables detected.');
  955. }
  956. $this->Task->connection = 'test';
  957. $this->Task->Fixture->expects($this->exactly($count))
  958. ->method('bake');
  959. $this->Task->Test->expects($this->exactly($count))
  960. ->method('bake');
  961. $filename = $this->_normalizePath(APP . 'Model/Table/ArticlesTagsTable.php');
  962. $this->Task->expects($this->at(1))
  963. ->method('createFile')
  964. ->with($filename, $this->stringContains('class ArticlesTagsTable extends'));
  965. $filename = $this->_normalizePath(APP . 'Model/Entity/ArticlesTag.php');
  966. $this->Task->expects($this->at(2))
  967. ->method('createFile')
  968. ->with($filename, $this->stringContains('class ArticlesTag extends'));
  969. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesTable.php');
  970. $this->Task->expects($this->at(3))
  971. ->method('createFile')
  972. ->with($filename, $this->stringContains('class BakeArticlesTable extends'));
  973. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  974. $this->Task->expects($this->at(4))
  975. ->method('createFile')
  976. ->with($filename, $this->stringContains('class BakeArticle extends'));
  977. $filename = $this->_normalizePath(APP . 'Model/Table/BakeArticlesBakeTagsTable.php');
  978. $this->Task->expects($this->at(5))
  979. ->method('createFile')
  980. ->with($filename, $this->stringContains('class BakeArticlesBakeTagsTable extends'));
  981. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  982. $this->Task->expects($this->at(6))
  983. ->method('createFile')
  984. ->with($filename, $this->stringContains('class BakeArticlesBakeTag extends'));
  985. $filename = $this->_normalizePath(APP . 'Model/Table/BakeCommentsTable.php');
  986. $this->Task->expects($this->at(7))
  987. ->method('createFile')
  988. ->with($filename, $this->stringContains('class BakeCommentsTable extends'));
  989. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  990. $this->Task->expects($this->at(8))
  991. ->method('createFile')
  992. ->with($filename, $this->stringContains('class BakeComment extends'));
  993. $filename = $this->_normalizePath(APP . 'Model/Table/BakeTagsTable.php');
  994. $this->Task->expects($this->at(9))
  995. ->method('createFile')
  996. ->with($filename, $this->stringContains('class BakeTagsTable extends'));
  997. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeTag.php');
  998. $this->Task->expects($this->at(10))
  999. ->method('createFile')
  1000. ->with($filename, $this->stringContains('class BakeTag extends'));
  1001. $filename = $this->_normalizePath(APP . 'Model/Table/CategoryThreadsTable.php');
  1002. $this->Task->expects($this->at(11))
  1003. ->method('createFile')
  1004. ->with($filename, $this->stringContains('class CategoryThreadsTable extends'));
  1005. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  1006. $this->Task->expects($this->at(12))
  1007. ->method('createFile')
  1008. ->with($filename, $this->stringContains('class CategoryThread extends'));
  1009. $this->Task->all();
  1010. }
  1011. /**
  1012. * test that skipTables changes how all() works.
  1013. *
  1014. * @return void
  1015. */
  1016. public function testSkipTablesAndAll() {
  1017. $count = count($this->Task->listAll('test'));
  1018. if ($count != count($this->fixtures)) {
  1019. $this->markTestSkipped('Additional tables detected.');
  1020. }
  1021. $this->Task->connection = 'test';
  1022. $this->Task->skipTables = ['articles_tags', 'bake_tags', 'counter_cache_posts'];
  1023. $this->Task->Fixture->expects($this->exactly(8))
  1024. ->method('bake');
  1025. $this->Task->Test->expects($this->exactly(8))
  1026. ->method('bake');
  1027. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticle.php');
  1028. $this->Task->expects($this->at(1))
  1029. ->method('createFile')
  1030. ->with($filename);
  1031. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeArticlesBakeTag.php');
  1032. $this->Task->expects($this->at(3))
  1033. ->method('createFile')
  1034. ->with($filename);
  1035. $filename = $this->_normalizePath(APP . 'Model/Entity/BakeComment.php');
  1036. $this->Task->expects($this->at(5))
  1037. ->method('createFile')
  1038. ->with($filename);
  1039. $filename = $this->_normalizePath(APP . 'Model/Entity/CategoryThread.php');
  1040. $this->Task->expects($this->at(7))
  1041. ->method('createFile')
  1042. ->with($filename);
  1043. $filename = $this->_normalizePath(APP . 'Model/Entity/CounterCacheUser.php');
  1044. $this->Task->expects($this->at(9))
  1045. ->method('createFile')
  1046. ->with($filename);
  1047. $filename = $this->_normalizePath(APP . 'Model/Entity/NumberTree.php');
  1048. $this->Task->expects($this->at(11))
  1049. ->method('createFile')
  1050. ->with($filename);
  1051. $this->Task->all();
  1052. }
  1053. }