| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- <?php
- /**
- * ObjectTest file
- *
- * PHP 5
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @package Cake.Test.Case.Core
- * @since CakePHP(tm) v 1.2.0.5432
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- App::uses('Object', 'Core');
- App::uses('Router', 'Routing');
- App::uses('Controller', 'Controller');
- App::uses('Model', 'Model');
- /**
- * RequestActionPost class
- *
- * @package Cake.Test.Case.Core
- */
- class RequestActionPost extends CakeTestModel {
- /**
- * name property
- *
- * @var string 'ControllerPost'
- */
- public $name = 'RequestActionPost';
- /**
- * useTable property
- *
- * @var string 'posts'
- */
- public $useTable = 'posts';
- }
- /**
- * RequestActionController class
- *
- * @package Cake.Test.Case.Core
- */
- class RequestActionController extends Controller {
- /**
- * uses property
- *
- * @var array
- * @access public
- */
- public $uses = array('RequestActionPost');
- /**
- * test_request_action method
- *
- * @access public
- * @return void
- */
- public function test_request_action() {
- return 'This is a test';
- }
- /**
- * another_ra_test method
- *
- * @param mixed $id
- * @param mixed $other
- * @access public
- * @return void
- */
- public function another_ra_test($id, $other) {
- return $id + $other;
- }
- /**
- * normal_request_action method
- *
- * @return void
- */
- public function normal_request_action() {
- return 'Hello World';
- }
- /**
- * returns $this->here
- *
- * @return void
- */
- public function return_here() {
- return $this->here;
- }
- /**
- * paginate_request_action method
- *
- * @return void
- */
- public function paginate_request_action() {
- $this->paginate();
- return true;
- }
- /**
- * post pass, testing post passing
- *
- * @return array
- */
- public function post_pass() {
- return $this->request->data;
- }
- /**
- * test param passing and parsing.
- *
- * @return array
- */
- public function params_pass() {
- return $this->request;
- }
- public function param_check() {
- $this->autoRender = false;
- $content = '';
- if (isset($this->request->params[0])) {
- $content = 'return found';
- }
- $this->response->body($content);
- }
- }
- /**
- * TestObject class
- *
- * @package Cake.Test.Case.Core
- */
- class TestObject extends Object {
- /**
- * firstName property
- *
- * @var string 'Joel'
- */
- public $firstName = 'Joel';
- /**
- * lastName property
- *
- * @var string 'Moss'
- */
- public $lastName = 'Moss';
- /**
- * methodCalls property
- *
- * @var array
- */
- public $methodCalls = array();
- /**
- * emptyMethod method
- *
- * @return void
- */
- public function emptyMethod() {
- $this->methodCalls[] = 'emptyMethod';
- }
- /**
- * oneParamMethod method
- *
- * @param mixed $param
- * @return void
- */
- public function oneParamMethod($param) {
- $this->methodCalls[] = array('oneParamMethod' => array($param));
- }
- /**
- * twoParamMethod method
- *
- * @param mixed $param
- * @param mixed $paramTwo
- * @return void
- */
- public function twoParamMethod($param, $paramTwo) {
- $this->methodCalls[] = array('twoParamMethod' => array($param, $paramTwo));
- }
- /**
- * threeParamMethod method
- *
- * @param mixed $param
- * @param mixed $paramTwo
- * @param mixed $paramThree
- * @return void
- */
- public function threeParamMethod($param, $paramTwo, $paramThree) {
- $this->methodCalls[] = array('threeParamMethod' => array($param, $paramTwo, $paramThree));
- }
- /**
- * fourParamMethod method
- *
- * @param mixed $param
- * @param mixed $paramTwo
- * @param mixed $paramThree
- * @param mixed $paramFour
- * @return void
- */
- public function fourParamMethod($param, $paramTwo, $paramThree, $paramFour) {
- $this->methodCalls[] = array('fourParamMethod' => array($param, $paramTwo, $paramThree, $paramFour));
- }
- /**
- * fiveParamMethod method
- *
- * @param mixed $param
- * @param mixed $paramTwo
- * @param mixed $paramThree
- * @param mixed $paramFour
- * @param mixed $paramFive
- * @return void
- */
- public function fiveParamMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive) {
- $this->methodCalls[] = array('fiveParamMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive));
- }
- /**
- * crazyMethod method
- *
- * @param mixed $param
- * @param mixed $paramTwo
- * @param mixed $paramThree
- * @param mixed $paramFour
- * @param mixed $paramFive
- * @param mixed $paramSix
- * @param mixed $paramSeven
- * @return void
- */
- public function crazyMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven = null) {
- $this->methodCalls[] = array('crazyMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven));
- }
- /**
- * methodWithOptionalParam method
- *
- * @param mixed $param
- * @return void
- */
- public function methodWithOptionalParam($param = null) {
- $this->methodCalls[] = array('methodWithOptionalParam' => array($param));
- }
- /**
- * undocumented function
- *
- * @return void
- */
- public function set($properties = array()) {
- return parent::_set($properties);
- }
- }
- /**
- * ObjectTestModel class
- *
- * @package Cake.Test.Case.Core
- */
- class ObjectTestModel extends CakeTestModel {
- public $useTable = false;
- public $name = 'ObjectTestModel';
- }
- /**
- * Object Test class
- *
- * @package Cake.Test.Case.Core
- */
- class ObjectTest extends CakeTestCase {
- /**
- * fixtures
- *
- * @var string
- */
- public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->object = new TestObject();
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- CakePlugin::unload();
- unset($this->object);
- }
- /**
- * testLog method
- *
- * @return void
- */
- public function testLog() {
- if (file_exists(LOGS . 'error.log')) {
- unlink(LOGS . 'error.log');
- }
- $this->assertTrue($this->object->log('Test warning 1'));
- $this->assertTrue($this->object->log(array('Test' => 'warning 2')));
- $result = file(LOGS . 'error.log');
- $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Test warning 1$/', $result[0]);
- $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Array$/', $result[1]);
- $this->assertRegExp('/^\($/', $result[2]);
- $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
- $this->assertRegExp('/^\)$/', $result[4]);
- unlink(LOGS . 'error.log');
- $this->assertTrue($this->object->log('Test warning 1', LOG_WARNING));
- $this->assertTrue($this->object->log(array('Test' => 'warning 2'), LOG_WARNING));
- $result = file(LOGS . 'error.log');
- $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1$/', $result[0]);
- $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Array$/', $result[1]);
- $this->assertRegExp('/^\($/', $result[2]);
- $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
- $this->assertRegExp('/^\)$/', $result[4]);
- unlink(LOGS . 'error.log');
- }
- /**
- * testSet method
- *
- * @return void
- */
- public function testSet() {
- $this->object->set('a string');
- $this->assertEquals('Joel', $this->object->firstName);
- $this->object->set(array('firstName'));
- $this->assertEquals('Joel', $this->object->firstName);
- $this->object->set(array('firstName' => 'Ashley'));
- $this->assertEquals('Ashley', $this->object->firstName);
- $this->object->set(array('firstName' => 'Joel', 'lastName' => 'Moose'));
- $this->assertEquals('Joel', $this->object->firstName);
- $this->assertEquals('Moose', $this->object->lastName);
- }
- /**
- * testToString method
- *
- * @return void
- */
- public function testToString() {
- $result = strtolower($this->object->toString());
- $this->assertEquals('testobject', $result);
- }
- /**
- * testMethodDispatching method
- *
- * @return void
- */
- public function testMethodDispatching() {
- $this->object->emptyMethod();
- $expected = array('emptyMethod');
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->oneParamMethod('Hello');
- $expected[] = array('oneParamMethod' => array('Hello'));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->twoParamMethod(true, false);
- $expected[] = array('twoParamMethod' => array(true, false));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->threeParamMethod(true, false, null);
- $expected[] = array('threeParamMethod' => array(true, false, null));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->crazyMethod(1, 2, 3, 4, 5, 6, 7);
- $expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object = new TestObject();
- $this->assertSame($this->object->methodCalls, array());
- $this->object->dispatchMethod('emptyMethod');
- $expected = array('emptyMethod');
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('oneParamMethod', array('Hello'));
- $expected[] = array('oneParamMethod' => array('Hello'));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('twoParamMethod', array(true, false));
- $expected[] = array('twoParamMethod' => array(true, false));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('threeParamMethod', array(true, false, null));
- $expected[] = array('threeParamMethod' => array(true, false, null));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('fourParamMethod', array(1, 2, 3, 4));
- $expected[] = array('fourParamMethod' => array(1, 2, 3, 4));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('fiveParamMethod', array(1, 2, 3, 4, 5));
- $expected[] = array('fiveParamMethod' => array(1, 2, 3, 4, 5));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('crazyMethod', array(1, 2, 3, 4, 5, 6, 7));
- $expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('methodWithOptionalParam', array('Hello'));
- $expected[] = array('methodWithOptionalParam' => array("Hello"));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('methodWithOptionalParam');
- $expected[] = array('methodWithOptionalParam' => array(null));
- $this->assertSame($this->object->methodCalls, $expected);
- }
- /**
- * testRequestAction method
- *
- * @return void
- */
- public function testRequestAction() {
- App::build(array(
- 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
- 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
- 'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS)
- ), App::RESET);
- $this->assertNull(Router::getRequest(), 'request stack should be empty.');
- $result = $this->object->requestAction('');
- $this->assertFalse($result);
- $result = $this->object->requestAction('/request_action/test_request_action');
- $expected = 'This is a test';
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction(
- Configure::read('App.fullBaseURL') . '/request_action/test_request_action'
- );
- $expected = 'This is a test';
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction('/request_action/another_ra_test/2/5');
- $expected = 7;
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction('/tests_apps/index', array('return'));
- $expected = 'This is the TestsAppsController index view ';
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction('/tests_apps/some_method');
- $expected = 5;
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction('/request_action/paginate_request_action');
- $this->assertTrue($result);
- $result = $this->object->requestAction('/request_action/normal_request_action');
- $expected = 'Hello World';
- $this->assertEquals($expected, $result);
- $this->assertNull(Router::getRequest(), 'requests were not popped off the stack, this will break url generation');
- }
- /**
- * test requestAction() and plugins.
- *
- * @return void
- */
- public function testRequestActionPlugins() {
- App::build(array(
- 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
- ), App::RESET);
- CakePlugin::load('TestPlugin');
- Router::reload();
- $result = $this->object->requestAction('/test_plugin/tests/index', array('return'));
- $expected = 'test plugin index';
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction('/test_plugin/tests/index/some_param', array('return'));
- $expected = 'test plugin index';
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction(
- array('controller' => 'tests', 'action' => 'index', 'plugin' => 'test_plugin'), array('return')
- );
- $expected = 'test plugin index';
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction('/test_plugin/tests/some_method');
- $expected = 25;
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction(
- array('controller' => 'tests', 'action' => 'some_method', 'plugin' => 'test_plugin')
- );
- $expected = 25;
- $this->assertEquals($expected, $result);
- }
- /**
- * test requestAction() with arrays.
- *
- * @return void
- */
- public function testRequestActionArray() {
- App::build(array(
- 'Model' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Model' . DS),
- 'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS),
- 'Controller' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Controller' . DS),
- 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
- ), App::RESET);
- CakePlugin::load(array('TestPlugin'));
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'test_request_action')
- );
- $expected = 'This is a test';
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'another_ra_test'),
- array('pass' => array('5', '7'))
- );
- $expected = 12;
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction(
- array('controller' => 'tests_apps', 'action' => 'index'), array('return')
- );
- $expected = 'This is the TestsAppsController index view ';
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction(array('controller' => 'tests_apps', 'action' => 'some_method'));
- $expected = 5;
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'normal_request_action')
- );
- $expected = 'Hello World';
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'paginate_request_action')
- );
- $this->assertTrue($result);
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'paginate_request_action'),
- array('pass' => array(5), 'named' => array('param' => 'value'))
- );
- $this->assertTrue($result);
- }
- /**
- * Test that requestAction() does not forward the 0 => return value.
- *
- * @return void
- */
- public function testRequestActionRemoveReturnParam() {
- $result = $this->object->requestAction(
- '/request_action/param_check', array('return')
- );
- $this->assertEquals('', $result, 'Return key was found');
- }
- /**
- * Test that requestAction() is populating $this->params properly
- *
- * @return void
- */
- public function testRequestActionParamParseAndPass() {
- $result = $this->object->requestAction('/request_action/params_pass');
- $this->assertEquals('request_action/params_pass', $result->url);
- $this->assertEquals('request_action', $result['controller']);
- $this->assertEquals('params_pass', $result['action']);
- $this->assertEquals(null, $result['plugin']);
- $result = $this->object->requestAction('/request_action/params_pass/sort:desc/limit:5');
- $expected = array('sort' => 'desc', 'limit' => 5,);
- $this->assertEquals($expected, $result['named']);
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'params_pass'),
- array('named' => array('sort' => 'desc', 'limit' => 5))
- );
- $this->assertEquals($expected, $result['named']);
- }
- /**
- * Test that requestAction handles get parameters correctly.
- *
- * @return void
- */
- public function testRequestActionGetParameters() {
- $result = $this->object->requestAction(
- '/request_action/params_pass?get=value&limit=5'
- );
- $this->assertEquals('value', $result->query['get']);
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'params_pass'),
- array('url' => array('get' => 'value', 'limit' => 5))
- );
- $this->assertEquals('value', $result->query['get']);
- }
- /**
- * test that requestAction does not fish data out of the POST
- * superglobal.
- *
- * @return void
- */
- public function testRequestActionNoPostPassing() {
- $_tmp = $_POST;
- $_POST = array('data' => array(
- 'item' => 'value'
- ));
- $result = $this->object->requestAction(array('controller' => 'request_action', 'action' => 'post_pass'));
- $this->assertEmpty($result);
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'post_pass'),
- array('data' => $_POST['data'])
- );
- $expected = $_POST['data'];
- $this->assertEquals($expected, $result);
- $result = $this->object->requestAction('/request_action/post_pass');
- $expected = $_POST['data'];
- $this->assertEquals($expected, $result);
- $_POST = $_tmp;
- }
- /**
- * Test requestAction with post data.
- *
- * @return void
- */
- public function testRequestActionPostWithData() {
- $data = array(
- 'Post' => array('id' => 2)
- );
- $result = $this->object->requestAction(
- array('controller' => 'request_action', 'action' => 'post_pass'),
- array('data' => $data)
- );
- $this->assertEquals($data, $result);
- $result = $this->object->requestAction(
- '/request_action/post_pass',
- array('data' => $data)
- );
- $this->assertEquals($data, $result);
- }
- }
|