2010-12-15 03:58:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ControllerTestCaseTest file
|
|
|
|
*
|
|
|
|
* Test Case for ControllerTestCase class
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2010-12-15 03:58:27 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2010-12-15 03:58:27 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.TestSuite
|
2010-12-15 03:58:27 +00:00
|
|
|
* @since CakePHP v 2.0
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-22 05:30:15 +00:00
|
|
|
App::uses('Controller', 'Controller');
|
|
|
|
App::uses('Model', 'Model');
|
|
|
|
App::uses('AppModel', 'Model');
|
|
|
|
App::uses('CakeHtmlReporter', 'TestSuite/Reporter');
|
|
|
|
|
2011-04-12 02:53:44 +00:00
|
|
|
require_once dirname(dirname(__FILE__)) . DS . 'Model' . DS . 'models.php';
|
2010-12-15 03:58:27 +00:00
|
|
|
|
2011-01-22 05:30:15 +00:00
|
|
|
if (!class_exists('AppController', false)) {
|
2012-04-09 00:39:52 +00:00
|
|
|
/**
|
|
|
|
* AppController class
|
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.TestSuite
|
|
|
|
*/
|
2010-12-15 03:58:27 +00:00
|
|
|
class AppController extends Controller {
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* helpers property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $helpers = array('Html');
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* uses property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $uses = array('ControllerPost');
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* components property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $components = array('Cookie');
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
} elseif (!defined('APP_CONTROLLER_EXISTS')) {
|
|
|
|
define('APP_CONTROLLER_EXISTS', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PostsController class
|
|
|
|
*/
|
2010-12-15 04:27:12 +00:00
|
|
|
if (!class_exists('PostsController')) {
|
2013-05-30 22:11:14 +00:00
|
|
|
|
2013-05-31 14:37:20 +00:00
|
|
|
/**
|
2016-08-10 10:22:09 +00:00
|
|
|
* PostsController
|
2013-05-31 14:37:20 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.TestSuite
|
|
|
|
*/
|
2010-12-15 04:27:12 +00:00
|
|
|
class PostsController extends AppController {
|
2010-12-15 03:58:27 +00:00
|
|
|
|
2010-12-15 04:27:12 +00:00
|
|
|
/**
|
|
|
|
* Components array
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $components = array(
|
|
|
|
'RequestHandler',
|
|
|
|
'Email',
|
2017-12-31 05:25:00 +00:00
|
|
|
'AliasedEmail' => array(
|
|
|
|
'className' => 'Email',
|
|
|
|
),
|
|
|
|
'AliasedPluginEmail' => array(
|
|
|
|
'className' => 'TestPlugin.TestPluginEmail',
|
|
|
|
),
|
2010-12-15 04:27:12 +00:00
|
|
|
'Auth'
|
|
|
|
);
|
|
|
|
}
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2011-01-28 02:32:51 +00:00
|
|
|
/**
|
|
|
|
* ControllerTestCaseTest controller
|
2013-05-30 22:11:14 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Test.Case.TestSuite
|
2011-01-28 02:32:51 +00:00
|
|
|
*/
|
|
|
|
class ControllerTestCaseTestController extends AppController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses array
|
|
|
|
*
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
public $uses = array('TestPlugin.TestPluginComment');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* ControllerTestCaseTest
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Test.Case.TestSuite
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
|
|
|
class ControllerTestCaseTest extends CakeTestCase {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fixtures property
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2011-01-28 02:32:51 +00:00
|
|
|
public $fixtures = array('core.post', 'core.author', 'core.test_plugin_comment');
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* reset environment.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function setUp() : void {
|
2010-12-16 03:12:48 +00:00
|
|
|
parent::setUp();
|
2010-12-15 03:58:27 +00:00
|
|
|
App::build(array(
|
2012-02-18 12:31:29 +00:00
|
|
|
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
|
2011-04-17 10:35:21 +00:00
|
|
|
'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
|
|
|
|
'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
|
|
|
|
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS)
|
2011-09-03 00:14:51 +00:00
|
|
|
), App::RESET);
|
2012-02-17 03:59:24 +00:00
|
|
|
CakePlugin::load(array('TestPlugin', 'TestPluginTwo'));
|
2011-08-20 13:46:01 +00:00
|
|
|
$this->Case = $this->getMockForAbstractClass('ControllerTestCase');
|
2010-12-15 03:58:27 +00:00
|
|
|
Router::reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-12-04 21:27:51 +00:00
|
|
|
* tearDown
|
2010-12-15 03:58:27 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2022-12-28 07:03:39 +00:00
|
|
|
public function tearDown() : void {
|
2010-12-16 03:12:48 +00:00
|
|
|
parent::tearDown();
|
2011-05-09 03:55:32 +00:00
|
|
|
CakePlugin::unload();
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->Case->controller = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that ControllerTestCase::generate() creates mock objects correctly
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testGenerate() {
|
2010-12-16 04:33:00 +00:00
|
|
|
if (defined('APP_CONTROLLER_EXISTS')) {
|
|
|
|
$this->markTestSkipped('AppController exists, cannot run.');
|
|
|
|
}
|
2010-12-15 03:58:27 +00:00
|
|
|
$Posts = $this->Case->generate('Posts');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('Posts', $Posts->name);
|
|
|
|
$this->assertEquals('Post', $Posts->modelClass);
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertNull($Posts->response->send());
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$Posts = $this->Case->generate('Posts', array(
|
|
|
|
'methods' => array(
|
|
|
|
'render'
|
|
|
|
)
|
|
|
|
));
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertNull($Posts->render('index'));
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$Posts = $this->Case->generate('Posts', array(
|
|
|
|
'models' => array('Post'),
|
|
|
|
'components' => array('RequestHandler')
|
|
|
|
));
|
2010-12-16 03:19:44 +00:00
|
|
|
|
2010-12-16 04:33:00 +00:00
|
|
|
$this->assertInstanceOf('Post', $Posts->Post);
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertNull($Posts->Post->save(array()));
|
|
|
|
$this->assertNull($Posts->Post->find('all'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('posts', $Posts->Post->useTable);
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertNull($Posts->RequestHandler->isAjax());
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$Posts = $this->Case->generate('Posts', array(
|
|
|
|
'models' => array(
|
|
|
|
'Post' => true
|
|
|
|
)
|
|
|
|
));
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertNull($Posts->Post->save(array()));
|
|
|
|
$this->assertNull($Posts->Post->find('all'));
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$Posts = $this->Case->generate('Posts', array(
|
|
|
|
'models' => array(
|
|
|
|
'Post' => array('save'),
|
|
|
|
)
|
|
|
|
));
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertNull($Posts->Post->save(array()));
|
2010-12-24 17:54:04 +00:00
|
|
|
$this->assertInternalType('array', $Posts->Post->find('all'));
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$Posts = $this->Case->generate('Posts', array(
|
|
|
|
'models' => array('Post'),
|
|
|
|
'components' => array(
|
|
|
|
'RequestHandler' => array('isPut'),
|
|
|
|
'Email' => array('send'),
|
|
|
|
'Session'
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$Posts->RequestHandler->expects($this->once())
|
|
|
|
->method('isPut')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->assertTrue($Posts->RequestHandler->isPut());
|
|
|
|
|
|
|
|
$Posts->Auth->Session->expects($this->any())
|
|
|
|
->method('write')
|
|
|
|
->will($this->returnValue('written!'));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('written!', $Posts->Auth->Session->write('something'));
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2013-08-19 09:46:07 +00:00
|
|
|
/**
|
|
|
|
* testGenerateWithComponentConfig
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2013-08-19 09:46:07 +00:00
|
|
|
*/
|
|
|
|
public function testGenerateWithComponentConfig() {
|
|
|
|
$Tests = $this->Case->generate('TestConfigs', array(
|
|
|
|
));
|
|
|
|
|
|
|
|
$expected = array('some' => 'config');
|
|
|
|
$settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
|
|
|
|
$this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
|
|
|
|
|
|
|
|
$Tests = $this->Case->generate('TestConfigs', array(
|
|
|
|
'components' => array(
|
|
|
|
'RequestHandler' => array('isPut')
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
|
|
|
$expected = array('some' => 'config');
|
|
|
|
$settings = array_intersect_key($Tests->RequestHandler->settings, array('some' => 'foo'));
|
|
|
|
$this->assertSame($expected, $settings, 'A mocked component should have the same config as an unmocked component');
|
|
|
|
}
|
|
|
|
|
2011-01-19 01:50:34 +00:00
|
|
|
/**
|
|
|
|
* Tests ControllerTestCase::generate() using classes from plugins
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2011-01-19 01:50:34 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testGenerateWithPlugin() {
|
2011-01-19 01:50:34 +00:00
|
|
|
$Tests = $this->Case->generate('TestPlugin.Tests', array(
|
|
|
|
'models' => array(
|
|
|
|
'TestPlugin.TestPluginComment'
|
|
|
|
),
|
|
|
|
'components' => array(
|
2012-01-06 00:26:01 +00:00
|
|
|
'TestPlugin.Plugins'
|
2011-01-19 01:50:34 +00:00
|
|
|
)
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('Tests', $Tests->name);
|
2012-01-06 00:26:01 +00:00
|
|
|
$this->assertInstanceOf('PluginsComponent', $Tests->Plugins);
|
2011-01-19 01:50:34 +00:00
|
|
|
|
|
|
|
$result = ClassRegistry::init('TestPlugin.TestPluginComment');
|
|
|
|
$this->assertInstanceOf('TestPluginComment', $result);
|
2011-01-28 02:32:51 +00:00
|
|
|
|
|
|
|
$Tests = $this->Case->generate('ControllerTestCaseTest', array(
|
|
|
|
'models' => array(
|
|
|
|
'TestPlugin.TestPluginComment' => array('save')
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$this->assertInstanceOf('TestPluginComment', $Tests->TestPluginComment);
|
|
|
|
$Tests->TestPluginComment->expects($this->at(0))
|
|
|
|
->method('save')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$Tests->TestPluginComment->expects($this->at(1))
|
|
|
|
->method('save')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
$this->assertTrue($Tests->TestPluginComment->save(array()));
|
|
|
|
$this->assertFalse($Tests->TestPluginComment->save(array()));
|
2011-01-19 01:50:34 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 05:25:00 +00:00
|
|
|
/**
|
|
|
|
* Tests ControllerTestCase::generate() using aliased component
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-12-31 08:05:26 +00:00
|
|
|
public function testGenerateWithMockedAliasedComponent() {
|
2017-12-31 05:25:00 +00:00
|
|
|
$Posts = $this->Case->generate('Posts', array(
|
|
|
|
'components' => array(
|
|
|
|
'AliasedEmail' => array('send')
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$Posts->AliasedEmail->expects($this->once())
|
|
|
|
->method('send')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->assertInstanceOf('EmailComponent', $Posts->AliasedEmail);
|
|
|
|
$this->assertTrue($Posts->AliasedEmail->send());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests ControllerTestCase::generate() using aliased plugin component
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-12-31 08:05:26 +00:00
|
|
|
public function testGenerateWithMockedAliasedPluginComponent() {
|
2017-12-31 05:25:00 +00:00
|
|
|
$Posts = $this->Case->generate('Posts', array(
|
|
|
|
'components' => array(
|
|
|
|
'AliasedPluginEmail' => array('send')
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$Posts->AliasedPluginEmail->expects($this->once())
|
|
|
|
->method('send')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->assertInstanceOf('TestPluginEmailComponent', $Posts->AliasedPluginEmail);
|
|
|
|
$this->assertTrue($Posts->AliasedPluginEmail->send());
|
|
|
|
}
|
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* Tests testAction
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTestAction() {
|
2015-09-25 15:22:00 +00:00
|
|
|
$this->Case->generate('TestsApps');
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->Case->testAction('/tests_apps/index');
|
2010-12-24 17:54:04 +00:00
|
|
|
$this->assertInternalType('array', $this->Case->controller->viewVars);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$this->Case->testAction('/tests_apps/set_action');
|
|
|
|
$results = $this->Case->controller->viewVars;
|
|
|
|
$expected = array(
|
|
|
|
'var' => 'string'
|
|
|
|
);
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertEquals($expected, $results);
|
2011-04-17 10:35:21 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
$result = $this->Case->controller->response->body();
|
2022-12-30 08:28:11 +00:00
|
|
|
$this->assertMatchesRegularExpression('/This is the TestsAppsController index view/', $result);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
2011-10-22 14:58:21 +00:00
|
|
|
$Controller = $this->Case->generate('TestsApps');
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->Case->testAction('/tests_apps/redirect_to');
|
|
|
|
$results = $this->Case->headers;
|
|
|
|
$expected = array(
|
2017-06-10 21:33:55 +00:00
|
|
|
'Location' => 'https://cakephp.org'
|
2010-12-15 03:58:27 +00:00
|
|
|
);
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertEquals($expected, $results);
|
2015-01-26 09:29:52 +00:00
|
|
|
$this->assertSame(302, $Controller->response->statusCode());
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2014-12-23 12:42:21 +00:00
|
|
|
/**
|
|
|
|
* Test array URLs with testAction()
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTestActionArrayUrls() {
|
2015-09-25 15:22:00 +00:00
|
|
|
$this->Case->generate('TestsApps');
|
2014-12-23 12:42:21 +00:00
|
|
|
$this->Case->testAction(array('controller' => 'tests_apps', 'action' => 'index'));
|
|
|
|
$this->assertInternalType('array', $this->Case->controller->viewVars);
|
|
|
|
}
|
|
|
|
|
2015-04-10 00:38:37 +00:00
|
|
|
/**
|
|
|
|
* Test that file responses don't trigger errors.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testActionWithFile() {
|
|
|
|
$Controller = $this->Case->generate('TestsApps');
|
|
|
|
$this->Case->testAction('/tests_apps/file');
|
|
|
|
$this->assertArrayHasKey('Content-Disposition', $Controller->response->header());
|
|
|
|
$this->assertArrayHasKey('Content-Length', $Controller->response->header());
|
|
|
|
}
|
|
|
|
|
2011-08-20 13:58:35 +00:00
|
|
|
/**
|
|
|
|
* Make sure testAction() can hit plugin controllers.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTestActionWithPlugin() {
|
2013-01-23 12:45:50 +00:00
|
|
|
$this->Case->generate('TestPlugin.Tests');
|
2011-08-20 13:58:35 +00:00
|
|
|
$this->Case->testAction('/test_plugin/tests/index');
|
|
|
|
$this->assertEquals('It is a variable', $this->Case->controller->viewVars['test_value']);
|
|
|
|
}
|
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* Tests using loaded routes during tests
|
2011-08-20 13:58:35 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testUseRoutes() {
|
2011-07-18 11:33:16 +00:00
|
|
|
Router::connect('/:controller/:action/*');
|
2011-04-17 10:35:21 +00:00
|
|
|
include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
|
2011-07-18 11:33:16 +00:00
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
$controller = $this->Case->generate('TestsApps');
|
|
|
|
$controller->Components->load('RequestHandler');
|
2011-11-29 02:36:44 +00:00
|
|
|
$result = $this->Case->testAction('/tests_apps/index.json', array('return' => 'contents'));
|
2010-12-15 03:58:27 +00:00
|
|
|
$result = json_decode($result, true);
|
|
|
|
$expected = array('cakephp' => 'cool');
|
2011-05-16 22:49:00 +00:00
|
|
|
$this->assertEquals($expected, $result);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
2011-04-17 10:35:21 +00:00
|
|
|
include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
|
2010-12-15 03:58:27 +00:00
|
|
|
$result = $this->Case->testAction('/some_alias');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(5, $result);
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests not using loaded routes during tests
|
2010-12-16 03:19:44 +00:00
|
|
|
*
|
2014-04-02 01:02:37 +00:00
|
|
|
* @return void
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testSkipRoutes() {
|
2022-12-31 04:13:42 +00:00
|
|
|
$this->expectException(MissingActionException::class);
|
2011-07-18 11:33:16 +00:00
|
|
|
Router::connect('/:controller/:action/*');
|
2011-04-17 10:35:21 +00:00
|
|
|
include CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS . 'routes.php';
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$this->Case->loadRoutes = false;
|
2013-01-23 12:45:50 +00:00
|
|
|
$this->Case->testAction('/tests_apps/missing_action.json', array('return' => 'view'));
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests backwards compatibility with setting the return type
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testBCSetReturn() {
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->Case->autoMock = true;
|
|
|
|
|
|
|
|
$result = $this->Case->testAction('/tests_apps/some_method');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(5, $result);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$data = array('var' => 'set');
|
|
|
|
$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
|
|
|
|
'data' => $data,
|
|
|
|
'return' => 'vars'
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result['data']);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$result = $this->Case->testAction('/tests_apps/set_action', array(
|
|
|
|
'return' => 'view'
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('This is the TestsAppsController index view string', $result);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$result = $this->Case->testAction('/tests_apps/set_action', array(
|
|
|
|
'return' => 'contents'
|
|
|
|
));
|
2022-12-30 08:28:11 +00:00
|
|
|
$this->assertMatchesRegularExpression('/<html/', $result);
|
|
|
|
$this->assertMatchesRegularExpression('/This is the TestsAppsController index view/', $result);
|
|
|
|
$this->assertMatchesRegularExpression('/<\/html>/', $result);
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests sending POST data to testAction
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTestActionPostData() {
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->Case->autoMock = true;
|
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'Post' => array(
|
|
|
|
'name' => 'Some Post'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$this->Case->testAction('/tests_apps_posts/post_var', array(
|
|
|
|
'data' => $data
|
|
|
|
));
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertEquals($this->Case->controller->viewVars['data'], $data);
|
|
|
|
$this->assertEquals($this->Case->controller->data, $data);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$this->Case->testAction('/tests_apps_posts/post_var/named:param', array(
|
|
|
|
'data' => $data
|
|
|
|
));
|
|
|
|
$expected = array(
|
|
|
|
'named' => 'param'
|
|
|
|
);
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($expected, $this->Case->controller->request->named);
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertEquals($this->Case->controller->data, $data);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
|
|
|
|
'return' => 'vars',
|
|
|
|
'method' => 'post',
|
|
|
|
'data' => array(
|
|
|
|
'name' => 'is jonas',
|
|
|
|
'pork' => 'and beans',
|
|
|
|
)
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('name', 'pork'), array_keys($result['data']));
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$result = $this->Case->testAction('/tests_apps_posts/add', array('return' => 'vars'));
|
|
|
|
$this->assertTrue(array_key_exists('posts', $result));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(4, count($result['posts']));
|
2011-08-29 21:24:22 +00:00
|
|
|
$this->assertTrue($this->Case->controller->request->is('post'));
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests sending GET data to testAction
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testTestActionGetData() {
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->Case->autoMock = true;
|
|
|
|
|
2015-09-25 15:22:00 +00:00
|
|
|
$this->Case->testAction('/tests_apps_posts/url_var', array(
|
2010-12-15 03:58:27 +00:00
|
|
|
'method' => 'get',
|
|
|
|
'data' => array(
|
|
|
|
'some' => 'var',
|
|
|
|
'lackof' => 'creativity'
|
|
|
|
)
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('var', $this->Case->controller->request->query['some']);
|
|
|
|
$this->assertEquals('creativity', $this->Case->controller->request->query['lackof']);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$result = $this->Case->testAction('/tests_apps_posts/url_var/var1:value1/var2:val2', array(
|
|
|
|
'return' => 'vars',
|
|
|
|
'method' => 'get',
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('var1', 'var2'), array_keys($result['params']['named']));
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$result = $this->Case->testAction('/tests_apps_posts/url_var/gogo/val2', array(
|
|
|
|
'return' => 'vars',
|
|
|
|
'method' => 'get',
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals(array('gogo', 'val2'), $result['params']['pass']);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
2015-09-25 15:22:00 +00:00
|
|
|
$this->Case->testAction('/tests_apps_posts/url_var', array(
|
2010-12-15 03:58:27 +00:00
|
|
|
'return' => 'vars',
|
|
|
|
'method' => 'get',
|
|
|
|
'data' => array(
|
|
|
|
'red' => 'health',
|
|
|
|
'blue' => 'mana'
|
|
|
|
)
|
|
|
|
));
|
2011-05-09 03:55:32 +00:00
|
|
|
$query = $this->Case->controller->request->query;
|
|
|
|
$this->assertTrue(isset($query['red']));
|
|
|
|
$this->assertTrue(isset($query['blue']));
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2011-11-12 02:26:46 +00:00
|
|
|
/**
|
|
|
|
* Test that REST actions with XML/JSON input work.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTestActionJsonData() {
|
|
|
|
$result = $this->Case->testAction('/tests_apps_posts/input_data', array(
|
|
|
|
'return' => 'vars',
|
|
|
|
'method' => 'post',
|
|
|
|
'data' => '{"key":"value","json":true}'
|
|
|
|
));
|
|
|
|
$this->assertEquals('value', $result['data']['key']);
|
|
|
|
$this->assertTrue($result['data']['json']);
|
|
|
|
}
|
|
|
|
|
2010-12-15 03:58:27 +00:00
|
|
|
/**
|
|
|
|
* Tests autoMock ability
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testAutoMock() {
|
2010-12-15 03:58:27 +00:00
|
|
|
$this->Case->autoMock = true;
|
|
|
|
$this->Case->testAction('/tests_apps/set_action');
|
|
|
|
$results = $this->Case->controller->viewVars;
|
|
|
|
$expected = array(
|
|
|
|
'var' => 'string'
|
|
|
|
);
|
2010-12-16 03:19:44 +00:00
|
|
|
$this->assertEquals($expected, $results);
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test using testAction and not mocking
|
2014-04-02 01:02:37 +00:00
|
|
|
*
|
|
|
|
* @return void
|
2010-12-15 03:58:27 +00:00
|
|
|
*/
|
2011-05-30 20:02:32 +00:00
|
|
|
public function testNoMocking() {
|
2010-12-15 03:58:27 +00:00
|
|
|
$result = $this->Case->testAction('/tests_apps/some_method');
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->Case->assertEquals(5, $result);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$data = array('var' => 'set');
|
|
|
|
$result = $this->Case->testAction('/tests_apps_posts/post_var', array(
|
|
|
|
'data' => $data,
|
|
|
|
'return' => 'vars'
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals($data, $result['data']);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$result = $this->Case->testAction('/tests_apps/set_action', array(
|
|
|
|
'return' => 'view'
|
|
|
|
));
|
2012-03-23 06:37:12 +00:00
|
|
|
$this->assertEquals('This is the TestsAppsController index view string', $result);
|
2010-12-15 03:58:27 +00:00
|
|
|
|
|
|
|
$result = $this->Case->testAction('/tests_apps/set_action', array(
|
|
|
|
'return' => 'contents'
|
|
|
|
));
|
2022-12-30 08:28:11 +00:00
|
|
|
$this->assertMatchesRegularExpression('/<html/', $result);
|
|
|
|
$this->assertMatchesRegularExpression('/This is the TestsAppsController index view/', $result);
|
|
|
|
$this->assertMatchesRegularExpression('/<\/html>/', $result);
|
2010-12-15 03:58:27 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 14:58:21 +00:00
|
|
|
/**
|
|
|
|
* Test that controllers don't get reused.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testNoControllerReuse() {
|
2011-11-05 21:27:55 +00:00
|
|
|
$this->Case->autoMock = true;
|
2011-10-22 14:58:21 +00:00
|
|
|
$result = $this->Case->testAction('/tests_apps/index', array(
|
|
|
|
'data' => array('var' => 'first call'),
|
|
|
|
'method' => 'get',
|
|
|
|
'return' => 'contents',
|
|
|
|
));
|
2022-12-30 08:53:58 +00:00
|
|
|
$this->assertStringContainsString('<html', $result);
|
|
|
|
$this->assertStringContainsString('This is the TestsAppsController index view', $result);
|
|
|
|
$this->assertStringContainsString('first call', $result);
|
|
|
|
$this->assertStringContainsString('</html>', $result);
|
2012-03-18 21:31:53 +00:00
|
|
|
|
2011-10-22 14:58:21 +00:00
|
|
|
$result = $this->Case->testAction('/tests_apps/index', array(
|
|
|
|
'data' => array('var' => 'second call'),
|
|
|
|
'method' => 'get',
|
|
|
|
'return' => 'contents'
|
|
|
|
));
|
2022-12-30 08:53:58 +00:00
|
|
|
$this->assertStringContainsString('second call', $result);
|
2011-10-22 14:58:21 +00:00
|
|
|
|
|
|
|
$result = $this->Case->testAction('/tests_apps/index', array(
|
|
|
|
'data' => array('var' => 'third call'),
|
|
|
|
'method' => 'get',
|
|
|
|
'return' => 'contents'
|
|
|
|
));
|
2022-12-30 08:53:58 +00:00
|
|
|
$this->assertStringContainsString('third call', $result);
|
2011-10-22 14:58:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-05 21:27:55 +00:00
|
|
|
/**
|
|
|
|
* Test that multiple calls to redirect in the same test method don't cause issues.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTestActionWithMultipleRedirect() {
|
2013-01-23 12:45:50 +00:00
|
|
|
$this->Case->generate('TestsApps');
|
2011-11-05 21:27:55 +00:00
|
|
|
|
|
|
|
$options = array('method' => 'get');
|
|
|
|
$this->Case->testAction('/tests_apps/redirect_to', $options);
|
|
|
|
$this->Case->testAction('/tests_apps/redirect_to', $options);
|
|
|
|
}
|
|
|
|
|
2012-03-24 19:04:29 +00:00
|
|
|
/**
|
|
|
|
* Tests that Components storing response or request objects internally during construct
|
|
|
|
* will always have a fresh reference to those object available
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testComponentsSameRequestAndResponse() {
|
|
|
|
$this->Case->generate('TestsApps');
|
|
|
|
$options = array('method' => 'get');
|
|
|
|
$this->Case->testAction('/tests_apps/index', $options);
|
|
|
|
$this->assertSame($this->Case->controller->response, $this->Case->controller->RequestHandler->response);
|
|
|
|
$this->assertSame($this->Case->controller->request, $this->Case->controller->RequestHandler->request);
|
|
|
|
}
|
|
|
|
|
2012-05-01 00:36:03 +00:00
|
|
|
/**
|
|
|
|
* Test that testAction() doesn't destroy data in GET & POST
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testRestoreGetPost() {
|
|
|
|
$restored = array('new' => 'value');
|
|
|
|
|
|
|
|
$_GET = $restored;
|
|
|
|
$_POST = $restored;
|
|
|
|
|
|
|
|
$this->Case->generate('TestsApps');
|
|
|
|
$options = array('method' => 'get');
|
|
|
|
$this->Case->testAction('/tests_apps/index', $options);
|
|
|
|
|
|
|
|
$this->assertEquals($restored, $_GET);
|
|
|
|
$this->assertEquals($restored, $_POST);
|
|
|
|
}
|
|
|
|
|
2017-04-13 11:39:17 +00:00
|
|
|
/**
|
|
|
|
* Tests that the `App.base` path is properly stripped from the URL generated from the
|
|
|
|
* given URL array, and that consequently the correct controller/action is being matched.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testAppBaseConfigCompatibilityWithArrayUrls() {
|
|
|
|
Configure::write('App.base', '/cakephp');
|
|
|
|
|
|
|
|
$this->Case->generate('TestsApps');
|
|
|
|
$this->Case->testAction(array('controller' => 'tests_apps', 'action' => 'index'));
|
|
|
|
|
|
|
|
$this->assertEquals('/cakephp', $this->Case->controller->request->base);
|
|
|
|
$this->assertEquals('/cakephp/', $this->Case->controller->request->webroot);
|
|
|
|
$this->assertEquals('/cakephp/tests_apps', $this->Case->controller->request->here);
|
|
|
|
$this->assertEquals('tests_apps', $this->Case->controller->request->url);
|
|
|
|
|
|
|
|
$expected = array(
|
|
|
|
'plugin' => null,
|
|
|
|
'controller' => 'tests_apps',
|
|
|
|
'action' => 'index',
|
|
|
|
'named' => array(),
|
|
|
|
'pass' => array(),
|
|
|
|
);
|
2017-04-15 19:27:29 +00:00
|
|
|
$this->assertEquals($expected, array_intersect_key($this->Case->controller->request->params, $expected));
|
2017-04-13 11:39:17 +00:00
|
|
|
}
|
2017-04-18 18:54:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that query string data from URL arrays properly makes it into the request object
|
|
|
|
* on GET requests.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTestActionWithArrayUrlQueryStringDataViaGetRequest() {
|
|
|
|
$query = array('foo' => 'bar');
|
|
|
|
|
|
|
|
$this->Case->generate('TestsApps');
|
|
|
|
$this->Case->testAction(
|
|
|
|
array(
|
|
|
|
'controller' => 'tests_apps',
|
|
|
|
'action' => 'index',
|
|
|
|
'?' => $query
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'method' => 'get'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('tests_apps', $this->Case->controller->request->url);
|
|
|
|
$this->assertEquals($query, $this->Case->controller->request->query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that query string data from URL arrays properly makes it into the request object
|
|
|
|
* on POST requests.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTestActionWithArrayUrlQueryStringDataViaPostRequest() {
|
|
|
|
$query = array('foo' => 'bar');
|
|
|
|
|
|
|
|
$this->Case->generate('TestsApps');
|
|
|
|
$this->Case->testAction(
|
|
|
|
array(
|
|
|
|
'controller' => 'tests_apps',
|
|
|
|
'action' => 'index',
|
|
|
|
'?' => $query
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'method' => 'post'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('tests_apps', $this->Case->controller->request->url);
|
|
|
|
$this->assertEquals($query, $this->Case->controller->request->query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that query string data from both, URL arrays as well as the `data` option,
|
|
|
|
* properly makes it into the request object.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function testTestActionWithArrayUrlQueryStringDataAndDataOptionViaGetRequest() {
|
|
|
|
$query = array('foo' => 'bar');
|
|
|
|
$data = array('bar' => 'foo');
|
|
|
|
|
|
|
|
$this->Case->generate('TestsApps');
|
|
|
|
$this->Case->testAction(
|
|
|
|
array(
|
|
|
|
'controller' => 'tests_apps',
|
|
|
|
'action' => 'index',
|
|
|
|
'?' => $query
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'method' => 'get',
|
|
|
|
'data' => $data
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('tests_apps', $this->Case->controller->request->url);
|
|
|
|
$this->assertEquals($data + $query, $this->Case->controller->request->query);
|
|
|
|
}
|
2011-04-17 10:35:21 +00:00
|
|
|
}
|