ViewTaskTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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\Console\Command\Task;
  16. use Cake\Console\Command\Task\TemplateTask;
  17. use Cake\Console\Command\Task\ViewTask;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\ORM\Table;
  22. use Cake\ORM\TableRegistry;
  23. use Cake\TestSuite\TestCase;
  24. /**
  25. * Test View Task Comment Model
  26. *
  27. */
  28. class ViewTaskCommentsTable extends Table {
  29. public function initialize(array $config) {
  30. $this->table('comments');
  31. $this->belongsTo('Articles', [
  32. 'foreignKey' => 'article_id'
  33. ]);
  34. }
  35. }
  36. /**
  37. * Test View Task Article Model
  38. *
  39. */
  40. class ViewTaskArticlesTable extends Table {
  41. public function intialize(array $config) {
  42. $this->table('articles');
  43. }
  44. }
  45. /**
  46. * Test View Task Comments Controller
  47. *
  48. */
  49. class ViewTaskCommentsController extends Controller {
  50. public $modelClass = 'Cake\Test\TestCase\Console\Command\Task\ViewTaskCommentsTable';
  51. /**
  52. * Testing public controller action
  53. *
  54. * @return void
  55. */
  56. public function index() {
  57. }
  58. /**
  59. * Testing public controller action
  60. *
  61. * @return void
  62. */
  63. public function add() {
  64. }
  65. }
  66. /**
  67. * ViewTaskTest class
  68. */
  69. class ViewTaskTest extends TestCase {
  70. /**
  71. * Fixtures
  72. *
  73. * @var array
  74. */
  75. public $fixtures = array('core.article', 'core.post', 'core.comment', 'core.articles_tag', 'core.tag');
  76. /**
  77. * setUp method
  78. *
  79. * Ensure that the default theme is used
  80. *
  81. * @return void
  82. */
  83. public function setUp() {
  84. parent::setUp();
  85. Configure::write('App.namespace', 'TestApp');
  86. $this->_setupTask(['in', 'err', 'createFile', '_stop']);
  87. TableRegistry::get('ViewTaskComments', [
  88. 'className' => __NAMESPACE__ . '\ViewTaskCommentsTable',
  89. ]);
  90. }
  91. protected function _setupTask($methods) {
  92. $out = $this->getMock('Cake\Console\ConsoleOutput', [], [], '', false);
  93. $in = $this->getMock('Cake\Console\ConsoleInput', [], [], '', false);
  94. $this->Task = $this->getMock('Cake\Console\Command\Task\ViewTask',
  95. $methods,
  96. [$out, $out, $in]
  97. );
  98. $this->Task->Template = new TemplateTask($out, $out, $in);
  99. $this->Task->Controller = $this->getMock('Cake\Console\Command\Task\ControllerTask', [], [$out, $out, $in]);
  100. $this->Task->Model = $this->getMock('Cake\Console\Command\Task\ModelTask', [], [$out, $out, $in]);
  101. $this->Task->Project = $this->getMock('Cake\Console\Command\Task\ProjectTask', [], [$out, $out, $in]);
  102. $this->Task->DbConfig = $this->getMock('Cake\Console\Command\Task\DbConfigTask', [], [$out, $out, $in]);
  103. $this->Task->path = TMP;
  104. $this->Task->Template->params['theme'] = 'default';
  105. $this->Task->Template->templatePaths = ['default' => CAKE . 'Console/Templates/default/'];
  106. }
  107. /**
  108. * tearDown method
  109. *
  110. * @return void
  111. */
  112. public function tearDown() {
  113. parent::tearDown();
  114. TableRegistry::clear();
  115. unset($this->Task);
  116. }
  117. /**
  118. * Test the controller() method.
  119. *
  120. * @return void
  121. */
  122. public function testController() {
  123. $this->Task->controller('Comments');
  124. $this->assertEquals('Comments', $this->Task->controllerName);
  125. $this->assertEquals(
  126. 'TestApp\Controller\CommentsController',
  127. $this->Task->controllerClass
  128. );
  129. }
  130. /**
  131. * Test the controller() method.
  132. *
  133. * @dataProvider nameVariations
  134. * @return void
  135. */
  136. public function testControllerVariations($name) {
  137. $this->Task->controller($name);
  138. $this->assertEquals('ViewTaskComments', $this->Task->controllerName);
  139. $this->assertEquals('ViewTaskComments', $this->Task->tableName);
  140. }
  141. /**
  142. * Test controller method with plugins.
  143. *
  144. * @return void
  145. */
  146. public function testControllerPlugin() {
  147. $this->Task->params['plugin'] = 'TestPlugin';
  148. $this->Task->controller('Tests');
  149. $this->assertEquals('Tests', $this->Task->controllerName);
  150. $this->assertEquals('Tests', $this->Task->tableName);
  151. $this->assertEquals(
  152. 'TestPlugin\Controller\TestsController',
  153. $this->Task->controllerClass
  154. );
  155. }
  156. /**
  157. * Test controller method with prefixes.
  158. *
  159. * @return void
  160. */
  161. public function testControllerPrefix() {
  162. $this->Task->params['prefix'] = 'Admin';
  163. $this->Task->controller('Posts');
  164. $this->assertEquals('Posts', $this->Task->controllerName);
  165. $this->assertEquals('Posts', $this->Task->tableName);
  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('Comments', $this->Task->tableName);
  174. $this->assertEquals(
  175. 'TestPlugin\Controller\Admin\CommentsController',
  176. $this->Task->controllerClass
  177. );
  178. }
  179. /**
  180. * test controller with a non-conventional controller name
  181. *
  182. * @return void
  183. */
  184. public function testControllerWithOverride() {
  185. $this->Task->controller('Comments', 'Posts');
  186. $this->assertEquals('Posts', $this->Task->controllerName);
  187. $this->assertEquals('Comments', $this->Task->tableName);
  188. $this->assertEquals(
  189. 'TestApp\Controller\PostsController',
  190. $this->Task->controllerClass
  191. );
  192. }
  193. /**
  194. * Test getPath()
  195. *
  196. * @return void
  197. */
  198. public function testGetPath() {
  199. $this->Task->controllerName = 'Posts';
  200. $this->Task->path = '/path/Template/';
  201. $result = $this->Task->getPath();
  202. $this->assertEquals('/path/Template/Posts/', $result);
  203. $this->Task->params['prefix'] = 'admin';
  204. $result = $this->Task->getPath();
  205. $this->assertEquals('/path/Template/Admin/Posts/', $result);
  206. }
  207. /**
  208. * Test getPath with plugins.
  209. *
  210. * @return void
  211. */
  212. public function testGetPathPlugin() {
  213. $this->Task->controllerName = 'Posts';
  214. $this->Task->path = '/path/Template/';
  215. $pluginPath = APP . 'Plugin/TestPlugin/';
  216. Plugin::load('TestPlugin', array('path' => $pluginPath));
  217. $this->Task->params['plugin'] = $this->Task->plugin = 'TestPlugin';
  218. $result = $this->Task->getPath();
  219. $this->assertEquals($pluginPath . 'Template/Posts/', $result);
  220. $this->Task->params['prefix'] = 'admin';
  221. $result = $this->Task->getPath();
  222. $this->assertEquals($pluginPath . 'Template/Admin/Posts/', $result);
  223. Plugin::unload('TestPlugin');
  224. }
  225. /**
  226. * Test getContent and parsing of Templates.
  227. *
  228. * @return void
  229. */
  230. public function testGetContent() {
  231. $vars = array(
  232. 'modelClass' => 'TestViewModel',
  233. 'schema' => [],
  234. 'primaryKey' => ['id'],
  235. 'displayField' => 'name',
  236. 'singularVar' => 'testViewModel',
  237. 'pluralVar' => 'testViewModels',
  238. 'singularHumanName' => 'Test View Model',
  239. 'pluralHumanName' => 'Test View Models',
  240. 'fields' => ['id', 'name', 'body'],
  241. 'associations' => [],
  242. 'keyFields' => [],
  243. );
  244. $result = $this->Task->getContent('view', $vars);
  245. $this->assertContains('Delete Test View Model', $result);
  246. $this->assertContains('Edit Test View Model', $result);
  247. $this->assertContains('List Test View Models', $result);
  248. $this->assertContains('New Test View Model', $result);
  249. $this->assertContains('$testViewModel->id', $result);
  250. $this->assertContains('$testViewModel->name', $result);
  251. $this->assertContains('$testViewModel->body', $result);
  252. }
  253. /**
  254. * test getContent() using a routing prefix action.
  255. *
  256. * @return void
  257. */
  258. public function testGetContentWithRoutingPrefix() {
  259. $vars = array(
  260. 'modelClass' => 'TestViewModel',
  261. 'schema' => [],
  262. 'primaryKey' => ['id'],
  263. 'displayField' => 'name',
  264. 'singularVar' => 'testViewModel',
  265. 'pluralVar' => 'testViewModels',
  266. 'singularHumanName' => 'Test View Model',
  267. 'pluralHumanName' => 'Test View Models',
  268. 'fields' => ['id', 'name', 'body'],
  269. 'keyFields' => [],
  270. 'associations' => []
  271. );
  272. $this->Task->params['prefix'] = 'Admin';
  273. $result = $this->Task->getContent('view', $vars);
  274. $this->assertContains('Delete Test View Model', $result);
  275. $this->assertContains('Edit Test View Model', $result);
  276. $this->assertContains('List Test View Models', $result);
  277. $this->assertContains('New Test View Model', $result);
  278. $this->assertContains('$testViewModel->id', $result);
  279. $this->assertContains('$testViewModel->name', $result);
  280. $this->assertContains('$testViewModel->body', $result);
  281. $result = $this->Task->getContent('add', $vars);
  282. $this->assertContains("input('name')", $result);
  283. $this->assertContains("input('body')", $result);
  284. $this->assertContains('List Test View Models', $result);
  285. }
  286. /**
  287. * test Bake method
  288. *
  289. * @return void
  290. */
  291. public function testBakeView() {
  292. $this->Task->controllerName = 'ViewTaskComments';
  293. $this->Task->tableName = 'ViewTaskComments';
  294. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  295. $this->Task->expects($this->at(0))
  296. ->method('createFile')
  297. ->with(
  298. TMP . 'ViewTaskComments/view.ctp',
  299. $this->stringContains('View Task Comments')
  300. );
  301. $this->Task->bake('view', true);
  302. }
  303. /**
  304. * test baking an edit file
  305. *
  306. * @return void
  307. */
  308. public function testBakeEdit() {
  309. $this->Task->controllerName = 'ViewTaskComments';
  310. $this->Task->tableName = 'ViewTaskComments';
  311. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  312. $this->Task->expects($this->at(0))->method('createFile')
  313. ->with(
  314. TMP . 'ViewTaskComments/edit.ctp',
  315. $this->anything()
  316. );
  317. $result = $this->Task->bake('edit', true);
  318. $this->assertContains("Form->input('id')", $result);
  319. $this->assertContains("Form->input('article_id', ['options' => \$articles])", $result);
  320. }
  321. /**
  322. * test baking an index
  323. *
  324. * @return void
  325. */
  326. public function testBakeIndex() {
  327. $this->Task->controllerName = 'ViewTaskComments';
  328. $this->Task->tableName = 'ViewTaskComments';
  329. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  330. $this->Task->expects($this->at(0))->method('createFile')
  331. ->with(
  332. TMP . 'ViewTaskComments/index.ctp',
  333. $this->stringContains("\$viewTaskComment->article->title")
  334. );
  335. $this->Task->bake('index', true);
  336. }
  337. /**
  338. * test that baking a view with no template doesn't make a file.
  339. *
  340. * @return void
  341. */
  342. public function testBakeWithNoTemplate() {
  343. $this->Task->controllerName = 'ViewTaskComments';
  344. $this->Task->tableName = 'ViewTaskComments';
  345. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  346. $this->Task->expects($this->never())->method('createFile');
  347. $this->Task->bake('delete', true);
  348. }
  349. /**
  350. * test bake actions baking multiple actions.
  351. *
  352. * @return void
  353. */
  354. public function testBakeActions() {
  355. $this->Task->controllerName = 'ViewTaskComments';
  356. $this->Task->tableName = 'ViewTaskComments';
  357. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  358. $this->Task->expects($this->at(0))
  359. ->method('createFile')
  360. ->with(
  361. TMP . 'ViewTaskComments/view.ctp',
  362. $this->stringContains('View Task Comments')
  363. );
  364. $this->Task->expects($this->at(1))->method('createFile')
  365. ->with(
  366. TMP . 'ViewTaskComments/edit.ctp',
  367. $this->stringContains('Edit View Task Comment')
  368. );
  369. $this->Task->expects($this->at(2))->method('createFile')
  370. ->with(
  371. TMP . 'ViewTaskComments/index.ctp',
  372. $this->stringContains('ViewTaskComment')
  373. );
  374. $this->Task->bakeActions(array('view', 'edit', 'index'), array());
  375. }
  376. /**
  377. * test baking a customAction (non crud)
  378. *
  379. * @return void
  380. */
  381. public function testCustomAction() {
  382. $this->Task->controllerName = 'ViewTaskComments';
  383. $this->Task->tableName = 'ViewTaskComments';
  384. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  385. $this->Task->expects($this->any())->method('in')
  386. ->will($this->onConsecutiveCalls('', 'my_action', 'y'));
  387. $this->Task->expects($this->once())->method('createFile')
  388. ->with(
  389. TMP . 'ViewTaskComments/my_action.ctp',
  390. $this->anything()
  391. );
  392. $this->Task->customAction();
  393. }
  394. /**
  395. * Test all()
  396. *
  397. * @return void
  398. */
  399. public function testExecuteIntoAll() {
  400. $this->_setupTask(['in', 'err', 'createFile', 'all', '_stop']);
  401. $this->Task->args[0] = 'all';
  402. $this->Task->expects($this->once())
  403. ->method('all');
  404. $this->Task->execute();
  405. }
  406. /**
  407. * test `cake bake view $controller view`
  408. *
  409. * @return void
  410. */
  411. public function testExecuteWithActionParam() {
  412. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  413. $this->Task->args[0] = 'ViewTaskComments';
  414. $this->Task->args[1] = 'view';
  415. $this->Task->expects($this->once())
  416. ->method('bake')
  417. ->with('view', true);
  418. $this->Task->execute();
  419. }
  420. /**
  421. * test `cake bake view $controller`
  422. * Ensure that views are only baked for actions that exist in the controller.
  423. *
  424. * @return void
  425. */
  426. public function testExecuteWithController() {
  427. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  428. $this->Task->args[0] = 'ViewTaskComments';
  429. $this->Task->expects($this->exactly(4))
  430. ->method('bake');
  431. $this->Task->expects($this->at(0))
  432. ->method('bake')
  433. ->with('index', $this->anything());
  434. $this->Task->expects($this->at(1))
  435. ->method('bake')
  436. ->with('view', $this->anything());
  437. $this->Task->expects($this->at(2))
  438. ->method('bake')
  439. ->with('add', $this->anything());
  440. $this->Task->expects($this->at(3))
  441. ->method('bake')
  442. ->with('edit', $this->anything());
  443. $this->Task->execute();
  444. }
  445. /**
  446. * static dataprovider for test cases
  447. *
  448. * @return void
  449. */
  450. public static function nameVariations() {
  451. return [['ViewTaskComments'], ['ViewTaskComment'], ['view_task_comment']];
  452. }
  453. /**
  454. * test `cake bake view $table --controller Blog`
  455. *
  456. * @return void
  457. */
  458. public function testExecuteWithControllerFlag() {
  459. $this->Task->args[0] = 'Posts';
  460. $this->Task->params['controller'] = 'Blog';
  461. $this->Task->expects($this->exactly(4))
  462. ->method('createFile');
  463. $views = array('index.ctp', 'view.ctp', 'add.ctp', 'edit.ctp');
  464. foreach ($views as $i => $view) {
  465. $this->Task->expects($this->at($i))->method('createFile')
  466. ->with(
  467. TMP . 'Blog/' . $view,
  468. $this->anything()
  469. );
  470. }
  471. $this->Task->execute();
  472. }
  473. /**
  474. * test `cake bake view $controller --prefix Admin`
  475. *
  476. * @return void
  477. */
  478. public function testExecuteWithControllerAndAdminFlag() {
  479. $this->Task->args[0] = 'Posts';
  480. $this->Task->params['prefix'] = 'Admin';
  481. $this->Task->expects($this->exactly(2))
  482. ->method('createFile');
  483. $views = array('index.ctp', 'add.ctp');
  484. foreach ($views as $i => $view) {
  485. $this->Task->expects($this->at($i))->method('createFile')
  486. ->with(
  487. TMP . 'Admin/Posts/' . $view,
  488. $this->anything()
  489. );
  490. }
  491. $this->Task->execute();
  492. }
  493. /**
  494. * test `cake bake view posts index list`
  495. *
  496. * @return void
  497. */
  498. public function testExecuteWithAlternateTemplates() {
  499. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  500. $this->Task->connection = 'test';
  501. $this->Task->args = ['ViewTaskComments', 'index', 'list'];
  502. $this->Task->params = [];
  503. $this->Task->expects($this->once())
  504. ->method('bake')
  505. ->with('list', true);
  506. $this->Task->execute();
  507. }
  508. /**
  509. * test getting templates, make sure noTemplateActions works and prefixed template is used before generic one.
  510. *
  511. * @return void
  512. */
  513. public function testGetTemplate() {
  514. $result = $this->Task->getTemplate('delete');
  515. $this->assertFalse($result);
  516. $result = $this->Task->getTemplate('add');
  517. $this->assertEquals('form', $result);
  518. $result = $this->Task->getTemplate('edit');
  519. $this->assertEquals('form', $result);
  520. $result = $this->Task->getTemplate('view');
  521. $this->assertEquals('view', $result);
  522. $result = $this->Task->getTemplate('index');
  523. $this->assertEquals('index', $result);
  524. }
  525. /**
  526. * Test getting prefixed views.
  527. *
  528. * @return void
  529. */
  530. public function testGetTemplatePrefixed() {
  531. $this->Task->params['prefix'] = 'Admin';
  532. $result = $this->Task->getTemplate('add');
  533. $this->assertEquals('form', $result);
  534. $this->Task->Template->templatePaths = array(
  535. 'test' => CORE_TESTS . '/test_app/TestApp/Console/Templates/test/'
  536. );
  537. $this->Task->Template->params['theme'] = 'test';
  538. $result = $this->Task->getTemplate('edit');
  539. $this->assertEquals('admin_edit', $result);
  540. $result = $this->Task->getTemplate('add');
  541. $this->assertEquals('admin_form', $result);
  542. $result = $this->Task->getTemplate('view');
  543. $this->assertEquals('view', $result);
  544. }
  545. }