ScaffoldTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 Method name.
  59. * @return bool true
  60. */
  61. public function beforeScaffold($method) {
  62. $this->set('scaffoldFields', array('title'));
  63. return true;
  64. }
  65. }
  66. /**
  67. * ScaffoldMockControllerWithError class
  68. *
  69. * @package Cake.Test.Case.Controller
  70. */
  71. class ScaffoldMockControllerWithError extends Controller {
  72. /**
  73. * name property
  74. *
  75. * @var string
  76. */
  77. public $name = 'ScaffoldMock';
  78. /**
  79. * scaffold property
  80. *
  81. * @var mixed
  82. */
  83. public $scaffold;
  84. /**
  85. * function beforeScaffold
  86. *
  87. * @param string $method Method name.
  88. * @return bool false
  89. */
  90. public function beforeScaffold($method) {
  91. return false;
  92. }
  93. }
  94. /**
  95. * TestScaffoldMock class
  96. *
  97. * @package Cake.Test.Case.Controller
  98. */
  99. class TestScaffoldMock extends Scaffold {
  100. /**
  101. * Overload _scaffold
  102. *
  103. * @param CakeRequest $request Request object for scaffolding
  104. * @return void
  105. */
  106. protected function _scaffold(CakeRequest $request) {
  107. $this->_params = $request;
  108. }
  109. /**
  110. * Get Params from the Controller.
  111. *
  112. * @return unknown
  113. */
  114. public function getParams() {
  115. return $this->_params;
  116. }
  117. }
  118. /**
  119. * Scaffold Test class
  120. *
  121. * @package Cake.Test.Case.Controller
  122. */
  123. class ScaffoldTest extends CakeTestCase {
  124. /**
  125. * Controller property
  126. *
  127. * @var SecurityTestController
  128. */
  129. public $Controller;
  130. /**
  131. * fixtures property
  132. *
  133. * @var array
  134. */
  135. public $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag');
  136. /**
  137. * setUp method
  138. *
  139. * @return void
  140. */
  141. public function setUp() {
  142. parent::setUp();
  143. Configure::write('Config.language', 'eng');
  144. $request = new CakeRequest(null, false);
  145. $this->Controller = new ScaffoldMockController($request);
  146. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  147. }
  148. /**
  149. * tearDown method
  150. *
  151. * @return void
  152. */
  153. public function tearDown() {
  154. parent::tearDown();
  155. unset($this->Controller);
  156. }
  157. /**
  158. * Test the correct Generation of Scaffold Params.
  159. * This ensures that the correct action and view will be generated
  160. *
  161. * @return void
  162. */
  163. public function testScaffoldParams() {
  164. $params = array(
  165. 'plugin' => null,
  166. 'pass' => array(),
  167. 'form' => array(),
  168. 'named' => array(),
  169. 'url' => array('url' => 'admin/scaffold_mock/edit'),
  170. 'controller' => 'scaffold_mock',
  171. 'action' => 'admin_edit',
  172. 'admin' => true,
  173. );
  174. $this->Controller->request->base = '';
  175. $this->Controller->request->webroot = '/';
  176. $this->Controller->request->here = '/admin/scaffold_mock/edit';
  177. $this->Controller->request->addParams($params);
  178. //set router.
  179. Router::setRequestInfo($this->Controller->request);
  180. $this->Controller->constructClasses();
  181. $Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request);
  182. $result = $Scaffold->getParams();
  183. $this->assertEquals('admin_edit', $result['action']);
  184. }
  185. /**
  186. * test that the proper names and variable values are set by Scaffold
  187. *
  188. * @return void
  189. */
  190. public function testScaffoldVariableSetting() {
  191. $params = array(
  192. 'plugin' => null,
  193. 'pass' => array(),
  194. 'form' => array(),
  195. 'named' => array(),
  196. 'url' => array('url' => 'admin/scaffold_mock/edit'),
  197. 'controller' => 'scaffold_mock',
  198. 'action' => 'admin_edit',
  199. 'admin' => true,
  200. );
  201. $this->Controller->request->base = '';
  202. $this->Controller->request->webroot = '/';
  203. $this->Controller->request->here = '/admin/scaffold_mock/edit';
  204. $this->Controller->request->addParams($params);
  205. //set router.
  206. Router::setRequestInfo($this->Controller->request);
  207. $this->Controller->constructClasses();
  208. $Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request);
  209. $result = $Scaffold->controller->viewVars;
  210. $this->assertEquals('Scaffold :: Admin Edit :: Scaffold Mock', $result['title_for_layout']);
  211. $this->assertEquals('Scaffold Mock', $result['singularHumanName']);
  212. $this->assertEquals('Scaffold Mock', $result['pluralHumanName']);
  213. $this->assertEquals('ScaffoldMock', $result['modelClass']);
  214. $this->assertEquals('id', $result['primaryKey']);
  215. $this->assertEquals('title', $result['displayField']);
  216. $this->assertEquals('scaffoldMock', $result['singularVar']);
  217. $this->assertEquals('scaffoldMock', $result['pluralVar']);
  218. $this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated'), $result['scaffoldFields']);
  219. $this->assertArrayHasKey('plugin', $result['associations']['belongsTo']['User']);
  220. }
  221. /**
  222. * test that Scaffold overrides the view property even if its set to 'Theme'
  223. *
  224. * @return void
  225. */
  226. public function testScaffoldChangingViewProperty() {
  227. $this->Controller->action = 'edit';
  228. $this->Controller->theme = 'TestTheme';
  229. $this->Controller->viewClass = 'Theme';
  230. $this->Controller->constructClasses();
  231. new TestScaffoldMock($this->Controller, $this->Controller->request);
  232. $this->assertEquals('Scaffold', $this->Controller->viewClass);
  233. }
  234. /**
  235. * test that scaffold outputs flash messages when sessions are unset.
  236. *
  237. * @return void
  238. */
  239. public function testScaffoldFlashMessages() {
  240. $params = array(
  241. 'plugin' => null,
  242. 'pass' => array(1),
  243. 'form' => array(),
  244. 'named' => array(),
  245. 'url' => array('url' => 'scaffold_mock'),
  246. 'controller' => 'scaffold_mock',
  247. 'action' => 'edit',
  248. );
  249. $this->Controller->request->base = '';
  250. $this->Controller->request->webroot = '/';
  251. $this->Controller->request->here = '/scaffold_mock/edit';
  252. $this->Controller->request->addParams($params);
  253. //set router.
  254. Router::reload();
  255. Router::setRequestInfo($this->Controller->request);
  256. $this->Controller->request->data = array(
  257. 'ScaffoldMock' => array(
  258. 'id' => 1,
  259. 'title' => 'New title',
  260. 'body' => 'new body'
  261. )
  262. );
  263. $this->Controller->constructClasses();
  264. unset($this->Controller->Session);
  265. ob_start();
  266. new Scaffold($this->Controller, $this->Controller->request);
  267. $this->Controller->response->send();
  268. $result = ob_get_clean();
  269. $this->assertRegExp('/Scaffold Mock has been updated/', $result);
  270. }
  271. /**
  272. * test that habtm relationship keys get added to scaffoldFields.
  273. *
  274. * @return void
  275. */
  276. public function testHabtmFieldAdditionWithScaffoldForm() {
  277. CakePlugin::unload();
  278. $params = array(
  279. 'plugin' => null,
  280. 'pass' => array(1),
  281. 'form' => array(),
  282. 'named' => array(),
  283. 'url' => array('url' => 'scaffold_mock'),
  284. 'controller' => 'scaffold_mock',
  285. 'action' => 'edit',
  286. );
  287. $this->Controller->request->base = '';
  288. $this->Controller->request->webroot = '/';
  289. $this->Controller->request->here = '/scaffold_mock/edit';
  290. $this->Controller->request->addParams($params);
  291. //set router.
  292. Router::reload();
  293. Router::setRequestInfo($this->Controller->request);
  294. $this->Controller->constructClasses();
  295. ob_start();
  296. $Scaffold = new Scaffold($this->Controller, $this->Controller->request);
  297. $this->Controller->response->send();
  298. $result = ob_get_clean();
  299. $this->assertRegExp('/name="data\[ScaffoldTag\]\[ScaffoldTag\]"/', $result);
  300. $result = $Scaffold->controller->viewVars;
  301. $this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated', 'ScaffoldTag'), $result['scaffoldFields']);
  302. }
  303. /**
  304. * test that the proper names and variable values are set by Scaffold
  305. *
  306. * @return void
  307. */
  308. public function testEditScaffoldWithScaffoldFields() {
  309. $request = new CakeRequest(null, false);
  310. $this->Controller = new ScaffoldMockControllerWithFields($request);
  311. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  312. $params = array(
  313. 'plugin' => null,
  314. 'pass' => array(1),
  315. 'form' => array(),
  316. 'named' => array(),
  317. 'url' => array('url' => 'scaffold_mock/edit'),
  318. 'controller' => 'scaffold_mock',
  319. 'action' => 'edit',
  320. );
  321. $this->Controller->request->base = '';
  322. $this->Controller->request->webroot = '/';
  323. $this->Controller->request->here = '/scaffold_mock/edit';
  324. $this->Controller->request->addParams($params);
  325. //set router.
  326. Router::reload();
  327. Router::setRequestInfo($this->Controller->request);
  328. $this->Controller->constructClasses();
  329. ob_start();
  330. new Scaffold($this->Controller, $this->Controller->request);
  331. $this->Controller->response->send();
  332. $result = ob_get_clean();
  333. $this->assertNotRegExp('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result);
  334. }
  335. /**
  336. * test in case of scaffold error
  337. *
  338. * @return void
  339. */
  340. public function testScaffoldError() {
  341. $request = new CakeRequest(null, false);
  342. $this->Controller = new ScaffoldMockControllerWithError($request);
  343. $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
  344. $params = array(
  345. 'plugin' => null,
  346. 'pass' => array(1),
  347. 'form' => array(),
  348. 'named' => array(),
  349. 'url' => array('url' => 'scaffold_mock/edit'),
  350. 'controller' => 'scaffold_mock',
  351. 'action' => 'edit',
  352. );
  353. $this->Controller->request->base = '';
  354. $this->Controller->request->webroot = '/';
  355. $this->Controller->request->here = '/scaffold_mock/edit';
  356. $this->Controller->request->addParams($params);
  357. //set router.
  358. Router::reload();
  359. Router::setRequestInfo($this->Controller->request);
  360. $this->Controller->constructClasses();
  361. ob_start();
  362. new Scaffold($this->Controller, $this->Controller->request);
  363. $this->Controller->response->send();
  364. $result = ob_get_clean();
  365. $this->assertRegExp('/Scaffold Error/', $result);
  366. }
  367. }