ViewTaskTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. }
  140. /**
  141. * Test controller method with plugins.
  142. *
  143. * @return void
  144. */
  145. public function testControllerPlugin() {
  146. $this->Task->params['plugin'] = 'TestPlugin';
  147. $this->Task->controller('Tests');
  148. $this->assertEquals('Tests', $this->Task->controllerName);
  149. $this->assertEquals(
  150. 'TestPlugin\Controller\TestsController',
  151. $this->Task->controllerClass
  152. );
  153. }
  154. /**
  155. * Test controller method with prefixes.
  156. *
  157. * @return void
  158. */
  159. public function testControllerPrefix() {
  160. $this->Task->params['prefix'] = 'Admin';
  161. $this->Task->controller('Posts');
  162. $this->assertEquals('Posts', $this->Task->controllerName);
  163. $this->assertEquals(
  164. 'TestApp\Controller\Admin\PostsController',
  165. $this->Task->controllerClass
  166. );
  167. $this->Task->params['plugin'] = 'TestPlugin';
  168. $this->Task->controller('Comments');
  169. $this->assertEquals('Comments', $this->Task->controllerName);
  170. $this->assertEquals(
  171. 'TestPlugin\Controller\Admin\CommentsController',
  172. $this->Task->controllerClass
  173. );
  174. }
  175. /**
  176. * Test getPath()
  177. *
  178. * @return void
  179. */
  180. public function testGetPath() {
  181. $this->Task->controllerName = 'Posts';
  182. $this->Task->path = '/path/Template/';
  183. $result = $this->Task->getPath();
  184. $this->assertEquals('/path/Template/Posts/', $result);
  185. $this->Task->params['prefix'] = 'admin';
  186. $result = $this->Task->getPath();
  187. $this->assertEquals('/path/Template/Admin/Posts/', $result);
  188. }
  189. /**
  190. * Test getPath with plugins.
  191. *
  192. * @return void
  193. */
  194. public function testGetPathPlugin() {
  195. $this->Task->controllerName = 'Posts';
  196. $this->Task->path = '/path/Template/';
  197. $pluginPath = APP . 'Plugin/TestPlugin/';
  198. Plugin::load('TestPlugin', array('path' => $pluginPath));
  199. $this->Task->params['plugin'] = $this->Task->plugin = 'TestPlugin';
  200. $result = $this->Task->getPath();
  201. $this->assertEquals($pluginPath . 'Template/Posts/', $result);
  202. $this->Task->params['prefix'] = 'admin';
  203. $result = $this->Task->getPath();
  204. $this->assertEquals($pluginPath . 'Template/Admin/Posts/', $result);
  205. Plugin::unload('TestPlugin');
  206. }
  207. /**
  208. * Test getContent and parsing of Templates.
  209. *
  210. * @return void
  211. */
  212. public function testGetContent() {
  213. $vars = array(
  214. 'modelClass' => 'TestViewModel',
  215. 'schema' => [],
  216. 'primaryKey' => ['id'],
  217. 'displayField' => 'name',
  218. 'singularVar' => 'testViewModel',
  219. 'pluralVar' => 'testViewModels',
  220. 'singularHumanName' => 'Test View Model',
  221. 'pluralHumanName' => 'Test View Models',
  222. 'fields' => ['id', 'name', 'body'],
  223. 'associations' => []
  224. );
  225. $result = $this->Task->getContent('view', $vars);
  226. $this->assertContains('Delete Test View Model', $result);
  227. $this->assertContains('Edit Test View Model', $result);
  228. $this->assertContains('List Test View Models', $result);
  229. $this->assertContains('New Test View Model', $result);
  230. $this->assertContains('$testViewModel->id', $result);
  231. $this->assertContains('$testViewModel->name', $result);
  232. $this->assertContains('$testViewModel->body', $result);
  233. }
  234. /**
  235. * test getContent() using a routing prefix action.
  236. *
  237. * @return void
  238. */
  239. public function testGetContentWithRoutingPrefix() {
  240. $vars = array(
  241. 'modelClass' => 'TestViewModel',
  242. 'schema' => [],
  243. 'primaryKey' => ['id'],
  244. 'displayField' => 'name',
  245. 'singularVar' => 'testViewModel',
  246. 'pluralVar' => 'testViewModels',
  247. 'singularHumanName' => 'Test View Model',
  248. 'pluralHumanName' => 'Test View Models',
  249. 'fields' => ['id', 'name', 'body'],
  250. 'associations' => []
  251. );
  252. $this->Task->params['prefix'] = 'Admin';
  253. $result = $this->Task->getContent('view', $vars);
  254. $this->assertContains('Delete Test View Model', $result);
  255. $this->assertContains('Edit Test View Model', $result);
  256. $this->assertContains('List Test View Models', $result);
  257. $this->assertContains('New Test View Model', $result);
  258. $this->assertContains('$testViewModel->id', $result);
  259. $this->assertContains('$testViewModel->name', $result);
  260. $this->assertContains('$testViewModel->body', $result);
  261. $result = $this->Task->getContent('add', $vars);
  262. $this->assertContains("input('name')", $result);
  263. $this->assertContains("input('body')", $result);
  264. $this->assertContains('List Test View Models', $result);
  265. }
  266. /**
  267. * test Bake method
  268. *
  269. * @return void
  270. */
  271. public function testBakeView() {
  272. $this->Task->controllerName = 'ViewTaskComments';
  273. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  274. $this->Task->expects($this->at(0))
  275. ->method('createFile')
  276. ->with(
  277. TMP . 'ViewTaskComments/view.ctp',
  278. $this->stringContains('View Task Comments')
  279. );
  280. $this->Task->bake('view', true);
  281. }
  282. /**
  283. * test baking an edit file
  284. *
  285. * @return void
  286. */
  287. public function testBakeEdit() {
  288. $this->Task->controllerName = 'ViewTaskComments';
  289. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  290. $this->Task->expects($this->at(0))->method('createFile')
  291. ->with(
  292. TMP . 'ViewTaskComments/edit.ctp',
  293. $this->anything()
  294. );
  295. $this->Task->bake('edit', true);
  296. }
  297. /**
  298. * test baking an index
  299. *
  300. * @return void
  301. */
  302. public function testBakeIndex() {
  303. $this->Task->controllerName = 'ViewTaskComments';
  304. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  305. $this->Task->expects($this->at(0))->method('createFile')
  306. ->with(
  307. TMP . 'ViewTaskComments/index.ctp',
  308. $this->stringContains("\$viewTaskComment->article->title")
  309. );
  310. $this->Task->bake('index', true);
  311. }
  312. /**
  313. * test that baking a view with no template doesn't make a file.
  314. *
  315. * @return void
  316. */
  317. public function testBakeWithNoTemplate() {
  318. $this->Task->controllerName = 'ViewTaskComments';
  319. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  320. $this->Task->expects($this->never())->method('createFile');
  321. $this->Task->bake('delete', true);
  322. }
  323. /**
  324. * test bake() with a -plugin param
  325. *
  326. * @return void
  327. */
  328. public function testBakeWithPlugin() {
  329. $this->markTestIncomplete('Model baking will not work as models do not work.');
  330. $this->Task->controllerName = 'ViewTaskComments';
  331. $this->Task->plugin = 'TestTest';
  332. $this->Task->name = 'View';
  333. //fake plugin path
  334. Plugin::load('TestTest', array('path' => APP . 'Plugin/TestTest/'));
  335. $path = APP . 'Plugin/TestTest/View/ViewTaskComments/view.ctp';
  336. $result = $this->Task->getContent('index');
  337. $this->assertNotContains('List Test Test.view Task Articles', $result);
  338. $this->Task->expects($this->once())
  339. ->method('createFile')
  340. ->with($path, $this->anything());
  341. $this->Task->bake('view', true);
  342. Plugin::unload();
  343. }
  344. /**
  345. * test bake actions baking multiple actions.
  346. *
  347. * @return void
  348. */
  349. public function testBakeActions() {
  350. $this->Task->controllerName = 'ViewTaskComments';
  351. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  352. $this->Task->expects($this->at(0))
  353. ->method('createFile')
  354. ->with(
  355. TMP . 'ViewTaskComments/view.ctp',
  356. $this->stringContains('View Task Comments')
  357. );
  358. $this->Task->expects($this->at(1))->method('createFile')
  359. ->with(
  360. TMP . 'ViewTaskComments/edit.ctp',
  361. $this->stringContains('Edit View Task Comment')
  362. );
  363. $this->Task->expects($this->at(2))->method('createFile')
  364. ->with(
  365. TMP . 'ViewTaskComments/index.ctp',
  366. $this->stringContains('ViewTaskComment')
  367. );
  368. $this->Task->bakeActions(array('view', 'edit', 'index'), array());
  369. }
  370. /**
  371. * test baking a customAction (non crud)
  372. *
  373. * @return void
  374. */
  375. public function testCustomAction() {
  376. $this->Task->controllerName = 'ViewTaskComments';
  377. $this->Task->controllerClass = __NAMESPACE__ . '\ViewTaskCommentsController';
  378. $this->Task->expects($this->any())->method('in')
  379. ->will($this->onConsecutiveCalls('', 'my_action', 'y'));
  380. $this->Task->expects($this->once())->method('createFile')
  381. ->with(
  382. TMP . 'ViewTaskComments/my_action.ctp',
  383. $this->anything()
  384. );
  385. $this->Task->customAction();
  386. }
  387. /**
  388. * Test all()
  389. *
  390. * @return void
  391. */
  392. public function testExecuteIntoAll() {
  393. $this->_setupTask(['in', 'err', 'createFile', 'all', '_stop']);
  394. $this->Task->args[0] = 'all';
  395. $this->Task->expects($this->once())
  396. ->method('all');
  397. $this->Task->execute();
  398. }
  399. /**
  400. * test `cake bake view $controller view`
  401. *
  402. * @return void
  403. */
  404. public function testExecuteWithActionParam() {
  405. $this->markTestIncomplete('Model baking will not work as models do not work.');
  406. $this->Task->args[0] = 'ViewTaskComments';
  407. $this->Task->args[1] = 'view';
  408. $this->Task->expects($this->once())->method('createFile')
  409. ->with(
  410. TMP . 'ViewTaskComments/view.ctp',
  411. $this->anything()
  412. );
  413. $this->Task->execute();
  414. }
  415. /**
  416. * test `cake bake view $controller`
  417. * Ensure that views are only baked for actions that exist in the controller.
  418. *
  419. * @return void
  420. */
  421. public function testExecuteWithController() {
  422. $this->markTestIncomplete('Model baking will not work as models do not work.');
  423. $this->Task->args[0] = 'ViewTaskComments';
  424. $this->Task->expects($this->at(0))->method('createFile')
  425. ->with(
  426. TMP . 'ViewTaskComments/index.ctp',
  427. $this->anything()
  428. );
  429. $this->Task->expects($this->at(1))->method('createFile')
  430. ->with(
  431. TMP . 'ViewTaskComments/add.ctp',
  432. $this->anything()
  433. );
  434. $this->Task->expects($this->exactly(2))->method('createFile');
  435. $this->Task->execute();
  436. }
  437. /**
  438. * static dataprovider for test cases
  439. *
  440. * @return void
  441. */
  442. public static function nameVariations() {
  443. return array(array('ViewTaskComments'), array('ViewTaskComment'), array('view_task_comment'));
  444. }
  445. /**
  446. * test `cake bake view $controller --admin`
  447. * Which only bakes admin methods, not non-admin methods.
  448. *
  449. * @return void
  450. */
  451. public function testExecuteWithControllerAndAdminFlag() {
  452. $this->Task->args[0] = 'Posts';
  453. $this->Task->params['prefix'] = 'Admin';
  454. $this->Task->expects($this->exactly(2))
  455. ->method('createFile');
  456. $views = array('index.ctp', 'add.ctp');
  457. foreach ($views as $i => $view) {
  458. $this->Task->expects($this->at($i))->method('createFile')
  459. ->with(
  460. TMP . 'Admin/Posts/' . $view,
  461. $this->anything()
  462. );
  463. }
  464. $this->Task->execute();
  465. }
  466. /**
  467. * test `cake bake view posts index list`
  468. *
  469. * @return void
  470. */
  471. public function testExecuteWithAlternateTemplates() {
  472. $this->markTestIncomplete('Model baking will not work as models do not work.');
  473. $this->Task->connection = 'test';
  474. $this->Task->args = array('ViewTaskComments', 'index', 'list');
  475. $this->Task->params = array();
  476. $this->Task->expects($this->once())->method('createFile')
  477. ->with(
  478. TMP . 'ViewTaskComments/list.ctp',
  479. $this->stringContains('ViewTaskComment')
  480. );
  481. $this->Task->execute();
  482. }
  483. /**
  484. * test getting templates, make sure noTemplateActions works and prefixed template is used before generic one.
  485. *
  486. * @return void
  487. */
  488. public function testGetTemplate() {
  489. $result = $this->Task->getTemplate('delete');
  490. $this->assertFalse($result);
  491. $result = $this->Task->getTemplate('add');
  492. $this->assertEquals('form', $result);
  493. $result = $this->Task->getTemplate('edit');
  494. $this->assertEquals('form', $result);
  495. $result = $this->Task->getTemplate('view');
  496. $this->assertEquals('view', $result);
  497. $result = $this->Task->getTemplate('index');
  498. $this->assertEquals('index', $result);
  499. }
  500. /**
  501. * Test getting prefixed views.
  502. *
  503. * @return void
  504. */
  505. public function testGetTemplatePrefixed() {
  506. $this->Task->params['prefix'] = 'Admin';
  507. $result = $this->Task->getTemplate('add');
  508. $this->assertEquals('form', $result);
  509. $this->Task->Template->templatePaths = array(
  510. 'test' => CORE_TESTS . '/test_app/TestApp/Console/Templates/test/'
  511. );
  512. $this->Task->Template->params['theme'] = 'test';
  513. $result = $this->Task->getTemplate('edit');
  514. $this->assertEquals('admin_edit', $result);
  515. $result = $this->Task->getTemplate('add');
  516. $this->assertEquals('admin_form', $result);
  517. $result = $this->Task->getTemplate('view');
  518. $this->assertEquals('view', $result);
  519. }
  520. }