* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) * * Licensed under The MIT License * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice * * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) * @link https://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests * @package Cake.Test.Case.View * @since CakePHP(tm) v 2.0 * @license https://opensource.org/licenses/mit-license.php MIT License */ App::uses('Controller', 'Controller'); App::uses('Scaffold', 'Controller'); App::uses('ScaffoldView', 'View'); App::uses('AppModel', 'Model'); require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php'; /** * TestScaffoldView class * * @package Cake.Test.Case.Controller */ class TestScaffoldView extends ScaffoldView { /** * testGetFilename method * * @param string $action * @return void */ public function testGetFilename($action) { return $this->_getViewFileName($action); } } /** * ScaffoldViewMockController class * * @package Cake.Test.Case.Controller */ class ScaffoldViewMockController extends Controller { /** * name property * * @var string */ public $name = 'ScaffoldMock'; /** * scaffold property * * @var mixed */ public $scaffold; } /** * ScaffoldViewTest class * * @package Cake.Test.Case.Controller */ class ScaffoldViewTest extends CakeTestCase { /** * fixtures property * * @var array */ public $fixtures = array('core.article', 'core.user', 'core.comment', 'core.join_thing', 'core.tag'); /** * setUp method * * @return void */ public function setUp() : void { parent::setUp(); $this->request = new CakeRequest(null, false); $this->Controller = new ScaffoldViewMockController($this->request); $this->Controller->response = $this->getMock('CakeResponse', array('_sendHeader')); App::build(array( 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS), 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS) )); CakePlugin::load('TestPlugin'); } /** * tearDown method * * @return void */ public function tearDown() : void { unset($this->Controller, $this->request); parent::tearDown(); } /** * testGetViewFilename method * * @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->assertEquals($expected, $result); $result = $ScaffoldView->testGetFilename('edit'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; $this->assertEquals($expected, $result); $result = $ScaffoldView->testGetFilename('add'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; $this->assertEquals($expected, $result); $result = $ScaffoldView->testGetFilename('view'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'view.ctp'; $this->assertEquals($expected, $result); $result = $ScaffoldView->testGetFilename('admin_index'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'index.ctp'; $this->assertEquals($expected, $result); $result = $ScaffoldView->testGetFilename('admin_view'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'view.ctp'; $this->assertEquals($expected, $result); $result = $ScaffoldView->testGetFilename('admin_edit'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; $this->assertEquals($expected, $result); $result = $ScaffoldView->testGetFilename('admin_add'); $expected = CAKE . 'View' . DS . 'Scaffolds' . DS . 'form.ctp'; $this->assertEquals($expected, $result); $result = $ScaffoldView->testGetFilename('error'); $expected = CAKE . 'View' . DS . 'Errors' . DS . 'scaffold_error.ctp'; $this->assertEquals($expected, $result); $Controller = new ScaffoldViewMockController($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->assertEquals($expected, $result); $result = $ScaffoldView->testGetFilename('edit'); $expected = CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Posts' . DS . 'scaffold.form.ctp'; $this->assertEquals($expected, $result); $Controller = new ScaffoldViewMockController($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->assertEquals($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->assertEquals($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->assertEquals($expected, $result); } /** * test default index scaffold generation * * @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); $this->Controller->response->send(); $result = ob_get_clean(); $this->assertRegExp('#