ScaffoldViewTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /**
  3. * ScaffoldViewTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.Controller
  16. * @since CakePHP(tm) v 2.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Controller', 'Controller');
  20. App::uses('Scaffold', 'Controller');
  21. App::uses('ScaffoldView', 'View');
  22. App::uses('AppModel', 'Model');
  23. require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php';
  24. /**
  25. * TestScaffoldView class
  26. *
  27. * @package Cake.Test.Case.Controller
  28. */
  29. class TestScaffoldView extends ScaffoldView {
  30. /**
  31. * testGetFilename method
  32. *
  33. * @param string $action
  34. * @return void
  35. */
  36. public function testGetFilename($action) {
  37. return $this->_getViewFileName($action);
  38. }
  39. }
  40. /**
  41. * ScaffoldViewMockController class
  42. *
  43. * @package Cake.Test.Case.Controller
  44. */
  45. class ScaffoldViewMockController extends Controller {
  46. /**
  47. * name property
  48. *
  49. * @var string 'ScaffoldMock'
  50. */
  51. public $name = 'ScaffoldMock';
  52. /**
  53. * scaffold property
  54. *
  55. * @var mixed
  56. */
  57. public $scaffold;
  58. }
  59. /**
  60. * ScaffoldViewTest class
  61. *
  62. * @package Cake.Test.Case.Controller
  63. */
  64. class ScaffoldViewTest extends CakeTestCase {
  65. /**
  66. * fixtures property
  67. *
  68. * @var array
  69. */
  70. public $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag');
  71. /**
  72. * setUp method
  73. *
  74. * @return void
  75. */
  76. public function setUp() {
  77. parent::setUp();
  78. $this->request = new CakeRequest(null, false);
  79. $this->Controller = new ScaffoldViewMockController($this->request);
  80. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  81. App::build(array(
  82. 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
  83. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  84. ));
  85. CakePlugin::load('TestPlugin');
  86. }
  87. /**
  88. * tearDown method
  89. *
  90. * @return void
  91. */
  92. public function tearDown() {
  93. unset($this->Controller, $this->request);
  94. parent::tearDown();
  95. }
  96. /**
  97. * testGetViewFilename method
  98. *
  99. * @return void
  100. */
  101. public function testGetViewFilename() {
  102. $_admin = Configure::read('Routing.prefixes');
  103. Configure::write('Routing.prefixes', array('admin'));
  104. $this->Controller->request->params['action'] = 'index';
  105. $ScaffoldView = new TestScaffoldView($this->Controller);
  106. $result = $ScaffoldView->testGetFilename('index');
  107. $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'index.ctp';
  108. $this->assertEquals($expected, $result);
  109. $result = $ScaffoldView->testGetFilename('edit');
  110. $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp';
  111. $this->assertEquals($expected, $result);
  112. $result = $ScaffoldView->testGetFilename('add');
  113. $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp';
  114. $this->assertEquals($expected, $result);
  115. $result = $ScaffoldView->testGetFilename('view');
  116. $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'view.ctp';
  117. $this->assertEquals($expected, $result);
  118. $result = $ScaffoldView->testGetFilename('admin_index');
  119. $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'index.ctp';
  120. $this->assertEquals($expected, $result);
  121. $result = $ScaffoldView->testGetFilename('admin_view');
  122. $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'view.ctp';
  123. $this->assertEquals($expected, $result);
  124. $result = $ScaffoldView->testGetFilename('admin_edit');
  125. $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp';
  126. $this->assertEquals($expected, $result);
  127. $result = $ScaffoldView->testGetFilename('admin_add');
  128. $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp';
  129. $this->assertEquals($expected, $result);
  130. $result = $ScaffoldView->testGetFilename('error');
  131. $expected = CAKE . 'View' . DS . 'Errors' . DS . 'scaffold_error.ctp';
  132. $this->assertEquals($expected, $result);
  133. $Controller = new ScaffoldViewMockController($this->request);
  134. $Controller->scaffold = 'admin';
  135. $Controller->viewPath = 'Posts';
  136. $Controller->request['action'] = 'admin_edit';
  137. $ScaffoldView = new TestScaffoldView($Controller);
  138. $result = $ScaffoldView->testGetFilename('admin_edit');
  139. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp';
  140. $this->assertEquals($expected, $result);
  141. $result = $ScaffoldView->testGetFilename('edit');
  142. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp';
  143. $this->assertEquals($expected, $result);
  144. $Controller = new ScaffoldViewMockController($this->request);
  145. $Controller->scaffold = 'admin';
  146. $Controller->viewPath = 'Tests';
  147. $Controller->request->addParams(array(
  148. 'plugin' => 'test_plugin',
  149. 'action' => 'admin_add',
  150. 'admin' => true
  151. ));
  152. $Controller->plugin = 'TestPlugin';
  153. $ScaffoldView = new TestScaffoldView($Controller);
  154. $result = $ScaffoldView->testGetFilename('admin_add');
  155. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' .
  156. DS . 'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'scaffold.form.ctp';
  157. $this->assertEquals($expected, $result);
  158. $result = $ScaffoldView->testGetFilename('add');
  159. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' .
  160. DS . 'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'scaffold.form.ctp';
  161. $this->assertEquals($expected, $result);
  162. Configure::write('Routing.prefixes', $_admin);
  163. }
  164. /**
  165. * test getting the view file name for themed scaffolds.
  166. *
  167. * @return void
  168. */
  169. public function testGetViewFileNameWithTheme() {
  170. $this->Controller->request['action'] = 'index';
  171. $this->Controller->viewPath = 'Posts';
  172. $this->Controller->theme = 'TestTheme';
  173. $ScaffoldView = new TestScaffoldView($this->Controller);
  174. $result = $ScaffoldView->testGetFilename('index');
  175. $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS .
  176. 'Themed' . DS . 'TestTheme' . DS . 'Posts' . DS . 'scaffold.index.ctp';
  177. $this->assertEquals($expected, $result);
  178. }
  179. /**
  180. * test default index scaffold generation
  181. *
  182. * @return void
  183. */
  184. public function testIndexScaffold() {
  185. $params = array(
  186. 'plugin' => null,
  187. 'pass' => array(),
  188. 'form' => array(),
  189. 'named' => array(),
  190. 'url' => array('url' => 'scaffold_mock'),
  191. 'controller' => 'scaffold_mock',
  192. 'action' => 'index',
  193. );
  194. $this->Controller->request->addParams($params);
  195. $this->Controller->request->webroot = '/';
  196. $this->Controller->request->base = '';
  197. $this->Controller->request->here = '/scaffold_mock/index';
  198. //set router.
  199. Router::reload();
  200. Router::setRequestInfo($this->Controller->request);
  201. $this->Controller->constructClasses();
  202. ob_start();
  203. new Scaffold($this->Controller, $this->Controller->request);
  204. $this->Controller->response->send();
  205. $result = ob_get_clean();
  206. $this->assertRegExp('#<h2>Scaffold Mock</h2>#', $result);
  207. $this->assertRegExp('#<table cellpadding="0" cellspacing="0">#', $result);
  208. $this->assertRegExp('#<a href="/scaffold_users/view/1">1</a>#', $result); //belongsTo links
  209. $this->assertRegExp('#<li><a href="/scaffold_mock/add">New Scaffold Mock</a></li>#', $result);
  210. $this->assertRegExp('#<li><a href="/scaffold_users">List Scaffold Users</a></li>#', $result);
  211. $this->assertRegExp('#<li><a href="/scaffold_comments/add">New Comment</a></li>#', $result);
  212. }
  213. /**
  214. * test default view scaffold generation
  215. *
  216. * @return void
  217. */
  218. public function testViewScaffold() {
  219. $this->Controller->request->base = '';
  220. $this->Controller->request->here = '/scaffold_mock';
  221. $this->Controller->request->webroot = '/';
  222. $params = array(
  223. 'plugin' => null,
  224. 'pass' => array(1),
  225. 'form' => array(),
  226. 'named' => array(),
  227. 'url' => array('url' => 'scaffold_mock/view/1'),
  228. 'controller' => 'scaffold_mock',
  229. 'action' => 'view',
  230. );
  231. $this->Controller->request->addParams($params);
  232. //set router.
  233. Router::reload();
  234. Router::setRequestInfo($this->Controller->request);
  235. $this->Controller->constructClasses();
  236. ob_start();
  237. new Scaffold($this->Controller, $this->Controller->request);
  238. $this->Controller->response->send();
  239. $result = ob_get_clean();
  240. $this->assertRegExp('/<h2>View Scaffold Mock<\/h2>/', $result);
  241. $this->assertRegExp('/<dl>/', $result);
  242. $this->assertRegExp('/<a href="\/scaffold_users\/view\/1">1<\/a>/', $result); //belongsTo links
  243. $this->assertRegExp('/<li><a href="\/scaffold_mock\/edit\/1">Edit Scaffold Mock<\/a>\s<\/li>/', $result);
  244. $this->assertRegExp('/<a href="\#" onclick="if[^>]*>Delete Scaffold Mock<\/a>\s<\/li>/', $result);
  245. //check related table
  246. $this->assertRegExp('/<div class="related">\s*<h3>Related Scaffold Comments<\/h3>\s*<table cellpadding="0" cellspacing="0">/', $result);
  247. $this->assertRegExp('/<li><a href="\/scaffold_comments\/add">New Comment<\/a><\/li>/', $result);
  248. $this->assertNotRegExp('/<th>JoinThing<\/th>/', $result);
  249. }
  250. /**
  251. * test default view scaffold generation
  252. *
  253. * @return void
  254. */
  255. public function testEditScaffold() {
  256. $this->Controller->request->base = '';
  257. $this->Controller->request->webroot = '/';
  258. $this->Controller->request->here = '/scaffold_mock/edit/1';
  259. $params = array(
  260. 'plugin' => null,
  261. 'pass' => array(1),
  262. 'form' => array(),
  263. 'named' => array(),
  264. 'url' => array('url' => 'scaffold_mock'),
  265. 'controller' => 'scaffold_mock',
  266. 'action' => 'edit',
  267. );
  268. $this->Controller->request->addParams($params);
  269. //set router.
  270. Router::reload();
  271. Router::setRequestInfo($this->Controller->request);
  272. $this->Controller->constructClasses();
  273. ob_start();
  274. new Scaffold($this->Controller, $this->Controller->request);
  275. $this->Controller->response->send();
  276. $result = ob_get_clean();
  277. $this->assertContains('<form action="/scaffold_mock/edit/1" id="ScaffoldMockEditForm" method="post"', $result);
  278. $this->assertContains('<legend>Edit Scaffold Mock</legend>', $result);
  279. $this->assertContains('input type="hidden" name="data[ScaffoldMock][id]" value="1" id="ScaffoldMockId"', $result);
  280. $this->assertContains('select name="data[ScaffoldMock][user_id]" id="ScaffoldMockUserId"', $result);
  281. $this->assertContains('input name="data[ScaffoldMock][title]" maxlength="255" type="text" value="First Article" id="ScaffoldMockTitle"', $result);
  282. $this->assertContains('input name="data[ScaffoldMock][published]" maxlength="1" type="text" value="Y" id="ScaffoldMockPublished"', $result);
  283. $this->assertContains('textarea name="data[ScaffoldMock][body]" cols="30" rows="6" id="ScaffoldMockBody"', $result);
  284. $this->assertRegExp('/<a href="\#" onclick="if[^>]*>Delete<\/a><\/li>/', $result);
  285. }
  286. /**
  287. * Test Admin Index Scaffolding.
  288. *
  289. * @return void
  290. */
  291. public function testAdminIndexScaffold() {
  292. $_backAdmin = Configure::read('Routing.prefixes');
  293. Configure::write('Routing.prefixes', array('admin'));
  294. $params = array(
  295. 'plugin' => null,
  296. 'pass' => array(),
  297. 'form' => array(),
  298. 'named' => array(),
  299. 'prefix' => 'admin',
  300. 'url' => array('url' => 'admin/scaffold_mock'),
  301. 'controller' => 'scaffold_mock',
  302. 'action' => 'admin_index',
  303. 'admin' => 1,
  304. );
  305. $this->Controller->request->base = '';
  306. $this->Controller->request->webroot = '/';
  307. $this->Controller->request->here = '/admin/scaffold_mock';
  308. $this->Controller->request->addParams($params);
  309. //reset, and set router.
  310. Router::reload();
  311. Router::setRequestInfo($this->Controller->request);
  312. $this->Controller->scaffold = 'admin';
  313. $this->Controller->constructClasses();
  314. ob_start();
  315. new Scaffold($this->Controller, $this->Controller->request);
  316. $this->Controller->response->send();
  317. $result = ob_get_clean();
  318. $this->assertRegExp('/<h2>Scaffold Mock<\/h2>/', $result);
  319. $this->assertRegExp('/<table cellpadding="0" cellspacing="0">/', $result);
  320. $this->assertRegExp('/<li><a href="\/admin\/scaffold_mock\/add">New Scaffold Mock<\/a><\/li>/', $result);
  321. Configure::write('Routing.prefixes', $_backAdmin);
  322. }
  323. /**
  324. * Test Admin Index Scaffolding.
  325. *
  326. * @return void
  327. */
  328. public function testAdminEditScaffold() {
  329. Configure::write('Routing.prefixes', array('admin'));
  330. $params = array(
  331. 'plugin' => null,
  332. 'pass' => array(1),
  333. 'form' => array(),
  334. 'named' => array(),
  335. 'prefix' => 'admin',
  336. 'url' => array('url' => 'admin/scaffold_mock/edit/1'),
  337. 'controller' => 'scaffold_mock',
  338. 'action' => 'admin_edit',
  339. 'admin' => 1,
  340. );
  341. $this->Controller->request->base = '';
  342. $this->Controller->request->webroot = '/';
  343. $this->Controller->request->here = '/admin/scaffold_mock/edit/1';
  344. $this->Controller->request->addParams($params);
  345. //reset, and set router.
  346. Router::reload();
  347. Router::setRequestInfo($this->Controller->request);
  348. $this->Controller->scaffold = 'admin';
  349. $this->Controller->constructClasses();
  350. ob_start();
  351. new Scaffold($this->Controller, $this->Controller->request);
  352. $this->Controller->response->send();
  353. $result = ob_get_clean();
  354. $this->assertRegExp('#admin/scaffold_mock/edit/1#', $result);
  355. $this->assertRegExp('#Scaffold Mock#', $result);
  356. }
  357. /**
  358. * Test Admin Index Scaffolding.
  359. *
  360. * @return void
  361. */
  362. public function testMultiplePrefixScaffold() {
  363. $_backAdmin = Configure::read('Routing.prefixes');
  364. Configure::write('Routing.prefixes', array('admin', 'member'));
  365. $params = array(
  366. 'plugin' => null,
  367. 'pass' => array(),
  368. 'form' => array(),
  369. 'named' => array(),
  370. 'prefix' => 'member',
  371. 'url' => array('url' => 'member/scaffold_mock'),
  372. 'controller' => 'scaffold_mock',
  373. 'action' => 'member_index',
  374. 'member' => 1,
  375. );
  376. $this->Controller->request->base = '';
  377. $this->Controller->request->webroot = '/';
  378. $this->Controller->request->here = '/member/scaffold_mock';
  379. $this->Controller->request->addParams($params);
  380. //reset, and set router.
  381. Router::reload();
  382. Router::setRequestInfo($this->Controller->request);
  383. $this->Controller->scaffold = 'member';
  384. $this->Controller->constructClasses();
  385. ob_start();
  386. new Scaffold($this->Controller, $this->Controller->request);
  387. $this->Controller->response->send();
  388. $result = ob_get_clean();
  389. $this->assertRegExp('/<h2>Scaffold Mock<\/h2>/', $result);
  390. $this->assertRegExp('/<table cellpadding="0" cellspacing="0">/', $result);
  391. $this->assertRegExp('/<li><a href="\/member\/scaffold_mock\/add">New Scaffold Mock<\/a><\/li>/', $result);
  392. Configure::write('Routing.prefixes', $_backAdmin);
  393. }
  394. }