2010-04-26 04:50:02 +00:00
< ? php
2010-09-12 18:05:57 +00:00
/**
* CakeRequest Test case file .
*
2017-06-10 21:33:55 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( https :// cakephp . org )
2017-06-10 22:10:52 +00:00
* Copyright ( c ) Cake Software Foundation , Inc . ( https :// cakefoundation . org )
2010-09-12 18:05:57 +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-09-12 18:05:57 +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 ( tm ) Project
2011-07-26 06:16:14 +00:00
* @ package Cake . Test . Case . Network
2010-09-12 18:05:57 +00:00
* @ since CakePHP ( tm ) v 2.0
2017-06-10 22:23:14 +00:00
* @ license https :// opensource . org / licenses / mit - license . php MIT License
2010-09-12 18:05:57 +00:00
*/
2010-12-09 05:13:11 +00:00
2011-01-09 03:27:27 +00:00
App :: uses ( 'Dispatcher' , 'Routing' );
2011-04-30 17:05:10 +00:00
App :: uses ( 'Xml' , 'Utility' );
2010-12-09 05:13:11 +00:00
App :: uses ( 'CakeRequest' , 'Network' );
2010-04-26 04:50:02 +00:00
2013-05-30 22:11:14 +00:00
/**
2016-08-10 10:22:09 +00:00
* TestCakeRequest
2013-05-30 22:11:14 +00:00
*
* @ package Cake . Test . Case . Network
*/
2012-06-06 20:58:27 +00:00
class TestCakeRequest extends CakeRequest {
2013-06-26 11:36:50 +00:00
/**
* reConstruct method
*
* @ param string $url
2014-07-03 13:36:42 +00:00
* @ param bool $parseEnvironment
2013-07-05 14:17:23 +00:00
* @ return void
2013-06-26 11:36:50 +00:00
*/
2012-06-06 20:58:27 +00:00
public function reConstruct ( $url = 'some/path' , $parseEnvironment = true ) {
$this -> _base ();
if ( empty ( $url )) {
$url = $this -> _url ();
}
2013-02-12 02:38:08 +00:00
if ( $url [ 0 ] === '/' ) {
2012-06-06 20:58:27 +00:00
$url = substr ( $url , 1 );
}
$this -> url = $url ;
if ( $parseEnvironment ) {
$this -> _processPost ();
$this -> _processGet ();
$this -> _processFiles ();
}
$this -> here = $this -> base . '/' . $this -> url ;
}
}
2013-06-26 11:36:50 +00:00
/**
2016-08-10 10:22:09 +00:00
* CakeRequestTest
2013-06-26 11:36:50 +00:00
*/
2011-09-13 03:00:35 +00:00
class CakeRequestTest extends CakeTestCase {
2012-03-17 02:19:01 +00:00
2010-04-26 04:50:02 +00:00
/**
2013-06-26 11:36:50 +00:00
* Setup callback
2010-04-26 04:50:02 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function setUp () {
2010-09-26 01:36:49 +00:00
parent :: setUp ();
2011-02-19 15:00:34 +00:00
$this -> _app = Configure :: read ( 'App' );
2011-04-25 03:31:44 +00:00
$this -> _case = null ;
if ( isset ( $_GET [ 'case' ])) {
$this -> _case = $_GET [ 'case' ];
unset ( $_GET [ 'case' ]);
}
2011-02-19 15:00:34 +00:00
Configure :: write ( 'App.baseUrl' , false );
2010-04-26 04:50:02 +00:00
}
/**
2013-06-26 11:36:50 +00:00
* TearDown
2010-04-26 04:50:02 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function tearDown () {
2010-09-26 01:36:49 +00:00
parent :: tearDown ();
2011-04-25 03:31:44 +00:00
if ( ! empty ( $this -> _case )) {
$_GET [ 'case' ] = $this -> _case ;
}
2011-02-19 15:00:34 +00:00
Configure :: write ( 'App' , $this -> _app );
2010-04-26 04:50:02 +00:00
}
2014-11-20 20:07:25 +00:00
/**
* Test the header detector .
*
* @ return void
*/
public function testHeaderDetector () {
2014-11-27 00:00:44 +00:00
$request = new CakeRequest ( 'some/path' );
$request -> addDetector ( 'host' , array ( 'header' => array ( 'host' => 'cakephp.org' )));
$_SERVER [ 'HTTP_HOST' ] = 'cakephp.org' ;
$this -> assertTrue ( $request -> is ( 'host' ));
$_SERVER [ 'HTTP_HOST' ] = 'php.net' ;
$this -> assertFalse ( $request -> is ( 'host' ));
}
/**
* Test the accept header detector .
*
* @ return void
*/
public function testExtensionDetector () {
$request = new CakeRequest ( 'some/path' );
$request -> params [ 'ext' ] = 'json' ;
$this -> assertTrue ( $request -> is ( 'json' ));
$request -> params [ 'ext' ] = 'xml' ;
$this -> assertFalse ( $request -> is ( 'json' ));
}
/**
* Test the accept header detector .
*
* @ return void
*/
public function testAcceptHeaderDetector () {
$request = new CakeRequest ( 'some/path' );
2014-11-22 16:30:53 +00:00
$_SERVER [ 'HTTP_ACCEPT' ] = 'application/json, text/plain, */*' ;
2014-11-27 00:00:44 +00:00
$this -> assertTrue ( $request -> is ( 'json' ));
$_SERVER [ 'HTTP_ACCEPT' ] = 'text/plain, */*' ;
$this -> assertFalse ( $request -> is ( 'json' ));
2014-11-20 20:07:25 +00:00
}
2010-05-04 02:50:38 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test that the autoparse = false constructor works .
2010-05-04 02:50:38 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testNoAutoParseConstruction () {
2010-05-04 02:50:38 +00:00
$_GET = array (
'one' => 'param'
);
$request = new CakeRequest ( null , false );
$this -> assertFalse ( isset ( $request -> query [ 'one' ]));
}
2016-02-16 02:44:27 +00:00
2016-02-16 01:31:24 +00:00
/**
* Test the content type method .
2016-08-08 03:08:55 +00:00
*
2016-02-16 01:31:24 +00:00
* @ return void
*/
2016-02-16 02:44:27 +00:00
public function testContentType () {
$_SERVER [ 'HTTP_CONTENT_TYPE' ] = 'application/json' ;
$request = new CakeRequest ( '/' , false );
$this -> assertEquals ( 'application/json' , $request -> contentType ());
$_SERVER [ 'CONTENT_TYPE' ] = 'application/xml' ;
$request = new CakeRequest ( '/' , false );
$this -> assertEquals ( 'application/xml' , $request -> contentType (), 'prefer non http header.' );
}
2010-05-04 02:50:38 +00:00
2010-04-26 04:50:02 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test construction
2010-04-26 04:50:02 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testConstructionGetParsing () {
2010-04-27 02:33:56 +00:00
$_GET = array (
2010-04-26 04:50:02 +00:00
'one' => 'param' ,
'two' => 'banana'
);
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $request -> query , $_GET );
2011-10-28 05:01:17 +00:00
2010-04-30 03:20:31 +00:00
$_GET = array (
'one' => 'param' ,
'two' => 'banana' ,
);
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $request -> query , $_GET );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'some/path' , $request -> url );
2010-04-26 04:56:54 +00:00
}
2010-05-01 15:04:41 +00:00
/**
2013-10-08 03:17:58 +00:00
* Test that querystring args provided in the URL string are parsed .
2010-05-01 15:04:41 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testQueryStringParsingFromInputUrl () {
2010-05-01 15:04:41 +00:00
$_GET = array ();
$request = new CakeRequest ( 'some/path?one=something&two=else' );
2011-02-21 02:52:20 +00:00
$expected = array ( 'one' => 'something' , 'two' => 'else' );
2011-12-09 02:48:40 +00:00
$this -> assertEquals ( $expected , $request -> query );
2017-01-05 03:34:19 +00:00
$this -> assertEquals ( 'some/path' , $request -> url );
$this -> assertStringEndsWith ( '/some/path?one=something&two=else' , $request -> here ());
2010-05-01 15:04:41 +00:00
}
2011-12-09 02:48:40 +00:00
/**
* Test that named arguments + querystrings are handled correctly .
*
* @ return void
*/
public function testQueryStringAndNamedParams () {
$_SERVER [ 'REQUEST_URI' ] = '/tasks/index/page:1?ts=123456' ;
$request = new CakeRequest ();
$this -> assertEquals ( 'tasks/index/page:1' , $request -> url );
$_SERVER [ 'REQUEST_URI' ] = '/tasks/index/page:1/?ts=123456' ;
$request = new CakeRequest ();
$this -> assertEquals ( 'tasks/index/page:1/' , $request -> url );
2012-12-21 15:12:51 +00:00
2017-06-10 21:33:55 +00:00
$_SERVER [ 'REQUEST_URI' ] = '/some/path?url=https://cakephp.org' ;
2012-12-21 15:12:51 +00:00
$request = new CakeRequest ();
$this -> assertEquals ( 'some/path' , $request -> url );
2012-12-21 18:55:20 +00:00
2017-06-10 21:33:55 +00:00
$_SERVER [ 'REQUEST_URI' ] = Configure :: read ( 'App.fullBaseUrl' ) . '/other/path?url=https://cakephp.org' ;
2012-12-21 18:55:20 +00:00
$request = new CakeRequest ();
$this -> assertEquals ( 'other/path' , $request -> url );
2011-12-09 02:48:40 +00:00
}
2010-05-01 15:06:51 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test addParams () method
2010-05-01 15:06:51 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAddParams () {
2010-05-01 15:06:51 +00:00
$request = new CakeRequest ( 'some/path' );
$request -> params = array ( 'controller' => 'posts' , 'action' => 'view' );
2010-05-02 05:22:34 +00:00
$result = $request -> addParams ( array ( 'plugin' => null , 'action' => 'index' ));
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $result , $request , 'Method did not return itself. %s' );
2010-05-01 15:06:51 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'posts' , $request -> controller );
$this -> assertEquals ( 'index' , $request -> action );
$this -> assertEquals ( null , $request -> plugin );
2010-05-01 15:06:51 +00:00
}
2010-05-02 05:22:34 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test splicing in paths .
2010-05-02 05:22:34 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAddPaths () {
2010-05-02 05:22:34 +00:00
$request = new CakeRequest ( 'some/path' );
$request -> webroot = '/some/path/going/here/' ;
$result = $request -> addPaths ( array (
'random' => '/something' , 'webroot' => '/' , 'here' => '/' , 'base' => '/base_dir'
));
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $result , $request , 'Method did not return itself. %s' );
2010-05-02 05:22:34 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/' , $request -> webroot );
$this -> assertEquals ( '/base_dir' , $request -> base );
$this -> assertEquals ( '/' , $request -> here );
2010-05-02 05:22:34 +00:00
$this -> assertFalse ( isset ( $request -> random ));
}
2010-04-26 04:56:54 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test parsing POST data into the object .
2010-04-26 04:56:54 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testPostParsing () {
2010-04-26 04:56:54 +00:00
$_POST = array ( 'data' => array (
'Article' => array ( 'title' )
));
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2012-02-22 21:23:52 +00:00
$this -> assertEquals ( $_POST [ 'data' ], $request -> data );
2010-04-26 04:56:54 +00:00
$_POST = array ( 'one' => 1 , 'two' => 'three' );
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2011-04-29 02:42:50 +00:00
$this -> assertEquals ( $_POST , $request -> data );
2012-02-19 03:18:16 +00:00
$_POST = array (
'data' => array (
'Article' => array ( 'title' => 'Testing' ),
),
'action' => 'update'
);
$request = new CakeRequest ( 'some/path' );
$expected = array (
'Article' => array ( 'title' => 'Testing' ),
'action' => 'update'
);
$this -> assertEquals ( $expected , $request -> data );
2012-02-22 20:53:51 +00:00
$_POST = array ( 'data' => array (
'Article' => array ( 'title' ),
'Tag' => array ( 'Tag' => array ( 1 , 2 ))
));
$request = new CakeRequest ( 'some/path' );
2012-02-22 21:31:28 +00:00
$this -> assertEquals ( $_POST [ 'data' ], $request -> data );
2012-02-22 21:23:52 +00:00
$_POST = array ( 'data' => array (
'Article' => array ( 'title' => 'some title' ),
'Tag' => array ( 'Tag' => array ( 1 , 2 ))
));
$request = new CakeRequest ( 'some/path' );
2012-02-22 21:31:28 +00:00
$this -> assertEquals ( $_POST [ 'data' ], $request -> data );
2012-03-07 01:22:13 +00:00
$_POST = array (
'a' => array ( 1 , 2 ),
'b' => array ( 1 , 2 )
);
$request = new CakeRequest ( 'some/path' );
$this -> assertEquals ( $_POST , $request -> data );
2010-04-26 04:50:02 +00:00
}
2010-04-26 05:08:33 +00:00
2012-06-06 20:58:27 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test parsing PUT data into the object .
2012-06-06 20:58:27 +00:00
*
* @ return void
*/
public function testPutParsing () {
$_SERVER [ 'REQUEST_METHOD' ] = 'PUT' ;
2012-08-14 16:40:33 +00:00
$_SERVER [ 'CONTENT_TYPE' ] = 'application/x-www-form-urlencoded; charset=UTF-8' ;
2012-06-06 20:58:27 +00:00
$data = array ( 'data' => array (
'Article' => array ( 'title' )
));
$request = $this -> getMock ( 'TestCakeRequest' , array ( '_readInput' ));
$request -> expects ( $this -> at ( 0 )) -> method ( '_readInput' )
-> will ( $this -> returnValue ( 'data[Article][]=title' ));
$request -> reConstruct ();
$this -> assertEquals ( $data [ 'data' ], $request -> data );
$data = array ( 'one' => 1 , 'two' => 'three' );
$request = $this -> getMock ( 'TestCakeRequest' , array ( '_readInput' ));
$request -> expects ( $this -> at ( 0 )) -> method ( '_readInput' )
-> will ( $this -> returnValue ( 'one=1&two=three' ));
$request -> reConstruct ();
$this -> assertEquals ( $data , $request -> data );
$request = $this -> getMock ( 'TestCakeRequest' , array ( '_readInput' ));
$request -> expects ( $this -> at ( 0 )) -> method ( '_readInput' )
-> will ( $this -> returnValue ( 'data[Article][title]=Testing&action=update' ));
$request -> reConstruct ();
$expected = array (
'Article' => array ( 'title' => 'Testing' ),
'action' => 'update'
);
$this -> assertEquals ( $expected , $request -> data );
2012-06-18 00:03:45 +00:00
$_SERVER [ 'REQUEST_METHOD' ] = 'DELETE' ;
2012-06-06 20:58:27 +00:00
$data = array ( 'data' => array (
'Article' => array ( 'title' ),
'Tag' => array ( 'Tag' => array ( 1 , 2 ))
));
$request = $this -> getMock ( 'TestCakeRequest' , array ( '_readInput' ));
$request -> expects ( $this -> at ( 0 )) -> method ( '_readInput' )
-> will ( $this -> returnValue ( 'data[Article][]=title&Tag[Tag][]=1&Tag[Tag][]=2' ));
$request -> reConstruct ();
$this -> assertEquals ( $data [ 'data' ], $request -> data );
$data = array ( 'data' => array (
'Article' => array ( 'title' => 'some title' ),
'Tag' => array ( 'Tag' => array ( 1 , 2 ))
));
$request = $this -> getMock ( 'TestCakeRequest' , array ( '_readInput' ));
$request -> expects ( $this -> at ( 0 )) -> method ( '_readInput' )
-> will ( $this -> returnValue ( 'data[Article][title]=some%20title&Tag[Tag][]=1&Tag[Tag][]=2' ));
$request -> reConstruct ();
$this -> assertEquals ( $data [ 'data' ], $request -> data );
$data = array (
'a' => array ( 1 , 2 ),
'b' => array ( 1 , 2 )
);
$request = $this -> getMock ( 'TestCakeRequest' , array ( '_readInput' ));
$request -> expects ( $this -> at ( 0 )) -> method ( '_readInput' )
-> will ( $this -> returnValue ( 'a[]=1&a[]=2&b[]=1&b[]=2' ));
$request -> reConstruct ();
$this -> assertEquals ( $data , $request -> data );
}
2012-07-02 22:18:12 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test parsing json PUT data into the object .
2012-07-02 22:18:12 +00:00
*
* @ return void
*/
public function testPutParsingJSON () {
$_SERVER [ 'REQUEST_METHOD' ] = 'PUT' ;
$_SERVER [ 'CONTENT_TYPE' ] = 'application/json' ;
$request = $this -> getMock ( 'TestCakeRequest' , array ( '_readInput' ));
$request -> expects ( $this -> at ( 0 )) -> method ( '_readInput' )
2012-11-01 01:02:47 +00:00
-> will ( $this -> returnValue ( '{"Article":["title"]}' ));
2012-07-02 22:18:12 +00:00
$request -> reConstruct ();
2012-11-01 01:02:47 +00:00
$result = $request -> input ( 'json_decode' , true );
$this -> assertEquals ( array ( 'title' ), $result [ 'Article' ]);
2012-07-02 22:18:12 +00:00
}
2010-04-26 05:20:05 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test parsing of FILES array
2010-04-26 05:20:05 +00:00
*
* @ return void
*/
2012-04-18 01:50:47 +00:00
public function testFilesParsing () {
$_FILES = array (
'data' => array (
'name' => array (
'File' => array (
array ( 'data' => 'cake_sqlserver_patch.patch' ),
array ( 'data' => 'controller.diff' ),
array ( 'data' => '' ),
array ( 'data' => '' ),
),
'Post' => array ( 'attachment' => 'jquery-1.2.1.js' ),
),
'type' => array (
'File' => array (
array ( 'data' => '' ),
array ( 'data' => '' ),
array ( 'data' => '' ),
array ( 'data' => '' ),
),
'Post' => array ( 'attachment' => 'application/x-javascript' ),
2010-04-26 05:20:05 +00:00
),
2012-04-18 01:50:47 +00:00
'tmp_name' => array (
'File' => array (
array ( 'data' => '/private/var/tmp/phpy05Ywj' ),
array ( 'data' => '/private/var/tmp/php7MBztY' ),
array ( 'data' => '' ),
array ( 'data' => '' ),
),
'Post' => array ( 'attachment' => '/private/var/tmp/phpEwlrIo' ),
2010-04-26 05:20:05 +00:00
),
2012-04-18 01:50:47 +00:00
'error' => array (
'File' => array (
array ( 'data' => 0 ),
array ( 'data' => 0 ),
array ( 'data' => 4 ),
array ( 'data' => 4 )
),
'Post' => array ( 'attachment' => 0 )
2010-04-26 05:20:05 +00:00
),
2012-04-18 01:50:47 +00:00
'size' => array (
'File' => array (
array ( 'data' => 6271 ),
array ( 'data' => 350 ),
array ( 'data' => 0 ),
array ( 'data' => 0 ),
),
'Post' => array ( 'attachment' => 80469 )
2010-04-26 05:20:05 +00:00
),
2012-04-18 01:50:47 +00:00
)
);
2010-04-26 05:20:05 +00:00
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2010-04-26 05:20:05 +00:00
$expected = array (
'File' => array (
array (
'data' => array (
2012-04-18 01:50:47 +00:00
'name' => 'cake_sqlserver_patch.patch' ,
'type' => '' ,
'tmp_name' => '/private/var/tmp/phpy05Ywj' ,
'error' => 0 ,
'size' => 6271 ,
)
),
array (
'data' => array (
'name' => 'controller.diff' ,
'type' => '' ,
'tmp_name' => '/private/var/tmp/php7MBztY' ,
'error' => 0 ,
'size' => 350 ,
)
),
array (
'data' => array (
'name' => '' ,
'type' => '' ,
'tmp_name' => '' ,
'error' => 4 ,
'size' => 0 ,
)
),
array (
'data' => array (
'name' => '' ,
'type' => '' ,
'tmp_name' => '' ,
'error' => 4 ,
'size' => 0 ,
)
),
2010-04-26 05:20:05 +00:00
),
2012-04-18 01:50:47 +00:00
'Post' => array (
'attachment' => array (
'name' => 'jquery-1.2.1.js' ,
'type' => 'application/x-javascript' ,
'tmp_name' => '/private/var/tmp/phpEwlrIo' ,
'error' => 0 ,
'size' => 80469 ,
)
)
2010-04-26 05:20:05 +00:00
);
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $expected , $request -> data );
2010-04-26 05:20:05 +00:00
$_FILES = array (
'data' => array (
'name' => array (
'Document' => array (
1 => array (
'birth_cert' => 'born on.txt' ,
'passport' => 'passport.txt' ,
'drivers_license' => 'ugly pic.jpg'
),
2 => array (
'birth_cert' => 'aunt betty.txt' ,
'passport' => 'betty-passport.txt' ,
'drivers_license' => 'betty-photo.jpg'
),
),
),
'type' => array (
'Document' => array (
1 => array (
'birth_cert' => 'application/octet-stream' ,
'passport' => 'application/octet-stream' ,
'drivers_license' => 'application/octet-stream' ,
),
2 => array (
'birth_cert' => 'application/octet-stream' ,
'passport' => 'application/octet-stream' ,
'drivers_license' => 'application/octet-stream' ,
)
)
),
'tmp_name' => array (
'Document' => array (
1 => array (
'birth_cert' => '/private/var/tmp/phpbsUWfH' ,
'passport' => '/private/var/tmp/php7f5zLt' ,
2012-04-18 01:50:47 +00:00
'drivers_license' => '/private/var/tmp/phpMXpZgT' ,
2010-04-26 05:20:05 +00:00
),
2 => array (
'birth_cert' => '/private/var/tmp/php5kHZt0' ,
2012-04-18 01:50:47 +00:00
'passport' => '/private/var/tmp/phpnYkOuM' ,
'drivers_license' => '/private/var/tmp/php9Rq0P3' ,
2010-04-26 05:20:05 +00:00
)
)
),
'error' => array (
'Document' => array (
1 => array (
'birth_cert' => 0 ,
'passport' => 0 ,
2012-04-18 01:50:47 +00:00
'drivers_license' => 0 ,
2010-04-26 05:20:05 +00:00
),
2 => array (
'birth_cert' => 0 ,
2012-04-18 01:50:47 +00:00
'passport' => 0 ,
'drivers_license' => 0 ,
2010-04-26 05:20:05 +00:00
)
)
),
'size' => array (
'Document' => array (
1 => array (
'birth_cert' => 123 ,
'passport' => 458 ,
2012-04-18 01:50:47 +00:00
'drivers_license' => 875 ,
2010-04-26 05:20:05 +00:00
),
2 => array (
'birth_cert' => 876 ,
2012-04-18 01:50:47 +00:00
'passport' => 976 ,
'drivers_license' => 9783 ,
2010-04-26 05:20:05 +00:00
)
)
)
)
);
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2010-04-26 05:20:05 +00:00
$expected = array (
'Document' => array (
1 => array (
'birth_cert' => array (
'name' => 'born on.txt' ,
'tmp_name' => '/private/var/tmp/phpbsUWfH' ,
'error' => 0 ,
'size' => 123 ,
'type' => 'application/octet-stream' ,
),
'passport' => array (
'name' => 'passport.txt' ,
'tmp_name' => '/private/var/tmp/php7f5zLt' ,
'error' => 0 ,
'size' => 458 ,
'type' => 'application/octet-stream' ,
),
'drivers_license' => array (
'name' => 'ugly pic.jpg' ,
'tmp_name' => '/private/var/tmp/phpMXpZgT' ,
'error' => 0 ,
'size' => 875 ,
'type' => 'application/octet-stream' ,
),
),
2 => array (
'birth_cert' => array (
'name' => 'aunt betty.txt' ,
'tmp_name' => '/private/var/tmp/php5kHZt0' ,
'error' => 0 ,
'size' => 876 ,
'type' => 'application/octet-stream' ,
),
'passport' => array (
'name' => 'betty-passport.txt' ,
'tmp_name' => '/private/var/tmp/phpnYkOuM' ,
'error' => 0 ,
'size' => 976 ,
'type' => 'application/octet-stream' ,
),
'drivers_license' => array (
'name' => 'betty-photo.jpg' ,
'tmp_name' => '/private/var/tmp/php9Rq0P3' ,
'error' => 0 ,
'size' => 9783 ,
'type' => 'application/octet-stream' ,
),
),
)
);
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $expected , $request -> data );
2010-04-26 05:20:05 +00:00
$_FILES = array (
'data' => array (
'name' => array ( 'birth_cert' => 'born on.txt' ),
'type' => array ( 'birth_cert' => 'application/octet-stream' ),
'tmp_name' => array ( 'birth_cert' => '/private/var/tmp/phpbsUWfH' ),
'error' => array ( 'birth_cert' => 0 ),
'size' => array ( 'birth_cert' => 123 )
)
);
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2010-04-26 05:20:05 +00:00
$expected = array (
'birth_cert' => array (
'name' => 'born on.txt' ,
'type' => 'application/octet-stream' ,
'tmp_name' => '/private/var/tmp/phpbsUWfH' ,
'error' => 0 ,
'size' => 123
)
);
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $expected , $request -> data );
2010-04-30 03:08:01 +00:00
2010-04-26 05:33:40 +00:00
$_FILES = array (
'something' => array (
'name' => 'something.txt' ,
'type' => 'text/plain' ,
'tmp_name' => '/some/file' ,
'error' => 0 ,
'size' => 123
)
);
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( $request -> params [ 'form' ], $_FILES );
2010-04-26 05:20:05 +00:00
}
2012-10-11 12:39:46 +00:00
/**
* Test that files in the 0 th index work .
2014-04-02 01:02:37 +00:00
*
* @ return void
2012-10-11 12:39:46 +00:00
*/
public function testFilesZeroithIndex () {
$_FILES = array (
0 => array (
'name' => 'cake_sqlserver_patch.patch' ,
'type' => 'text/plain' ,
'tmp_name' => '/private/var/tmp/phpy05Ywj' ,
'error' => 0 ,
'size' => 6271 ,
),
);
$request = new CakeRequest ( 'some/path' );
$this -> assertEquals ( $_FILES , $request -> params [ 'form' ]);
}
2010-04-26 05:08:33 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test method overrides coming in from POST data .
2010-04-26 05:08:33 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testMethodOverrides () {
2010-04-26 05:08:33 +00:00
$_POST = array ( '_method' => 'POST' );
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( env ( 'REQUEST_METHOD' ), 'POST' );
2010-04-26 05:08:33 +00:00
$_POST = array ( '_method' => 'DELETE' );
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( env ( 'REQUEST_METHOD' ), 'DELETE' );
2010-04-26 05:20:05 +00:00
$_SERVER [ 'HTTP_X_HTTP_METHOD_OVERRIDE' ] = 'PUT' ;
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( env ( 'REQUEST_METHOD' ), 'PUT' );
2010-04-26 05:08:33 +00:00
}
/**
2013-06-26 11:36:50 +00:00
* Test the clientIp method .
2010-04-26 05:08:33 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testclientIp () {
2010-04-26 05:08:33 +00:00
$_SERVER [ 'HTTP_X_FORWARDED_FOR' ] = '192.168.1.5, 10.0.1.1, proxy.com' ;
$_SERVER [ 'HTTP_CLIENT_IP' ] = '192.168.1.2' ;
$_SERVER [ 'REMOTE_ADDR' ] = '192.168.1.3' ;
2016-03-11 03:02:34 +00:00
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2016-03-11 03:02:34 +00:00
$this -> assertEquals ( '192.168.1.3' , $request -> clientIp (), 'Use remote_addr in safe mode' );
$this -> assertEquals ( '192.168.1.5' , $request -> clientIp ( false ), 'Use x-forwarded' );
2010-04-26 05:08:33 +00:00
unset ( $_SERVER [ 'HTTP_X_FORWARDED_FOR' ]);
2016-03-11 03:02:34 +00:00
$this -> assertEquals ( '192.168.1.3' , $request -> clientIp (), 'safe uses remote_addr' );
$this -> assertEquals ( '192.168.1.2' , $request -> clientIp ( false ), 'unsafe reads from client_ip' );
2010-04-26 05:08:33 +00:00
unset ( $_SERVER [ 'HTTP_CLIENT_IP' ]);
2016-03-11 03:02:34 +00:00
$this -> assertEquals ( '192.168.1.3' , $request -> clientIp (), 'use remote_addr' );
$this -> assertEquals ( '192.168.1.3' , $request -> clientIp ( false ), 'use remote_addr' );
2010-04-26 05:08:33 +00:00
}
2010-04-27 02:33:56 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test the referrer function .
2010-04-27 02:33:56 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testReferer () {
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2010-05-15 04:53:15 +00:00
$request -> webroot = '/' ;
2010-04-27 02:33:56 +00:00
2017-06-10 21:33:55 +00:00
$_SERVER [ 'HTTP_REFERER' ] = 'https://cakephp.org' ;
2010-04-27 02:33:56 +00:00
$result = $request -> referer ();
2017-06-10 21:33:55 +00:00
$this -> assertSame ( $result , 'https://cakephp.org' );
2010-04-27 02:33:56 +00:00
$_SERVER [ 'HTTP_REFERER' ] = '' ;
$result = $request -> referer ();
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $result , '/' );
2010-04-27 02:33:56 +00:00
2016-03-02 17:30:48 +00:00
$_SERVER [ 'HTTP_REFERER' ] = Configure :: read ( 'App.fullBaseUrl' ) . '/' ;
$result = $request -> referer ( true );
$this -> assertSame ( $result , '/' );
2013-07-31 14:36:57 +00:00
$_SERVER [ 'HTTP_REFERER' ] = Configure :: read ( 'App.fullBaseUrl' ) . '/some/path' ;
2010-04-27 02:33:56 +00:00
$result = $request -> referer ( true );
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $result , '/some/path' );
2010-04-27 02:33:56 +00:00
2013-07-31 14:36:57 +00:00
$_SERVER [ 'HTTP_REFERER' ] = Configure :: read ( 'App.fullBaseUrl' ) . '/some/path' ;
2010-05-15 04:53:15 +00:00
$result = $request -> referer ( false );
2013-07-31 14:36:57 +00:00
$this -> assertSame ( $result , Configure :: read ( 'App.fullBaseUrl' ) . '/some/path' );
2010-04-27 02:33:56 +00:00
2013-07-31 14:36:57 +00:00
$_SERVER [ 'HTTP_REFERER' ] = Configure :: read ( 'App.fullBaseUrl' ) . '/recipes/add' ;
2010-04-27 02:33:56 +00:00
$result = $request -> referer ( true );
2011-11-16 00:07:56 +00:00
$this -> assertSame ( $result , '/recipes/add' );
2010-04-27 02:33:56 +00:00
}
2010-04-27 03:23:35 +00:00
2013-06-30 03:22:33 +00:00
/**
* Test referer () with a base path that duplicates the
* first segment .
*
* @ return void
*/
public function testRefererBasePath () {
$request = new CakeRequest ( 'some/path' );
$request -> url = 'users/login' ;
$request -> webroot = '/waves/' ;
$request -> base = '/waves' ;
$request -> here = '/waves/users/login' ;
$_SERVER [ 'HTTP_REFERER' ] = FULL_BASE_URL . '/waves/waves/add' ;
$result = $request -> referer ( true );
$this -> assertSame ( $result , '/waves/add' );
}
2010-04-27 03:23:35 +00:00
/**
* test the simple uses of is ()
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testIsHttpMethods () {
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2010-04-27 03:23:35 +00:00
$this -> assertFalse ( $request -> is ( 'undefined-behavior' ));
$_SERVER [ 'REQUEST_METHOD' ] = 'GET' ;
$this -> assertTrue ( $request -> is ( 'get' ));
$_SERVER [ 'REQUEST_METHOD' ] = 'POST' ;
$this -> assertTrue ( $request -> is ( 'POST' ));
$_SERVER [ 'REQUEST_METHOD' ] = 'PUT' ;
$this -> assertTrue ( $request -> is ( 'put' ));
$this -> assertFalse ( $request -> is ( 'get' ));
$_SERVER [ 'REQUEST_METHOD' ] = 'DELETE' ;
$this -> assertTrue ( $request -> is ( 'delete' ));
2010-04-27 03:34:30 +00:00
$this -> assertTrue ( $request -> isDelete ());
2010-04-27 03:23:35 +00:00
$_SERVER [ 'REQUEST_METHOD' ] = 'delete' ;
$this -> assertFalse ( $request -> is ( 'delete' ));
}
2014-11-22 16:30:53 +00:00
/**
* Test is () with json and xml .
*
* @ return void
*/
public function testIsJsonAndXml () {
$request = new CakeRequest ( 'some/path' );
$_SERVER [ 'HTTP_ACCEPT' ] = 'application/json, text/plain, */*' ;
$this -> assertTrue ( $request -> is ( array ( 'json' )));
$_SERVER [ 'HTTP_ACCEPT' ] = 'application/xml, text/plain, */*' ;
$this -> assertTrue ( $request -> is ( array ( 'xml' )));
$_SERVER [ 'HTTP_ACCEPT' ] = 'text/xml, */*' ;
$this -> assertTrue ( $request -> is ( array ( 'xml' )));
}
2013-04-02 01:41:39 +00:00
/**
* Test is () with multiple types .
*
* @ return void
*/
public function testIsMultiple () {
$request = new CakeRequest ( 'some/path' );
$_SERVER [ 'REQUEST_METHOD' ] = 'GET' ;
$this -> assertTrue ( $request -> is ( array ( 'get' , 'post' )));
$_SERVER [ 'REQUEST_METHOD' ] = 'POST' ;
$this -> assertTrue ( $request -> is ( array ( 'get' , 'post' )));
$_SERVER [ 'REQUEST_METHOD' ] = 'PUT' ;
$this -> assertFalse ( $request -> is ( array ( 'get' , 'post' )));
}
2013-04-02 01:51:54 +00:00
/**
* Test isAll ()
*
* @ return void
*/
public function testIsAll () {
$request = new CakeRequest ( 'some/path' );
$_SERVER [ 'HTTP_X_REQUESTED_WITH' ] = 'XMLHttpRequest' ;
$_SERVER [ 'REQUEST_METHOD' ] = 'GET' ;
$this -> assertTrue ( $request -> isAll ( array ( 'ajax' , 'get' )));
$this -> assertFalse ( $request -> isAll ( array ( 'post' , 'get' )));
$this -> assertFalse ( $request -> isAll ( array ( 'ajax' , 'post' )));
}
2010-08-22 04:15:13 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test the method () method .
2010-08-22 04:15:13 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testMethod () {
2010-08-22 04:15:13 +00:00
$_SERVER [ 'REQUEST_METHOD' ] = 'delete' ;
$request = new CakeRequest ( 'some/path' );
$this -> assertEquals ( 'delete' , $request -> method ());
}
2010-08-22 04:44:05 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test host retrieval .
2010-08-22 04:44:05 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testHost () {
2010-08-22 04:44:05 +00:00
$_SERVER [ 'HTTP_HOST' ] = 'localhost' ;
2013-12-23 16:31:54 +00:00
$_SERVER [ 'HTTP_X_FORWARDED_HOST' ] = 'cakephp.org' ;
2010-08-22 04:44:05 +00:00
$request = new CakeRequest ( 'some/path' );
2011-10-28 05:01:17 +00:00
2010-08-22 04:44:05 +00:00
$this -> assertEquals ( 'localhost' , $request -> host ());
2013-12-23 16:31:54 +00:00
$this -> assertEquals ( 'cakephp.org' , $request -> host ( true ));
2010-08-22 04:44:05 +00:00
}
/**
2013-06-26 11:36:50 +00:00
* Test domain retrieval .
2010-08-22 04:44:05 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testDomain () {
2010-08-22 04:44:05 +00:00
$_SERVER [ 'HTTP_HOST' ] = 'something.example.com' ;
$request = new CakeRequest ( 'some/path' );
$this -> assertEquals ( 'example.com' , $request -> domain ());
$_SERVER [ 'HTTP_HOST' ] = 'something.example.co.uk' ;
$this -> assertEquals ( 'example.co.uk' , $request -> domain ( 2 ));
}
/**
2013-06-26 11:36:50 +00:00
* Test getting subdomains for a host .
2010-08-22 04:44:05 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testSubdomain () {
2010-08-22 04:44:05 +00:00
$_SERVER [ 'HTTP_HOST' ] = 'something.example.com' ;
$request = new CakeRequest ( 'some/path' );
$this -> assertEquals ( array ( 'something' ), $request -> subdomains ());
$_SERVER [ 'HTTP_HOST' ] = 'www.something.example.com' ;
$this -> assertEquals ( array ( 'www' , 'something' ), $request -> subdomains ());
$_SERVER [ 'HTTP_HOST' ] = 'www.something.example.co.uk' ;
$this -> assertEquals ( array ( 'www' , 'something' ), $request -> subdomains ( 2 ));
2010-08-22 04:54:55 +00:00
$_SERVER [ 'HTTP_HOST' ] = 'example.co.uk' ;
$this -> assertEquals ( array (), $request -> subdomains ( 2 ));
2010-08-22 04:44:05 +00:00
}
2010-04-27 03:23:35 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test ajax , flash and friends
2010-04-27 03:23:35 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testisAjaxFlashAndFriends () {
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2010-04-27 03:23:35 +00:00
$_SERVER [ 'HTTP_USER_AGENT' ] = 'Shockwave Flash' ;
$this -> assertTrue ( $request -> is ( 'flash' ));
$_SERVER [ 'HTTP_USER_AGENT' ] = 'Adobe Flash' ;
$this -> assertTrue ( $request -> is ( 'flash' ));
$_SERVER [ 'HTTP_X_REQUESTED_WITH' ] = 'XMLHttpRequest' ;
$this -> assertTrue ( $request -> is ( 'ajax' ));
$_SERVER [ 'HTTP_X_REQUESTED_WITH' ] = 'XMLHTTPREQUEST' ;
$this -> assertFalse ( $request -> is ( 'ajax' ));
2010-04-27 03:34:30 +00:00
$this -> assertFalse ( $request -> isAjax ());
2010-04-27 03:23:35 +00:00
$_SERVER [ 'HTTP_USER_AGENT' ] = 'Android 2.0' ;
$this -> assertTrue ( $request -> is ( 'mobile' ));
2010-04-27 03:34:30 +00:00
$this -> assertTrue ( $request -> isMobile ());
2011-10-28 05:01:17 +00:00
2010-10-28 02:32:15 +00:00
$_SERVER [ 'HTTP_USER_AGENT' ] = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b6pre) Gecko/20100902 Firefox/4.0b6pre Fennec/2.0b1pre' ;
$this -> assertTrue ( $request -> is ( 'mobile' ));
$this -> assertTrue ( $request -> isMobile ());
2012-01-01 17:57:35 +00:00
$_SERVER [ 'HTTP_USER_AGENT' ] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; OMNIA7)' ;
$this -> assertTrue ( $request -> is ( 'mobile' ));
$this -> assertTrue ( $request -> isMobile ());
2010-10-28 02:32:15 +00:00
}
2010-05-04 03:41:13 +00:00
2010-10-28 02:32:15 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test __call exceptions
2010-10-28 02:32:15 +00:00
*
2010-12-12 00:01:07 +00:00
* @ expectedException CakeException
2010-10-28 02:32:15 +00:00
* @ return void
*/
2012-03-17 02:19:01 +00:00
public function testMagicCallExceptionOnUnknownMethod () {
2010-10-28 02:32:15 +00:00
$request = new CakeRequest ( 'some/path' );
2010-05-04 03:41:13 +00:00
$request -> IamABanana ();
2010-04-27 03:23:35 +00:00
}
/**
2013-06-26 11:36:50 +00:00
* Test is ( ssl )
2010-04-27 03:23:35 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testIsSsl () {
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2010-04-27 03:23:35 +00:00
$_SERVER [ 'HTTPS' ] = 1 ;
$this -> assertTrue ( $request -> is ( 'ssl' ));
2010-04-30 03:08:01 +00:00
2010-04-27 03:23:35 +00:00
$_SERVER [ 'HTTPS' ] = 'on' ;
$this -> assertTrue ( $request -> is ( 'ssl' ));
$_SERVER [ 'HTTPS' ] = '1' ;
$this -> assertTrue ( $request -> is ( 'ssl' ));
$_SERVER [ 'HTTPS' ] = 'I am not empty' ;
$this -> assertTrue ( $request -> is ( 'ssl' ));
$_SERVER [ 'HTTPS' ] = 1 ;
$this -> assertTrue ( $request -> is ( 'ssl' ));
$_SERVER [ 'HTTPS' ] = 'off' ;
$this -> assertFalse ( $request -> is ( 'ssl' ));
$_SERVER [ 'HTTPS' ] = false ;
$this -> assertFalse ( $request -> is ( 'ssl' ));
$_SERVER [ 'HTTPS' ] = '' ;
$this -> assertFalse ( $request -> is ( 'ssl' ));
}
2010-04-27 03:37:36 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test getting request params with object properties .
2010-04-27 03:37:36 +00:00
*
* @ return void
*/
2012-03-17 02:19:01 +00:00
public function testMagicget () {
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2010-04-27 03:37:36 +00:00
$request -> params = array ( 'controller' => 'posts' , 'action' => 'view' , 'plugin' => 'blogs' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'posts' , $request -> controller );
$this -> assertEquals ( 'view' , $request -> action );
$this -> assertEquals ( 'blogs' , $request -> plugin );
2013-09-18 22:17:21 +00:00
$this -> assertNull ( $request -> banana );
2010-04-27 03:37:36 +00:00
}
2010-04-27 03:46:47 +00:00
2011-11-17 01:20:50 +00:00
/**
* Test isset () / empty () with overloaded properties .
*
* @ return void
*/
2012-03-17 02:19:01 +00:00
public function testMagicisset () {
2011-11-17 01:20:50 +00:00
$request = new CakeRequest ( 'some/path' );
$request -> params = array (
'controller' => 'posts' ,
'action' => 'view' ,
'plugin' => 'blogs' ,
'named' => array ()
);
$this -> assertTrue ( isset ( $request -> controller ));
$this -> assertFalse ( isset ( $request -> notthere ));
$this -> assertFalse ( empty ( $request -> controller ));
$this -> assertTrue ( empty ( $request -> named ));
2010-04-27 03:37:36 +00:00
}
2010-04-27 03:46:47 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test the array access implementation
2010-04-27 03:46:47 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testArrayAccess () {
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2010-04-27 03:46:47 +00:00
$request -> params = array ( 'controller' => 'posts' , 'action' => 'view' , 'plugin' => 'blogs' );
2010-04-30 03:08:01 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'posts' , $request [ 'controller' ]);
2010-04-30 03:08:01 +00:00
2010-04-27 03:46:47 +00:00
$request [ 'slug' ] = 'speedy-slug' ;
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'speedy-slug' , $request -> slug );
$this -> assertEquals ( 'speedy-slug' , $request [ 'slug' ]);
2010-04-27 03:46:47 +00:00
$this -> assertTrue ( isset ( $request [ 'action' ]));
$this -> assertFalse ( isset ( $request [ 'wrong-param' ]));
$this -> assertTrue ( isset ( $request [ 'plugin' ]));
unset ( $request [ 'plugin' ]);
$this -> assertFalse ( isset ( $request [ 'plugin' ]));
$this -> assertNull ( $request [ 'plugin' ]);
$this -> assertNull ( $request -> plugin );
2010-05-01 15:04:41 +00:00
$request = new CakeRequest ( 'some/path?one=something&two=else' );
$this -> assertTrue ( isset ( $request [ 'url' ][ 'one' ]));
2010-05-02 03:46:10 +00:00
$request -> data = array ( 'Post' => array ( 'title' => 'something' ));
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'something' , $request [ 'data' ][ 'Post' ][ 'title' ]);
2010-04-27 03:46:47 +00:00
}
2010-04-30 03:08:01 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test adding detectors and having them work .
2010-04-30 03:08:01 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAddDetector () {
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ( 'some/path' );
2010-04-30 03:08:01 +00:00
$request -> addDetector ( 'compare' , array ( 'env' => 'TEST_VAR' , 'value' => 'something' ));
$_SERVER [ 'TEST_VAR' ] = 'something' ;
2010-08-30 03:28:54 +00:00
$this -> assertTrue ( $request -> is ( 'compare' ), 'Value match failed.' );
2010-04-30 03:08:01 +00:00
$_SERVER [ 'TEST_VAR' ] = 'wrong' ;
2010-08-30 03:28:54 +00:00
$this -> assertFalse ( $request -> is ( 'compare' ), 'Value mis-match failed.' );
2012-03-17 02:19:01 +00:00
2012-02-28 16:05:24 +00:00
$request -> addDetector ( 'compareCamelCase' , array ( 'env' => 'TEST_VAR' , 'value' => 'foo' ));
$_SERVER [ 'TEST_VAR' ] = 'foo' ;
$this -> assertTrue ( $request -> is ( 'compareCamelCase' ), 'Value match failed.' );
2012-08-20 15:41:10 +00:00
$this -> assertTrue ( $request -> is ( 'comparecamelcase' ), 'detectors should be case insensitive' );
$this -> assertTrue ( $request -> is ( 'COMPARECAMELCASE' ), 'detectors should be case insensitive' );
$_SERVER [ 'TEST_VAR' ] = 'not foo' ;
$this -> assertFalse ( $request -> is ( 'compareCamelCase' ), 'Value match failed.' );
$this -> assertFalse ( $request -> is ( 'comparecamelcase' ), 'detectors should be case insensitive' );
$this -> assertFalse ( $request -> is ( 'COMPARECAMELCASE' ), 'detectors should be case insensitive' );
2010-04-30 03:08:01 +00:00
$request -> addDetector ( 'banana' , array ( 'env' => 'TEST_VAR' , 'pattern' => '/^ban.*$/' ));
$_SERVER [ 'TEST_VAR' ] = 'banana' ;
$this -> assertTrue ( $request -> isBanana ());
$_SERVER [ 'TEST_VAR' ] = 'wrong value' ;
$this -> assertFalse ( $request -> isBanana ());
$request -> addDetector ( 'mobile' , array ( 'options' => array ( 'Imagination' )));
$_SERVER [ 'HTTP_USER_AGENT' ] = 'Imagination land' ;
$this -> assertTrue ( $request -> isMobile ());
$_SERVER [ 'HTTP_USER_AGENT' ] = 'iPhone 3.0' ;
$this -> assertTrue ( $request -> isMobile ());
2010-04-30 03:11:15 +00:00
2012-02-23 08:03:31 +00:00
$request -> addDetector ( 'callme' , array ( 'env' => 'TEST_VAR' , 'callback' => array ( $this , 'detectCallback' )));
2010-04-30 03:11:15 +00:00
2011-10-21 01:11:42 +00:00
$request -> addDetector ( 'index' , array ( 'param' => 'action' , 'value' => 'index' ));
$request -> params [ 'action' ] = 'index' ;
$this -> assertTrue ( $request -> isIndex ());
$request -> params [ 'action' ] = 'add' ;
$this -> assertFalse ( $request -> isIndex ());
2010-04-30 03:11:15 +00:00
$request -> return = true ;
$this -> assertTrue ( $request -> isCallMe ());
$request -> return = false ;
$this -> assertFalse ( $request -> isCallMe ());
2013-08-27 08:20:07 +00:00
$request -> addDetector ( 'extension' , array ( 'param' => 'ext' , 'options' => array ( 'pdf' , 'png' , 'txt' )));
$request -> params [ 'ext' ] = 'pdf' ;
$this -> assertTrue ( $request -> is ( 'extension' ));
$request -> params [ 'ext' ] = 'exe' ;
$this -> assertFalse ( $request -> isExtension ());
2010-04-30 03:11:15 +00:00
}
/**
2013-06-26 11:36:50 +00:00
* Helper function for testing callbacks .
2010-04-30 03:11:15 +00:00
*
2013-06-26 11:36:50 +00:00
* @ param $request
2014-07-03 13:36:42 +00:00
* @ return bool
2010-04-30 03:11:15 +00:00
*/
2012-02-23 08:03:31 +00:00
public function detectCallback ( $request ) {
2012-09-14 17:54:29 +00:00
return ( bool ) $request -> return ;
2010-04-30 03:08:01 +00:00
}
2010-04-30 04:13:24 +00:00
2010-06-29 04:24:29 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test getting headers
2010-06-29 04:24:29 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testHeader () {
2015-04-08 20:33:28 +00:00
$_SERVER [ 'HTTP_X_THING' ] = '' ;
2010-09-20 01:40:35 +00:00
$_SERVER [ 'HTTP_HOST' ] = 'localhost' ;
$_SERVER [ 'HTTP_USER_AGENT' ] = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_4; en-ca) AppleWebKit/534.8+ (KHTML, like Gecko) Version/5.0 Safari/533.16' ;
2016-08-12 01:53:46 +00:00
$_SERVER [ 'Authorization' ] = 'foobar' ;
2010-06-29 04:24:29 +00:00
$request = new CakeRequest ( '/' , false );
$this -> assertEquals ( $_SERVER [ 'HTTP_HOST' ], $request -> header ( 'host' ));
$this -> assertEquals ( $_SERVER [ 'HTTP_USER_AGENT' ], $request -> header ( 'User-Agent' ));
2015-04-08 20:33:28 +00:00
$this -> assertSame ( '' , $request -> header ( 'X-thing' ));
2016-08-12 01:53:46 +00:00
$this -> assertEquals ( $_SERVER [ 'Authorization' ], $request -> header ( 'Authorization' ));
$this -> assertFalse ( $request -> header ( 'authorization' ));
2010-06-29 04:24:29 +00:00
}
2010-07-01 03:25:45 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test accepts () with and without parameters
2010-07-01 03:25:45 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAccepts () {
2010-07-01 03:25:45 +00:00
$_SERVER [ 'HTTP_ACCEPT' ] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png' ;
$request = new CakeRequest ( '/' , false );
2011-10-28 05:01:17 +00:00
2010-07-01 03:25:45 +00:00
$result = $request -> accepts ();
$expected = array (
2011-08-31 23:15:27 +00:00
'text/xml' , 'application/xhtml+xml' , 'text/html' , 'text/plain' , 'image/png' , 'application/xml'
2010-07-01 03:25:45 +00:00
);
$this -> assertEquals ( $expected , $result , 'Content types differ.' );
2011-08-26 02:28:02 +00:00
2010-07-01 03:25:45 +00:00
$result = $request -> accepts ( 'text/html' );
$this -> assertTrue ( $result );
2011-08-26 02:28:02 +00:00
2010-07-01 03:25:45 +00:00
$result = $request -> accepts ( 'image/gif' );
$this -> assertFalse ( $result );
}
2011-08-26 02:28:02 +00:00
/**
* Test that accept header types are trimmed for comparisons .
*
* @ return void
*/
public function testAcceptWithWhitespace () {
$_SERVER [ 'HTTP_ACCEPT' ] = 'text/xml , text/html , text/plain,image/png' ;
$request = new CakeRequest ( '/' , false );
$result = $request -> accepts ();
$expected = array (
'text/xml' , 'text/html' , 'text/plain' , 'image/png'
);
$this -> assertEquals ( $expected , $result , 'Content types differ.' );
$this -> assertTrue ( $request -> accepts ( 'text/html' ));
}
2011-08-27 15:39:22 +00:00
/**
* Content types from accepts () should respect the client ' s q preference values .
*
* @ return void
*/
public function testAcceptWithQvalueSorting () {
$_SERVER [ 'HTTP_ACCEPT' ] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0' ;
$request = new CakeRequest ( '/' , false );
$result = $request -> accepts ();
$expected = array ( 'application/xml' , 'text/html' , 'application/json' );
$this -> assertEquals ( $expected , $result );
}
2011-08-31 23:15:27 +00:00
/**
* Test the raw parsing of accept headers into the q value formatting .
*
* @ return void
*/
public function testParseAcceptWithQValue () {
$_SERVER [ 'HTTP_ACCEPT' ] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png' ;
$request = new CakeRequest ( '/' , false );
$result = $request -> parseAccept ();
$expected = array (
'1.0' => array ( 'application/xml' , 'image/png' ),
'0.8' => array ( 'text/html' ),
'0.7' => array ( 'application/json' ),
);
$this -> assertEquals ( $expected , $result );
}
2012-04-29 18:48:45 +00:00
/**
* Test parsing accept with a confusing accept value .
*
* @ return void
*/
public function testParseAcceptNoQValues () {
$_SERVER [ 'HTTP_ACCEPT' ] = 'application/json, text/plain, */*' ;
$request = new CakeRequest ( '/' , false );
$result = $request -> parseAccept ();
$expected = array (
'1.0' => array ( 'application/json' , 'text/plain' , '*/*' ),
);
$this -> assertEquals ( $expected , $result );
}
2013-10-02 15:28:55 +00:00
/**
* Test parsing accept ignores index param
*
* @ return void
*/
public function testParseAcceptIgnoreAcceptExtensions () {
$_SERVER [ 'HTTP_ACCEPT' ] = 'application/json;level=1, text/plain, */*' ;
$request = new CakeRequest ( '/' , false );
$result = $request -> parseAccept ();
$expected = array (
'1.0' => array ( 'application/json' , 'text/plain' , '*/*' ),
);
$this -> assertEquals ( $expected , $result );
}
/**
* Test that parsing accept headers with invalid syntax works .
*
* The header used is missing a q value for application / xml .
*
* @ return void
*/
public function testParseAcceptInvalidSyntax () {
$_SERVER [ 'HTTP_ACCEPT' ] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8' ;
$request = new CakeRequest ( '/' , false );
$result = $request -> parseAccept ();
$expected = array (
'1.0' => array ( 'text/html' , 'application/xhtml+xml' , 'application/xml' , 'image/jpeg' ),
'0.9' => array ( 'image/*' ),
'0.8' => array ( '*/*' ),
);
$this -> assertEquals ( $expected , $result );
}
2010-04-30 04:13:24 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test baseUrl and webroot with ModRewrite
2010-04-30 04:13:24 +00:00
*
* @ return void
*/
public function testBaseUrlAndWebrootWithModRewrite () {
2010-05-01 04:18:17 +00:00
Configure :: write ( 'App.baseUrl' , false );
2013-05-10 15:04:46 +00:00
$_SERVER [ 'DOCUMENT_ROOT' ] = '/cake/repo/branches' ;
$_SERVER [ 'PHP_SELF' ] = '/urlencode me/app/webroot/index.php' ;
$_SERVER [ 'PATH_INFO' ] = '/posts/view/1' ;
$request = new CakeRequest ();
$this -> assertEquals ( '/urlencode%20me' , $request -> base );
$this -> assertEquals ( '/urlencode%20me/' , $request -> webroot );
$this -> assertEquals ( 'posts/view/1' , $request -> url );
2010-04-30 04:13:24 +00:00
$_SERVER [ 'DOCUMENT_ROOT' ] = '/cake/repo/branches' ;
2012-01-05 02:10:29 +00:00
$_SERVER [ 'PHP_SELF' ] = '/1.2.x.x/app/webroot/index.php' ;
2011-02-20 03:20:55 +00:00
$_SERVER [ 'PATH_INFO' ] = '/posts/view/1' ;
2010-04-30 04:13:24 +00:00
2010-05-01 03:37:16 +00:00
$request = new CakeRequest ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/1.2.x.x' , $request -> base );
$this -> assertEquals ( '/1.2.x.x/' , $request -> webroot );
$this -> assertEquals ( 'posts/view/1' , $request -> url );
2010-05-01 04:18:17 +00:00
2010-04-30 04:13:24 +00:00
$_SERVER [ 'DOCUMENT_ROOT' ] = '/cake/repo/branches/1.2.x.x/app/webroot' ;
2012-01-05 02:10:29 +00:00
$_SERVER [ 'PHP_SELF' ] = '/index.php' ;
2011-02-20 03:20:55 +00:00
$_SERVER [ 'PATH_INFO' ] = '/posts/add' ;
2010-05-01 03:37:16 +00:00
$request = new CakeRequest ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '' , $request -> base );
$this -> assertEquals ( '/' , $request -> webroot );
$this -> assertEquals ( 'posts/add' , $request -> url );
2010-04-30 04:13:24 +00:00
$_SERVER [ 'DOCUMENT_ROOT' ] = '/cake/repo/branches/1.2.x.x/test/' ;
2012-01-05 02:10:29 +00:00
$_SERVER [ 'PHP_SELF' ] = '/webroot/index.php' ;
2010-05-01 03:37:16 +00:00
$request = new CakeRequest ();
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( '' , $request -> base );
$this -> assertEquals ( '/' , $request -> webroot );
2010-05-01 03:37:16 +00:00
2010-04-30 04:13:24 +00:00
$_SERVER [ 'DOCUMENT_ROOT' ] = '/some/apps/where' ;
2012-01-05 02:10:29 +00:00
$_SERVER [ 'PHP_SELF' ] = '/app/webroot/index.php' ;
2010-05-01 03:37:16 +00:00
$request = new CakeRequest ();
2010-04-30 04:13:24 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '' , $request -> base );
$this -> assertEquals ( '/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
Configure :: write ( 'App.dir' , 'auth' );
$_SERVER [ 'DOCUMENT_ROOT' ] = '/cake/repo/branches' ;
2012-01-05 02:10:29 +00:00
$_SERVER [ 'PHP_SELF' ] = '/demos/auth/webroot/index.php' ;
2010-04-30 04:13:24 +00:00
2010-05-01 03:37:16 +00:00
$request = new CakeRequest ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/demos/auth' , $request -> base );
$this -> assertEquals ( '/demos/auth/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
Configure :: write ( 'App.dir' , 'code' );
$_SERVER [ 'DOCUMENT_ROOT' ] = '/Library/WebServer/Documents' ;
2012-01-05 02:10:29 +00:00
$_SERVER [ 'PHP_SELF' ] = '/clients/PewterReport/code/webroot/index.php' ;
2010-05-01 03:37:16 +00:00
$request = new CakeRequest ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/clients/PewterReport/code' , $request -> base );
$this -> assertEquals ( '/clients/PewterReport/code/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
}
/**
2013-06-26 11:36:50 +00:00
* Test baseUrl with ModRewrite alias
2010-04-30 04:13:24 +00:00
*
* @ return void
*/
public function testBaseUrlwithModRewriteAlias () {
$_SERVER [ 'DOCUMENT_ROOT' ] = '/home/aplusnur/public_html' ;
2012-01-05 02:10:29 +00:00
$_SERVER [ 'PHP_SELF' ] = '/control/index.php' ;
2010-04-30 04:13:24 +00:00
Configure :: write ( 'App.base' , '/control' );
$request = new CakeRequest ();
2010-05-01 03:37:16 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/control' , $request -> base );
$this -> assertEquals ( '/control/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
Configure :: write ( 'App.base' , false );
Configure :: write ( 'App.dir' , 'affiliate' );
Configure :: write ( 'App.webroot' , 'newaffiliate' );
$_SERVER [ 'DOCUMENT_ROOT' ] = '/var/www/abtravaff/html' ;
2012-01-05 02:10:29 +00:00
$_SERVER [ 'PHP_SELF' ] = '/newaffiliate/index.php' ;
2010-04-30 04:13:24 +00:00
$request = new CakeRequest ();
2010-05-01 03:37:16 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/newaffiliate' , $request -> base );
$this -> assertEquals ( '/newaffiliate/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
}
2010-05-01 04:18:17 +00:00
/**
2013-10-08 03:17:58 +00:00
* Test base , webroot , URL and here parsing when there is URL rewriting but
* CakePHP gets called with index . php in URL nonetheless .
2013-06-26 11:36:50 +00:00
*
* Tests uri with
* - index . php /
* - index . php /
* - index . php / apples /
* - index . php / bananas / eat / tasty_banana
*
2014-04-02 01:02:37 +00:00
* @ return void
2013-06-26 11:36:50 +00:00
*/
public function testBaseUrlWithModRewriteAndIndexPhp () {
$_SERVER [ 'REQUEST_URI' ] = '/cakephp/app/webroot/index.php' ;
2013-07-02 02:56:00 +00:00
$_SERVER [ 'PHP_SELF' ] = '/cakephp/app/webroot/index.php' ;
2013-06-26 11:36:50 +00:00
unset ( $_SERVER [ 'PATH_INFO' ]);
$request = new CakeRequest ();
$this -> assertEquals ( '/cakephp' , $request -> base );
$this -> assertEquals ( '/cakephp/' , $request -> webroot );
$this -> assertEquals ( '' , $request -> url );
$this -> assertEquals ( '/cakephp/' , $request -> here );
$_SERVER [ 'REQUEST_URI' ] = '/cakephp/app/webroot/index.php/' ;
2013-07-02 02:56:00 +00:00
$_SERVER [ 'PHP_SELF' ] = '/cakephp/app/webroot/index.php/' ;
$_SERVER [ 'PATH_INFO' ] = '/' ;
2013-06-26 11:36:50 +00:00
$request = new CakeRequest ();
$this -> assertEquals ( '/cakephp' , $request -> base );
$this -> assertEquals ( '/cakephp/' , $request -> webroot );
$this -> assertEquals ( '' , $request -> url );
$this -> assertEquals ( '/cakephp/' , $request -> here );
$_SERVER [ 'REQUEST_URI' ] = '/cakephp/app/webroot/index.php/apples' ;
2013-07-02 02:56:00 +00:00
$_SERVER [ 'PHP_SELF' ] = '/cakephp/app/webroot/index.php/apples' ;
$_SERVER [ 'PATH_INFO' ] = '/apples' ;
2013-06-26 11:36:50 +00:00
$request = new CakeRequest ();
$this -> assertEquals ( '/cakephp' , $request -> base );
$this -> assertEquals ( '/cakephp/' , $request -> webroot );
$this -> assertEquals ( 'apples' , $request -> url );
$this -> assertEquals ( '/cakephp/apples' , $request -> here );
$_SERVER [ 'REQUEST_URI' ] = '/cakephp/app/webroot/index.php/melons/share/' ;
2013-07-02 02:56:00 +00:00
$_SERVER [ 'PHP_SELF' ] = '/cakephp/app/webroot/index.php/melons/share/' ;
$_SERVER [ 'PATH_INFO' ] = '/melons/share/' ;
2013-06-26 11:36:50 +00:00
$request = new CakeRequest ();
$this -> assertEquals ( '/cakephp' , $request -> base );
$this -> assertEquals ( '/cakephp/' , $request -> webroot );
$this -> assertEquals ( 'melons/share/' , $request -> url );
$this -> assertEquals ( '/cakephp/melons/share/' , $request -> here );
$_SERVER [ 'REQUEST_URI' ] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana' ;
2013-07-02 02:56:00 +00:00
$_SERVER [ 'PHP_SELF' ] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana' ;
$_SERVER [ 'PATH_INFO' ] = '/bananas/eat/tasty_banana' ;
2013-06-26 11:36:50 +00:00
$request = new CakeRequest ();
$this -> assertEquals ( '/cakephp' , $request -> base );
$this -> assertEquals ( '/cakephp/' , $request -> webroot );
$this -> assertEquals ( 'bananas/eat/tasty_banana' , $request -> url );
$this -> assertEquals ( '/cakephp/bananas/eat/tasty_banana' , $request -> here );
}
2015-06-15 01:56:48 +00:00
/**
* Test that even if mod_rewrite is on , and the url contains index . php
* and there are numerous //s that the base/webroot is calculated correctly.
*
* @ return void
*/
2015-06-07 19:45:16 +00:00
public function testBaseUrlWithModRewriteAndExtraSlashes () {
$_SERVER [ 'REQUEST_URI' ] = '/cakephp/webroot///index.php/bananas/eat' ;
$_SERVER [ 'PHP_SELF' ] = '/cakephp/webroot///index.php/bananas/eat' ;
$_SERVER [ 'PATH_INFO' ] = '/bananas/eat' ;
$request = new CakeRequest ();
$this -> assertEquals ( '/cakephp' , $request -> base );
$this -> assertEquals ( '/cakephp/' , $request -> webroot );
$this -> assertEquals ( 'bananas/eat' , $request -> url );
$this -> assertEquals ( '/cakephp/bananas/eat' , $request -> here );
}
2013-06-26 11:36:50 +00:00
/**
2013-10-08 03:17:58 +00:00
* Test base , webroot , and URL parsing when there is no URL rewriting
2010-05-01 04:18:17 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testBaseUrlWithNoModRewrite () {
2010-05-01 04:18:17 +00:00
$_SERVER [ 'DOCUMENT_ROOT' ] = '/Users/markstory/Sites' ;
$_SERVER [ 'SCRIPT_FILENAME' ] = '/Users/markstory/Sites/cake/index.php' ;
$_SERVER [ 'PHP_SELF' ] = '/cake/index.php/posts/index' ;
$_SERVER [ 'REQUEST_URI' ] = '/cake/index.php/posts/index' ;
Configure :: write ( 'App' , array (
'dir' => APP_DIR ,
'webroot' => WEBROOT_DIR ,
'base' => false ,
'baseUrl' => '/cake/index.php'
));
$request = new CakeRequest ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/cake/index.php' , $request -> base );
$this -> assertEquals ( '/cake/app/webroot/' , $request -> webroot );
$this -> assertEquals ( 'posts/index' , $request -> url );
2010-05-01 04:18:17 +00:00
}
2010-04-30 04:13:24 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test baseUrl and webroot with baseUrl
2010-04-30 04:13:24 +00:00
*
* @ return void
*/
public function testBaseUrlAndWebrootWithBaseUrl () {
Configure :: write ( 'App.dir' , 'app' );
Configure :: write ( 'App.baseUrl' , '/app/webroot/index.php' );
$request = new CakeRequest ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/app/webroot/index.php' , $request -> base );
$this -> assertEquals ( '/app/webroot/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
Configure :: write ( 'App.baseUrl' , '/app/webroot/test.php' );
$request = new CakeRequest ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/app/webroot/test.php' , $request -> base );
$this -> assertEquals ( '/app/webroot/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
Configure :: write ( 'App.baseUrl' , '/app/index.php' );
$request = new CakeRequest ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/app/index.php' , $request -> base );
$this -> assertEquals ( '/app/webroot/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
Configure :: write ( 'App.baseUrl' , '/CakeBB/app/webroot/index.php' );
$request = new CakeRequest ();
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/CakeBB/app/webroot/index.php' , $request -> base );
$this -> assertEquals ( '/CakeBB/app/webroot/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
Configure :: write ( 'App.baseUrl' , '/CakeBB/app/index.php' );
$request = new CakeRequest ();
2010-05-01 03:37:16 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/CakeBB/app/index.php' , $request -> base );
$this -> assertEquals ( '/CakeBB/app/webroot/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
Configure :: write ( 'App.baseUrl' , '/CakeBB/index.php' );
$request = new CakeRequest ();
2010-05-01 03:37:16 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/CakeBB/index.php' , $request -> base );
$this -> assertEquals ( '/CakeBB/app/webroot/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
Configure :: write ( 'App.baseUrl' , '/dbhauser/index.php' );
$_SERVER [ 'DOCUMENT_ROOT' ] = '/kunden/homepages/4/d181710652/htdocs/joomla' ;
$_SERVER [ 'SCRIPT_FILENAME' ] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php' ;
$request = new CakeRequest ();
2010-05-01 03:37:16 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( '/dbhauser/index.php' , $request -> base );
$this -> assertEquals ( '/dbhauser/app/webroot/' , $request -> webroot );
2010-04-30 04:13:24 +00:00
}
2011-02-05 17:20:09 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test baseUrl with no rewrite and using the top level index . php .
2011-02-05 17:20:09 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testBaseUrlNoRewriteTopLevelIndex () {
2011-02-05 17:20:09 +00:00
Configure :: write ( 'App.baseUrl' , '/index.php' );
$_SERVER [ 'DOCUMENT_ROOT' ] = '/Users/markstory/Sites/cake_dev' ;
$_SERVER [ 'SCRIPT_FILENAME' ] = '/Users/markstory/Sites/cake_dev/index.php' ;
$request = new CakeRequest ();
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( '/index.php' , $request -> base );
$this -> assertEquals ( '/app/webroot/' , $request -> webroot );
2011-02-05 17:20:09 +00:00
}
2011-12-03 04:39:33 +00:00
/**
* Check that a sub - directory containing app | webroot doesn ' t get mishandled when re - writing is off .
*
* @ return void
*/
public function testBaseUrlWithAppAndWebrootInDirname () {
Configure :: write ( 'App.baseUrl' , '/approval/index.php' );
$_SERVER [ 'DOCUMENT_ROOT' ] = '/Users/markstory/Sites/' ;
$_SERVER [ 'SCRIPT_FILENAME' ] = '/Users/markstory/Sites/approval/index.php' ;
$request = new CakeRequest ();
$this -> assertEquals ( '/approval/index.php' , $request -> base );
$this -> assertEquals ( '/approval/app/webroot/' , $request -> webroot );
Configure :: write ( 'App.baseUrl' , '/webrootable/index.php' );
$_SERVER [ 'DOCUMENT_ROOT' ] = '/Users/markstory/Sites/' ;
$_SERVER [ 'SCRIPT_FILENAME' ] = '/Users/markstory/Sites/webrootable/index.php' ;
$request = new CakeRequest ();
$this -> assertEquals ( '/webrootable/index.php' , $request -> base );
$this -> assertEquals ( '/webrootable/app/webroot/' , $request -> webroot );
}
2011-02-05 17:20:09 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test baseUrl with no rewrite , and using the app / webroot / index . php file as is normal with virtual hosts .
2011-02-05 17:20:09 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testBaseUrlNoRewriteWebrootIndex () {
2011-02-05 17:20:09 +00:00
Configure :: write ( 'App.baseUrl' , '/index.php' );
$_SERVER [ 'DOCUMENT_ROOT' ] = '/Users/markstory/Sites/cake_dev/app/webroot' ;
$_SERVER [ 'SCRIPT_FILENAME' ] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php' ;
$request = new CakeRequest ();
2011-11-16 00:07:56 +00:00
$this -> assertEquals ( '/index.php' , $request -> base );
$this -> assertEquals ( '/' , $request -> webroot );
2011-02-05 17:20:09 +00:00
}
2011-11-27 02:24:53 +00:00
/**
* Test that a request with a . in the main GET parameter is filtered out .
* PHP changes GET parameter keys containing dots to _ .
*
* @ return void
*/
public function testGetParamsWithDot () {
2012-02-23 07:47:25 +00:00
$_GET = array ();
2011-11-27 02:24:53 +00:00
$_GET [ '/posts/index/add_add' ] = '' ;
2014-01-21 19:56:37 +00:00
$_SERVER [ 'PHP_SELF' ] = '/app/webroot/index.php' ;
$_SERVER [ 'REQUEST_URI' ] = '/posts/index/add.add' ;
$request = new CakeRequest ();
$this -> assertEquals ( '' , $request -> base );
$this -> assertEquals ( array (), $request -> query );
$_GET = array ();
$_GET [ '/cake_dev/posts/index/add_add' ] = '' ;
2012-01-05 02:10:29 +00:00
$_SERVER [ 'PHP_SELF' ] = '/cake_dev/app/webroot/index.php' ;
2011-11-27 02:24:53 +00:00
$_SERVER [ 'REQUEST_URI' ] = '/cake_dev/posts/index/add.add' ;
$request = new CakeRequest ();
2014-01-21 19:56:37 +00:00
$this -> assertEquals ( '/cake_dev' , $request -> base );
2011-11-27 02:24:53 +00:00
$this -> assertEquals ( array (), $request -> query );
}
2012-02-18 15:53:47 +00:00
/**
* Test that a request with urlencoded bits in the main GET parameter are filtered out .
*
* @ return void
*/
public function testGetParamWithUrlencodedElement () {
2012-02-23 07:47:25 +00:00
$_GET = array ();
2012-02-18 15:53:47 +00:00
$_GET [ '/posts/add/∂∂' ] = '' ;
2014-01-21 19:56:37 +00:00
$_SERVER [ 'PHP_SELF' ] = '/app/webroot/index.php' ;
$_SERVER [ 'REQUEST_URI' ] = '/posts/add/%E2%88%82%E2%88%82' ;
$request = new CakeRequest ();
$this -> assertEquals ( '' , $request -> base );
$this -> assertEquals ( array (), $request -> query );
$_GET = array ();
$_GET [ '/cake_dev/posts/add/∂∂' ] = '' ;
2012-02-18 15:53:47 +00:00
$_SERVER [ 'PHP_SELF' ] = '/cake_dev/app/webroot/index.php' ;
2012-02-18 16:18:03 +00:00
$_SERVER [ 'REQUEST_URI' ] = '/cake_dev/posts/add/%E2%88%82%E2%88%82' ;
2012-02-18 15:53:47 +00:00
$request = new CakeRequest ();
2014-01-21 19:56:37 +00:00
$this -> assertEquals ( '/cake_dev' , $request -> base );
2011-11-27 02:24:53 +00:00
$this -> assertEquals ( array (), $request -> query );
}
2010-05-01 05:06:09 +00:00
/**
2013-06-26 11:36:50 +00:00
* Generator for environment configurations
2010-05-01 05:06:09 +00:00
*
2013-06-26 11:36:50 +00:00
* @ return array Environment array
2010-05-01 05:06:09 +00:00
*/
2011-02-19 17:36:27 +00:00
public static function environmentGenerator () {
return array (
array (
'IIS - No rewrite base path' ,
2012-11-21 14:39:03 +00:00
array (
2010-05-01 05:06:09 +00:00
'App' => array (
2011-10-28 05:01:17 +00:00
'base' => false ,
2011-02-19 23:20:54 +00:00
'baseUrl' => '/index.php' ,
2011-02-05 21:12:50 +00:00
'dir' => 'app' ,
'webroot' => 'webroot'
2010-05-01 05:06:09 +00:00
),
'SERVER' => array (
'SCRIPT_NAME' => '/index.php' ,
'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot' ,
'QUERY_STRING' => '' ,
2011-02-05 21:12:50 +00:00
'REQUEST_URI' => '/index.php' ,
'URL' => '/index.php' ,
'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php' ,
'ORIG_PATH_INFO' => '/index.php' ,
'PATH_INFO' => '' ,
'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php' ,
'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot' ,
'PHP_SELF' => '/index.php' ,
2010-05-01 05:06:09 +00:00
),
2011-02-19 17:36:27 +00:00
),
array (
2011-02-19 23:20:54 +00:00
'base' => '/index.php' ,
2011-02-05 21:12:50 +00:00
'webroot' => '/app/webroot/' ,
2010-05-01 05:06:09 +00:00
'url' => ''
),
2011-02-19 17:36:27 +00:00
),
array (
2011-02-19 23:20:54 +00:00
'IIS - No rewrite with path, no PHP_SELF' ,
2011-02-19 17:36:27 +00:00
array (
'App' => array (
2011-10-28 05:01:17 +00:00
'base' => false ,
2011-02-19 17:36:27 +00:00
'baseUrl' => '/index.php?' ,
'dir' => 'app' ,
'webroot' => 'webroot'
),
2010-05-01 05:06:09 +00:00
'SERVER' => array (
'QUERY_STRING' => '/posts/add' ,
'REQUEST_URI' => '/index.php?/posts/add' ,
2011-02-19 23:20:54 +00:00
'PHP_SELF' => '' ,
2010-05-01 05:06:09 +00:00
'URL' => '/index.php?/posts/add' ,
2011-05-09 04:58:01 +00:00
'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot' ,
2010-05-01 05:06:09 +00:00
'argv' => array ( '/posts/add' ),
'argc' => 1
),
),
2011-02-19 17:36:27 +00:00
array (
2010-05-01 05:06:09 +00:00
'url' => 'posts/add' ,
'base' => '/index.php?' ,
2011-02-05 21:12:50 +00:00
'webroot' => '/app/webroot/'
2011-02-19 17:36:27 +00:00
)
),
array (
'IIS - No rewrite sub dir 2' ,
array (
2010-05-01 05:06:09 +00:00
'App' => array (
2011-10-28 05:01:17 +00:00
'base' => false ,
'baseUrl' => '/site/index.php' ,
'dir' => 'app' ,
'webroot' => 'webroot' ,
2010-05-01 05:06:09 +00:00
),
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'SCRIPT_NAME' => '/site/index.php' ,
'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot' ,
'QUERY_STRING' => '' ,
'REQUEST_URI' => '/site/index.php' ,
'URL' => '/site/index.php' ,
'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php' ,
'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot' ,
'PHP_SELF' => '/site/index.php' ,
'argv' => array (),
2010-05-01 05:06:09 +00:00
'argc' => 0
),
2011-02-19 17:36:27 +00:00
),
array (
2010-05-01 05:06:09 +00:00
'url' => '' ,
2011-02-19 23:20:54 +00:00
'base' => '/site/index.php' ,
2010-05-01 05:06:09 +00:00
'webroot' => '/site/app/webroot/'
),
2011-02-19 17:36:27 +00:00
),
array (
'IIS - No rewrite sub dir 2 with path' ,
array (
'App' => array (
2011-10-28 05:01:17 +00:00
'base' => false ,
2011-02-19 23:20:54 +00:00
'baseUrl' => '/site/index.php' ,
2011-02-19 17:36:27 +00:00
'dir' => 'app' ,
'webroot' => 'webroot'
),
2010-05-01 05:06:09 +00:00
'GET' => array ( '/posts/add' => '' ),
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'SCRIPT_NAME' => '/site/index.php' ,
'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot' ,
'QUERY_STRING' => '/posts/add' ,
'REQUEST_URI' => '/site/index.php/posts/add' ,
'URL' => '/site/index.php/posts/add' ,
'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php' ,
'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot' ,
'PHP_SELF' => '/site/index.php/posts/add' ,
'argv' => array ( '/posts/add' ),
2010-05-01 05:06:09 +00:00
'argc' => 1
),
2011-02-19 17:36:27 +00:00
),
array (
2010-05-01 05:06:09 +00:00
'url' => 'posts/add' ,
2011-02-19 23:20:54 +00:00
'base' => '/site/index.php' ,
2010-05-01 05:06:09 +00:00
'webroot' => '/site/app/webroot/'
)
),
2011-02-19 17:36:27 +00:00
array (
'Apache - No rewrite, document root set to webroot, requesting path' ,
array (
2010-05-01 05:06:09 +00:00
'App' => array (
2011-10-28 05:01:17 +00:00
'base' => false ,
'baseUrl' => '/index.php' ,
'dir' => 'app' ,
2010-05-01 05:06:09 +00:00
'webroot' => 'webroot'
),
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php' ,
'QUERY_STRING' => '' ,
'REQUEST_URI' => '/index.php/posts/index' ,
2011-02-19 15:00:34 +00:00
'SCRIPT_NAME' => '/index.php' ,
'PATH_INFO' => '/posts/index' ,
'PHP_SELF' => '/index.php/posts/index' ,
2010-05-01 05:06:09 +00:00
),
2011-02-19 17:36:27 +00:00
),
array (
2011-02-19 15:00:34 +00:00
'url' => 'posts/index' ,
2010-05-01 05:06:09 +00:00
'base' => '/index.php' ,
'webroot' => '/'
),
2011-02-19 17:36:27 +00:00
),
array (
'Apache - No rewrite, document root set to webroot, requesting root' ,
array (
'App' => array (
2011-10-28 05:01:17 +00:00
'base' => false ,
'baseUrl' => '/index.php' ,
'dir' => 'app' ,
2011-02-19 17:36:27 +00:00
'webroot' => 'webroot'
),
2010-05-01 05:06:09 +00:00
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php' ,
'QUERY_STRING' => '' ,
'REQUEST_URI' => '/index.php' ,
'SCRIPT_NAME' => '/index.php' ,
'PATH_INFO' => '' ,
'PHP_SELF' => '/index.php' ,
2010-05-01 05:06:09 +00:00
),
2011-02-19 17:36:27 +00:00
),
array (
2011-02-19 15:00:34 +00:00
'url' => '' ,
2010-05-01 05:06:09 +00:00
'base' => '/index.php' ,
'webroot' => '/'
),
2011-02-19 17:36:27 +00:00
),
array (
'Apache - No rewrite, document root set above top level cake dir, requesting path' ,
array (
2010-05-01 05:06:09 +00:00
'App' => array (
2011-10-28 05:01:17 +00:00
'base' => false ,
'baseUrl' => '/site/index.php' ,
'dir' => 'app' ,
2010-05-01 05:06:09 +00:00
'webroot' => 'webroot'
),
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'SERVER_NAME' => 'localhost' ,
'DOCUMENT_ROOT' => '/Library/WebServer/Documents' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php' ,
'REQUEST_URI' => '/site/index.php/posts/index' ,
2011-02-19 15:00:34 +00:00
'SCRIPT_NAME' => '/site/index.php' ,
'PATH_INFO' => '/posts/index' ,
'PHP_SELF' => '/site/index.php/posts/index' ,
),
2011-02-19 17:36:27 +00:00
),
array (
2011-02-19 15:00:34 +00:00
'url' => 'posts/index' ,
'base' => '/site/index.php' ,
'webroot' => '/site/app/webroot/' ,
),
2011-02-19 17:36:27 +00:00
),
array (
2011-04-25 03:31:44 +00:00
'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO' ,
2011-02-19 17:36:27 +00:00
array (
2010-05-01 05:06:09 +00:00
'App' => array (
2011-10-28 05:01:17 +00:00
'base' => false ,
'baseUrl' => '/site/index.php' ,
'dir' => 'app' ,
2010-05-01 05:06:09 +00:00
'webroot' => 'webroot'
),
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'SERVER_NAME' => 'localhost' ,
'DOCUMENT_ROOT' => '/Library/WebServer/Documents' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php' ,
2011-02-22 03:30:01 +00:00
'REQUEST_URI' => '/site/index.php/' ,
2011-02-19 15:00:34 +00:00
'SCRIPT_NAME' => '/site/index.php' ,
'PHP_SELF' => '/site/index.php/' ,
2010-05-01 05:06:09 +00:00
),
2011-02-19 17:36:27 +00:00
),
array (
2010-05-01 05:06:09 +00:00
'url' => '' ,
2011-02-19 15:00:34 +00:00
'base' => '/site/index.php' ,
'webroot' => '/site/app/webroot/' ,
2010-05-01 05:06:09 +00:00
),
2011-02-19 17:36:27 +00:00
),
array (
'Apache - No rewrite, document root set above top level cake dir, request path, with GET' ,
array (
2010-05-01 05:06:09 +00:00
'App' => array (
2011-10-28 05:01:17 +00:00
'base' => false ,
'baseUrl' => '/site/index.php' ,
'dir' => 'app' ,
2010-05-01 05:06:09 +00:00
'webroot' => 'webroot'
),
2011-02-19 23:20:54 +00:00
'GET' => array ( 'a' => 'b' , 'c' => 'd' ),
2010-05-01 05:06:09 +00:00
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'SERVER_NAME' => 'localhost' ,
'DOCUMENT_ROOT' => '/Library/WebServer/Documents' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php' ,
'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d' ,
2011-02-19 15:00:34 +00:00
'SCRIPT_NAME' => '/site/index.php' ,
'PATH_INFO' => '/posts/index' ,
'PHP_SELF' => '/site/index.php/posts/index' ,
'QUERY_STRING' => 'a=b&c=d'
2010-05-01 05:06:09 +00:00
),
2011-02-19 17:36:27 +00:00
),
array (
2011-02-19 15:00:34 +00:00
'urlParams' => array ( 'a' => 'b' , 'c' => 'd' ),
'url' => 'posts/index' ,
'base' => '/site/index.php' ,
'webroot' => '/site/app/webroot/' ,
),
2011-02-22 03:30:01 +00:00
),
array (
'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO' ,
array (
'App' => array (
'base' => false ,
'baseUrl' => false ,
'dir' => 'app' ,
'webroot' => 'webroot'
),
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'SERVER_NAME' => 'localhost' ,
'DOCUMENT_ROOT' => '/Library/WebServer/Documents' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php' ,
2011-02-22 03:30:01 +00:00
'REQUEST_URI' => '/site/' ,
'SCRIPT_NAME' => '/site/app/webroot/index.php' ,
'PHP_SELF' => '/site/app/webroot/index.php' ,
),
),
array (
'url' => '' ,
'base' => '/site' ,
'webroot' => '/site/' ,
),
),
array (
'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI' ,
array (
'App' => array (
'base' => false ,
'baseUrl' => false ,
'dir' => 'app' ,
'webroot' => 'webroot'
),
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'SERVER_NAME' => 'localhost' ,
'DOCUMENT_ROOT' => '/Library/WebServer/Documents' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php' ,
2011-02-22 03:30:01 +00:00
'SCRIPT_NAME' => '/site/app/webroot/index.php' ,
'PHP_SELF' => '/site/app/webroot/index.php' ,
'PATH_INFO' => null ,
'REQUEST_URI' => null ,
),
),
array (
'url' => '' ,
'base' => '/site' ,
'webroot' => '/site/' ,
),
),
2011-02-26 20:37:08 +00:00
array (
'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI' ,
array (
'App' => array (
'base' => false ,
'baseUrl' => false ,
'dir' => 'app' ,
'webroot' => 'webroot'
),
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'SERVER_NAME' => 'localhost' ,
'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php' ,
2011-02-26 20:37:08 +00:00
'SCRIPT_NAME' => '/index.php' ,
'PHP_SELF' => '/index.php' ,
'PATH_INFO' => null ,
'REQUEST_URI' => null ,
),
),
array (
'url' => '' ,
'base' => '' ,
'webroot' => '/' ,
),
),
2012-10-14 15:58:07 +00:00
array (
'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI' ,
array (
'App' => array (
'base' => false ,
'baseUrl' => false ,
'dir' => 'app' ,
'webroot' => 'webroot'
),
'SERVER' => array (
'SERVER_NAME' => 'localhost' ,
'DOCUMENT_ROOT' => '/Library/WebServer/Documents' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php' ,
2013-05-30 14:07:26 +00:00
'REQUEST_URI' => '/site/posts/index' ,
2012-10-14 15:58:07 +00:00
'SCRIPT_NAME' => '/site/app/webroot/index.php' ,
'PHP_SELF' => '/site/app/webroot/index.php' ,
),
),
array (
'url' => 'posts/index' ,
'base' => '/site' ,
'webroot' => '/site/' ,
),
),
2011-08-27 05:59:50 +00:00
array (
'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO' ,
array (
'App' => array (
'base' => false ,
'baseUrl' => false ,
'dir' => 'app' ,
'webroot' => 'webroot'
),
'GET' => array ( '/posts/add' => '' ),
'SERVER' => array (
2011-10-28 05:01:17 +00:00
'SERVER_NAME' => 'localhost' ,
'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php' ,
2011-08-27 05:59:50 +00:00
'SCRIPT_NAME' => '/index.php' ,
'QUERY_STRING' => '/posts/add&' ,
'PHP_SELF' => '/index.php' ,
'PATH_INFO' => null ,
'REQUEST_URI' => '/posts/add' ,
),
),
array (
'url' => 'posts/add' ,
'base' => '' ,
'webroot' => '/' ,
'urlParams' => array ()
),
),
2014-01-21 19:56:37 +00:00
array (
'Nginx - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO, base parameter set' ,
array (
'App' => array (
'base' => false ,
'baseUrl' => false ,
'dir' => 'app' ,
'webroot' => 'webroot'
),
'GET' => array ( '/site/posts/add' => '' ),
'SERVER' => array (
'SERVER_NAME' => 'localhost' ,
'DOCUMENT_ROOT' => '/Library/WebServer/Documents' ,
'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php' ,
'SCRIPT_NAME' => '/site/app/webroot/index.php' ,
'QUERY_STRING' => '/site/posts/add&' ,
'PHP_SELF' => '/site/app/webroot/index.php' ,
'PATH_INFO' => null ,
'REQUEST_URI' => '/site/posts/add' ,
),
),
array (
'url' => 'posts/add' ,
'base' => '/site' ,
'webroot' => '/site/' ,
'urlParams' => array ()
),
),
2010-05-01 05:06:09 +00:00
);
}
2010-05-02 03:49:22 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test environment detection
2010-05-02 03:49:22 +00:00
*
2011-02-19 17:36:27 +00:00
* @ dataProvider environmentGenerator
2013-06-26 11:36:50 +00:00
* @ param $name
* @ param $env
* @ param $expected
2010-05-02 03:49:22 +00:00
* @ return void
*/
2011-02-19 17:36:27 +00:00
public function testEnvironmentDetection ( $name , $env , $expected ) {
2011-05-09 04:58:01 +00:00
$_GET = array ();
2012-10-09 16:35:21 +00:00
$this -> _loadEnvironment ( $env );
2010-05-02 03:49:22 +00:00
$request = new CakeRequest ();
2011-02-19 17:36:27 +00:00
$this -> assertEquals ( $expected [ 'url' ], $request -> url , " url error " );
$this -> assertEquals ( $expected [ 'base' ], $request -> base , " base error " );
2011-02-22 03:30:01 +00:00
$this -> assertEquals ( $expected [ 'webroot' ], $request -> webroot , " webroot error " );
2011-02-19 17:36:27 +00:00
if ( isset ( $expected [ 'urlParams' ])) {
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( $expected [ 'urlParams' ], $request -> query , " GET param mismatch " );
2010-05-01 05:06:09 +00:00
}
2010-05-02 03:49:22 +00:00
}
2012-09-18 17:38:53 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test the query () method
2012-09-18 17:38:53 +00:00
*
* @ return void
*/
public function testQuery () {
$_GET = array ();
$_GET [ 'foo' ] = 'bar' ;
$request = new CakeRequest ();
$result = $request -> query ( 'foo' );
$this -> assertEquals ( 'bar' , $result );
$result = $request -> query ( 'imaginary' );
$this -> assertNull ( $result );
}
2012-09-19 01:14:55 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test the query () method with arrays passed via $_GET
2012-09-19 01:14:55 +00:00
*
* @ return void
*/
public function testQueryWithArray () {
$_GET = array ();
$_GET [ 'test' ] = array ( 'foo' , 'bar' );
$request = new CakeRequest ();
$result = $request -> query ( 'test' );
$this -> assertEquals ( array ( 'foo' , 'bar' ), $result );
$result = $request -> query ( 'test.1' );
$this -> assertEquals ( 'bar' , $result );
$result = $request -> query ( 'test.2' );
$this -> assertNull ( $result );
}
2010-09-12 18:05:57 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test the data () method reading
2010-09-12 18:05:57 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testDataReading () {
2010-09-12 18:05:57 +00:00
$_POST [ 'data' ] = array (
'Model' => array (
'field' => 'value'
)
);
$request = new CakeRequest ( 'posts/index' );
$result = $request -> data ( 'Model' );
$this -> assertEquals ( $_POST [ 'data' ][ 'Model' ], $result );
$result = $request -> data ( 'Model.imaginary' );
$this -> assertNull ( $result );
}
/**
2013-06-26 11:36:50 +00:00
* Test writing with data ()
2010-09-12 18:05:57 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testDataWriting () {
2010-09-12 18:05:57 +00:00
$_POST [ 'data' ] = array (
'Model' => array (
'field' => 'value'
)
);
$request = new CakeRequest ( 'posts/index' );
$result = $request -> data ( 'Model.new_value' , 'new value' );
$this -> assertSame ( $result , $request , 'Return was not $this' );
2011-10-28 05:01:17 +00:00
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'new value' , $request -> data [ 'Model' ][ 'new_value' ]);
2010-09-12 18:05:57 +00:00
$request -> data ( 'Post.title' , 'New post' ) -> data ( 'Comment.1.author' , 'Mark' );
2012-03-23 06:37:12 +00:00
$this -> assertEquals ( 'New post' , $request -> data [ 'Post' ][ 'title' ]);
$this -> assertEquals ( 'Mark' , $request -> data [ 'Comment' ][ '1' ][ 'author' ]);
2010-09-12 18:05:57 +00:00
}
2010-09-12 18:08:17 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test writing falsey values .
2010-09-12 18:08:17 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testDataWritingFalsey () {
2010-09-12 18:08:17 +00:00
$request = new CakeRequest ( 'posts/index' );
$request -> data ( 'Post.null' , null );
$this -> assertNull ( $request -> data [ 'Post' ][ 'null' ]);
2011-10-28 05:01:17 +00:00
2010-09-12 18:08:17 +00:00
$request -> data ( 'Post.false' , false );
$this -> assertFalse ( $request -> data [ 'Post' ][ 'false' ]);
2011-10-28 05:01:17 +00:00
2010-09-12 18:08:17 +00:00
$request -> data ( 'Post.zero' , 0 );
$this -> assertSame ( 0 , $request -> data [ 'Post' ][ 'zero' ]);
2011-10-28 05:01:17 +00:00
2010-09-12 18:08:17 +00:00
$request -> data ( 'Post.empty' , '' );
$this -> assertSame ( '' , $request -> data [ 'Post' ][ 'empty' ]);
}
2014-05-24 23:46:40 +00:00
/**
2014-05-28 18:58:17 +00:00
* Test reading params
*
2014-05-24 23:46:40 +00:00
* @ dataProvider paramReadingDataProvider
*/
public function testParamReading ( $toRead , $expected ) {
$request = new CakeRequest ( '/' );
$request -> addParams ( array (
'action' => 'index' ,
'foo' => 'bar' ,
'baz' => array (
'a' => array (
'b' => 'c' ,
),
),
'admin' => true ,
'truthy' => 1 ,
'zero' => '0' ,
));
$this -> assertEquals ( $expected , $request -> param ( $toRead ));
}
2014-05-28 18:58:17 +00:00
/**
* Data provider for testing reading values with CakeRequest :: param ()
2014-06-21 16:28:10 +00:00
*
2014-05-28 18:58:17 +00:00
* @ return array
*/
2014-05-24 23:46:40 +00:00
public function paramReadingDataProvider () {
return array (
array (
'action' ,
'index' ,
),
array (
'baz' ,
array (
'a' => array (
'b' => 'c' ,
),
),
),
array (
'baz.a.b' ,
'c' ,
),
array (
'does_not_exist' ,
false ,
),
array (
'admin' ,
true ,
),
array (
'truthy' ,
1 ,
),
array (
'zero' ,
'0' ,
),
);
}
2014-05-28 18:58:17 +00:00
/**
* test writing request params with param ()
*
* @ return void
*/
2014-05-24 23:52:30 +00:00
public function testParamWriting () {
$request = new CakeRequest ( '/' );
$request -> addParams ( array (
'action' => 'index' ,
));
$this -> assertInstanceOf ( 'CakeRequest' , $request -> param ( 'some' , 'thing' ), 'Method has not returned $this' );
$request -> param ( 'Post.null' , null );
$this -> assertNull ( $request -> params [ 'Post' ][ 'null' ]);
$request -> param ( 'Post.false' , false );
$this -> assertFalse ( $request -> params [ 'Post' ][ 'false' ]);
$request -> param ( 'Post.zero' , 0 );
$this -> assertSame ( 0 , $request -> params [ 'Post' ][ 'zero' ]);
$request -> param ( 'Post.empty' , '' );
$this -> assertSame ( '' , $request -> params [ 'Post' ][ 'empty' ]);
$this -> assertSame ( 'index' , $request -> action );
$request -> param ( 'action' , 'edit' );
$this -> assertSame ( 'edit' , $request -> action );
}
2010-11-07 05:35:36 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test accept language
2010-11-07 05:35:36 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testAcceptLanguage () {
2012-08-29 03:52:05 +00:00
// Weird language
2010-11-07 05:35:36 +00:00
$_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ] = 'inexistent,en-ca' ;
$result = CakeRequest :: acceptLanguage ();
$this -> assertEquals ( array ( 'inexistent' , 'en-ca' ), $result , 'Languages do not match' );
2012-08-29 03:52:05 +00:00
// No qualifier
$_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ] = 'es_mx,en_ca' ;
2010-11-07 05:35:36 +00:00
$result = CakeRequest :: acceptLanguage ();
$this -> assertEquals ( array ( 'es-mx' , 'en-ca' ), $result , 'Languages do not match' );
2011-10-28 05:01:17 +00:00
2012-08-29 03:52:05 +00:00
// With qualifier
$_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4' ;
$result = CakeRequest :: acceptLanguage ();
$this -> assertEquals ( array ( 'en-us' , 'en' , 'pt-br' , 'pt' ), $result , 'Languages do not match' );
// With spaces
$_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ] = 'da, en-gb;q=0.8, en;q=0.7' ;
$result = CakeRequest :: acceptLanguage ();
$this -> assertEquals ( array ( 'da' , 'en-gb' , 'en' ), $result , 'Languages do not match' );
// Checking if requested
$_SERVER [ 'HTTP_ACCEPT_LANGUAGE' ] = 'es_mx,en_ca' ;
2015-09-25 15:22:00 +00:00
CakeRequest :: acceptLanguage ();
2012-08-29 03:52:05 +00:00
2010-11-07 05:35:36 +00:00
$result = CakeRequest :: acceptLanguage ( 'en-ca' );
$this -> assertTrue ( $result );
2012-08-29 03:52:05 +00:00
$result = CakeRequest :: acceptLanguage ( 'en-CA' );
$this -> assertTrue ( $result );
2010-11-07 05:35:36 +00:00
$result = CakeRequest :: acceptLanguage ( 'en-us' );
$this -> assertFalse ( $result );
2012-08-29 03:52:05 +00:00
$result = CakeRequest :: acceptLanguage ( 'en-US' );
$this -> assertFalse ( $result );
2010-11-07 05:35:36 +00:00
}
2011-03-03 02:46:28 +00:00
/**
2013-06-26 11:36:50 +00:00
* Test the here () method
2011-03-03 02:46:28 +00:00
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testHere () {
2011-03-03 02:46:28 +00:00
Configure :: write ( 'App.base' , '/base_path' );
$_GET = array ( 'test' => 'value' );
$request = new CakeRequest ( '/posts/add/1/name:value' );
$result = $request -> here ();
$this -> assertEquals ( '/base_path/posts/add/1/name:value?test=value' , $result );
$result = $request -> here ( false );
$this -> assertEquals ( '/posts/add/1/name:value?test=value' , $result );
$request = new CakeRequest ( '/posts/base_path/1/name:value' );
$result = $request -> here ();
$this -> assertEquals ( '/base_path/posts/base_path/1/name:value?test=value' , $result );
$result = $request -> here ( false );
$this -> assertEquals ( '/posts/base_path/1/name:value?test=value' , $result );
}
2014-07-24 08:58:34 +00:00
/**
* Test the here () with space in URL
*
* @ return void
*/
public function testHereWithSpaceInUrl () {
Configure :: write ( 'App.base' , '' );
$_GET = array ( '/admin/settings/settings/prefix/Access_Control' => '' );
$request = new CakeRequest ( '/admin/settings/settings/prefix/Access%20Control' );
$result = $request -> here ();
$this -> assertEquals ( '/admin/settings/settings/prefix/Access%20Control' , $result );
}
2014-06-21 16:28:10 +00:00
/**
* Test the input () method .
*
* @ return void
*/
public function testSetInput () {
$request = new CakeRequest ( '/' );
$request -> setInput ( 'I came from setInput' );
$result = $request -> input ();
$this -> assertEquals ( 'I came from setInput' , $result );
$result = $request -> input ();
$this -> assertEquals ( 'I came from setInput' , $result );
}
2011-04-27 03:03:17 +00:00
/**
* Test the input () method .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testInput () {
2011-09-25 01:56:37 +00:00
$request = $this -> getMock ( 'CakeRequest' , array ( '_readInput' ));
$request -> expects ( $this -> once ()) -> method ( '_readInput' )
2011-04-27 03:03:17 +00:00
-> will ( $this -> returnValue ( 'I came from stdin' ));
2011-10-28 05:01:17 +00:00
2011-04-27 03:03:17 +00:00
$result = $request -> input ();
$this -> assertEquals ( 'I came from stdin' , $result );
}
/**
* Test input () decoding .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testInputDecode () {
2011-09-25 01:56:37 +00:00
$request = $this -> getMock ( 'CakeRequest' , array ( '_readInput' ));
$request -> expects ( $this -> once ()) -> method ( '_readInput' )
2011-04-27 03:03:17 +00:00
-> will ( $this -> returnValue ( '{"name":"value"}' ));
$result = $request -> input ( 'json_decode' );
$this -> assertEquals ( array ( 'name' => 'value' ), ( array ) $result );
}
2011-04-30 17:05:10 +00:00
2011-10-28 05:01:17 +00:00
/**
2011-04-30 17:05:10 +00:00
* Test input () decoding with additional arguments .
*
* @ return void
*/
2011-05-30 20:02:32 +00:00
public function testInputDecodeExtraParams () {
2011-04-30 17:05:10 +00:00
$xml = <<< XML
< ? xml version = " 1.0 " encoding = " utf-8 " ?>
< post >
< title id = " title " > Test </ title >
</ post >
XML ;
2011-09-25 01:56:37 +00:00
$request = $this -> getMock ( 'CakeRequest' , array ( '_readInput' ));
$request -> expects ( $this -> once ()) -> method ( '_readInput' )
2011-04-30 17:05:10 +00:00
-> will ( $this -> returnValue ( $xml ));
$result = $request -> input ( 'Xml::build' , array ( 'return' => 'domdocument' ));
$this -> assertInstanceOf ( 'DOMDocument' , $result );
$this -> assertEquals (
2011-10-28 05:01:17 +00:00
'Test' ,
2011-04-30 17:05:10 +00:00
$result -> getElementsByTagName ( 'title' ) -> item ( 0 ) -> childNodes -> item ( 0 ) -> wholeText
);
}
2011-10-21 01:11:42 +00:00
/**
* Test is ( 'requested' ) and isRequested ()
*
* @ return void
*/
public function testIsRequested () {
$request = new CakeRequest ( '/posts/index' );
$request -> addParams ( array (
'controller' => 'posts' ,
'action' => 'index' ,
'plugin' => null ,
'requested' => 1
));
$this -> assertTrue ( $request -> is ( 'requested' ));
$this -> assertTrue ( $request -> isRequested ());
$request = new CakeRequest ( '/posts/index' );
$request -> addParams ( array (
'controller' => 'posts' ,
'action' => 'index' ,
'plugin' => null ,
));
$this -> assertFalse ( $request -> is ( 'requested' ));
$this -> assertFalse ( $request -> isRequested ());
}
2012-08-09 14:11:32 +00:00
/**
2014-02-10 11:53:20 +00:00
* Test allowMethod method
2012-08-09 14:11:32 +00:00
*
* @ return void
*/
2014-02-10 11:53:20 +00:00
public function testAllowMethod () {
2012-08-09 14:11:32 +00:00
$_SERVER [ 'REQUEST_METHOD' ] = 'PUT' ;
$request = new CakeRequest ( '/posts/edit/1' );
2014-02-10 11:53:20 +00:00
$this -> assertTrue ( $request -> allowMethod ( array ( 'put' )));
// BC check
2012-08-09 14:11:32 +00:00
$this -> assertTrue ( $request -> onlyAllow ( array ( 'put' )));
$_SERVER [ 'REQUEST_METHOD' ] = 'DELETE' ;
2014-02-10 11:53:20 +00:00
$this -> assertTrue ( $request -> allowMethod ( 'post' , 'delete' ));
// BC check
2012-08-09 14:11:32 +00:00
$this -> assertTrue ( $request -> onlyAllow ( 'post' , 'delete' ));
}
/**
2014-02-10 11:53:20 +00:00
* Test allowMethod throwing exception
2012-08-09 14:11:32 +00:00
*
2013-07-05 14:17:23 +00:00
* @ return void
2012-08-09 14:11:32 +00:00
*/
2014-02-10 11:53:20 +00:00
public function testAllowMethodException () {
2012-08-09 14:11:32 +00:00
$_SERVER [ 'REQUEST_METHOD' ] = 'PUT' ;
$request = new CakeRequest ( '/posts/edit/1' );
try {
2014-02-10 11:53:20 +00:00
$request -> allowMethod ( 'POST' , 'DELETE' );
2012-08-09 14:11:32 +00:00
$this -> fail ( 'An expected exception has not been raised.' );
} catch ( MethodNotAllowedException $e ) {
$this -> assertEquals ( array ( 'Allow' => 'POST, DELETE' ), $e -> responseHeader ());
}
$this -> setExpectedException ( 'MethodNotAllowedException' );
2014-02-10 11:53:20 +00:00
$request -> allowMethod ( 'POST' );
2012-08-09 14:11:32 +00:00
}
2016-01-19 01:04:32 +00:00
/**
* Tests that overriding the method to GET will clean all request
* data , to better simulate a GET request .
*
* @ return void
*/
2016-01-19 18:22:16 +00:00
public function testMethodOverrideEmptyData () {
$_POST = array ( '_method' => 'GET' , 'foo' => 'bar' );
2016-01-19 01:04:32 +00:00
$_SERVER [ 'REQUEST_METHOD' ] = 'PUT' ;
$request = new CakeRequest ( '/posts/edit/1' );
$this -> assertEmpty ( $request -> data );
2016-01-19 15:47:06 +00:00
$_POST = array ( 'foo' => 'bar' );
2016-01-19 01:04:32 +00:00
$_SERVER [ 'HTTP_X_HTTP_METHOD_OVERRIDE' ] = 'GET' ;
$request = new CakeRequest ( '/posts/edit/1' );
$this -> assertEmpty ( $request -> data );
}
2010-05-01 05:06:09 +00:00
/**
* loadEnvironment method
*
2012-05-13 00:43:31 +00:00
* @ param array $env
2010-05-01 05:06:09 +00:00
* @ return void
*/
2012-10-09 16:35:21 +00:00
protected function _loadEnvironment ( $env ) {
2010-05-01 05:06:09 +00:00
if ( isset ( $env [ 'App' ])) {
Configure :: write ( 'App' , $env [ 'App' ]);
}
if ( isset ( $env [ 'GET' ])) {
foreach ( $env [ 'GET' ] as $key => $val ) {
$_GET [ $key ] = $val ;
}
}
if ( isset ( $env [ 'POST' ])) {
foreach ( $env [ 'POST' ] as $key => $val ) {
$_POST [ $key ] = $val ;
}
}
if ( isset ( $env [ 'SERVER' ])) {
foreach ( $env [ 'SERVER' ] as $key => $val ) {
$_SERVER [ $key ] = $val ;
}
}
}
2011-04-27 03:03:17 +00:00
}