2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* ScaffoldTest file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2012-04-26 19:49:18 -07:00
|
|
|
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
|
2013-02-08 20:59:49 +09:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-10-03 12:31:21 -04:00
|
|
|
* Licensed under The MIT License
|
2013-02-08 21:22:51 +09:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-10-03 12:31:21 -04:00
|
|
|
* Redistributions of files must retain the above copyright notice
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2013-02-08 20:59:49 +09:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2012-04-26 19:49:18 -07:00
|
|
|
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Controller
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 1.2.0.5436
|
2013-05-31 00:11:14 +02:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-05-31 00:11:14 +02:00
|
|
|
|
2011-09-16 22:05:09 -04:00
|
|
|
App::uses('Router', 'Routing');
|
2010-12-09 01:25:24 -04:30
|
|
|
App::uses('Controller', 'Controller');
|
2011-02-22 22:47:07 -04:30
|
|
|
App::uses('Scaffold', 'Controller');
|
|
|
|
App::uses('ScaffoldView', 'View');
|
2011-09-16 22:05:09 -04:00
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
|
|
|
|
require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php';
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* ScaffoldMockController class
|
2008-10-02 00:18:47 +00:00
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Controller
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2008-05-30 11:40:08 +00:00
|
|
|
class ScaffoldMockController extends Controller {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-06-02 19:22:55 +00:00
|
|
|
/**
|
|
|
|
* scaffold property
|
2008-10-02 00:18:47 +00:00
|
|
|
*
|
|
|
|
* @var mixed
|
2008-06-02 19:22:55 +00:00
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $scaffold;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-07-03 00:14:36 +00:00
|
|
|
/**
|
|
|
|
* ScaffoldMockControllerWithFields class
|
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Controller
|
2009-07-03 00:14:36 +00:00
|
|
|
*/
|
|
|
|
class ScaffoldMockControllerWithFields extends Controller {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-07-03 00:14:36 +00:00
|
|
|
/**
|
|
|
|
* name property
|
|
|
|
*
|
2013-06-08 04:29:08 +02:00
|
|
|
* @var string
|
2009-07-03 00:14:36 +00:00
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $name = 'ScaffoldMock';
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-07-03 00:14:36 +00:00
|
|
|
/**
|
|
|
|
* scaffold property
|
|
|
|
*
|
|
|
|
* @var mixed
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $scaffold;
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-07-03 00:14:36 +00:00
|
|
|
/**
|
2011-08-24 21:45:33 -04:00
|
|
|
* function beforeScaffold
|
2009-07-03 00:14:36 +00:00
|
|
|
*
|
|
|
|
* @param string method
|
|
|
|
*/
|
2011-08-24 21:45:33 -04:00
|
|
|
public function beforeScaffold($method) {
|
2009-07-03 00:14:36 +00:00
|
|
|
$this->set('scaffoldFields', array('title'));
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-11 22:20:25 -04:00
|
|
|
|
2009-07-03 00:14:36 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-09-27 03:48:23 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* TestScaffoldMock class
|
2008-09-27 03:48:23 +00:00
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Controller
|
2008-09-27 03:48:23 +00:00
|
|
|
*/
|
|
|
|
class TestScaffoldMock extends Scaffold {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-09-27 03:48:23 +00:00
|
|
|
/**
|
2011-11-12 20:43:55 -05:00
|
|
|
* Overload _scaffold
|
2008-09-27 03:48:23 +00:00
|
|
|
*
|
|
|
|
* @param unknown_type $params
|
|
|
|
*/
|
2012-02-16 23:13:12 -08:00
|
|
|
protected function _scaffold(CakeRequest $request) {
|
2011-12-15 22:52:07 -08:00
|
|
|
$this->_params = $request;
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-09-27 03:48:23 +00:00
|
|
|
/**
|
|
|
|
* Get Params from the Controller.
|
|
|
|
*
|
|
|
|
* @return unknown
|
|
|
|
*/
|
2012-02-16 23:13:12 -08:00
|
|
|
public function getParams() {
|
2011-12-15 22:52:07 -08:00
|
|
|
return $this->_params;
|
|
|
|
}
|
2012-03-11 22:20:25 -04:00
|
|
|
|
2008-09-27 03:48:23 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-09-27 03:48:23 +00:00
|
|
|
/**
|
2009-03-18 17:55:58 +00:00
|
|
|
* Scaffold Test class
|
2008-09-27 03:48:23 +00:00
|
|
|
*
|
2011-07-26 01:46:14 -04:30
|
|
|
* @package Cake.Test.Case.Controller
|
2008-09-27 03:48:23 +00:00
|
|
|
*/
|
2009-03-18 17:55:58 +00:00
|
|
|
class ScaffoldTest extends CakeTestCase {
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
|
|
|
* Controller property
|
|
|
|
*
|
|
|
|
* @var SecurityTestController
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $Controller;
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-09-27 03:48:23 +00:00
|
|
|
/**
|
|
|
|
* fixtures property
|
2008-10-02 00:18:47 +00:00
|
|
|
*
|
2008-09-27 03:48:23 +00:00
|
|
|
* @var array
|
|
|
|
*/
|
2010-04-04 17:14:00 +10:00
|
|
|
public $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag');
|
2011-12-06 12:52:48 -08:00
|
|
|
|
2008-09-27 03:48:23 +00:00
|
|
|
/**
|
2010-09-25 21:36:49 -04:00
|
|
|
* setUp method
|
2008-10-02 00:18:47 +00:00
|
|
|
*
|
2008-09-27 03:48:23 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function setUp() {
|
2010-09-25 21:36:49 -04:00
|
|
|
parent::setUp();
|
2013-06-11 20:03:05 +02:00
|
|
|
Configure::write('Config.language', 'eng');
|
2010-05-17 23:11:47 -04:00
|
|
|
$request = new CakeRequest(null, false);
|
|
|
|
$this->Controller = new ScaffoldMockController($request);
|
2010-09-19 00:38:00 -04:00
|
|
|
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
2008-09-27 03:48:23 +00:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-03-18 17:55:58 +00:00
|
|
|
/**
|
2010-09-25 21:36:49 -04:00
|
|
|
* tearDown method
|
2009-03-18 17:55:58 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function tearDown() {
|
2010-09-25 21:36:49 -04:00
|
|
|
parent::tearDown();
|
2009-03-18 17:55:58 +00:00
|
|
|
unset($this->Controller);
|
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2008-09-27 03:48:23 +00:00
|
|
|
/**
|
|
|
|
* Test the correct Generation of Scaffold Params.
|
|
|
|
* This ensures that the correct action and view will be generated
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testScaffoldParams() {
|
2008-09-27 03:48:23 +00:00
|
|
|
$params = array(
|
|
|
|
'plugin' => null,
|
|
|
|
'pass' => array(),
|
|
|
|
'form' => array(),
|
|
|
|
'named' => array(),
|
2011-11-30 23:21:31 -08:00
|
|
|
'url' => array('url' => 'admin/scaffold_mock/edit'),
|
2008-09-27 03:48:23 +00:00
|
|
|
'controller' => 'scaffold_mock',
|
|
|
|
'action' => 'admin_edit',
|
|
|
|
'admin' => true,
|
|
|
|
);
|
2010-05-17 23:11:47 -04:00
|
|
|
$this->Controller->request->base = '';
|
|
|
|
$this->Controller->request->webroot = '/';
|
|
|
|
$this->Controller->request->here = '/admin/scaffold_mock/edit';
|
|
|
|
$this->Controller->request->addParams($params);
|
|
|
|
|
2008-09-27 03:48:23 +00:00
|
|
|
//set router.
|
2010-05-17 23:11:47 -04:00
|
|
|
Router::setRequestInfo($this->Controller->request);
|
2008-09-27 03:48:23 +00:00
|
|
|
|
|
|
|
$this->Controller->constructClasses();
|
2010-05-17 23:11:47 -04:00
|
|
|
$Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request);
|
2008-09-27 03:48:23 +00:00
|
|
|
$result = $Scaffold->getParams();
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('admin_edit', $result['action']);
|
2008-09-27 03:48:23 +00:00
|
|
|
}
|
2010-08-01 22:25:29 -04:00
|
|
|
|
2009-05-13 01:00:38 +00:00
|
|
|
/**
|
|
|
|
* test that the proper names and variable values are set by Scaffold
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testScaffoldVariableSetting() {
|
2009-05-13 01:00:38 +00:00
|
|
|
$params = array(
|
|
|
|
'plugin' => null,
|
|
|
|
'pass' => array(),
|
|
|
|
'form' => array(),
|
|
|
|
'named' => array(),
|
2011-11-30 23:21:31 -08:00
|
|
|
'url' => array('url' => 'admin/scaffold_mock/edit'),
|
2009-05-13 01:00:38 +00:00
|
|
|
'controller' => 'scaffold_mock',
|
|
|
|
'action' => 'admin_edit',
|
|
|
|
'admin' => true,
|
|
|
|
);
|
2010-05-17 23:11:47 -04:00
|
|
|
$this->Controller->request->base = '';
|
|
|
|
$this->Controller->request->webroot = '/';
|
|
|
|
$this->Controller->request->here = '/admin/scaffold_mock/edit';
|
|
|
|
$this->Controller->request->addParams($params);
|
|
|
|
|
2009-05-13 01:00:38 +00:00
|
|
|
//set router.
|
2010-05-17 23:11:47 -04:00
|
|
|
Router::setRequestInfo($this->Controller->request);
|
2009-05-13 01:00:38 +00:00
|
|
|
|
|
|
|
$this->Controller->constructClasses();
|
2010-05-17 23:11:47 -04:00
|
|
|
$Scaffold = new TestScaffoldMock($this->Controller, $this->Controller->request);
|
2009-08-03 17:38:40 +00:00
|
|
|
$result = $Scaffold->controller->viewVars;
|
2009-05-13 01:00:38 +00:00
|
|
|
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('Scaffold :: Admin Edit :: Scaffold Mock', $result['title_for_layout']);
|
|
|
|
$this->assertEquals('Scaffold Mock', $result['singularHumanName']);
|
|
|
|
$this->assertEquals('Scaffold Mock', $result['pluralHumanName']);
|
|
|
|
$this->assertEquals('ScaffoldMock', $result['modelClass']);
|
|
|
|
$this->assertEquals('id', $result['primaryKey']);
|
|
|
|
$this->assertEquals('title', $result['displayField']);
|
|
|
|
$this->assertEquals('scaffoldMock', $result['singularVar']);
|
|
|
|
$this->assertEquals('scaffoldMock', $result['pluralVar']);
|
|
|
|
$this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated'), $result['scaffoldFields']);
|
2012-11-27 21:57:12 -05:00
|
|
|
$this->assertArrayHasKey('plugin', $result['associations']['belongsTo']['User']);
|
2009-05-13 01:00:38 +00:00
|
|
|
}
|
2010-08-01 22:25:29 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test that Scaffold overrides the view property even if its set to 'Theme'
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testScaffoldChangingViewProperty() {
|
2010-08-01 22:25:29 -04:00
|
|
|
$this->Controller->action = 'edit';
|
2011-05-17 00:31:59 +02:00
|
|
|
$this->Controller->theme = 'TestTheme';
|
2011-02-20 21:52:20 -05:00
|
|
|
$this->Controller->viewClass = 'Theme';
|
2010-08-01 22:25:29 -04:00
|
|
|
$this->Controller->constructClasses();
|
2013-01-23 13:45:50 +01:00
|
|
|
new TestScaffoldMock($this->Controller, $this->Controller->request);
|
2010-08-01 22:25:29 -04:00
|
|
|
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals('Scaffold', $this->Controller->viewClass);
|
2010-08-01 22:25:29 -04:00
|
|
|
}
|
2009-07-24 21:18:37 +02:00
|
|
|
|
2009-08-31 13:48:32 -04:00
|
|
|
/**
|
|
|
|
* test that scaffold outputs flash messages when sessions are unset.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testScaffoldFlashMessages() {
|
2009-08-31 13:48:32 -04:00
|
|
|
$params = array(
|
|
|
|
'plugin' => null,
|
|
|
|
'pass' => array(1),
|
|
|
|
'form' => array(),
|
|
|
|
'named' => array(),
|
2011-11-30 23:21:31 -08:00
|
|
|
'url' => array('url' => 'scaffold_mock'),
|
2009-08-31 13:48:32 -04:00
|
|
|
'controller' => 'scaffold_mock',
|
|
|
|
'action' => 'edit',
|
|
|
|
);
|
2010-05-17 23:11:47 -04:00
|
|
|
$this->Controller->request->base = '';
|
|
|
|
$this->Controller->request->webroot = '/';
|
|
|
|
$this->Controller->request->here = '/scaffold_mock/edit';
|
|
|
|
$this->Controller->request->addParams($params);
|
|
|
|
|
2009-08-31 13:48:32 -04:00
|
|
|
//set router.
|
|
|
|
Router::reload();
|
2010-05-17 23:11:47 -04:00
|
|
|
Router::setRequestInfo($this->Controller->request);
|
|
|
|
$this->Controller->request->data = array(
|
2009-08-31 13:48:32 -04:00
|
|
|
'ScaffoldMock' => array(
|
|
|
|
'id' => 1,
|
|
|
|
'title' => 'New title',
|
|
|
|
'body' => 'new body'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->Controller->constructClasses();
|
|
|
|
unset($this->Controller->Session);
|
|
|
|
|
|
|
|
ob_start();
|
2010-05-17 23:11:47 -04:00
|
|
|
new Scaffold($this->Controller, $this->Controller->request);
|
2011-07-28 18:09:19 -04:00
|
|
|
$this->Controller->response->send();
|
2009-08-31 13:48:32 -04:00
|
|
|
$result = ob_get_clean();
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp('/Scaffold Mock has been updated/', $result);
|
2009-08-31 13:48:32 -04:00
|
|
|
}
|
2011-12-06 12:52:48 -08:00
|
|
|
|
2009-08-25 23:34:19 -04:00
|
|
|
/**
|
|
|
|
* test that habtm relationship keys get added to scaffoldFields.
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2012-02-16 23:13:12 -08:00
|
|
|
public function testHabtmFieldAdditionWithScaffoldForm() {
|
2011-05-27 02:28:38 -04:30
|
|
|
CakePlugin::unload();
|
2009-08-25 23:34:19 -04:00
|
|
|
$params = array(
|
|
|
|
'plugin' => null,
|
|
|
|
'pass' => array(1),
|
|
|
|
'form' => array(),
|
|
|
|
'named' => array(),
|
2011-11-30 23:21:31 -08:00
|
|
|
'url' => array('url' => 'scaffold_mock'),
|
2009-08-25 23:34:19 -04:00
|
|
|
'controller' => 'scaffold_mock',
|
|
|
|
'action' => 'edit',
|
|
|
|
);
|
2010-05-17 23:11:47 -04:00
|
|
|
$this->Controller->request->base = '';
|
|
|
|
$this->Controller->request->webroot = '/';
|
|
|
|
$this->Controller->request->here = '/scaffold_mock/edit';
|
|
|
|
$this->Controller->request->addParams($params);
|
|
|
|
|
2009-08-25 23:34:19 -04:00
|
|
|
//set router.
|
|
|
|
Router::reload();
|
2010-05-17 23:11:47 -04:00
|
|
|
Router::setRequestInfo($this->Controller->request);
|
|
|
|
|
2009-08-25 23:34:19 -04:00
|
|
|
$this->Controller->constructClasses();
|
|
|
|
ob_start();
|
2010-05-17 23:11:47 -04:00
|
|
|
$Scaffold = new Scaffold($this->Controller, $this->Controller->request);
|
2011-07-28 18:09:19 -04:00
|
|
|
$this->Controller->response->send();
|
2009-08-25 23:34:19 -04:00
|
|
|
$result = ob_get_clean();
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertRegExp('/name="data\[ScaffoldTag\]\[ScaffoldTag\]"/', $result);
|
2009-08-25 23:34:19 -04:00
|
|
|
|
|
|
|
$result = $Scaffold->controller->viewVars;
|
2012-03-22 23:37:12 -07:00
|
|
|
$this->assertEquals(array('id', 'user_id', 'title', 'body', 'published', 'created', 'updated', 'ScaffoldTag'), $result['scaffoldFields']);
|
2009-08-25 23:34:19 -04:00
|
|
|
}
|
2011-12-06 12:52:48 -08:00
|
|
|
|
2009-07-03 00:14:36 +00:00
|
|
|
/**
|
|
|
|
* test that the proper names and variable values are set by Scaffold
|
|
|
|
*
|
|
|
|
* @return void
|
2009-11-14 23:19:25 +11:00
|
|
|
*/
|
2011-05-30 22:02:32 +02:00
|
|
|
public function testEditScaffoldWithScaffoldFields() {
|
2010-05-17 23:11:47 -04:00
|
|
|
$request = new CakeRequest(null, false);
|
|
|
|
$this->Controller = new ScaffoldMockControllerWithFields($request);
|
2010-09-19 00:38:00 -04:00
|
|
|
$this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader'));
|
|
|
|
|
2009-07-03 00:14:36 +00:00
|
|
|
$params = array(
|
|
|
|
'plugin' => null,
|
|
|
|
'pass' => array(1),
|
|
|
|
'form' => array(),
|
|
|
|
'named' => array(),
|
2011-11-30 23:21:31 -08:00
|
|
|
'url' => array('url' => 'scaffold_mock/edit'),
|
2009-07-03 00:14:36 +00:00
|
|
|
'controller' => 'scaffold_mock',
|
|
|
|
'action' => 'edit',
|
|
|
|
);
|
2010-05-17 23:11:47 -04:00
|
|
|
$this->Controller->request->base = '';
|
|
|
|
$this->Controller->request->webroot = '/';
|
|
|
|
$this->Controller->request->here = '/scaffold_mock/edit';
|
|
|
|
$this->Controller->request->addParams($params);
|
|
|
|
|
2009-07-03 00:14:36 +00:00
|
|
|
//set router.
|
|
|
|
Router::reload();
|
2010-05-17 23:11:47 -04:00
|
|
|
Router::setRequestInfo($this->Controller->request);
|
|
|
|
|
2009-07-03 00:14:36 +00:00
|
|
|
$this->Controller->constructClasses();
|
|
|
|
ob_start();
|
2010-05-17 23:11:47 -04:00
|
|
|
new Scaffold($this->Controller, $this->Controller->request);
|
2011-07-28 18:09:19 -04:00
|
|
|
$this->Controller->response->send();
|
2009-07-03 00:14:36 +00:00
|
|
|
$result = ob_get_clean();
|
|
|
|
|
2011-11-15 16:07:56 -08:00
|
|
|
$this->assertNotRegExp('/textarea name="data\[ScaffoldMock\]\[body\]" cols="30" rows="6" id="ScaffoldMockBody"/', $result);
|
2009-07-03 00:14:36 +00:00
|
|
|
}
|
2012-03-11 22:20:25 -04:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|