ViewTaskTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. /**
  92. * Generate the mock objects used in tests.
  93. *
  94. * @return void
  95. */
  96. protected function _setupTask($methods) {
  97. $io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);
  98. $this->Task = $this->getMock('Cake\Console\Command\Task\ViewTask',
  99. $methods,
  100. [$io]
  101. );
  102. $this->Task->Template = new TemplateTask($io);
  103. $this->Task->Model = $this->getMock('Cake\Console\Command\Task\ModelTask', [], [$io]);
  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. $result = $this->Task->getPath();
  201. $this->assertPathEquals(APP . 'Template/Posts/', $result);
  202. $this->Task->params['prefix'] = 'admin';
  203. $result = $this->Task->getPath();
  204. $this->assertPathEquals(APP . 'Template/Admin/Posts/', $result);
  205. }
  206. /**
  207. * Test getPath with plugins.
  208. *
  209. * @return void
  210. */
  211. public function testGetPathPlugin() {
  212. $this->Task->controllerName = 'Posts';
  213. $pluginPath = APP . 'Plugin/TestPlugin/';
  214. Plugin::load('TestPlugin', array('path' => $pluginPath));
  215. $this->Task->params['plugin'] = $this->Task->plugin = 'TestPlugin';
  216. $result = $this->Task->getPath();
  217. $this->assertPathEquals($pluginPath . 'Template/Posts/', $result);
  218. $this->Task->params['prefix'] = 'admin';
  219. $result = $this->Task->getPath();
  220. $this->assertPathEquals($pluginPath . 'Template/Admin/Posts/', $result);
  221. Plugin::unload('TestPlugin');
  222. }
  223. /**
  224. * Test getContent and parsing of Templates.
  225. *
  226. * @return void
  227. */
  228. public function testGetContent() {
  229. $vars = array(
  230. 'modelClass' => 'TestViewModel',
  231. 'schema' => [],
  232. 'primaryKey' => ['id'],
  233. 'displayField' => 'name',
  234. 'singularVar' => 'testViewModel',
  235. 'pluralVar' => 'testViewModels',
  236. 'singularHumanName' => 'Test View Model',
  237. 'pluralHumanName' => 'Test View Models',
  238. 'fields' => ['id', 'name', 'body'],
  239. 'associations' => [],
  240. 'keyFields' => [],
  241. );
  242. $result = $this->Task->getContent('view', $vars);
  243. $this->assertContains('Delete Test View Model', $result);
  244. $this->assertContains('Edit Test View Model', $result);
  245. $this->assertContains('List Test View Models', $result);
  246. $this->assertContains('New Test View Model', $result);
  247. $this->assertContains('$testViewModel->id', $result);
  248. $this->assertContains('$testViewModel->name', $result);
  249. $this->assertContains('$testViewModel->body', $result);
  250. }
  251. /**
  252. * test getContent() using a routing prefix action.
  253. *
  254. * @return void
  255. */
  256. public function testGetContentWithRoutingPrefix() {
  257. $vars = array(
  258. 'modelClass' => 'TestViewModel',
  259. 'schema' => [],
  260. 'primaryKey' => ['id'],
  261. 'displayField' => 'name',
  262. 'singularVar' => 'testViewModel',
  263. 'pluralVar' => 'testViewModels',
  264. 'singularHumanName' => 'Test View Model',
  265. 'pluralHumanName' => 'Test View Models',
  266. 'fields' => ['id', 'name', 'body'],
  267. 'keyFields' => [],
  268. 'associations' => []
  269. );
  270. $this->Task->params['prefix'] = 'Admin';
  271. $result = $this->Task->getContent('view', $vars);
  272. $this->assertContains('Delete Test View Model', $result);
  273. $this->assertContains('Edit Test View Model', $result);
  274. $this->assertContains('List Test View Models', $result);
  275. $this->assertContains('New Test View Model', $result);
  276. $this->assertContains('$testViewModel->id', $result);
  277. $this->assertContains('$testViewModel->name', $result);
  278. $this->assertContains('$testViewModel->body', $result);
  279. $result = $this->Task->getContent('add', $vars);
  280. $this->assertContains("input('name')", $result);
  281. $this->assertContains("input('body')", $result);
  282. $this->assertContains('List Test View Models', $result);
  283. }
  284. /**
  285. * test Bake method
  286. *
  287. * @return void
  288. */
  289. public function testBakeView() {
  290. $this->Task->controllerName = 'ViewTaskComments';
  291. $this->Task->tableName = 'ViewTaskComments';
  292. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  293. $this->Task->expects($this->at(0))
  294. ->method('createFile')
  295. ->with(
  296. $this->_normalizePath(APP . 'Template/ViewTaskComments/view.ctp'),
  297. $this->stringContains('View Task Comments')
  298. );
  299. $this->Task->bake('view', true);
  300. }
  301. /**
  302. * test baking an edit file
  303. *
  304. * @return void
  305. */
  306. public function testBakeEdit() {
  307. $this->Task->controllerName = 'ViewTaskComments';
  308. $this->Task->tableName = 'ViewTaskComments';
  309. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  310. $this->Task->expects($this->at(0))->method('createFile')
  311. ->with(
  312. $this->_normalizePath(APP . 'Template/ViewTaskComments/edit.ctp'),
  313. $this->anything()
  314. );
  315. $result = $this->Task->bake('edit', true);
  316. $this->assertContains("Form->input('id')", $result);
  317. $this->assertContains("Form->input('article_id', ['options' => \$articles])", $result);
  318. }
  319. /**
  320. * test baking an index
  321. *
  322. * @return void
  323. */
  324. public function testBakeIndex() {
  325. $this->Task->controllerName = 'ViewTaskComments';
  326. $this->Task->tableName = 'ViewTaskComments';
  327. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  328. $this->Task->expects($this->at(0))->method('createFile')
  329. ->with(
  330. $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
  331. $this->stringContains("\$viewTaskComment->article->title")
  332. );
  333. $this->Task->bake('index', true);
  334. }
  335. /**
  336. * test that baking a view with no template doesn't make a file.
  337. *
  338. * @return void
  339. */
  340. public function testBakeWithNoTemplate() {
  341. $this->Task->controllerName = 'ViewTaskComments';
  342. $this->Task->tableName = 'ViewTaskComments';
  343. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  344. $this->Task->expects($this->never())->method('createFile');
  345. $this->Task->bake('delete', true);
  346. }
  347. /**
  348. * test bake actions baking multiple actions.
  349. *
  350. * @return void
  351. */
  352. public function testBakeActions() {
  353. $this->Task->controllerName = 'ViewTaskComments';
  354. $this->Task->tableName = 'ViewTaskComments';
  355. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  356. $this->Task->expects($this->at(0))
  357. ->method('createFile')
  358. ->with(
  359. $this->_normalizePath(APP . 'Template/ViewTaskComments/view.ctp'),
  360. $this->stringContains('View Task Comments')
  361. );
  362. $this->Task->expects($this->at(1))->method('createFile')
  363. ->with(
  364. $this->_normalizePath(APP . 'Template/ViewTaskComments/edit.ctp'),
  365. $this->stringContains('Edit View Task Comment')
  366. );
  367. $this->Task->expects($this->at(2))->method('createFile')
  368. ->with(
  369. $this->_normalizePath(APP . 'Template/ViewTaskComments/index.ctp'),
  370. $this->stringContains('ViewTaskComment')
  371. );
  372. $this->Task->bakeActions(array('view', 'edit', 'index'), array());
  373. }
  374. /**
  375. * test baking a customAction (non crud)
  376. *
  377. * @return void
  378. */
  379. public function testCustomAction() {
  380. $this->Task->controllerName = 'ViewTaskComments';
  381. $this->Task->tableName = 'ViewTaskComments';
  382. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  383. $this->Task->expects($this->any())->method('in')
  384. ->will($this->onConsecutiveCalls('', 'my_action', 'y'));
  385. $this->Task->expects($this->once())->method('createFile')
  386. ->with(
  387. $this->_normalizePath(APP . 'Template/ViewTaskComments/my_action.ctp'),
  388. $this->anything()
  389. );
  390. $this->Task->customAction();
  391. }
  392. /**
  393. * Test execute no args.
  394. *
  395. * @return void
  396. */
  397. public function testMainNoArgs() {
  398. $this->_setupTask(['in', 'err', 'bake', 'createFile', '_stop']);
  399. $this->Task->Model->expects($this->once())
  400. ->method('listAll')
  401. ->will($this->returnValue(['comments', 'articles']));
  402. $this->Task->expects($this->never())
  403. ->method('bake');
  404. $this->Task->main();
  405. }
  406. /**
  407. * Test all() calls execute
  408. *
  409. * @return void
  410. */
  411. public function testAllCallsMain() {
  412. $this->_setupTask(['in', 'err', 'createFile', 'main', '_stop']);
  413. $this->Task->Model->expects($this->once())
  414. ->method('listAll')
  415. ->will($this->returnValue(['comments', 'articles']));
  416. $this->Task->expects($this->exactly(2))
  417. ->method('main');
  418. $this->Task->expects($this->at(0))
  419. ->method('main')
  420. ->with('comments');
  421. $this->Task->expects($this->at(1))
  422. ->method('main')
  423. ->with('articles');
  424. $this->Task->all();
  425. }
  426. /**
  427. * test `cake bake view $controller view`
  428. *
  429. * @return void
  430. */
  431. public function testMainWithActionParam() {
  432. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  433. $this->Task->expects($this->once())
  434. ->method('bake')
  435. ->with('view', true);
  436. $this->Task->main('ViewTaskComments', 'view');
  437. }
  438. /**
  439. * test `cake bake view $controller`
  440. * Ensure that views are only baked for actions that exist in the controller.
  441. *
  442. * @return void
  443. */
  444. public function testMainWithController() {
  445. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  446. $this->Task->expects($this->exactly(4))
  447. ->method('bake');
  448. $this->Task->expects($this->at(0))
  449. ->method('bake')
  450. ->with('index', $this->anything());
  451. $this->Task->expects($this->at(1))
  452. ->method('bake')
  453. ->with('view', $this->anything());
  454. $this->Task->expects($this->at(2))
  455. ->method('bake')
  456. ->with('add', $this->anything());
  457. $this->Task->expects($this->at(3))
  458. ->method('bake')
  459. ->with('edit', $this->anything());
  460. $this->Task->main('ViewTaskComments');
  461. }
  462. /**
  463. * test that plugin.name works.
  464. *
  465. * @return void
  466. */
  467. public function testMainWithPluginName() {
  468. $this->_setupTask(['in', 'err', 'createFile']);
  469. $this->Task->connection = 'test';
  470. $filename = $this->_normalizePath(TEST_APP . 'Plugin/TestPlugin/Template/ViewTaskComments/index.ctp');
  471. Plugin::load('TestPlugin');
  472. $this->Task->expects($this->at(0))
  473. ->method('createFile')
  474. ->with($filename);
  475. $this->Task->main('TestPlugin.ViewTaskComments');
  476. }
  477. /**
  478. * static dataprovider for test cases
  479. *
  480. * @return void
  481. */
  482. public static function nameVariations() {
  483. return [['ViewTaskComments'], ['ViewTaskComment'], ['view_task_comment']];
  484. }
  485. /**
  486. * test `cake bake view $table --controller Blog`
  487. *
  488. * @return void
  489. */
  490. public function testMainWithControllerFlag() {
  491. $this->Task->params['controller'] = 'Blog';
  492. $this->Task->expects($this->exactly(4))
  493. ->method('createFile');
  494. $views = array('index.ctp', 'view.ctp', 'add.ctp', 'edit.ctp');
  495. foreach ($views as $i => $view) {
  496. $this->Task->expects($this->at($i))->method('createFile')
  497. ->with(
  498. $this->_normalizePath(APP . 'Template/Blog/' . $view),
  499. $this->anything()
  500. );
  501. }
  502. $this->Task->main('Posts');
  503. }
  504. /**
  505. * test `cake bake view $controller --prefix Admin`
  506. *
  507. * @return void
  508. */
  509. public function testMainWithControllerAndAdminFlag() {
  510. $this->Task->params['prefix'] = 'Admin';
  511. $this->Task->expects($this->exactly(2))
  512. ->method('createFile');
  513. $views = array('index.ctp', 'add.ctp');
  514. foreach ($views as $i => $view) {
  515. $this->Task->expects($this->at($i))->method('createFile')
  516. ->with(
  517. $this->_normalizePath(APP . 'Template/Admin/Posts/' . $view),
  518. $this->anything()
  519. );
  520. }
  521. $this->Task->main('Posts');
  522. }
  523. /**
  524. * test `cake bake view posts index list`
  525. *
  526. * @return void
  527. */
  528. public function testMainWithAlternateTemplates() {
  529. $this->_setupTask(['in', 'err', 'createFile', 'bake', '_stop']);
  530. $this->Task->connection = 'test';
  531. $this->Task->params = [];
  532. $this->Task->expects($this->once())
  533. ->method('bake')
  534. ->with('list', true);
  535. $this->Task->main('ViewTaskComments', 'index', 'list');
  536. }
  537. /**
  538. * test getting templates, make sure noTemplateActions works and prefixed template is used before generic one.
  539. *
  540. * @return void
  541. */
  542. public function testGetTemplate() {
  543. $result = $this->Task->getTemplate('delete');
  544. $this->assertFalse($result);
  545. $result = $this->Task->getTemplate('add');
  546. $this->assertEquals('form', $result);
  547. $result = $this->Task->getTemplate('edit');
  548. $this->assertEquals('form', $result);
  549. $result = $this->Task->getTemplate('view');
  550. $this->assertEquals('view', $result);
  551. $result = $this->Task->getTemplate('index');
  552. $this->assertEquals('index', $result);
  553. }
  554. /**
  555. * Test getting prefixed views.
  556. *
  557. * @return void
  558. */
  559. public function testGetTemplatePrefixed() {
  560. $this->Task->params['prefix'] = 'Admin';
  561. $result = $this->Task->getTemplate('add');
  562. $this->assertEquals('form', $result);
  563. $this->Task->Template->templatePaths = array(
  564. 'test' => CORE_TESTS . '/test_app/TestApp/Console/Templates/test/'
  565. );
  566. $this->Task->Template->params['theme'] = 'test';
  567. $result = $this->Task->getTemplate('edit');
  568. $this->assertEquals('admin_edit', $result);
  569. $result = $this->Task->getTemplate('add');
  570. $this->assertEquals('admin_form', $result);
  571. $result = $this->Task->getTemplate('view');
  572. $this->assertEquals('view', $result);
  573. }
  574. }