ScaffoldTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 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 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. protected function _scaffold(CakeRequest $request) {
  84. $this->_params = $request;
  85. }
  86. /**
  87. * Get Params from the Controller.
  88. *
  89. * @return unknown
  90. */
  91. public 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('admin_edit', $result['action']);
  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('Scaffold :: Admin Edit :: Scaffold Mock', $result['title_for_layout']);
  187. $this->assertEquals('Scaffold Mock', $result['singularHumanName']);
  188. $this->assertEquals('Scaffold Mock', $result['pluralHumanName']);
  189. $this->assertEquals('ScaffoldMock', $result['modelClass']);
  190. $this->assertEquals('id', $result['primaryKey']);
  191. $this->assertEquals('title', $result['displayField']);
  192. $this->assertEquals('scaffoldMock', $result['singularVar']);
  193. $this->assertEquals('scaffoldMock', $result['pluralVar']);
  194. $this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated'), $result['scaffoldFields']);
  195. $this->assertArrayHasKey('plugin', $result['associations']['belongsTo']['User']);
  196. }
  197. /**
  198. * test that Scaffold overrides the view property even if its set to 'Theme'
  199. *
  200. * @return void
  201. */
  202. public function testScaffoldChangingViewProperty() {
  203. $this->Controller->action = 'edit';
  204. $this->Controller->theme = 'TestTheme';
  205. $this->Controller->viewClass = 'Theme';
  206. $this->Controller->constructClasses();
  207. new TestScaffoldMock($this->Controller, $this->Controller->request);
  208. $this->assertEquals('Scaffold', $this->Controller->viewClass);
  209. }
  210. /**
  211. * test that scaffold outputs flash messages when sessions are unset.
  212. *
  213. * @return void
  214. */
  215. public function testScaffoldFlashMessages() {
  216. $params = array(
  217. 'plugin' => null,
  218. 'pass' => array(1),
  219. 'form' => array(),
  220. 'named' => array(),
  221. 'url' => array('url' => 'scaffold_mock'),
  222. 'controller' => 'scaffold_mock',
  223. 'action' => 'edit',
  224. );
  225. $this->Controller->request->base = '';
  226. $this->Controller->request->webroot = '/';
  227. $this->Controller->request->here = '/scaffold_mock/edit';
  228. $this->Controller->request->addParams($params);
  229. //set router.
  230. Router::reload();
  231. Router::setRequestInfo($this->Controller->request);
  232. $this->Controller->request->data = array(
  233. 'ScaffoldMock' => array(
  234. 'id' => 1,
  235. 'title' => 'New title',
  236. 'body' => 'new body'
  237. )
  238. );
  239. $this->Controller->constructClasses();
  240. unset($this->Controller->Session);
  241. ob_start();
  242. new Scaffold($this->Controller, $this->Controller->request);
  243. $this->Controller->response->send();
  244. $result = ob_get_clean();
  245. $this->assertRegExp('/Scaffold Mock has been updated/', $result);
  246. }
  247. /**
  248. * test that habtm relationship keys get added to scaffoldFields.
  249. *
  250. * @return void
  251. */
  252. public function testHabtmFieldAdditionWithScaffoldForm() {
  253. CakePlugin::unload();
  254. $params = array(
  255. 'plugin' => null,
  256. 'pass' => array(1),
  257. 'form' => array(),
  258. 'named' => array(),
  259. 'url' => array('url' => 'scaffold_mock'),
  260. 'controller' => 'scaffold_mock',
  261. 'action' => 'edit',
  262. );
  263. $this->Controller->request->base = '';
  264. $this->Controller->request->webroot = '/';
  265. $this->Controller->request->here = '/scaffold_mock/edit';
  266. $this->Controller->request->addParams($params);
  267. //set router.
  268. Router::reload();
  269. Router::setRequestInfo($this->Controller->request);
  270. $this->Controller->constructClasses();
  271. ob_start();
  272. $Scaffold = new Scaffold($this->Controller, $this->Controller->request);
  273. $this->Controller->response->send();
  274. $result = ob_get_clean();
  275. $this->assertRegExp('/name="data\[ScaffoldTag\]\[ScaffoldTag\]"/', $result);
  276. $result = $Scaffold->controller->viewVars;
  277. $this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated', 'ScaffoldTag'), $result['scaffoldFields']);
  278. }
  279. /**
  280. * test that the proper names and variable values are set by Scaffold
  281. *
  282. * @return void
  283. */
  284. public function testEditScaffoldWithScaffoldFields() {
  285. $request = new CakeRequest(null, false);
  286. $this->Controller = new ScaffoldMockControllerWithFields($request);
  287. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  288. $params = array(
  289. 'plugin' => null,
  290. 'pass' => array(1),
  291. 'form' => array(),
  292. 'named' => array(),
  293. 'url' => array('url' => 'scaffold_mock/edit'),
  294. 'controller' => 'scaffold_mock',
  295. 'action' => 'edit',
  296. );
  297. $this->Controller->request->base = '';
  298. $this->Controller->request->webroot = '/';
  299. $this->Controller->request->here = '/scaffold_mock/edit';
  300. $this->Controller->request->addParams($params);
  301. //set router.
  302. Router::reload();
  303. Router::setRequestInfo($this->Controller->request);
  304. $this->Controller->constructClasses();
  305. ob_start();
  306. new Scaffold($this->Controller, $this->Controller->request);
  307. $this->Controller->response->send();
  308. $result = ob_get_clean();
  309. $this->assertNotRegExp('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result);
  310. }
  311. }