ScaffoldViewTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  11. * @since CakePHP(tm) v 2.0
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. namespace Cake\Test\TestCase\View;
  15. use Cake\Controller\Controller;
  16. use Cake\Controller\Scaffold;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Network\Request;
  21. use Cake\Routing\Router;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\View\ScaffoldView;
  24. use TestApp\Controller\ScaffoldArticlesController;
  25. require_once dirname(__DIR__) . DS . 'Model' . DS . 'models.php';
  26. if (!class_exists('Cake\Model\ScaffoldMock')) {
  27. class_alias('Cake\Test\TestCase\Model\ScaffoldMock', 'Cake\Model\ScaffoldMock');
  28. }
  29. /**
  30. * TestScaffoldView class
  31. *
  32. */
  33. class TestScaffoldView extends ScaffoldView {
  34. /**
  35. * testGetFilename method
  36. *
  37. * @param string $action
  38. * @return void
  39. */
  40. public function testGetFilename($action) {
  41. return $this->_getViewFileName($action);
  42. }
  43. }
  44. /**
  45. * ScaffoldViewTest class
  46. *
  47. */
  48. class ScaffoldViewTest extends TestCase {
  49. /**
  50. * fixtures property
  51. *
  52. * @var array
  53. */
  54. public $fixtures = [
  55. 'core.article',
  56. 'core.user',
  57. 'core.comment',
  58. 'core.articles_tag',
  59. 'core.tag'
  60. ];
  61. /**
  62. * setUp method
  63. *
  64. * @return void
  65. */
  66. public function setUp() {
  67. parent::setUp();
  68. $this->markTestIncomplete('Need to revisit once models work again.');
  69. Configure::write('App.namespace', 'TestApp');
  70. Router::connect('/:controller/:action/*');
  71. Router::connect('/:controller', ['action' => 'index']);
  72. $this->request = new Request();
  73. $this->Controller = new ScaffoldArticlesController($this->request);
  74. $this->Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
  75. Plugin::load('TestPlugin');
  76. }
  77. /**
  78. * tearDown method
  79. *
  80. * @return void
  81. */
  82. public function tearDown() {
  83. unset($this->Controller, $this->request);
  84. parent::tearDown();
  85. }
  86. /**
  87. * testGetViewFilename method
  88. *
  89. * @return void
  90. */
  91. public function testGetViewFilename() {
  92. Configure::write('Routing.prefixes', array('admin'));
  93. $this->Controller->request->params['action'] = 'index';
  94. $ScaffoldView = new TestScaffoldView($this->Controller);
  95. $result = $ScaffoldView->testGetFilename('index');
  96. $expected = CAKE . 'View' . DS . 'Scaffold' . DS . 'index.ctp';
  97. $this->assertEquals($expected, $result);
  98. $result = $ScaffoldView->testGetFilename('edit');
  99. $expected = CAKE . 'View' . DS . 'Scaffold' . DS . 'form.ctp';
  100. $this->assertEquals($expected, $result);
  101. $result = $ScaffoldView->testGetFilename('add');
  102. $expected = CAKE . 'View' . DS . 'Scaffold' . DS . 'form.ctp';
  103. $this->assertEquals($expected, $result);
  104. $result = $ScaffoldView->testGetFilename('view');
  105. $expected = CAKE . 'View' . DS . 'Scaffold' . DS . 'view.ctp';
  106. $this->assertEquals($expected, $result);
  107. $result = $ScaffoldView->testGetFilename('admin_index');
  108. $expected = CAKE . 'View' . DS . 'Scaffold' . DS . 'index.ctp';
  109. $this->assertEquals($expected, $result);
  110. $result = $ScaffoldView->testGetFilename('admin_view');
  111. $expected = CAKE . 'View' . DS . 'Scaffold' . DS . 'view.ctp';
  112. $this->assertEquals($expected, $result);
  113. $result = $ScaffoldView->testGetFilename('admin_edit');
  114. $expected = CAKE . 'View' . DS . 'Scaffold' . DS . 'form.ctp';
  115. $this->assertEquals($expected, $result);
  116. $result = $ScaffoldView->testGetFilename('admin_add');
  117. $expected = CAKE . 'View' . DS . 'Scaffold' . DS . 'form.ctp';
  118. $this->assertEquals($expected, $result);
  119. $result = $ScaffoldView->testGetFilename('error');
  120. $expected = CAKE . 'View' . DS . 'Error' . DS . 'scaffold_error.ctp';
  121. $this->assertEquals($expected, $result);
  122. $Controller = new ScaffoldArticlesController($this->request);
  123. $Controller->scaffold = 'admin';
  124. $Controller->viewPath = 'Posts';
  125. $Controller->request['action'] = 'admin_edit';
  126. $ScaffoldView = new TestScaffoldView($Controller);
  127. $result = $ScaffoldView->testGetFilename('admin_edit');
  128. $expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp';
  129. $this->assertEquals($expected, $result);
  130. $result = $ScaffoldView->testGetFilename('edit');
  131. $expected = CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp';
  132. $this->assertEquals($expected, $result);
  133. $Controller = new ScaffoldArticlesController($this->request);
  134. $Controller->scaffold = 'admin';
  135. $Controller->viewPath = 'Tests';
  136. $Controller->request->addParams(array(
  137. 'plugin' => 'test_plugin',
  138. 'action' => 'admin_add',
  139. 'admin' => true
  140. ));
  141. $Controller->plugin = 'TestPlugin';
  142. $ScaffoldView = new TestScaffoldView($Controller);
  143. $pluginPath = CAKE . 'Test' . DS . 'TestApp' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
  144. $result = $ScaffoldView->testGetFilename('admin_add');
  145. $expected = $pluginPath . 'View' . DS . 'Tests' . DS . 'scaffold.form.ctp';
  146. $this->assertEquals($expected, $result);
  147. $result = $ScaffoldView->testGetFilename('add');
  148. $expected = $pluginPath . 'View' . DS . 'Tests' . DS . 'scaffold.form.ctp';
  149. $this->assertEquals($expected, $result);
  150. }
  151. /**
  152. * test getting the view file name for themed scaffolds.
  153. *
  154. * @return void
  155. */
  156. public function testGetViewFileNameWithTheme() {
  157. $this->Controller->request['action'] = 'index';
  158. $this->Controller->viewPath = 'Posts';
  159. $this->Controller->theme = 'TestTheme';
  160. $ScaffoldView = new TestScaffoldView($this->Controller);
  161. $themePath = CAKE . 'Test' . DS . 'TestApp' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS;
  162. $result = $ScaffoldView->testGetFilename('index');
  163. $expected = $themePath . 'Posts' . DS . 'scaffold.index.ctp';
  164. $this->assertEquals($expected, $result);
  165. }
  166. /**
  167. * test default index scaffold generation
  168. *
  169. * @return void
  170. */
  171. public function testIndexScaffold() {
  172. $params = [
  173. 'plugin' => null,
  174. 'pass' => [],
  175. 'controller' => 'articles',
  176. 'action' => 'index',
  177. ];
  178. $this->Controller->request->addParams($params);
  179. $this->Controller->request->webroot = '/';
  180. $this->Controller->request->base = '';
  181. $this->Controller->request->here = '/articles/index';
  182. Router::pushRequest($this->Controller->request);
  183. $this->Controller->constructClasses();
  184. ob_start();
  185. new Scaffold($this->Controller, $this->Controller->request);
  186. $this->Controller->response->send();
  187. $result = ob_get_clean();
  188. $this->assertRegExp('#<h2>Articles</h2>#', $result);
  189. $this->assertRegExp('#<table cellpadding="0" cellspacing="0">#', $result);
  190. $this->assertRegExp('#<a href="/users/view/1">1</a>#', $result); //belongsTo links
  191. $this->assertRegExp('#<li><a href="/articles/add">New Article</a></li>#', $result);
  192. $this->assertRegExp('#<li><a href="/users">List Users</a></li>#', $result);
  193. $this->assertRegExp('#<li><a href="/comments/add">New Comment</a></li>#', $result);
  194. }
  195. /**
  196. * test default view scaffold generation
  197. *
  198. * @return void
  199. */
  200. public function testViewScaffold() {
  201. $this->Controller->request->base = '';
  202. $this->Controller->request->webroot = '/';
  203. $params = [
  204. 'plugin' => null,
  205. 'pass' => [1],
  206. 'controller' => 'articles',
  207. 'action' => 'view',
  208. ];
  209. $this->Controller->request->addParams($params);
  210. //set router.
  211. Router::pushRequest($this->Controller->request);
  212. $this->Controller->constructClasses();
  213. ob_start();
  214. new Scaffold($this->Controller, $this->Controller->request);
  215. $this->Controller->response->send();
  216. $result = ob_get_clean();
  217. $this->assertRegExp('#<h2>View Article</h2>#', $result);
  218. $this->assertRegExp('#<dl>#', $result);
  219. $this->assertRegExp('#<a href="/users/view/1">1</a>#', $result); //belongsTo links
  220. $this->assertRegExp('#<li><a href="/articles/edit/1">Edit Article</a>\s*</li>#', $result);
  221. $this->assertRegExp('#<a href="\#" onclick="if[^>]*>Delete Article</a>\s*</li>#', $result);
  222. //check related table
  223. $this->assertRegExp('#<div class="related">\s*<h3>Related Comments</h3>\s*<table cellpadding="0" cellspacing="0">#', $result);
  224. $this->assertRegExp('#<li><a href="/comments/add">New Comment</a></li>#', $result);
  225. $this->assertNotRegExp('#<th>JoinThing</th>#', $result);
  226. }
  227. /**
  228. * test default edit scaffold generation
  229. *
  230. * @return void
  231. */
  232. public function testEditScaffold() {
  233. $this->Controller->request->base = '';
  234. $this->Controller->request->webroot = '/';
  235. $this->Controller->request->here = '/articles/edit/1';
  236. $params = [
  237. 'plugin' => null,
  238. 'pass' => [1],
  239. 'controller' => 'articles',
  240. 'action' => 'edit',
  241. ];
  242. $this->Controller->request->addParams($params);
  243. //set router.
  244. Router::pushRequest($this->Controller->request);
  245. $this->Controller->constructClasses();
  246. ob_start();
  247. new Scaffold($this->Controller, $this->Controller->request);
  248. $this->Controller->response->send();
  249. $result = ob_get_clean();
  250. $this->assertContains('<form action="/articles/edit/1" id="ArticleEditForm" method="post"', $result);
  251. $this->assertContains('<legend>Edit Article</legend>', $result);
  252. $this->assertContains('input type="hidden" name="Article[id]" value="1" id="ArticleId"', $result);
  253. $this->assertContains('select name="Article[user_id]" id="ArticleUserId"', $result);
  254. $this->assertContains('input name="Article[title]" maxlength="255" type="text" value="First Article" id="ArticleTitle"', $result);
  255. $this->assertContains('input name="Article[published]" maxlength="1" type="text" value="Y" id="ArticlePublished"', $result);
  256. $this->assertContains('textarea name="Article[body]" cols="30" rows="6" id="ArticleBody"', $result);
  257. $this->assertRegExp('/<a href="\#" onclick="if[^>]*>Delete<\/a><\/li>/', $result);
  258. }
  259. }