PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Test/Case/Controller/ScaffoldTest.php

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