ScaffoldTest.php 9.1 KB

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