PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Controller/ScaffoldTest.php

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