ViewTaskTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Shell\Task;
  16. use Cake\Controller\Controller;
  17. use Cake\Core\Configure;
  18. use Cake\Core\Plugin;
  19. use Cake\ORM\Table;
  20. use Cake\ORM\TableRegistry;
  21. use Cake\Shell\Task\TemplateTask;
  22. use Cake\Shell\Task\ViewTask;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Test View Task Comment Model
  26. */
  27. class ViewTaskCommentsTable extends Table {
  28. public function initialize(array $config) {
  29. $this->table('comments');
  30. $this->belongsTo('Articles', [
  31. 'foreignKey' => 'article_id'
  32. ]);
  33. }
  34. }
  35. /**
  36. * Test View Task Article Model
  37. */
  38. class ViewTaskArticlesTable extends Table {
  39. public function intialize(array $config) {
  40. $this->table('articles');
  41. }
  42. }
  43. /**
  44. * Test View Task Comments Controller
  45. */
  46. class ViewTaskCommentsController extends Controller {
  47. public $modelClass = 'Cake\Test\TestCase\Shell\Task\ViewTaskCommentsTable';
  48. /**
  49. * Testing public controller action
  50. *
  51. * @return void
  52. */
  53. public function index() {
  54. }
  55. /**
  56. * Testing public controller action
  57. *
  58. * @return void
  59. */
  60. public function add() {
  61. }
  62. }
  63. /**
  64. * ViewTaskTest class
  65. */
  66. class ViewTaskTest extends TestCase {
  67. /**
  68. * Fixtures
  69. *
  70. * @var array
  71. */
  72. public $fixtures = array(
  73. 'core.articles', 'core.posts', 'core.comments',
  74. 'core.articles_tags',
  75. 'core.tags',
  76. 'core.test_plugin_comments',
  77. 'core.category_threads',
  78. );
  79. /**
  80. * setUp method
  81. *
  82. * Ensure that the default template is used
  83. *
  84. * @return void
  85. */
  86. public function setUp() {
  87. parent::setUp();
  88. Configure::write('App.namespace', 'TestApp');
  89. $this->_setupTask(['in', 'err', 'error', 'createFile', '_stop']);
  90. TableRegistry::get('ViewTaskComments', [
  91. 'className' => __NAMESPACE__ . '\ViewTaskCommentsTable',
  92. ]);
  93. }
  94. /**
  95. * Generate the mock objects used in tests.
  96. *
  97. * @return void
  98. */
  99. protected function _setupTask($methods) {
  100. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  101. $this->Task = $this->getMock('Cake\Shell\Task\ViewTask',
  102. $methods,
  103. [$io]
  104. );
  105. $this->Task->Template = new TemplateTask($io);
  106. $this->Task->Model = $this->getMock('Cake\Shell\Task\ModelTask', [], [$io]);
  107. $this->Task->Template->params['template'] = 'default';
  108. $this->Task->Template->templatePaths = ['default' => CAKE . 'Template/Bake/default/'];
  109. }
  110. /**
  111. * tearDown method
  112. *
  113. * @return void
  114. */
  115. public function tearDown() {
  116. parent::tearDown();
  117. TableRegistry::clear();
  118. unset($this->Task);
  119. }
  120. /**
  121. * Test the controller() method.
  122. *
  123. * @return void
  124. */
  125. public function testController() {
  126. $this->Task->controller('Comments');
  127. $this->assertEquals('Comments', $this->Task->controllerName);
  128. $this->assertEquals(
  129. 'TestApp\Controller\CommentsController',
  130. $this->Task->controllerClass
  131. );
  132. }
  133. /**
  134. * Test the controller() method.
  135. *
  136. * @dataProvider nameVariations
  137. * @return void
  138. */
  139. public function testControllerVariations($name) {
  140. $this->Task->controller($name);
  141. $this->assertEquals('ViewTaskComments', $this->Task->controllerName);
  142. }
  143. /**
  144. * Test controller method with plugins.
  145. *
  146. * @return void
  147. */
  148. public function testControllerPlugin() {
  149. $this->Task->params['plugin'] = 'TestPlugin';
  150. $this->Task->controller('Tests');
  151. $this->assertEquals('Tests', $this->Task->controllerName);
  152. $this->assertEquals(
  153. 'TestPlugin\Controller\TestsController',
  154. $this->Task->controllerClass
  155. );
  156. }
  157. /**
  158. * Test controller method with prefixes.
  159. *
  160. * @return void
  161. */
  162. public function testControllerPrefix() {
  163. $this->Task->params['prefix'] = 'Admin';
  164. $this->Task->controller('Posts');
  165. $this->assertEquals('Posts', $this->Task->controllerName);
  166. $this->assertEquals(
  167. 'TestApp\Controller\Admin\PostsController',
  168. $this->Task->controllerClass
  169. );
  170. $this->Task->params['plugin'] = 'TestPlugin';
  171. $this->Task->controller('Comments');
  172. $this->assertEquals('Comments', $this->Task->controllerName);
  173. $this->assertEquals(
  174. 'TestPlugin\Controller\Admin\CommentsController',
  175. $this->Task->controllerClass
  176. );
  177. }
  178. /**
  179. * test controller with a non-conventional controller name
  180. *
  181. * @return void
  182. */
  183. public function testControllerWithOverride() {
  184. $this->Task->controller('Comments', 'Posts');
  185. $this->assertEquals('Posts', $this->Task->controllerName);
  186. $this->assertEquals(
  187. 'TestApp\Controller\PostsController',
  188. $this->Task->controllerClass
  189. );
  190. }
  191. /**
  192. * Test the model() method.
  193. *
  194. * @return void
  195. */
  196. public function testModel() {
  197. $this->Task->model('Articles');
  198. $this->assertEquals('Articles', $this->Task->modelName);
  199. $this->Task->model('NotThere');
  200. $this->assertEquals('NotThere', $this->Task->modelName);
  201. }
  202. /**
  203. * Test model() method with plugins.
  204. *
  205. * @return void
  206. */
  207. public function testModelPlugin() {
  208. $this->Task->params['plugin'] = 'TestPlugin';
  209. $this->Task->model('TestPluginComments');
  210. $this->assertEquals(
  211. 'TestPlugin.TestPluginComments',
  212. $this->Task->modelName
  213. );
  214. }
  215. /**
  216. * Test getPath()
  217. *
  218. * @return void
  219. */
  220. public function testGetPath() {
  221. $this->Task->controllerName = 'Posts';
  222. $result = $this->Task->getPath();
  223. $this->assertPathEquals(APP . 'Template/Posts/', $result);
  224. $this->Task->params['prefix'] = 'admin';
  225. $result = $this->Task->getPath();
  226. $this->assertPathEquals(APP . 'Template/Admin/Posts/', $result);
  227. }
  228. /**
  229. * Test getPath with plugins.
  230. *
  231. * @return void
  232. */
  233. public function testGetPathPlugin() {
  234. $this->Task->controllerName = 'Posts';
  235. $pluginPath = APP . 'Plugin/TestPlugin/';
  236. Plugin::load('TestPlugin', array('path' => $pluginPath));
  237. $this->Task->params['plugin'] = $this->Task->plugin = 'TestPlugin';
  238. $result = $this->Task->getPath();
  239. $this->assertPathEquals($pluginPath . 'src/Template/Posts/', $result);
  240. $this->Task->params['prefix'] = 'admin';
  241. $result = $this->Task->getPath();
  242. $this->assertPathEquals($pluginPath . 'src/Template/Admin/Posts/', $result);
  243. Plugin::unload('TestPlugin');
  244. }
  245. /**
  246. * Test getContent and parsing of Templates.
  247. *
  248. * @return void
  249. */
  250. public function testGetContent() {
  251. $vars = array(
  252. 'modelClass' => 'TestViewModel',
  253. 'schema' => TableRegistry::get('ViewTaskComments')->schema(),
  254. 'primaryKey' => ['id'],
  255. 'displayField' => 'name',
  256. 'singularVar' => 'testViewModel',
  257. 'pluralVar' => 'testViewModels',
  258. 'singularHumanName' => 'Test View Model',
  259. 'pluralHumanName' => 'Test View Models',
  260. 'fields' => ['id', 'name', 'body'],
  261. 'associations' => [],
  262. 'keyFields' => [],
  263. );
  264. $result = $this->Task->getContent('view', $vars);
  265. $this->assertContains('Delete Test View Model', $result);
  266. $this->assertContains('Edit Test View Model', $result);
  267. $this->assertContains('List Test View Models', $result);
  268. $this->assertContains('New Test View Model', $result);
  269. $this->assertContains('$testViewModel->id', $result);
  270. $this->assertContains('$testViewModel->name', $result);
  271. $this->assertContains('$testViewModel->body', $result);
  272. }
  273. /**
  274. * Test getContent with associations
  275. *
  276. * @return void
  277. */
  278. public function testGetContentAssociations() {
  279. $vars = array(
  280. 'modelClass' => 'ViewTaskComments',
  281. 'schema' => TableRegistry::get('ViewTaskComments')->schema(),
  282. 'primaryKey' => ['id'],
  283. 'displayField' => 'name',
  284. 'singularVar' => 'viewTaskComment',
  285. 'pluralVar' => 'viewTaskComments',
  286. 'singularHumanName' => 'View Task Comment',
  287. 'pluralHumanName' => 'View Task Comments',
  288. 'fields' => ['id', 'name', 'body'],
  289. 'associations' => [
  290. 'belongsTo' => [
  291. 'Authors' => [
  292. 'property' => 'author',
  293. 'variable' => 'author',
  294. 'primaryKey' => ['id'],
  295. 'displayField' => 'name',
  296. 'foreignKey' => 'author_id',
  297. 'alias' => 'Authors',
  298. 'controller' => 'ViewTaskAuthors',
  299. 'fields' => ['name'],
  300. ]
  301. ]
  302. ],
  303. 'keyFields' => [],
  304. );
  305. $result = $this->Task->getContent('view', $vars);
  306. $this->assertContains('Delete View Task Comment', $result);
  307. $this->assertContains('Edit View Task Comment', $result);
  308. $this->assertContains('List Authors', $result);
  309. $this->assertContains('New Author', $result);
  310. $this->assertContains("'controller' => 'ViewTaskAuthors'", $result);
  311. }
  312. /**
  313. * Test getContent with no pk
  314. *
  315. * @return void
  316. */
  317. public function testGetContentWithNoPrimaryKey() {
  318. $vars = array(
  319. 'modelClass' => 'TestViewModel',
  320. 'schema' => TableRegistry::get('ViewTaskComments')->schema(),
  321. 'primaryKey' => [],
  322. 'displayField' => 'name',
  323. 'singularVar' => 'testViewModel',
  324. 'pluralVar' => 'testViewModels',
  325. 'singularHumanName' => 'Test View Model',
  326. 'pluralHumanName' => 'Test View Models',
  327. 'fields' => ['id', 'name', 'body'],
  328. 'associations' => [],
  329. 'keyFields' => [],
  330. );
  331. $this->Task->expects($this->once())
  332. ->method('error')
  333. ->with($this->stringContains('Cannot generate views for models'));
  334. $result = $this->Task->getContent('view', $vars);
  335. $this->assertFalse($result);
  336. }
  337. /**
  338. * test getContent() using a routing prefix action.
  339. *
  340. * @return void
  341. */
  342. public function testGetContentWithRoutingPrefix() {
  343. $vars = array(
  344. 'modelClass' => 'TestViewModel',
  345. 'schema' => TableRegistry::get('ViewTaskComments')->schema(),
  346. 'primaryKey' => ['id'],
  347. 'displayField' => 'name',
  348. 'singularVar' => 'testViewModel',
  349. 'pluralVar' => 'testViewModels',
  350. 'singularHumanName' => 'Test View Model',
  351. 'pluralHumanName' => 'Test View Models',
  352. 'fields' => ['id', 'name', 'body'],
  353. 'keyFields' => [],
  354. 'associations' => []
  355. );
  356. $this->Task->params['prefix'] = 'Admin';
  357. $result = $this->Task->getContent('view', $vars);
  358. $this->assertContains('Delete Test View Model', $result);
  359. $this->assertContains('Edit Test View Model', $result);
  360. $this->assertContains('List Test View Models', $result);
  361. $this->assertContains('New Test View Model', $result);
  362. $this->assertContains('$testViewModel->id', $result);
  363. $this->assertContains('$testViewModel->name', $result);
  364. $this->assertContains('$testViewModel->body', $result);
  365. $result = $this->Task->getContent('add', $vars);
  366. $this->assertContains("input('name')", $result);
  367. $this->assertContains("input('body')", $result);
  368. $this->assertContains('List Test View Models', $result);
  369. }
  370. /**
  371. * test Bake method
  372. *
  373. * @return void
  374. */
  375. public function testBakeView() {
  376. $this->Task->controllerName = 'ViewTaskComments';
  377. $this->Task->modelName = 'ViewTaskComments';
  378. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  379. $this->Task->expects($this->at(0))
  380. ->method('createFile')
  381. ->with(
  382. $this->_normalizePath(APP . 'Template/ViewTaskComments/view.ctp'),
  383. $this->stringContains('View Task Comments')
  384. );
  385. $this->Task->bake('view', true);
  386. }
  387. /**
  388. * test baking an edit file
  389. *
  390. * @return void
  391. */
  392. public function testBakeEdit() {
  393. $this->Task->controllerName = 'ViewTaskComments';
  394. $this->Task->modelName = 'ViewTaskComments';
  395. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  396. $this->Task->expects($this->at(0))->method('createFile')
  397. ->with(
  398. $this->_normalizePath(APP . 'Template/ViewTaskComments/edit.ctp'),
  399. $this->anything()
  400. );
  401. $result = $this->Task->bake('edit', true);
  402. $this->assertNotContains("Form->input('id')", $result);
  403. $this->assertContains("Form->input('article_id', ['options' => \$articles])", $result);
  404. }
  405. /**
  406. * test baking an index
  407. *
  408. * @return void
  409. */
  410. public function testBakeIndex() {
  411. $this->Task->controllerName = 'ViewTaskComments';
  412. $this->Task->modelName = 'ViewTaskComments';
  413. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  414. $this->Task->expects($this->at(0))->method('createFile')
  415. ->with(
  416. $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
  417. $this->stringContains("\$viewTaskComment->article->title")
  418. );
  419. $this->Task->bake('index', true);
  420. }
  421. /**
  422. * test Bake with plugins
  423. *
  424. * @return void
  425. */
  426. public function testBakeIndexPlugin() {
  427. $this->Task->controllerName = 'ViewTaskComments';
  428. $this->Task->modelName = 'TestPlugin.TestPluginComments';
  429. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  430. $table = TableRegistry::get('TestPlugin.TestPluginComments');
  431. $table->belongsTo('Articles');
  432. $this->Task->expects($this->at(0))
  433. ->method('createFile')
  434. ->with(
  435. $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
  436. $this->stringContains('$viewTaskComment->article->id')
  437. );
  438. $this->Task->bake('index', true);
  439. }
  440. /**
  441. * Ensure that models associated with themselves do not have action
  442. * links generated.
  443. *
  444. * @return void
  445. */
  446. public function testBakeSelfAssociations() {
  447. $this->Task->controllerName = 'CategoryThreads';
  448. $this->Task->modelName = 'TestApp\Model\Table\CategoryThreadsTable';
  449. $this->Task->expects($this->once())
  450. ->method('createFile')
  451. ->with(
  452. $this->_normalizePath(APP . 'Template/CategoryThreads/index.ctp'),
  453. $this->logicalNot($this->stringContains('ParentCategoryThread'))
  454. );
  455. $this->Task->bake('index', true);
  456. }
  457. /**
  458. * test that baking a view with no template doesn't make a file.
  459. *
  460. * @return void
  461. */
  462. public function testBakeWithNoTemplate() {
  463. $this->Task->controllerName = 'ViewTaskComments';
  464. $this->Task->modelName = 'ViewTaskComments';
  465. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  466. $this->Task->expects($this->never())->method('createFile');
  467. $this->Task->bake('delete', true);
  468. }
  469. /**
  470. * test bake actions baking multiple actions.
  471. *
  472. * @return void
  473. */
  474. public function testBakeActions() {
  475. $this->Task->controllerName = 'ViewTaskComments';
  476. $this->Task->modelName = 'ViewTaskComments';
  477. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  478. $this->Task->expects($this->at(0))
  479. ->method('createFile')
  480. ->with(
  481. $this->_normalizePath(APP . 'Template/ViewTaskComments/view.ctp'),
  482. $this->stringContains('View Task Comments')
  483. );
  484. $this->Task->expects($this->at(1))->method('createFile')
  485. ->with(
  486. $this->_normalizePath(APP . 'Template/ViewTaskComments/edit.ctp'),
  487. $this->stringContains('Edit View Task Comment')
  488. );
  489. $this->Task->expects($this->at(2))->method('createFile')
  490. ->with(
  491. $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
  492. $this->stringContains('ViewTaskComment')
  493. );
  494. $this->Task->bakeActions(array('view', 'edit', 'index'), array());
  495. }
  496. /**
  497. * test baking a customAction (non crud)
  498. *
  499. * @return void
  500. */
  501. public function testCustomAction() {
  502. $this->Task->controllerName = 'ViewTaskComments';
  503. $this->Task->modelName = 'ViewTaskComments';
  504. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  505. $this->Task->expects($this->any())->method('in')
  506. ->will($this->onConsecutiveCalls('', 'my_action', 'y'));
  507. $this->Task->expects($this->once())->method('createFile')
  508. ->with(
  509. $this->_normalizePath(APP . 'Template/ViewTaskComments/my_action.ctp'),
  510. $this->anything()
  511. );
  512. $this->Task->customAction();
  513. }
  514. /**
  515. * Test execute no args.
  516. *
  517. * @return void
  518. */
  519. public function testMainNoArgs() {
  520. $this->_setupTask(['in', 'err', 'bake', 'createFile', '_stop']);
  521. $this->Task->Model->expects($this->once())
  522. ->method('listAll')
  523. ->will($this->returnValue(['comments', 'articles']));
  524. $this->Task->expects($this->never())
  525. ->method('bake');
  526. $this->Task->main();
  527. }
  528. /**
  529. * Test all() calls execute
  530. *
  531. * @return void
  532. */
  533. public function testAllCallsMain() {
  534. $this->_setupTask(['in', 'err', 'createFile', 'main', '_stop']);
  535. $this->Task->Model->expects($this->once())
  536. ->method('listAll')
  537. ->will($this->returnValue(['comments', 'articles']));
  538. $this->Task->expects($this->exactly(2))
  539. ->method('main');
  540. $this->Task->expects($this->at(0))
  541. ->method('main')
  542. ->with('comments');
  543. $this->Task->expects($this->at(1))
  544. ->method('main')
  545. ->with('articles');
  546. $this->Task->all();
  547. }
  548. /**
  549. * test `cake bake view $controller view`
  550. *
  551. * @return void
  552. */
  553. public function testMainWithActionParam() {
  554. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  555. $this->Task->expects($this->once())
  556. ->method('bake')
  557. ->with('view', true);
  558. $this->Task->main('ViewTaskComments', 'view');
  559. }
  560. /**
  561. * test `cake bake view $controller`
  562. * Ensure that views are only baked for actions that exist in the controller.
  563. *
  564. * @return void
  565. */
  566. public function testMainWithController() {
  567. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  568. $this->Task->expects($this->exactly(4))
  569. ->method('bake');
  570. $this->Task->expects($this->at(0))
  571. ->method('bake')
  572. ->with('index', $this->anything());
  573. $this->Task->expects($this->at(1))
  574. ->method('bake')
  575. ->with('view', $this->anything());
  576. $this->Task->expects($this->at(2))
  577. ->method('bake')
  578. ->with('add', $this->anything());
  579. $this->Task->expects($this->at(3))
  580. ->method('bake')
  581. ->with('edit', $this->anything());
  582. $this->Task->main('ViewTaskComments');
  583. }
  584. /**
  585. * test that plugin.name works.
  586. *
  587. * @return void
  588. */
  589. public function testMainWithPluginName() {
  590. $this->_setupTask(['in', 'err', 'createFile']);
  591. $this->Task->connection = 'test';
  592. $filename = $this->_normalizePath(TEST_APP . 'Plugin/TestPlugin/src/Template/ViewTaskComments/index.ctp');
  593. Plugin::load('TestPlugin');
  594. $this->Task->expects($this->at(0))
  595. ->method('createFile')
  596. ->with($filename);
  597. $this->Task->main('TestPlugin.ViewTaskComments');
  598. }
  599. /**
  600. * static dataprovider for test cases
  601. *
  602. * @return void
  603. */
  604. public static function nameVariations() {
  605. return [['ViewTaskComments'], ['view_task_comments']];
  606. }
  607. /**
  608. * test `cake bake view $table --controller Blog`
  609. *
  610. * @return void
  611. */
  612. public function testMainWithControllerFlag() {
  613. $this->Task->params['controller'] = 'Blog';
  614. $this->Task->expects($this->exactly(4))
  615. ->method('createFile');
  616. $views = array('index.ctp', 'view.ctp', 'add.ctp', 'edit.ctp');
  617. foreach ($views as $i => $view) {
  618. $this->Task->expects($this->at($i))->method('createFile')
  619. ->with(
  620. $this->_normalizePath(APP . 'Template/Blog/' . $view),
  621. $this->anything()
  622. );
  623. }
  624. $this->Task->main('Posts');
  625. }
  626. /**
  627. * test `cake bake view $controller --prefix Admin`
  628. *
  629. * @return void
  630. */
  631. public function testMainWithControllerAndAdminFlag() {
  632. $this->Task->params['prefix'] = 'Admin';
  633. $this->Task->expects($this->exactly(2))
  634. ->method('createFile');
  635. $views = array('index.ctp', 'add.ctp');
  636. foreach ($views as $i => $view) {
  637. $this->Task->expects($this->at($i))->method('createFile')
  638. ->with(
  639. $this->_normalizePath(APP . 'Template/Admin/Posts/' . $view),
  640. $this->anything()
  641. );
  642. }
  643. $this->Task->main('Posts');
  644. }
  645. /**
  646. * test `cake bake view posts index list`
  647. *
  648. * @return void
  649. */
  650. public function testMainWithAlternateTemplates() {
  651. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  652. $this->Task->connection = 'test';
  653. $this->Task->params = [];
  654. $this->Task->expects($this->once())
  655. ->method('bake')
  656. ->with('list', true);
  657. $this->Task->main('ViewTaskComments', 'index', 'list');
  658. }
  659. /**
  660. * test getting templates, make sure noTemplateActions works and prefixed template is used before generic one.
  661. *
  662. * @return void
  663. */
  664. public function testGetTemplate() {
  665. $result = $this->Task->getTemplate('delete');
  666. $this->assertFalse($result);
  667. $result = $this->Task->getTemplate('add');
  668. $this->assertEquals('form', $result);
  669. $result = $this->Task->getTemplate('edit');
  670. $this->assertEquals('form', $result);
  671. $result = $this->Task->getTemplate('view');
  672. $this->assertEquals('view', $result);
  673. $result = $this->Task->getTemplate('index');
  674. $this->assertEquals('index', $result);
  675. }
  676. /**
  677. * Test getting prefixed views.
  678. *
  679. * @return void
  680. */
  681. public function testGetTemplatePrefixed() {
  682. $this->Task->params['prefix'] = 'Admin';
  683. $result = $this->Task->getTemplate('add');
  684. $this->assertEquals('form', $result);
  685. $this->Task->Template->templatePaths = array(
  686. 'test' => CORE_TESTS . '/test_app/TestApp/Template/Bake/test/'
  687. );
  688. $this->Task->Template->params['template'] = 'test';
  689. $result = $this->Task->getTemplate('edit');
  690. $this->assertEquals('admin_edit', $result);
  691. $result = $this->Task->getTemplate('add');
  692. $this->assertEquals('admin_form', $result);
  693. $result = $this->Task->getTemplate('view');
  694. $this->assertEquals('view', $result);
  695. }
  696. }