ModelTaskTest.php 31 KB

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