PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/tests/cases/libs/controller/scaffold.test.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 897 lines | 498 code | 85 blank | 314 comment | 0 complexity | c5c5e9618f154c728ec746f9bb467cdb MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * ScaffoldTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs.controller
  17. * @since CakePHP(tm) v 1.2.0.5436
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. App::import('Core', 'Scaffold');
  21. /**
  22. * ScaffoldMockController class
  23. *
  24. * @package cake
  25. * @subpackage cake.tests.cases.libs.controller
  26. */
  27. class ScaffoldMockController extends Controller {
  28. /**
  29. * name property
  30. *
  31. * @var string 'ScaffoldMock'
  32. * @access public
  33. */
  34. var $name = 'ScaffoldMock';
  35. /**
  36. * scaffold property
  37. *
  38. * @var mixed
  39. * @access public
  40. */
  41. var $scaffold;
  42. }
  43. /**
  44. * ScaffoldMockControllerWithFields class
  45. *
  46. * @package cake
  47. * @subpackage cake.tests.cases.libs.controller
  48. */
  49. class ScaffoldMockControllerWithFields extends Controller {
  50. /**
  51. * name property
  52. *
  53. * @var string 'ScaffoldMock'
  54. * @access public
  55. */
  56. var $name = 'ScaffoldMock';
  57. /**
  58. * scaffold property
  59. *
  60. * @var mixed
  61. * @access public
  62. */
  63. var $scaffold;
  64. /**
  65. * function _beforeScaffold
  66. *
  67. * @param string method
  68. */
  69. function _beforeScaffold($method) {
  70. $this->set('scaffoldFields', array('title'));
  71. return true;
  72. }
  73. }
  74. /**
  75. * TestScaffoldMock class
  76. *
  77. * @package cake
  78. * @subpackage cake.tests.cases.libs.controller
  79. */
  80. class TestScaffoldMock extends Scaffold {
  81. /**
  82. * Overload __scaffold
  83. *
  84. * @param unknown_type $params
  85. */
  86. function __scaffold($params) {
  87. $this->_params = $params;
  88. }
  89. /**
  90. * Get Params from the Controller.
  91. *
  92. * @return unknown
  93. */
  94. function getParams() {
  95. return $this->_params;
  96. }
  97. }
  98. /**
  99. * ScaffoldMock class
  100. *
  101. * @package cake
  102. * @subpackage cake.tests.cases.libs.controller
  103. */
  104. class ScaffoldMock extends CakeTestModel {
  105. /**
  106. * useTable property
  107. *
  108. * @var string 'posts'
  109. * @access public
  110. */
  111. var $useTable = 'articles';
  112. /**
  113. * belongsTo property
  114. *
  115. * @var array
  116. * @access public
  117. */
  118. var $belongsTo = array(
  119. 'User' => array(
  120. 'className' => 'ScaffoldUser',
  121. 'foreignKey' => 'user_id',
  122. )
  123. );
  124. /**
  125. * hasMany property
  126. *
  127. * @var array
  128. * @access public
  129. */
  130. var $hasMany = array(
  131. 'Comment' => array(
  132. 'className' => 'ScaffoldComment',
  133. 'foreignKey' => 'article_id',
  134. )
  135. );
  136. /**
  137. * hasAndBelongsToMany property
  138. *
  139. * @var string
  140. */
  141. var $hasAndBelongsToMany = array(
  142. 'ScaffoldTag' => array(
  143. 'className' => 'ScaffoldTag',
  144. 'foreignKey' => 'something_id',
  145. 'associationForeignKey' => 'something_else_id',
  146. 'joinTable' => 'join_things'
  147. )
  148. );
  149. }
  150. /**
  151. * ScaffoldUser class
  152. *
  153. * @package cake
  154. * @subpackage cake.tests.cases.libs.controller
  155. */
  156. class ScaffoldUser extends CakeTestModel {
  157. /**
  158. * useTable property
  159. *
  160. * @var string 'posts'
  161. * @access public
  162. */
  163. var $useTable = 'users';
  164. /**
  165. * hasMany property
  166. *
  167. * @var array
  168. * @access public
  169. */
  170. var $hasMany = array(
  171. 'Article' => array(
  172. 'className' => 'ScaffoldMock',
  173. 'foreignKey' => 'article_id',
  174. )
  175. );
  176. }
  177. /**
  178. * ScaffoldComment class
  179. *
  180. * @package cake
  181. * @subpackage cake.tests.cases.libs.controller
  182. */
  183. class ScaffoldComment extends CakeTestModel {
  184. /**
  185. * useTable property
  186. *
  187. * @var string 'posts'
  188. * @access public
  189. */
  190. var $useTable = 'comments';
  191. /**
  192. * belongsTo property
  193. *
  194. * @var array
  195. * @access public
  196. */
  197. var $belongsTo = array(
  198. 'Article' => array(
  199. 'className' => 'ScaffoldMock',
  200. 'foreignKey' => 'article_id',
  201. )
  202. );
  203. }
  204. /**
  205. * ScaffoldTag class
  206. *
  207. * @package cake
  208. * @subpackage cake.tests.cases.libs.controller
  209. */
  210. class ScaffoldTag extends CakeTestModel {
  211. /**
  212. * useTable property
  213. *
  214. * @var string 'posts'
  215. * @access public
  216. */
  217. var $useTable = 'tags';
  218. }
  219. /**
  220. * TestScaffoldView class
  221. *
  222. * @package cake
  223. * @subpackage cake.tests.cases.libs.controller
  224. */
  225. class TestScaffoldView extends ScaffoldView {
  226. /**
  227. * testGetFilename method
  228. *
  229. * @param mixed $action
  230. * @access public
  231. * @return void
  232. */
  233. function testGetFilename($action) {
  234. return $this->_getViewFileName($action);
  235. }
  236. }
  237. /**
  238. * ScaffoldViewTest class
  239. *
  240. * @package cake
  241. * @subpackage cake.tests.cases.libs.controller
  242. */
  243. class ScaffoldViewTest extends CakeTestCase {
  244. /**
  245. * fixtures property
  246. *
  247. * @var array
  248. * @access public
  249. */
  250. var $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag');
  251. /**
  252. * startTest method
  253. *
  254. * @access public
  255. * @return void
  256. */
  257. function startTest() {
  258. $this->Controller =& new ScaffoldMockController();
  259. App::build(array(
  260. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS),
  261. 'plugins' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins' . DS)
  262. ));
  263. }
  264. /**
  265. * endTest method
  266. *
  267. * @access public
  268. * @return void
  269. */
  270. function endTest() {
  271. unset($this->Controller);
  272. App::build();
  273. }
  274. /**
  275. * testGetViewFilename method
  276. *
  277. * @access public
  278. * @return void
  279. */
  280. function testGetViewFilename() {
  281. $_admin = Configure::read('Routing.prefixes');
  282. Configure::write('Routing.prefixes', array('admin'));
  283. $this->Controller->action = 'index';
  284. $ScaffoldView =& new TestScaffoldView($this->Controller);
  285. $result = $ScaffoldView->testGetFilename('index');
  286. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'index.ctp';
  287. $this->assertEqual($result, $expected);
  288. $result = $ScaffoldView->testGetFilename('edit');
  289. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'edit.ctp';
  290. $this->assertEqual($result, $expected);
  291. $result = $ScaffoldView->testGetFilename('add');
  292. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'edit.ctp';
  293. $this->assertEqual($result, $expected);
  294. $result = $ScaffoldView->testGetFilename('view');
  295. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'view.ctp';
  296. $this->assertEqual($result, $expected);
  297. $result = $ScaffoldView->testGetFilename('admin_index');
  298. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'index.ctp';
  299. $this->assertEqual($result, $expected);
  300. $result = $ScaffoldView->testGetFilename('admin_view');
  301. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'view.ctp';
  302. $this->assertEqual($result, $expected);
  303. $result = $ScaffoldView->testGetFilename('admin_edit');
  304. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'edit.ctp';
  305. $this->assertEqual($result, $expected);
  306. $result = $ScaffoldView->testGetFilename('admin_add');
  307. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'libs' . DS . 'view' . DS . 'scaffolds' . DS . 'edit.ctp';
  308. $this->assertEqual($result, $expected);
  309. $result = $ScaffoldView->testGetFilename('error');
  310. $expected = 'cake' . DS . 'libs' . DS . 'view' . DS . 'errors' . DS . 'scaffold_error.ctp';
  311. $this->assertEqual($result, $expected);
  312. $Controller =& new ScaffoldMockController();
  313. $Controller->scaffold = 'admin';
  314. $Controller->viewPath = 'posts';
  315. $Controller->action = 'admin_edit';
  316. $ScaffoldView =& new TestScaffoldView($Controller);
  317. $result = $ScaffoldView->testGetFilename('admin_edit');
  318. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' .DS . 'views' . DS . 'posts' . DS . 'scaffold.edit.ctp';
  319. $this->assertEqual($result, $expected);
  320. $result = $ScaffoldView->testGetFilename('edit');
  321. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' .DS . 'views' . DS . 'posts' . DS . 'scaffold.edit.ctp';
  322. $this->assertEqual($result, $expected);
  323. $Controller =& new ScaffoldMockController();
  324. $Controller->scaffold = 'admin';
  325. $Controller->viewPath = 'tests';
  326. $Controller->plugin = 'test_plugin';
  327. $Controller->action = 'admin_add';
  328. $ScaffoldView =& new TestScaffoldView($Controller);
  329. $result = $ScaffoldView->testGetFilename('admin_add');
  330. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins'
  331. . DS .'test_plugin' . DS . 'views' . DS . 'tests' . DS . 'scaffold.edit.ctp';
  332. $this->assertEqual($result, $expected);
  333. $result = $ScaffoldView->testGetFilename('add');
  334. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'plugins'
  335. . DS .'test_plugin' . DS . 'views' . DS . 'tests' . DS . 'scaffold.edit.ctp';
  336. $this->assertEqual($result, $expected);
  337. Configure::write('Routing.prefixes', $_admin);
  338. }
  339. /**
  340. * test getting the view file name for themed scaffolds.
  341. *
  342. * @return void
  343. */
  344. function testGetViewFileNameWithTheme() {
  345. $this->Controller->action = 'index';
  346. $this->Controller->viewPath = 'posts';
  347. $this->Controller->theme = 'test_theme';
  348. $ScaffoldView =& new TestScaffoldView($this->Controller);
  349. $result = $ScaffoldView->testGetFilename('index');
  350. $expected = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views' . DS
  351. . 'themed' . DS . 'test_theme' . DS . 'posts' . DS . 'scaffold.index.ctp';
  352. $this->assertEqual($result, $expected);
  353. }
  354. /**
  355. * test default index scaffold generation
  356. *
  357. * @access public
  358. * @return void
  359. */
  360. function testIndexScaffold() {
  361. $this->Controller->action = 'index';
  362. $this->Controller->here = '/scaffold_mock';
  363. $this->Controller->webroot = '/';
  364. $params = array(
  365. 'plugin' => null,
  366. 'pass' => array(),
  367. 'form' => array(),
  368. 'named' => array(),
  369. 'url' => array('url' =>'scaffold_mock'),
  370. 'controller' => 'scaffold_mock',
  371. 'action' => 'index',
  372. );
  373. //set router.
  374. Router::reload();
  375. Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/')));
  376. $this->Controller->params = $params;
  377. $this->Controller->controller = 'scaffold_mock';
  378. $this->Controller->base = '/';
  379. $this->Controller->constructClasses();
  380. ob_start();
  381. new Scaffold($this->Controller, $params);
  382. $result = ob_get_clean();
  383. $this->assertPattern('#<h2>Scaffold Mock</h2>#', $result);
  384. $this->assertPattern('#<table cellpadding="0" cellspacing="0">#', $result);
  385. $this->assertPattern('#<a href="/scaffold_users/view/1">1</a>#', $result); //belongsTo links
  386. $this->assertPattern('#<li><a href="/scaffold_mock/add">New Scaffold Mock</a></li>#', $result);
  387. $this->assertPattern('#<li><a href="/scaffold_users">List Scaffold Users</a></li>#', $result);
  388. $this->assertPattern('#<li><a href="/scaffold_comments/add">New Comment</a></li>#', $result);
  389. }
  390. /**
  391. * test default view scaffold generation
  392. *
  393. * @access public
  394. * @return void
  395. */
  396. function testViewScaffold() {
  397. $this->Controller->action = 'view';
  398. $this->Controller->here = '/scaffold_mock';
  399. $this->Controller->webroot = '/';
  400. $params = array(
  401. 'plugin' => null,
  402. 'pass' => array(1),
  403. 'form' => array(),
  404. 'named' => array(),
  405. 'url' => array('url' =>'scaffold_mock'),
  406. 'controller' => 'scaffold_mock',
  407. 'action' => 'view',
  408. );
  409. //set router.
  410. Router::reload();
  411. Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/')));
  412. $this->Controller->params = $params;
  413. $this->Controller->controller = 'scaffold_mock';
  414. $this->Controller->base = '/';
  415. $this->Controller->constructClasses();
  416. ob_start();
  417. new Scaffold($this->Controller, $params);
  418. $result = ob_get_clean();
  419. $this->assertPattern('/<h2>View Scaffold Mock<\/h2>/', $result);
  420. $this->assertPattern('/<dl>/', $result);
  421. //TODO: add specific tests for fields.
  422. $this->assertPattern('/<a href="\/scaffold_users\/view\/1">1<\/a>/', $result); //belongsTo links
  423. $this->assertPattern('/<li><a href="\/scaffold_mock\/edit\/1">Edit Scaffold Mock<\/a>\s<\/li>/', $result);
  424. $this->assertPattern('/<li><a href="\/scaffold_mock\/delete\/1"[^>]*>Delete Scaffold Mock<\/a>\s*<\/li>/', $result);
  425. //check related table
  426. $this->assertPattern('/<div class="related">\s*<h3>Related Scaffold Comments<\/h3>\s*<table cellpadding="0" cellspacing="0">/', $result);
  427. $this->assertPattern('/<li><a href="\/scaffold_comments\/add">New Comment<\/a><\/li>/', $result);
  428. $this->assertNoPattern('/<th>JoinThing<\/th>/', $result);
  429. }
  430. /**
  431. * test default view scaffold generation
  432. *
  433. * @access public
  434. * @return void
  435. */
  436. function testEditScaffold() {
  437. $this->Controller->action = 'edit';
  438. $this->Controller->here = '/scaffold_mock';
  439. $this->Controller->webroot = '/';
  440. $params = array(
  441. 'plugin' => null,
  442. 'pass' => array(1),
  443. 'form' => array(),
  444. 'named' => array(),
  445. 'url' => array('url' =>'scaffold_mock'),
  446. 'controller' => 'scaffold_mock',
  447. 'action' => 'edit',
  448. );
  449. //set router.
  450. Router::reload();
  451. Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/')));
  452. $this->Controller->params = $params;
  453. $this->Controller->controller = 'scaffold_mock';
  454. $this->Controller->base = '/';
  455. $this->Controller->constructClasses();
  456. ob_start();
  457. new Scaffold($this->Controller, $params);
  458. $result = ob_get_clean();
  459. $this->assertPattern('/<form id="ScaffoldMockEditForm" method="post" action="\/scaffold_mock\/edit\/1"/', $result);
  460. $this->assertPattern('/<legend>Edit Scaffold Mock<\/legend>/', $result);
  461. $this->assertPattern('/input type="hidden" name="data\[ScaffoldMock\]\[id\]" value="1" id="ScaffoldMockId"/', $result);
  462. $this->assertPattern('/select name="data\[ScaffoldMock\]\[user_id\]" id="ScaffoldMockUserId"/', $result);
  463. $this->assertPattern('/input name="data\[ScaffoldMock\]\[title\]" type="text" maxlength="255" value="First Article" id="ScaffoldMockTitle"/', $result);
  464. $this->assertPattern('/input name="data\[ScaffoldMock\]\[published\]" type="text" maxlength="1" value="Y" id="ScaffoldMockPublished"/', $result);
  465. $this->assertPattern('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result);
  466. $this->assertPattern('/<li><a href="\/scaffold_mock\/delete\/1"[^>]*>Delete<\/a>\s*<\/li>/', $result);
  467. }
  468. /**
  469. * Test Admin Index Scaffolding.
  470. *
  471. * @access public
  472. * @return void
  473. */
  474. function testAdminIndexScaffold() {
  475. $_backAdmin = Configure::read('Routing.prefixes');
  476. Configure::write('Routing.prefixes', array('admin'));
  477. $params = array(
  478. 'plugin' => null,
  479. 'pass' => array(),
  480. 'form' => array(),
  481. 'named' => array(),
  482. 'prefix' => 'admin',
  483. 'url' => array('url' =>'admin/scaffold_mock'),
  484. 'controller' => 'scaffold_mock',
  485. 'action' => 'admin_index',
  486. 'admin' => 1,
  487. );
  488. //reset, and set router.
  489. Router::reload();
  490. Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/admin/scaffold_mock', 'webroot' => '/')));
  491. $this->Controller->params = $params;
  492. $this->Controller->controller = 'scaffold_mock';
  493. $this->Controller->base = '/';
  494. $this->Controller->action = 'admin_index';
  495. $this->Controller->here = '/tests/admin/scaffold_mock';
  496. $this->Controller->webroot = '/';
  497. $this->Controller->scaffold = 'admin';
  498. $this->Controller->constructClasses();
  499. ob_start();
  500. $Scaffold = new Scaffold($this->Controller, $params);
  501. $result = ob_get_clean();
  502. $this->assertPattern('/<h2>Scaffold Mock<\/h2>/', $result);
  503. $this->assertPattern('/<table cellpadding="0" cellspacing="0">/', $result);
  504. //TODO: add testing for table generation
  505. $this->assertPattern('/<li><a href="\/admin\/scaffold_mock\/add">New Scaffold Mock<\/a><\/li>/', $result);
  506. Configure::write('Routing.prefixes', $_backAdmin);
  507. }
  508. /**
  509. * Test Admin Index Scaffolding.
  510. *
  511. * @access public
  512. * @return void
  513. */
  514. function testAdminEditScaffold() {
  515. $_backAdmin = Configure::read('Routing.prefixes');
  516. Configure::write('Routing.prefixes', array('admin'));
  517. $params = array(
  518. 'plugin' => null,
  519. 'pass' => array(),
  520. 'form' => array(),
  521. 'named' => array(),
  522. 'prefix' => 'admin',
  523. 'url' => array('url' =>'admin/scaffold_mock/edit'),
  524. 'controller' => 'scaffold_mock',
  525. 'action' => 'admin_edit',
  526. 'admin' => 1,
  527. );
  528. //reset, and set router.
  529. Router::reload();
  530. Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/admin/scaffold_mock/edit', 'webroot' => '/')));
  531. $this->Controller->params = $params;
  532. $this->Controller->controller = 'scaffold_mock';
  533. $this->Controller->base = '/';
  534. $this->Controller->action = 'admin_index';
  535. $this->Controller->here = '/tests/admin/scaffold_mock';
  536. $this->Controller->webroot = '/';
  537. $this->Controller->scaffold = 'admin';
  538. $this->Controller->constructClasses();
  539. ob_start();
  540. $Scaffold = new Scaffold($this->Controller, $params);
  541. $result = ob_get_clean();
  542. $this->assertPattern('#admin/scaffold_mock/edit/1#', $result);
  543. $this->assertPattern('#Scaffold Mock#', $result);
  544. Configure::write('Routing.prefixes', $_backAdmin);
  545. }
  546. /**
  547. * Test Admin Index Scaffolding.
  548. *
  549. * @access public
  550. * @return void
  551. */
  552. function testMultiplePrefixScaffold() {
  553. $_backAdmin = Configure::read('Routing.prefixes');
  554. Configure::write('Routing.prefixes', array('admin', 'member'));
  555. $params = array(
  556. 'plugin' => null,
  557. 'pass' => array(),
  558. 'form' => array(),
  559. 'named' => array(),
  560. 'prefix' => 'member',
  561. 'url' => array('url' =>'member/scaffold_mock'),
  562. 'controller' => 'scaffold_mock',
  563. 'action' => 'member_index',
  564. 'member' => 1,
  565. );
  566. //reset, and set router.
  567. Router::reload();
  568. Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/member/scaffold_mock', 'webroot' => '/')));
  569. $this->Controller->params = $params;
  570. $this->Controller->controller = 'scaffold_mock';
  571. $this->Controller->base = '/';
  572. $this->Controller->action = 'member_index';
  573. $this->Controller->here = '/tests/member/scaffold_mock';
  574. $this->Controller->webroot = '/';
  575. $this->Controller->scaffold = 'member';
  576. $this->Controller->constructClasses();
  577. ob_start();
  578. $Scaffold = new Scaffold($this->Controller, $params);
  579. $result = ob_get_clean();
  580. $this->assertPattern('/<h2>Scaffold Mock<\/h2>/', $result);
  581. $this->assertPattern('/<table cellpadding="0" cellspacing="0">/', $result);
  582. //TODO: add testing for table generation
  583. $this->assertPattern('/<li><a href="\/member\/scaffold_mock\/add">New Scaffold Mock<\/a><\/li>/', $result);
  584. Configure::write('Routing.prefixes', $_backAdmin);
  585. }
  586. }
  587. /**
  588. * Scaffold Test class
  589. *
  590. * @package cake
  591. * @subpackage cake.tests.cases.libs.controller
  592. */
  593. class ScaffoldTest extends CakeTestCase {
  594. /**
  595. * Controller property
  596. *
  597. * @var SecurityTestController
  598. * @access public
  599. */
  600. var $Controller;
  601. /**
  602. * fixtures property
  603. *
  604. * @var array
  605. * @access public
  606. */
  607. var $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag');
  608. /**
  609. * startTest method
  610. *
  611. * @access public
  612. * @return void
  613. */
  614. function startTest() {
  615. $this->Controller =& new ScaffoldMockController();
  616. }
  617. /**
  618. * endTest method
  619. *
  620. * @access public
  621. * @return void
  622. */
  623. function endTest() {
  624. unset($this->Controller);
  625. }
  626. /**
  627. * Test the correct Generation of Scaffold Params.
  628. * This ensures that the correct action and view will be generated
  629. *
  630. * @access public
  631. * @return void
  632. */
  633. function testScaffoldParams() {
  634. $this->Controller->action = 'admin_edit';
  635. $this->Controller->here = '/admin/scaffold_mock/edit';
  636. $this->Controller->webroot = '/';
  637. $params = array(
  638. 'plugin' => null,
  639. 'pass' => array(),
  640. 'form' => array(),
  641. 'named' => array(),
  642. 'url' => array('url' =>'admin/scaffold_mock/edit'),
  643. 'controller' => 'scaffold_mock',
  644. 'action' => 'admin_edit',
  645. 'admin' => true,
  646. );
  647. //set router.
  648. Router::setRequestInfo(array($params, array('base' => '/', 'here' => 'admin/scaffold_mock', 'webroot' => '/')));
  649. $this->Controller->params = $params;
  650. $this->Controller->controller = 'scaffold_mock';
  651. $this->Controller->base = '/';
  652. $this->Controller->constructClasses();
  653. $Scaffold =& new TestScaffoldMock($this->Controller, $params);
  654. $result = $Scaffold->getParams();
  655. $this->assertEqual($result['action'], 'admin_edit');
  656. }
  657. /**
  658. * test that the proper names and variable values are set by Scaffold
  659. *
  660. * @return void
  661. */
  662. function testScaffoldVariableSetting() {
  663. $this->Controller->action = 'admin_edit';
  664. $this->Controller->here = '/admin/scaffold_mock/edit';
  665. $this->Controller->webroot = '/';
  666. $params = array(
  667. 'plugin' => null,
  668. 'pass' => array(),
  669. 'form' => array(),
  670. 'named' => array(),
  671. 'url' => array('url' =>'admin/scaffold_mock/edit'),
  672. 'controller' => 'scaffold_mock',
  673. 'action' => 'admin_edit',
  674. 'admin' => true,
  675. );
  676. //set router.
  677. Router::setRequestInfo(array($params, array('base' => '/', 'here' => 'admin/scaffold_mock', 'webroot' => '/')));
  678. $this->Controller->params = $params;
  679. $this->Controller->controller = 'scaffold_mock';
  680. $this->Controller->base = '/';
  681. $this->Controller->constructClasses();
  682. $Scaffold =& new TestScaffoldMock($this->Controller, $params);
  683. $result = $Scaffold->controller->viewVars;
  684. $this->assertEqual($result['title_for_layout'], 'Scaffold :: Admin Edit :: Scaffold Mock');
  685. $this->assertEqual($result['singularHumanName'], 'Scaffold Mock');
  686. $this->assertEqual($result['pluralHumanName'], 'Scaffold Mock');
  687. $this->assertEqual($result['modelClass'], 'ScaffoldMock');
  688. $this->assertEqual($result['primaryKey'], 'id');
  689. $this->assertEqual($result['displayField'], 'title');
  690. $this->assertEqual($result['singularVar'], 'scaffoldMock');
  691. $this->assertEqual($result['pluralVar'], 'scaffoldMock');
  692. $this->assertEqual($result['scaffoldFields'], array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated'));
  693. }
  694. function getTests() {
  695. return array('start', 'startCase', 'testScaffoldChangingViewProperty', 'endCase', 'end');
  696. }
  697. /**
  698. * test that Scaffold overrides the view property even if its set to 'Theme'
  699. *
  700. * @return void
  701. */
  702. function testScaffoldChangingViewProperty() {
  703. $this->Controller->action = 'edit';
  704. $this->Controller->theme = 'test_theme';
  705. $this->Controller->view = 'Theme';
  706. $this->Controller->constructClasses();
  707. $Scaffold =& new TestScaffoldMock($this->Controller, array());
  708. $this->assertEqual($this->Controller->view, 'Scaffold');
  709. }
  710. /**
  711. * test that scaffold outputs flash messages when sessions are unset.
  712. *
  713. * @return void
  714. */
  715. function testScaffoldFlashMessages() {
  716. $this->Controller->action = 'edit';
  717. $this->Controller->here = '/scaffold_mock';
  718. $this->Controller->webroot = '/';
  719. $params = array(
  720. 'plugin' => null,
  721. 'pass' => array(1),
  722. 'form' => array(),
  723. 'named' => array(),
  724. 'url' => array('url' =>'scaffold_mock'),
  725. 'controller' => 'scaffold_mock',
  726. 'action' => 'edit',
  727. );
  728. //set router.
  729. Router::reload();
  730. Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/')));
  731. $this->Controller->params = $params;
  732. $this->Controller->controller = 'scaffold_mock';
  733. $this->Controller->base = '/';
  734. $this->Controller->data = array(
  735. 'ScaffoldMock' => array(
  736. 'id' => 1,
  737. 'title' => 'New title',
  738. 'body' => 'new body'
  739. )
  740. );
  741. $this->Controller->constructClasses();
  742. unset($this->Controller->Session);
  743. ob_start();
  744. new Scaffold($this->Controller, $params);
  745. $result = ob_get_clean();
  746. $this->assertPattern('/Scaffold Mock has been updated/', $result);
  747. }
  748. /**
  749. * test that habtm relationship keys get added to scaffoldFields.
  750. *
  751. * @see http://code.cakephp.org/tickets/view/48
  752. * @return void
  753. */
  754. function testHabtmFieldAdditionWithScaffoldForm() {
  755. $this->Controller->action = 'edit';
  756. $this->Controller->here = '/scaffold_mock';
  757. $this->Controller->webroot = '/';
  758. $params = array(
  759. 'plugin' => null,
  760. 'pass' => array(1),
  761. 'form' => array(),
  762. 'named' => array(),
  763. 'url' => array('url' =>'scaffold_mock'),
  764. 'controller' => 'scaffold_mock',
  765. 'action' => 'edit',
  766. );
  767. //set router.
  768. Router::reload();
  769. Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/')));
  770. $this->Controller->params = $params;
  771. $this->Controller->controller = 'scaffold_mock';
  772. $this->Controller->base = '/';
  773. $this->Controller->constructClasses();
  774. ob_start();
  775. $Scaffold = new Scaffold($this->Controller, $params);
  776. $result = ob_get_clean();
  777. $this->assertPattern('/name="data\[ScaffoldTag\]\[ScaffoldTag\]"/', $result);
  778. $result = $Scaffold->controller->viewVars;
  779. $this->assertEqual($result['scaffoldFields'], array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated', 'ScaffoldTag'));
  780. }
  781. /**
  782. * test that the proper names and variable values are set by Scaffold
  783. *
  784. * @return void
  785. */
  786. function testEditScaffoldWithScaffoldFields() {
  787. $this->Controller = new ScaffoldMockControllerWithFields();
  788. $this->Controller->action = 'edit';
  789. $this->Controller->here = '/scaffold_mock';
  790. $this->Controller->webroot = '/';
  791. $params = array(
  792. 'plugin' => null,
  793. 'pass' => array(1),
  794. 'form' => array(),
  795. 'named' => array(),
  796. 'url' => array('url' =>'scaffold_mock'),
  797. 'controller' => 'scaffold_mock',
  798. 'action' => 'edit',
  799. );
  800. //set router.
  801. Router::reload();
  802. Router::setRequestInfo(array($params, array('base' => '/', 'here' => '/scaffold_mock', 'webroot' => '/')));
  803. $this->Controller->params = $params;
  804. $this->Controller->controller = 'scaffold_mock';
  805. $this->Controller->base = '/';
  806. $this->Controller->constructClasses();
  807. ob_start();
  808. new Scaffold($this->Controller, $params);
  809. $result = ob_get_clean();
  810. $this->assertNoPattern('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result);
  811. }
  812. }