ScaffoldTest.php 9.1 KB

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