* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice * * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests * @package Cake.Test.Case.Controller * @since CakePHP(tm) v 1.2.0.5436 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ App::uses('Controller', 'Controller'); App::uses('Scaffold', 'Controller'); App::uses('ScaffoldView', 'View'); /** * ScaffoldMockController class * * @package Cake.Test.Case.Controller */ class ScaffoldMockController extends Controller { /** * name property * * @var string 'ScaffoldMock' * @access public */ public $name = 'ScaffoldMock'; /** * scaffold property * * @var mixed * @access public */ public $scaffold; } /** * ScaffoldMockControllerWithFields class * * @package Cake.Test.Case.Controller */ class ScaffoldMockControllerWithFields extends Controller { /** * name property * * @var string 'ScaffoldMock' * @access public */ public $name = 'ScaffoldMock'; /** * scaffold property * * @var mixed * @access public */ public $scaffold; /** * function _beforeScaffold * * @param string method */ function _beforeScaffold($method) { $this->set('scaffoldFields', array('title')); return true; } } /** * TestScaffoldMock class * * @package Cake.Test.Case.Controller */ class TestScaffoldMock extends Scaffold { /** * Overload __scaffold * * @param unknown_type $params */ function _scaffold($params) { $this->_params = $params; } /** * Get Params from the Controller. * * @return unknown */ function getParams() { return $this->_params; } } /** * ScaffoldMock class * * @package Cake.Test.Case.Controller */ class ScaffoldMock extends CakeTestModel { /** * useTable property * * @var string 'posts' * @access public */ public $useTable = 'articles'; /** * belongsTo property * * @var array * @access public */ public $belongsTo = array( 'User' => array( 'className' => 'ScaffoldUser', 'foreignKey' => 'user_id', ) ); /** * hasMany property * * @var array * @access public */ public $hasMany = array( 'Comment' => array( 'className' => 'ScaffoldComment', 'foreignKey' => 'article_id', ) ); /** * hasAndBelongsToMany property * * @var string */ public $hasAndBelongsToMany = array( 'ScaffoldTag' => array( 'className' => 'ScaffoldTag', 'foreignKey' => 'something_id', 'associationForeignKey' => 'something_else_id', 'joinTable' => 'join_things' ) ); } /** * ScaffoldUser class * * @package Cake.Test.Case.Controller */ class ScaffoldUser extends CakeTestModel { /** * useTable property * * @var string 'posts' * @access public */ public $useTable = 'users'; /** * hasMany property * * @var array * @access public */ public $hasMany = array( 'Article' => array( 'className' => 'ScaffoldMock', 'foreignKey' => 'article_id', ) ); } /** * ScaffoldComment class * * @package Cake.Test.Case.Controller */ class ScaffoldComment extends CakeTestModel { /** * useTable property * * @var string 'posts' * @access public */ public $useTable = 'comments'; /** * belongsTo property * * @var array * @access public */ public $belongsTo = array( 'Article' => array( 'className' => 'ScaffoldMock', 'foreignKey' => 'article_id', ) ); } /** * ScaffoldTag class * * @package Cake.Test.Case.Controller */ class ScaffoldTag extends CakeTestModel { /** * useTable property * * @var string 'posts' * @access public */ public $useTable = 'tags'; } /** * TestScaffoldView class * * @package Cake.Test.Case.Controller */ class TestScaffoldView extends ScaffoldView { /** * testGetFilename method * * @param mixed $action * @access public * @return void */ public function testGetFilename($action) { return $this->_getViewFileName($action); } } /** * ScaffoldViewTest class * * @package Cake.Test.Case.Controller */ class ScaffoldViewTest extends CakeTestCase { /** * fixtures property * * @var array * @access public */ public $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag'); /** * setUp method * * @access public * @return void */ public function setUp() { parent::setUp(); $this->request = new CakeRequest(null, false); $this->Controller = new ScaffoldMockController($this->request); $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader')); App::build(array( 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS), 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS) )); CakePlugin::load('TestPlugin'); } /** * teardown method * * @return void */ public function tearDown() { parent::tearDown(); unset($this->Controller, $this->request); CakePlugin::unload(); } /** * testGetViewFilename method * * @access public * @return void */ public function testGetViewFilename() { $_admin = Configure::read('Routing.prefixes'); Configure::write('Routing.prefixes', array('admin')); $this->Controller->request->params['action'] = 'index'; $ScaffoldView = new TestScaffoldView($this->Controller); $result = $ScaffoldView->testGetFilename('index'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'index.ctp'; $this->assertEqual($expected, $result); $result = $ScaffoldView->testGetFilename('edit'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; $this->assertEqual($expected, $result); $result = $ScaffoldView->testGetFilename('add'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; $this->assertEqual($expected, $result); $result = $ScaffoldView->testGetFilename('view'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'view.ctp'; $this->assertEqual($expected, $result); $result = $ScaffoldView->testGetFilename('admin_index'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'index.ctp'; $this->assertEqual($expected, $result); $result = $ScaffoldView->testGetFilename('admin_view'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'view.ctp'; $this->assertEqual($expected, $result); $result = $ScaffoldView->testGetFilename('admin_edit'); $expected =CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; $this->assertEqual($expected, $result); $result = $ScaffoldView->testGetFilename('admin_add'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; $this->assertEqual($expected, $result); $result = $ScaffoldView->testGetFilename('error'); $expected = CAKE . 'View' . DS . 'Errors' . DS . 'scaffold_error.ctp'; $this->assertEqual($expected, $result); $Controller = new ScaffoldMockController($this->request); $Controller->scaffold = 'admin'; $Controller->viewPath = 'Posts'; $Controller->request['action'] = 'admin_edit'; $ScaffoldView = new TestScaffoldView($Controller); $result = $ScaffoldView->testGetFilename('admin_edit'); $expected = CAKE . 'Test' . DS . 'test_app' .DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp'; $this->assertEqual($expected, $result); $result = $ScaffoldView->testGetFilename('edit'); $expected = CAKE . 'Test' . DS . 'test_app' .DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp'; $this->assertEqual($expected, $result); $Controller = new ScaffoldMockController($this->request); $Controller->scaffold = 'admin'; $Controller->viewPath = 'Tests'; $Controller->request->addParams(array( 'plugin' => 'test_plugin', 'action' => 'admin_add', 'admin' => true )); $Controller->plugin = 'TestPlugin'; $ScaffoldView = new TestScaffoldView($Controller); $result = $ScaffoldView->testGetFilename('admin_add'); $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS .'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'scaffold.form.ctp'; $this->assertEqual($expected, $result); $result = $ScaffoldView->testGetFilename('add'); $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS .'TestPlugin' . DS . 'View' . DS . 'Tests' . DS . 'scaffold.form.ctp'; $this->assertEqual($expected, $result); Configure::write('Routing.prefixes', $_admin); } /** * test getting the view file name for themed scaffolds. * * @return void */ public function testGetViewFileNameWithTheme() { $this->Controller->request['action'] = 'index'; $this->Controller->viewPath = 'Posts'; $this->Controller->theme = 'TestTheme'; $ScaffoldView = new TestScaffoldView($this->Controller); $result = $ScaffoldView->testGetFilename('index'); $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'Posts' . DS . 'scaffold.index.ctp'; $this->assertEqual($expected, $result); } /** * test default index scaffold generation * * @access public * @return void */ public function testIndexScaffold() { $params = array( 'plugin' => null, 'pass' => array(), 'form' => array(), 'named' => array(), 'url' => array('url' =>'scaffold_mock'), 'controller' => 'scaffold_mock', 'action' => 'index', ); $this->Controller->request->addParams($params); $this->Controller->request->webroot = '/'; $this->Controller->request->base = ''; $this->Controller->request->here = '/scaffold_mock/index'; //set router. Router::reload(); Router::setRequestInfo($this->Controller->request); $this->Controller->constructClasses(); ob_start(); new Scaffold($this->Controller, $this->Controller->request); $result = ob_get_clean(); $this->assertPattern('#