ScaffoldTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * ScaffoldTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  16. * @since CakePHP(tm) v 1.2.0.5436
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Test\TestCase\Controller;
  20. use Cake\Controller\Controller;
  21. use Cake\Controller\Scaffold;
  22. use Cake\Core\Plugin;
  23. use Cake\Network\Request;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. require_once dirname(__DIR__) . DS . 'Model/models.php';
  27. class_alias('Cake\Test\TestCase\Model\ScaffoldMock', 'Cake\Model\ScaffoldMock');
  28. /**
  29. * ScaffoldMockController class
  30. *
  31. */
  32. class ScaffoldMockController extends Controller {
  33. /**
  34. * scaffold property
  35. *
  36. * @var mixed
  37. */
  38. public $scaffold;
  39. }
  40. /**
  41. * ScaffoldMockControllerWithFields class
  42. *
  43. */
  44. class ScaffoldMockControllerWithFields extends Controller {
  45. /**
  46. * name property
  47. *
  48. * @var string
  49. */
  50. public $name = 'ScaffoldMock';
  51. /**
  52. * scaffold property
  53. *
  54. * @var mixed
  55. */
  56. public $scaffold;
  57. /**
  58. * function beforeScaffold
  59. *
  60. * @param string method
  61. */
  62. public function beforeScaffold($method) {
  63. $this->set('scaffoldFields', array('title'));
  64. return true;
  65. }
  66. }
  67. /**
  68. * TestScaffoldMock class
  69. *
  70. */
  71. class TestScaffoldMock extends Scaffold {
  72. /**
  73. * Overload _scaffold
  74. *
  75. * @param unknown_type $params
  76. */
  77. protected function _scaffold(Request $request) {
  78. $this->_params = $request;
  79. }
  80. /**
  81. * Get Params from the Controller.
  82. *
  83. * @return unknown
  84. */
  85. public function getParams() {
  86. return $this->_params;
  87. }
  88. }
  89. /**
  90. * Scaffold Test class
  91. *
  92. */
  93. class ScaffoldTest extends TestCase {
  94. /**
  95. * Controller property
  96. *
  97. * @var SecurityTestController
  98. */
  99. public $Controller;
  100. /**
  101. * fixtures property
  102. *
  103. * @var array
  104. */
  105. public $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag');
  106. /**
  107. * setUp method
  108. *
  109. * @return void
  110. */
  111. public function setUp() {
  112. parent::setUp();
  113. $this->markTestIncomplete('Need to revisit once models work again.');
  114. Configure::write('Config.language', 'eng');
  115. $request = new Request();
  116. $this->Controller = new ScaffoldMockController($request);
  117. $this->Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
  118. }
  119. /**
  120. * tearDown method
  121. *
  122. * @return void
  123. */
  124. public function tearDown() {
  125. parent::tearDown();
  126. unset($this->Controller);
  127. }
  128. /**
  129. * Test the correct Generation of Scaffold Params.
  130. * This ensures that the correct action and view will be generated
  131. *
  132. * @return void
  133. */
  134. public function testScaffoldParams() {
  135. $params = array(
  136. 'plugin' => null,
  137. 'pass' => array(),
  138. 'controller' => 'scaffold_mock',
  139. 'action' => 'admin_edit',
  140. 'admin' => true,
  141. );
  142. $this->Controller->request->base = '';
  143. $this->Controller->request->webroot = '/';
  144. $this->Controller->request->here = '/admin/scaffold_mock/edit';
  145. $this->Controller->request->addParams($params);
  146. //set router.
  147. Router::setRequestInfo($this->Controller->request);
  148. $this->Controller->constructClasses();
  149. $Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request);
  150. $result = $Scaffold->getParams();
  151. $this->assertEquals('admin_edit', $result['action']);
  152. }
  153. /**
  154. * test that the proper names and variable values are set by Scaffold
  155. *
  156. * @return void
  157. */
  158. public function testScaffoldVariableSetting() {
  159. $params = array(
  160. 'plugin' => null,
  161. 'pass' => array(),
  162. 'controller' => 'scaffold_mock',
  163. 'action' => 'admin_edit',
  164. 'admin' => true,
  165. );
  166. $this->Controller->request->base = '';
  167. $this->Controller->request->webroot = '/';
  168. $this->Controller->request->here = '/admin/scaffold_mock/edit';
  169. $this->Controller->request->addParams($params);
  170. //set router.
  171. Router::setRequestInfo($this->Controller->request);
  172. $this->Controller->constructClasses();
  173. $Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request);
  174. $result = $Scaffold->controller->viewVars;
  175. $this->assertEquals('Scaffold :: Admin Edit :: Scaffold Mock', $result['title_for_layout']);
  176. $this->assertEquals('Scaffold Mock', $result['singularHumanName']);
  177. $this->assertEquals('Scaffold Mock', $result['pluralHumanName']);
  178. $this->assertEquals('ScaffoldMock', $result['modelClass']);
  179. $this->assertEquals('id', $result['primaryKey']);
  180. $this->assertEquals('title', $result['displayField']);
  181. $this->assertEquals('scaffoldMock', $result['singularVar']);
  182. $this->assertEquals('scaffoldMock', $result['pluralVar']);
  183. $this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated'), $result['scaffoldFields']);
  184. $this->assertArrayHasKey('plugin', $result['associations']['belongsTo']['User']);
  185. }
  186. /**
  187. * test that Scaffold overrides the view property even if its set to 'Theme'
  188. *
  189. * @return void
  190. */
  191. public function testScaffoldChangingViewProperty() {
  192. $this->Controller->action = 'edit';
  193. $this->Controller->theme = 'TestTheme';
  194. $this->Controller->viewClass = 'Theme';
  195. $this->Controller->constructClasses();
  196. new TestScaffoldMock($this->Controller, $this->Controller->request);
  197. $this->assertEquals('Scaffold', $this->Controller->viewClass);
  198. }
  199. /**
  200. * test that scaffold outputs flash messages when sessions are unset.
  201. *
  202. * @return void
  203. */
  204. public function testScaffoldFlashMessages() {
  205. $params = array(
  206. 'plugin' => null,
  207. 'pass' => array(1),
  208. 'url' => array('url' => 'scaffold_mock'),
  209. 'controller' => 'scaffold_mock',
  210. 'action' => 'edit',
  211. );
  212. $this->Controller->request->base = '';
  213. $this->Controller->request->webroot = '/';
  214. $this->Controller->request->here = '/scaffold_mock/edit';
  215. $this->Controller->request->addParams($params);
  216. //set router.
  217. Router::reload();
  218. Router::setRequestInfo($this->Controller->request);
  219. $this->Controller->request->data = array(
  220. 'ScaffoldMock' => array(
  221. 'id' => 1,
  222. 'title' => 'New title',
  223. 'body' => 'new body'
  224. )
  225. );
  226. $this->Controller->constructClasses();
  227. unset($this->Controller->Session);
  228. ob_start();
  229. new Scaffold($this->Controller, $this->Controller->request);
  230. $this->Controller->response->send();
  231. $result = ob_get_clean();
  232. $this->assertRegExp('/Scaffold Mock has been updated/', $result);
  233. }
  234. /**
  235. * test that habtm relationship keys get added to scaffoldFields.
  236. *
  237. * @return void
  238. */
  239. public function testHabtmFieldAdditionWithScaffoldForm() {
  240. Plugin::unload();
  241. $params = array(
  242. 'plugin' => null,
  243. 'pass' => array(1),
  244. 'url' => array('url' => 'scaffold_mock'),
  245. 'controller' => 'scaffold_mock',
  246. 'action' => 'edit',
  247. );
  248. $this->Controller->request->base = '';
  249. $this->Controller->request->webroot = '/';
  250. $this->Controller->request->here = '/scaffold_mock/edit';
  251. $this->Controller->request->addParams($params);
  252. //set router.
  253. Router::reload();
  254. Router::setRequestInfo($this->Controller->request);
  255. $this->Controller->constructClasses();
  256. ob_start();
  257. $Scaffold = new Scaffold($this->Controller, $this->Controller->request);
  258. $this->Controller->response->send();
  259. $result = ob_get_clean();
  260. $this->assertContains('name="ScaffoldTag[ScaffoldTag]"', $result);
  261. $result = $Scaffold->controller->viewVars;
  262. $this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated', 'ScaffoldTag'), $result['scaffoldFields']);
  263. }
  264. /**
  265. * test that the proper names and variable values are set by Scaffold
  266. *
  267. * @return void
  268. */
  269. public function testEditScaffoldWithScaffoldFields() {
  270. $request = new Request();
  271. $this->Controller = new ScaffoldMockControllerWithFields($request);
  272. $this->Controller->response = $this->getMock('Cake\Network\Response', array('_sendHeader'));
  273. $params = array(
  274. 'plugin' => null,
  275. 'pass' => array(1),
  276. 'url' => array('url' => 'scaffold_mock/edit'),
  277. 'controller' => 'scaffold_mock',
  278. 'action' => 'edit',
  279. );
  280. $this->Controller->request->base = '';
  281. $this->Controller->request->webroot = '/';
  282. $this->Controller->request->here = '/scaffold_mock/edit';
  283. $this->Controller->request->addParams($params);
  284. //set router.
  285. Router::reload();
  286. Router::setRequestInfo($this->Controller->request);
  287. $this->Controller->constructClasses();
  288. ob_start();
  289. new Scaffold($this->Controller, $this->Controller->request);
  290. $this->Controller->response->send();
  291. $result = ob_get_clean();
  292. $this->assertNotRegExp('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result);
  293. }
  294. }