ModelTaskTest.php 29 KB

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