CakeRequestTest.php 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  1. <?php
  2. /**
  3. * CakeRequest Test case file.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.Test.Case.Network
  17. * @since CakePHP(tm) v 2.0
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. App::uses('Dispatcher', 'Routing');
  21. App::uses('Xml', 'Utility');
  22. App::uses('CakeRequest', 'Network');
  23. /**
  24. * Class TestCakeRequest
  25. *
  26. * @package Cake.Test.Case.Network
  27. */
  28. class TestCakeRequest extends CakeRequest {
  29. /**
  30. * reConstruct method
  31. *
  32. * @param string $url
  33. * @param boolean $parseEnvironment
  34. * @return void
  35. */
  36. public function reConstruct($url = 'some/path', $parseEnvironment = true) {
  37. $this->_base();
  38. if (empty($url)) {
  39. $url = $this->_url();
  40. }
  41. if ($url[0] === '/') {
  42. $url = substr($url, 1);
  43. }
  44. $this->url = $url;
  45. if ($parseEnvironment) {
  46. $this->_processPost();
  47. $this->_processGet();
  48. $this->_processFiles();
  49. }
  50. $this->here = $this->base . '/' . $this->url;
  51. }
  52. }
  53. /**
  54. * Class CakeRequestTest
  55. */
  56. class CakeRequestTest extends CakeTestCase {
  57. /**
  58. * Setup callback
  59. *
  60. * @return void
  61. */
  62. public function setUp() {
  63. parent::setUp();
  64. $this->_app = Configure::read('App');
  65. $this->_case = null;
  66. if (isset($_GET['case'])) {
  67. $this->_case = $_GET['case'];
  68. unset($_GET['case']);
  69. }
  70. Configure::write('App.baseUrl', false);
  71. }
  72. /**
  73. * TearDown
  74. *
  75. * @return void
  76. */
  77. public function tearDown() {
  78. parent::tearDown();
  79. if (!empty($this->_case)) {
  80. $_GET['case'] = $this->_case;
  81. }
  82. Configure::write('App', $this->_app);
  83. }
  84. /**
  85. * Test that the autoparse = false constructor works.
  86. *
  87. * @return void
  88. */
  89. public function testNoAutoParseConstruction() {
  90. $_GET = array(
  91. 'one' => 'param'
  92. );
  93. $request = new CakeRequest(null, false);
  94. $this->assertFalse(isset($request->query['one']));
  95. }
  96. /**
  97. * Test construction
  98. *
  99. * @return void
  100. */
  101. public function testConstructionGetParsing() {
  102. $_GET = array(
  103. 'one' => 'param',
  104. 'two' => 'banana'
  105. );
  106. $request = new CakeRequest('some/path');
  107. $this->assertEquals($request->query, $_GET);
  108. $_GET = array(
  109. 'one' => 'param',
  110. 'two' => 'banana',
  111. );
  112. $request = new CakeRequest('some/path');
  113. $this->assertEquals($request->query, $_GET);
  114. $this->assertEquals('some/path', $request->url);
  115. }
  116. /**
  117. * Test that querystring args provided in the url string are parsed.
  118. *
  119. * @return void
  120. */
  121. public function testQueryStringParsingFromInputUrl() {
  122. $_GET = array();
  123. $request = new CakeRequest('some/path?one=something&two=else');
  124. $expected = array('one' => 'something', 'two' => 'else');
  125. $this->assertEquals($expected, $request->query);
  126. $this->assertEquals('some/path?one=something&two=else', $request->url);
  127. }
  128. /**
  129. * Test that named arguments + querystrings are handled correctly.
  130. *
  131. * @return void
  132. */
  133. public function testQueryStringAndNamedParams() {
  134. $_SERVER['REQUEST_URI'] = '/tasks/index/page:1?ts=123456';
  135. $request = new CakeRequest();
  136. $this->assertEquals('tasks/index/page:1', $request->url);
  137. $_SERVER['REQUEST_URI'] = '/tasks/index/page:1/?ts=123456';
  138. $request = new CakeRequest();
  139. $this->assertEquals('tasks/index/page:1/', $request->url);
  140. $_SERVER['REQUEST_URI'] = '/some/path?url=http://cakephp.org';
  141. $request = new CakeRequest();
  142. $this->assertEquals('some/path', $request->url);
  143. $_SERVER['REQUEST_URI'] = Configure::read('App.fullBaseUrl') . '/other/path?url=http://cakephp.org';
  144. $request = new CakeRequest();
  145. $this->assertEquals('other/path', $request->url);
  146. }
  147. /**
  148. * Test addParams() method
  149. *
  150. * @return void
  151. */
  152. public function testAddParams() {
  153. $request = new CakeRequest('some/path');
  154. $request->params = array('controller' => 'posts', 'action' => 'view');
  155. $result = $request->addParams(array('plugin' => null, 'action' => 'index'));
  156. $this->assertSame($result, $request, 'Method did not return itself. %s');
  157. $this->assertEquals('posts', $request->controller);
  158. $this->assertEquals('index', $request->action);
  159. $this->assertEquals(null, $request->plugin);
  160. }
  161. /**
  162. * Test splicing in paths.
  163. *
  164. * @return void
  165. */
  166. public function testAddPaths() {
  167. $request = new CakeRequest('some/path');
  168. $request->webroot = '/some/path/going/here/';
  169. $result = $request->addPaths(array(
  170. 'random' => '/something', 'webroot' => '/', 'here' => '/', 'base' => '/base_dir'
  171. ));
  172. $this->assertSame($result, $request, 'Method did not return itself. %s');
  173. $this->assertEquals('/', $request->webroot);
  174. $this->assertEquals('/base_dir', $request->base);
  175. $this->assertEquals('/', $request->here);
  176. $this->assertFalse(isset($request->random));
  177. }
  178. /**
  179. * Test parsing POST data into the object.
  180. *
  181. * @return void
  182. */
  183. public function testPostParsing() {
  184. $_POST = array('data' => array(
  185. 'Article' => array('title')
  186. ));
  187. $request = new CakeRequest('some/path');
  188. $this->assertEquals($_POST['data'], $request->data);
  189. $_POST = array('one' => 1, 'two' => 'three');
  190. $request = new CakeRequest('some/path');
  191. $this->assertEquals($_POST, $request->data);
  192. $_POST = array(
  193. 'data' => array(
  194. 'Article' => array('title' => 'Testing'),
  195. ),
  196. 'action' => 'update'
  197. );
  198. $request = new CakeRequest('some/path');
  199. $expected = array(
  200. 'Article' => array('title' => 'Testing'),
  201. 'action' => 'update'
  202. );
  203. $this->assertEquals($expected, $request->data);
  204. $_POST = array('data' => array(
  205. 'Article' => array('title'),
  206. 'Tag' => array('Tag' => array(1, 2))
  207. ));
  208. $request = new CakeRequest('some/path');
  209. $this->assertEquals($_POST['data'], $request->data);
  210. $_POST = array('data' => array(
  211. 'Article' => array('title' => 'some title'),
  212. 'Tag' => array('Tag' => array(1, 2))
  213. ));
  214. $request = new CakeRequest('some/path');
  215. $this->assertEquals($_POST['data'], $request->data);
  216. $_POST = array(
  217. 'a' => array(1, 2),
  218. 'b' => array(1, 2)
  219. );
  220. $request = new CakeRequest('some/path');
  221. $this->assertEquals($_POST, $request->data);
  222. }
  223. /**
  224. * Test parsing PUT data into the object.
  225. *
  226. * @return void
  227. */
  228. public function testPutParsing() {
  229. $_SERVER['REQUEST_METHOD'] = 'PUT';
  230. $_SERVER['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=UTF-8';
  231. $data = array('data' => array(
  232. 'Article' => array('title')
  233. ));
  234. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  235. $request->expects($this->at(0))->method('_readInput')
  236. ->will($this->returnValue('data[Article][]=title'));
  237. $request->reConstruct();
  238. $this->assertEquals($data['data'], $request->data);
  239. $data = array('one' => 1, 'two' => 'three');
  240. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  241. $request->expects($this->at(0))->method('_readInput')
  242. ->will($this->returnValue('one=1&two=three'));
  243. $request->reConstruct();
  244. $this->assertEquals($data, $request->data);
  245. $data = array(
  246. 'data' => array(
  247. 'Article' => array('title' => 'Testing'),
  248. ),
  249. 'action' => 'update'
  250. );
  251. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  252. $request->expects($this->at(0))->method('_readInput')
  253. ->will($this->returnValue('data[Article][title]=Testing&action=update'));
  254. $request->reConstruct();
  255. $expected = array(
  256. 'Article' => array('title' => 'Testing'),
  257. 'action' => 'update'
  258. );
  259. $this->assertEquals($expected, $request->data);
  260. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  261. $data = array('data' => array(
  262. 'Article' => array('title'),
  263. 'Tag' => array('Tag' => array(1, 2))
  264. ));
  265. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  266. $request->expects($this->at(0))->method('_readInput')
  267. ->will($this->returnValue('data[Article][]=title&Tag[Tag][]=1&Tag[Tag][]=2'));
  268. $request->reConstruct();
  269. $this->assertEquals($data['data'], $request->data);
  270. $data = array('data' => array(
  271. 'Article' => array('title' => 'some title'),
  272. 'Tag' => array('Tag' => array(1, 2))
  273. ));
  274. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  275. $request->expects($this->at(0))->method('_readInput')
  276. ->will($this->returnValue('data[Article][title]=some%20title&Tag[Tag][]=1&Tag[Tag][]=2'));
  277. $request->reConstruct();
  278. $this->assertEquals($data['data'], $request->data);
  279. $data = array(
  280. 'a' => array(1, 2),
  281. 'b' => array(1, 2)
  282. );
  283. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  284. $request->expects($this->at(0))->method('_readInput')
  285. ->will($this->returnValue('a[]=1&a[]=2&b[]=1&b[]=2'));
  286. $request->reConstruct();
  287. $this->assertEquals($data, $request->data);
  288. }
  289. /**
  290. * Test parsing json PUT data into the object.
  291. *
  292. * @return void
  293. */
  294. public function testPutParsingJSON() {
  295. $_SERVER['REQUEST_METHOD'] = 'PUT';
  296. $_SERVER['CONTENT_TYPE'] = 'application/json';
  297. $request = $this->getMock('TestCakeRequest', array('_readInput'));
  298. $request->expects($this->at(0))->method('_readInput')
  299. ->will($this->returnValue('{"Article":["title"]}'));
  300. $request->reConstruct();
  301. $result = $request->input('json_decode', true);
  302. $this->assertEquals(array('title'), $result['Article']);
  303. }
  304. /**
  305. * Test parsing of FILES array
  306. *
  307. * @return void
  308. */
  309. public function testFilesParsing() {
  310. $_FILES = array(
  311. 'data' => array(
  312. 'name' => array(
  313. 'File' => array(
  314. array('data' => 'cake_sqlserver_patch.patch'),
  315. array('data' => 'controller.diff'),
  316. array('data' => ''),
  317. array('data' => ''),
  318. ),
  319. 'Post' => array('attachment' => 'jquery-1.2.1.js'),
  320. ),
  321. 'type' => array(
  322. 'File' => array(
  323. array('data' => ''),
  324. array('data' => ''),
  325. array('data' => ''),
  326. array('data' => ''),
  327. ),
  328. 'Post' => array('attachment' => 'application/x-javascript'),
  329. ),
  330. 'tmp_name' => array(
  331. 'File' => array(
  332. array('data' => '/private/var/tmp/phpy05Ywj'),
  333. array('data' => '/private/var/tmp/php7MBztY'),
  334. array('data' => ''),
  335. array('data' => ''),
  336. ),
  337. 'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
  338. ),
  339. 'error' => array(
  340. 'File' => array(
  341. array('data' => 0),
  342. array('data' => 0),
  343. array('data' => 4),
  344. array('data' => 4)
  345. ),
  346. 'Post' => array('attachment' => 0)
  347. ),
  348. 'size' => array(
  349. 'File' => array(
  350. array('data' => 6271),
  351. array('data' => 350),
  352. array('data' => 0),
  353. array('data' => 0),
  354. ),
  355. 'Post' => array('attachment' => 80469)
  356. ),
  357. )
  358. );
  359. $request = new CakeRequest('some/path');
  360. $expected = array(
  361. 'File' => array(
  362. array(
  363. 'data' => array(
  364. 'name' => 'cake_sqlserver_patch.patch',
  365. 'type' => '',
  366. 'tmp_name' => '/private/var/tmp/phpy05Ywj',
  367. 'error' => 0,
  368. 'size' => 6271,
  369. )
  370. ),
  371. array(
  372. 'data' => array(
  373. 'name' => 'controller.diff',
  374. 'type' => '',
  375. 'tmp_name' => '/private/var/tmp/php7MBztY',
  376. 'error' => 0,
  377. 'size' => 350,
  378. )
  379. ),
  380. array(
  381. 'data' => array(
  382. 'name' => '',
  383. 'type' => '',
  384. 'tmp_name' => '',
  385. 'error' => 4,
  386. 'size' => 0,
  387. )
  388. ),
  389. array(
  390. 'data' => array(
  391. 'name' => '',
  392. 'type' => '',
  393. 'tmp_name' => '',
  394. 'error' => 4,
  395. 'size' => 0,
  396. )
  397. ),
  398. ),
  399. 'Post' => array(
  400. 'attachment' => array(
  401. 'name' => 'jquery-1.2.1.js',
  402. 'type' => 'application/x-javascript',
  403. 'tmp_name' => '/private/var/tmp/phpEwlrIo',
  404. 'error' => 0,
  405. 'size' => 80469,
  406. )
  407. )
  408. );
  409. $this->assertEquals($expected, $request->data);
  410. $_FILES = array(
  411. 'data' => array(
  412. 'name' => array(
  413. 'Document' => array(
  414. 1 => array(
  415. 'birth_cert' => 'born on.txt',
  416. 'passport' => 'passport.txt',
  417. 'drivers_license' => 'ugly pic.jpg'
  418. ),
  419. 2 => array(
  420. 'birth_cert' => 'aunt betty.txt',
  421. 'passport' => 'betty-passport.txt',
  422. 'drivers_license' => 'betty-photo.jpg'
  423. ),
  424. ),
  425. ),
  426. 'type' => array(
  427. 'Document' => array(
  428. 1 => array(
  429. 'birth_cert' => 'application/octet-stream',
  430. 'passport' => 'application/octet-stream',
  431. 'drivers_license' => 'application/octet-stream',
  432. ),
  433. 2 => array(
  434. 'birth_cert' => 'application/octet-stream',
  435. 'passport' => 'application/octet-stream',
  436. 'drivers_license' => 'application/octet-stream',
  437. )
  438. )
  439. ),
  440. 'tmp_name' => array(
  441. 'Document' => array(
  442. 1 => array(
  443. 'birth_cert' => '/private/var/tmp/phpbsUWfH',
  444. 'passport' => '/private/var/tmp/php7f5zLt',
  445. 'drivers_license' => '/private/var/tmp/phpMXpZgT',
  446. ),
  447. 2 => array(
  448. 'birth_cert' => '/private/var/tmp/php5kHZt0',
  449. 'passport' => '/private/var/tmp/phpnYkOuM',
  450. 'drivers_license' => '/private/var/tmp/php9Rq0P3',
  451. )
  452. )
  453. ),
  454. 'error' => array(
  455. 'Document' => array(
  456. 1 => array(
  457. 'birth_cert' => 0,
  458. 'passport' => 0,
  459. 'drivers_license' => 0,
  460. ),
  461. 2 => array(
  462. 'birth_cert' => 0,
  463. 'passport' => 0,
  464. 'drivers_license' => 0,
  465. )
  466. )
  467. ),
  468. 'size' => array(
  469. 'Document' => array(
  470. 1 => array(
  471. 'birth_cert' => 123,
  472. 'passport' => 458,
  473. 'drivers_license' => 875,
  474. ),
  475. 2 => array(
  476. 'birth_cert' => 876,
  477. 'passport' => 976,
  478. 'drivers_license' => 9783,
  479. )
  480. )
  481. )
  482. )
  483. );
  484. $request = new CakeRequest('some/path');
  485. $expected = array(
  486. 'Document' => array(
  487. 1 => array(
  488. 'birth_cert' => array(
  489. 'name' => 'born on.txt',
  490. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  491. 'error' => 0,
  492. 'size' => 123,
  493. 'type' => 'application/octet-stream',
  494. ),
  495. 'passport' => array(
  496. 'name' => 'passport.txt',
  497. 'tmp_name' => '/private/var/tmp/php7f5zLt',
  498. 'error' => 0,
  499. 'size' => 458,
  500. 'type' => 'application/octet-stream',
  501. ),
  502. 'drivers_license' => array(
  503. 'name' => 'ugly pic.jpg',
  504. 'tmp_name' => '/private/var/tmp/phpMXpZgT',
  505. 'error' => 0,
  506. 'size' => 875,
  507. 'type' => 'application/octet-stream',
  508. ),
  509. ),
  510. 2 => array(
  511. 'birth_cert' => array(
  512. 'name' => 'aunt betty.txt',
  513. 'tmp_name' => '/private/var/tmp/php5kHZt0',
  514. 'error' => 0,
  515. 'size' => 876,
  516. 'type' => 'application/octet-stream',
  517. ),
  518. 'passport' => array(
  519. 'name' => 'betty-passport.txt',
  520. 'tmp_name' => '/private/var/tmp/phpnYkOuM',
  521. 'error' => 0,
  522. 'size' => 976,
  523. 'type' => 'application/octet-stream',
  524. ),
  525. 'drivers_license' => array(
  526. 'name' => 'betty-photo.jpg',
  527. 'tmp_name' => '/private/var/tmp/php9Rq0P3',
  528. 'error' => 0,
  529. 'size' => 9783,
  530. 'type' => 'application/octet-stream',
  531. ),
  532. ),
  533. )
  534. );
  535. $this->assertEquals($expected, $request->data);
  536. $_FILES = array(
  537. 'data' => array(
  538. 'name' => array('birth_cert' => 'born on.txt'),
  539. 'type' => array('birth_cert' => 'application/octet-stream'),
  540. 'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
  541. 'error' => array('birth_cert' => 0),
  542. 'size' => array('birth_cert' => 123)
  543. )
  544. );
  545. $request = new CakeRequest('some/path');
  546. $expected = array(
  547. 'birth_cert' => array(
  548. 'name' => 'born on.txt',
  549. 'type' => 'application/octet-stream',
  550. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  551. 'error' => 0,
  552. 'size' => 123
  553. )
  554. );
  555. $this->assertEquals($expected, $request->data);
  556. $_FILES = array(
  557. 'something' => array(
  558. 'name' => 'something.txt',
  559. 'type' => 'text/plain',
  560. 'tmp_name' => '/some/file',
  561. 'error' => 0,
  562. 'size' => 123
  563. )
  564. );
  565. $request = new CakeRequest('some/path');
  566. $this->assertEquals($request->params['form'], $_FILES);
  567. }
  568. /**
  569. * Test that files in the 0th index work.
  570. */
  571. public function testFilesZeroithIndex() {
  572. $_FILES = array(
  573. 0 => array(
  574. 'name' => 'cake_sqlserver_patch.patch',
  575. 'type' => 'text/plain',
  576. 'tmp_name' => '/private/var/tmp/phpy05Ywj',
  577. 'error' => 0,
  578. 'size' => 6271,
  579. ),
  580. );
  581. $request = new CakeRequest('some/path');
  582. $this->assertEquals($_FILES, $request->params['form']);
  583. }
  584. /**
  585. * Test method overrides coming in from POST data.
  586. *
  587. * @return void
  588. */
  589. public function testMethodOverrides() {
  590. $_POST = array('_method' => 'POST');
  591. $request = new CakeRequest('some/path');
  592. $this->assertEquals(env('REQUEST_METHOD'), 'POST');
  593. $_POST = array('_method' => 'DELETE');
  594. $request = new CakeRequest('some/path');
  595. $this->assertEquals(env('REQUEST_METHOD'), 'DELETE');
  596. $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
  597. $request = new CakeRequest('some/path');
  598. $this->assertEquals(env('REQUEST_METHOD'), 'PUT');
  599. }
  600. /**
  601. * Test the clientIp method.
  602. *
  603. * @return void
  604. */
  605. public function testclientIp() {
  606. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
  607. $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
  608. $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
  609. $request = new CakeRequest('some/path');
  610. $this->assertEquals('192.168.1.5', $request->clientIp(false));
  611. $this->assertEquals('192.168.1.2', $request->clientIp());
  612. unset($_SERVER['HTTP_X_FORWARDED_FOR']);
  613. $this->assertEquals('192.168.1.2', $request->clientIp());
  614. unset($_SERVER['HTTP_CLIENT_IP']);
  615. $this->assertEquals('192.168.1.3', $request->clientIp());
  616. $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
  617. $this->assertEquals('10.0.1.2', $request->clientIp());
  618. }
  619. /**
  620. * Test the referrer function.
  621. *
  622. * @return void
  623. */
  624. public function testReferer() {
  625. $request = new CakeRequest('some/path');
  626. $request->webroot = '/';
  627. $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
  628. $result = $request->referer();
  629. $this->assertSame($result, 'http://cakephp.org');
  630. $_SERVER['HTTP_REFERER'] = '';
  631. $result = $request->referer();
  632. $this->assertSame($result, '/');
  633. $_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/some/path';
  634. $result = $request->referer(true);
  635. $this->assertSame($result, '/some/path');
  636. $_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/some/path';
  637. $result = $request->referer(false);
  638. $this->assertSame($result, Configure::read('App.fullBaseUrl') . '/some/path');
  639. $_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/some/path';
  640. $result = $request->referer(true);
  641. $this->assertSame($result, '/some/path');
  642. $_SERVER['HTTP_REFERER'] = Configure::read('App.fullBaseUrl') . '/recipes/add';
  643. $result = $request->referer(true);
  644. $this->assertSame($result, '/recipes/add');
  645. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
  646. $result = $request->referer();
  647. $this->assertSame($result, 'cakephp.org');
  648. }
  649. /**
  650. * Test referer() with a base path that duplicates the
  651. * first segment.
  652. *
  653. * @return void
  654. */
  655. public function testRefererBasePath() {
  656. $request = new CakeRequest('some/path');
  657. $request->url = 'users/login';
  658. $request->webroot = '/waves/';
  659. $request->base = '/waves';
  660. $request->here = '/waves/users/login';
  661. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/waves/waves/add';
  662. $result = $request->referer(true);
  663. $this->assertSame($result, '/waves/add');
  664. }
  665. /**
  666. * test the simple uses of is()
  667. *
  668. * @return void
  669. */
  670. public function testIsHttpMethods() {
  671. $request = new CakeRequest('some/path');
  672. $this->assertFalse($request->is('undefined-behavior'));
  673. $_SERVER['REQUEST_METHOD'] = 'GET';
  674. $this->assertTrue($request->is('get'));
  675. $_SERVER['REQUEST_METHOD'] = 'POST';
  676. $this->assertTrue($request->is('POST'));
  677. $_SERVER['REQUEST_METHOD'] = 'PUT';
  678. $this->assertTrue($request->is('put'));
  679. $this->assertFalse($request->is('get'));
  680. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  681. $this->assertTrue($request->is('delete'));
  682. $this->assertTrue($request->isDelete());
  683. $_SERVER['REQUEST_METHOD'] = 'delete';
  684. $this->assertFalse($request->is('delete'));
  685. }
  686. /**
  687. * Test is() with multiple types.
  688. *
  689. * @return void
  690. */
  691. public function testIsMultiple() {
  692. $request = new CakeRequest('some/path');
  693. $_SERVER['REQUEST_METHOD'] = 'GET';
  694. $this->assertTrue($request->is(array('get', 'post')));
  695. $_SERVER['REQUEST_METHOD'] = 'POST';
  696. $this->assertTrue($request->is(array('get', 'post')));
  697. $_SERVER['REQUEST_METHOD'] = 'PUT';
  698. $this->assertFalse($request->is(array('get', 'post')));
  699. }
  700. /**
  701. * Test isAll()
  702. *
  703. * @return void
  704. */
  705. public function testIsAll() {
  706. $request = new CakeRequest('some/path');
  707. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  708. $_SERVER['REQUEST_METHOD'] = 'GET';
  709. $this->assertTrue($request->isAll(array('ajax', 'get')));
  710. $this->assertFalse($request->isAll(array('post', 'get')));
  711. $this->assertFalse($request->isAll(array('ajax', 'post')));
  712. }
  713. /**
  714. * Test the method() method.
  715. *
  716. * @return void
  717. */
  718. public function testMethod() {
  719. $_SERVER['REQUEST_METHOD'] = 'delete';
  720. $request = new CakeRequest('some/path');
  721. $this->assertEquals('delete', $request->method());
  722. }
  723. /**
  724. * Test host retrieval.
  725. *
  726. * @return void
  727. */
  728. public function testHost() {
  729. $_SERVER['HTTP_HOST'] = 'localhost';
  730. $request = new CakeRequest('some/path');
  731. $this->assertEquals('localhost', $request->host());
  732. }
  733. /**
  734. * Test domain retrieval.
  735. *
  736. * @return void
  737. */
  738. public function testDomain() {
  739. $_SERVER['HTTP_HOST'] = 'something.example.com';
  740. $request = new CakeRequest('some/path');
  741. $this->assertEquals('example.com', $request->domain());
  742. $_SERVER['HTTP_HOST'] = 'something.example.co.uk';
  743. $this->assertEquals('example.co.uk', $request->domain(2));
  744. }
  745. /**
  746. * Test getting subdomains for a host.
  747. *
  748. * @return void
  749. */
  750. public function testSubdomain() {
  751. $_SERVER['HTTP_HOST'] = 'something.example.com';
  752. $request = new CakeRequest('some/path');
  753. $this->assertEquals(array('something'), $request->subdomains());
  754. $_SERVER['HTTP_HOST'] = 'www.something.example.com';
  755. $this->assertEquals(array('www', 'something'), $request->subdomains());
  756. $_SERVER['HTTP_HOST'] = 'www.something.example.co.uk';
  757. $this->assertEquals(array('www', 'something'), $request->subdomains(2));
  758. $_SERVER['HTTP_HOST'] = 'example.co.uk';
  759. $this->assertEquals(array(), $request->subdomains(2));
  760. }
  761. /**
  762. * Test ajax, flash and friends
  763. *
  764. * @return void
  765. */
  766. public function testisAjaxFlashAndFriends() {
  767. $request = new CakeRequest('some/path');
  768. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  769. $this->assertTrue($request->is('flash'));
  770. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
  771. $this->assertTrue($request->is('flash'));
  772. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  773. $this->assertTrue($request->is('ajax'));
  774. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
  775. $this->assertFalse($request->is('ajax'));
  776. $this->assertFalse($request->isAjax());
  777. $_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
  778. $this->assertTrue($request->is('mobile'));
  779. $this->assertTrue($request->isMobile());
  780. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b6pre) Gecko/20100902 Firefox/4.0b6pre Fennec/2.0b1pre';
  781. $this->assertTrue($request->is('mobile'));
  782. $this->assertTrue($request->isMobile());
  783. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; OMNIA7)';
  784. $this->assertTrue($request->is('mobile'));
  785. $this->assertTrue($request->isMobile());
  786. }
  787. /**
  788. * Test __call exceptions
  789. *
  790. * @expectedException CakeException
  791. * @return void
  792. */
  793. public function testMagicCallExceptionOnUnknownMethod() {
  794. $request = new CakeRequest('some/path');
  795. $request->IamABanana();
  796. }
  797. /**
  798. * Test is(ssl)
  799. *
  800. * @return void
  801. */
  802. public function testIsSsl() {
  803. $request = new CakeRequest('some/path');
  804. $_SERVER['HTTPS'] = 1;
  805. $this->assertTrue($request->is('ssl'));
  806. $_SERVER['HTTPS'] = 'on';
  807. $this->assertTrue($request->is('ssl'));
  808. $_SERVER['HTTPS'] = '1';
  809. $this->assertTrue($request->is('ssl'));
  810. $_SERVER['HTTPS'] = 'I am not empty';
  811. $this->assertTrue($request->is('ssl'));
  812. $_SERVER['HTTPS'] = 1;
  813. $this->assertTrue($request->is('ssl'));
  814. $_SERVER['HTTPS'] = 'off';
  815. $this->assertFalse($request->is('ssl'));
  816. $_SERVER['HTTPS'] = false;
  817. $this->assertFalse($request->is('ssl'));
  818. $_SERVER['HTTPS'] = '';
  819. $this->assertFalse($request->is('ssl'));
  820. }
  821. /**
  822. * Test getting request params with object properties.
  823. *
  824. * @return void
  825. */
  826. public function testMagicget() {
  827. $request = new CakeRequest('some/path');
  828. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  829. $this->assertEquals('posts', $request->controller);
  830. $this->assertEquals('view', $request->action);
  831. $this->assertEquals('blogs', $request->plugin);
  832. $this->assertSame($request->banana, null);
  833. }
  834. /**
  835. * Test isset()/empty() with overloaded properties.
  836. *
  837. * @return void
  838. */
  839. public function testMagicisset() {
  840. $request = new CakeRequest('some/path');
  841. $request->params = array(
  842. 'controller' => 'posts',
  843. 'action' => 'view',
  844. 'plugin' => 'blogs',
  845. 'named' => array()
  846. );
  847. $this->assertTrue(isset($request->controller));
  848. $this->assertFalse(isset($request->notthere));
  849. $this->assertFalse(empty($request->controller));
  850. $this->assertTrue(empty($request->named));
  851. }
  852. /**
  853. * Test the array access implementation
  854. *
  855. * @return void
  856. */
  857. public function testArrayAccess() {
  858. $request = new CakeRequest('some/path');
  859. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  860. $this->assertEquals('posts', $request['controller']);
  861. $request['slug'] = 'speedy-slug';
  862. $this->assertEquals('speedy-slug', $request->slug);
  863. $this->assertEquals('speedy-slug', $request['slug']);
  864. $this->assertTrue(isset($request['action']));
  865. $this->assertFalse(isset($request['wrong-param']));
  866. $this->assertTrue(isset($request['plugin']));
  867. unset($request['plugin']);
  868. $this->assertFalse(isset($request['plugin']));
  869. $this->assertNull($request['plugin']);
  870. $this->assertNull($request->plugin);
  871. $request = new CakeRequest('some/path?one=something&two=else');
  872. $this->assertTrue(isset($request['url']['one']));
  873. $request->data = array('Post' => array('title' => 'something'));
  874. $this->assertEquals('something', $request['data']['Post']['title']);
  875. }
  876. /**
  877. * Test adding detectors and having them work.
  878. *
  879. * @return void
  880. */
  881. public function testAddDetector() {
  882. $request = new CakeRequest('some/path');
  883. $request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
  884. $_SERVER['TEST_VAR'] = 'something';
  885. $this->assertTrue($request->is('compare'), 'Value match failed.');
  886. $_SERVER['TEST_VAR'] = 'wrong';
  887. $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
  888. $request->addDetector('compareCamelCase', array('env' => 'TEST_VAR', 'value' => 'foo'));
  889. $_SERVER['TEST_VAR'] = 'foo';
  890. $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
  891. $this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive');
  892. $this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  893. $_SERVER['TEST_VAR'] = 'not foo';
  894. $this->assertFalse($request->is('compareCamelCase'), 'Value match failed.');
  895. $this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive');
  896. $this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  897. $request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
  898. $_SERVER['TEST_VAR'] = 'banana';
  899. $this->assertTrue($request->isBanana());
  900. $_SERVER['TEST_VAR'] = 'wrong value';
  901. $this->assertFalse($request->isBanana());
  902. $request->addDetector('mobile', array('options' => array('Imagination')));
  903. $_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
  904. $this->assertTrue($request->isMobile());
  905. $_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
  906. $this->assertTrue($request->isMobile());
  907. $request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, 'detectCallback')));
  908. $request->addDetector('index', array('param' => 'action', 'value' => 'index'));
  909. $request->params['action'] = 'index';
  910. $this->assertTrue($request->isIndex());
  911. $request->params['action'] = 'add';
  912. $this->assertFalse($request->isIndex());
  913. $request->return = true;
  914. $this->assertTrue($request->isCallMe());
  915. $request->return = false;
  916. $this->assertFalse($request->isCallMe());
  917. $request->addDetector('extension', array('param' => 'ext', 'options' => array('pdf', 'png', 'txt')));
  918. $request->params['ext'] = 'pdf';
  919. $this->assertTrue($request->is('extension'));
  920. $request->params['ext'] = 'exe';
  921. $this->assertFalse($request->isExtension());
  922. }
  923. /**
  924. * Helper function for testing callbacks.
  925. *
  926. * @param $request
  927. * @return bool
  928. */
  929. public function detectCallback($request) {
  930. return (bool)$request->return;
  931. }
  932. /**
  933. * Test getting headers
  934. *
  935. * @return void
  936. */
  937. public function testHeader() {
  938. $_SERVER['HTTP_HOST'] = 'localhost';
  939. $_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';
  940. $request = new CakeRequest('/', false);
  941. $this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
  942. $this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
  943. }
  944. /**
  945. * Test accepts() with and without parameters
  946. *
  947. * @return void
  948. */
  949. public function testAccepts() {
  950. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
  951. $request = new CakeRequest('/', false);
  952. $result = $request->accepts();
  953. $expected = array(
  954. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
  955. );
  956. $this->assertEquals($expected, $result, 'Content types differ.');
  957. $result = $request->accepts('text/html');
  958. $this->assertTrue($result);
  959. $result = $request->accepts('image/gif');
  960. $this->assertFalse($result);
  961. }
  962. /**
  963. * Test that accept header types are trimmed for comparisons.
  964. *
  965. * @return void
  966. */
  967. public function testAcceptWithWhitespace() {
  968. $_SERVER['HTTP_ACCEPT'] = 'text/xml , text/html , text/plain,image/png';
  969. $request = new CakeRequest('/', false);
  970. $result = $request->accepts();
  971. $expected = array(
  972. 'text/xml', 'text/html', 'text/plain', 'image/png'
  973. );
  974. $this->assertEquals($expected, $result, 'Content types differ.');
  975. $this->assertTrue($request->accepts('text/html'));
  976. }
  977. /**
  978. * Content types from accepts() should respect the client's q preference values.
  979. *
  980. * @return void
  981. */
  982. public function testAcceptWithQvalueSorting() {
  983. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
  984. $request = new CakeRequest('/', false);
  985. $result = $request->accepts();
  986. $expected = array('application/xml', 'text/html', 'application/json');
  987. $this->assertEquals($expected, $result);
  988. }
  989. /**
  990. * Test the raw parsing of accept headers into the q value formatting.
  991. *
  992. * @return void
  993. */
  994. public function testParseAcceptWithQValue() {
  995. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
  996. $request = new CakeRequest('/', false);
  997. $result = $request->parseAccept();
  998. $expected = array(
  999. '1.0' => array('application/xml', 'image/png'),
  1000. '0.8' => array('text/html'),
  1001. '0.7' => array('application/json'),
  1002. );
  1003. $this->assertEquals($expected, $result);
  1004. }
  1005. /**
  1006. * Test parsing accept with a confusing accept value.
  1007. *
  1008. * @return void
  1009. */
  1010. public function testParseAcceptNoQValues() {
  1011. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*';
  1012. $request = new CakeRequest('/', false);
  1013. $result = $request->parseAccept();
  1014. $expected = array(
  1015. '1.0' => array('application/json', 'text/plain', '*/*'),
  1016. );
  1017. $this->assertEquals($expected, $result);
  1018. }
  1019. /**
  1020. * Test baseUrl and webroot with ModRewrite
  1021. *
  1022. * @return void
  1023. */
  1024. public function testBaseUrlAndWebrootWithModRewrite() {
  1025. Configure::write('App.baseUrl', false);
  1026. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  1027. $_SERVER['PHP_SELF'] = '/urlencode me/app/webroot/index.php';
  1028. $_SERVER['PATH_INFO'] = '/posts/view/1';
  1029. $request = new CakeRequest();
  1030. $this->assertEquals('/urlencode%20me', $request->base);
  1031. $this->assertEquals('/urlencode%20me/', $request->webroot);
  1032. $this->assertEquals('posts/view/1', $request->url);
  1033. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  1034. $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
  1035. $_SERVER['PATH_INFO'] = '/posts/view/1';
  1036. $request = new CakeRequest();
  1037. $this->assertEquals('/1.2.x.x', $request->base);
  1038. $this->assertEquals('/1.2.x.x/', $request->webroot);
  1039. $this->assertEquals('posts/view/1', $request->url);
  1040. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
  1041. $_SERVER['PHP_SELF'] = '/index.php';
  1042. $_SERVER['PATH_INFO'] = '/posts/add';
  1043. $request = new CakeRequest();
  1044. $this->assertEquals('', $request->base);
  1045. $this->assertEquals('/', $request->webroot);
  1046. $this->assertEquals('posts/add', $request->url);
  1047. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  1048. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  1049. $request = new CakeRequest();
  1050. $this->assertEquals('', $request->base);
  1051. $this->assertEquals('/', $request->webroot);
  1052. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  1053. $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
  1054. $request = new CakeRequest();
  1055. $this->assertEquals('', $request->base);
  1056. $this->assertEquals('/', $request->webroot);
  1057. Configure::write('App.dir', 'auth');
  1058. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  1059. $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
  1060. $request = new CakeRequest();
  1061. $this->assertEquals('/demos/auth', $request->base);
  1062. $this->assertEquals('/demos/auth/', $request->webroot);
  1063. Configure::write('App.dir', 'code');
  1064. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  1065. $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
  1066. $request = new CakeRequest();
  1067. $this->assertEquals('/clients/PewterReport/code', $request->base);
  1068. $this->assertEquals('/clients/PewterReport/code/', $request->webroot);
  1069. }
  1070. /**
  1071. * Test baseUrl with ModRewrite alias
  1072. *
  1073. * @return void
  1074. */
  1075. public function testBaseUrlwithModRewriteAlias() {
  1076. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  1077. $_SERVER['PHP_SELF'] = '/control/index.php';
  1078. Configure::write('App.base', '/control');
  1079. $request = new CakeRequest();
  1080. $this->assertEquals('/control', $request->base);
  1081. $this->assertEquals('/control/', $request->webroot);
  1082. Configure::write('App.base', false);
  1083. Configure::write('App.dir', 'affiliate');
  1084. Configure::write('App.webroot', 'newaffiliate');
  1085. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  1086. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  1087. $request = new CakeRequest();
  1088. $this->assertEquals('/newaffiliate', $request->base);
  1089. $this->assertEquals('/newaffiliate/', $request->webroot);
  1090. }
  1091. /**
  1092. * Test base, webroot, url and here parsing when there is url rewriting but
  1093. * CakePHP gets called with index.php in url nonetheless.
  1094. *
  1095. * Tests uri with
  1096. * - index.php/
  1097. * - index.php/
  1098. * - index.php/apples/
  1099. * - index.php/bananas/eat/tasty_banana
  1100. *
  1101. * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3318
  1102. */
  1103. public function testBaseUrlWithModRewriteAndIndexPhp() {
  1104. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php';
  1105. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php';
  1106. unset($_SERVER['PATH_INFO']);
  1107. $request = new CakeRequest();
  1108. $this->assertEquals('/cakephp', $request->base);
  1109. $this->assertEquals('/cakephp/', $request->webroot);
  1110. $this->assertEquals('', $request->url);
  1111. $this->assertEquals('/cakephp/', $request->here);
  1112. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/';
  1113. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/';
  1114. $_SERVER['PATH_INFO'] = '/';
  1115. $request = new CakeRequest();
  1116. $this->assertEquals('/cakephp', $request->base);
  1117. $this->assertEquals('/cakephp/', $request->webroot);
  1118. $this->assertEquals('', $request->url);
  1119. $this->assertEquals('/cakephp/', $request->here);
  1120. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/apples';
  1121. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/apples';
  1122. $_SERVER['PATH_INFO'] = '/apples';
  1123. $request = new CakeRequest();
  1124. $this->assertEquals('/cakephp', $request->base);
  1125. $this->assertEquals('/cakephp/', $request->webroot);
  1126. $this->assertEquals('apples', $request->url);
  1127. $this->assertEquals('/cakephp/apples', $request->here);
  1128. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/melons/share/';
  1129. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/melons/share/';
  1130. $_SERVER['PATH_INFO'] = '/melons/share/';
  1131. $request = new CakeRequest();
  1132. $this->assertEquals('/cakephp', $request->base);
  1133. $this->assertEquals('/cakephp/', $request->webroot);
  1134. $this->assertEquals('melons/share/', $request->url);
  1135. $this->assertEquals('/cakephp/melons/share/', $request->here);
  1136. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana';
  1137. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana';
  1138. $_SERVER['PATH_INFO'] = '/bananas/eat/tasty_banana';
  1139. $request = new CakeRequest();
  1140. $this->assertEquals('/cakephp', $request->base);
  1141. $this->assertEquals('/cakephp/', $request->webroot);
  1142. $this->assertEquals('bananas/eat/tasty_banana', $request->url);
  1143. $this->assertEquals('/cakephp/bananas/eat/tasty_banana', $request->here);
  1144. }
  1145. /**
  1146. * Test base, webroot, and url parsing when there is no url rewriting
  1147. *
  1148. * @return void
  1149. */
  1150. public function testBaseUrlWithNoModRewrite() {
  1151. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
  1152. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
  1153. $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
  1154. $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
  1155. Configure::write('App', array(
  1156. 'dir' => APP_DIR,
  1157. 'webroot' => WEBROOT_DIR,
  1158. 'base' => false,
  1159. 'baseUrl' => '/cake/index.php'
  1160. ));
  1161. $request = new CakeRequest();
  1162. $this->assertEquals('/cake/index.php', $request->base);
  1163. $this->assertEquals('/cake/app/webroot/', $request->webroot);
  1164. $this->assertEquals('posts/index', $request->url);
  1165. }
  1166. /**
  1167. * Test baseUrl and webroot with baseUrl
  1168. *
  1169. * @return void
  1170. */
  1171. public function testBaseUrlAndWebrootWithBaseUrl() {
  1172. Configure::write('App.dir', 'app');
  1173. Configure::write('App.baseUrl', '/app/webroot/index.php');
  1174. $request = new CakeRequest();
  1175. $this->assertEquals('/app/webroot/index.php', $request->base);
  1176. $this->assertEquals('/app/webroot/', $request->webroot);
  1177. Configure::write('App.baseUrl', '/app/webroot/test.php');
  1178. $request = new CakeRequest();
  1179. $this->assertEquals('/app/webroot/test.php', $request->base);
  1180. $this->assertEquals('/app/webroot/', $request->webroot);
  1181. Configure::write('App.baseUrl', '/app/index.php');
  1182. $request = new CakeRequest();
  1183. $this->assertEquals('/app/index.php', $request->base);
  1184. $this->assertEquals('/app/webroot/', $request->webroot);
  1185. Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
  1186. $request = new CakeRequest();
  1187. $this->assertEquals('/CakeBB/app/webroot/index.php', $request->base);
  1188. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1189. Configure::write('App.baseUrl', '/CakeBB/app/index.php');
  1190. $request = new CakeRequest();
  1191. $this->assertEquals('/CakeBB/app/index.php', $request->base);
  1192. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1193. Configure::write('App.baseUrl', '/CakeBB/index.php');
  1194. $request = new CakeRequest();
  1195. $this->assertEquals('/CakeBB/index.php', $request->base);
  1196. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1197. Configure::write('App.baseUrl', '/dbhauser/index.php');
  1198. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  1199. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  1200. $request = new CakeRequest();
  1201. $this->assertEquals('/dbhauser/index.php', $request->base);
  1202. $this->assertEquals('/dbhauser/app/webroot/', $request->webroot);
  1203. }
  1204. /**
  1205. * Test baseUrl with no rewrite and using the top level index.php.
  1206. *
  1207. * @return void
  1208. */
  1209. public function testBaseUrlNoRewriteTopLevelIndex() {
  1210. Configure::write('App.baseUrl', '/index.php');
  1211. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
  1212. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
  1213. $request = new CakeRequest();
  1214. $this->assertEquals('/index.php', $request->base);
  1215. $this->assertEquals('/app/webroot/', $request->webroot);
  1216. }
  1217. /**
  1218. * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
  1219. *
  1220. * @return void
  1221. */
  1222. public function testBaseUrlWithAppAndWebrootInDirname() {
  1223. Configure::write('App.baseUrl', '/approval/index.php');
  1224. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1225. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
  1226. $request = new CakeRequest();
  1227. $this->assertEquals('/approval/index.php', $request->base);
  1228. $this->assertEquals('/approval/app/webroot/', $request->webroot);
  1229. Configure::write('App.baseUrl', '/webrootable/index.php');
  1230. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1231. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
  1232. $request = new CakeRequest();
  1233. $this->assertEquals('/webrootable/index.php', $request->base);
  1234. $this->assertEquals('/webrootable/app/webroot/', $request->webroot);
  1235. }
  1236. /**
  1237. * Test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
  1238. *
  1239. * @return void
  1240. */
  1241. public function testBaseUrlNoRewriteWebrootIndex() {
  1242. Configure::write('App.baseUrl', '/index.php');
  1243. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
  1244. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
  1245. $request = new CakeRequest();
  1246. $this->assertEquals('/index.php', $request->base);
  1247. $this->assertEquals('/', $request->webroot);
  1248. }
  1249. /**
  1250. * Test that a request with a . in the main GET parameter is filtered out.
  1251. * PHP changes GET parameter keys containing dots to _.
  1252. *
  1253. * @return void
  1254. */
  1255. public function testGetParamsWithDot() {
  1256. $_GET = array();
  1257. $_GET['/posts/index/add_add'] = '';
  1258. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1259. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
  1260. $request = new CakeRequest();
  1261. $this->assertEquals(array(), $request->query);
  1262. }
  1263. /**
  1264. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  1265. *
  1266. * @return void
  1267. */
  1268. public function testGetParamWithUrlencodedElement() {
  1269. $_GET = array();
  1270. $_GET['/posts/add/∂∂'] = '';
  1271. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1272. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
  1273. $request = new CakeRequest();
  1274. $this->assertEquals(array(), $request->query);
  1275. }
  1276. /**
  1277. * Generator for environment configurations
  1278. *
  1279. * @return array Environment array
  1280. */
  1281. public static function environmentGenerator() {
  1282. return array(
  1283. array(
  1284. 'IIS - No rewrite base path',
  1285. array(
  1286. 'App' => array(
  1287. 'base' => false,
  1288. 'baseUrl' => '/index.php',
  1289. 'dir' => 'app',
  1290. 'webroot' => 'webroot'
  1291. ),
  1292. 'SERVER' => array(
  1293. 'SCRIPT_NAME' => '/index.php',
  1294. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1295. 'QUERY_STRING' => '',
  1296. 'REQUEST_URI' => '/index.php',
  1297. 'URL' => '/index.php',
  1298. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  1299. 'ORIG_PATH_INFO' => '/index.php',
  1300. 'PATH_INFO' => '',
  1301. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  1302. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1303. 'PHP_SELF' => '/index.php',
  1304. ),
  1305. ),
  1306. array(
  1307. 'base' => '/index.php',
  1308. 'webroot' => '/app/webroot/',
  1309. 'url' => ''
  1310. ),
  1311. ),
  1312. array(
  1313. 'IIS - No rewrite with path, no PHP_SELF',
  1314. array(
  1315. 'App' => array(
  1316. 'base' => false,
  1317. 'baseUrl' => '/index.php?',
  1318. 'dir' => 'app',
  1319. 'webroot' => 'webroot'
  1320. ),
  1321. 'SERVER' => array(
  1322. 'QUERY_STRING' => '/posts/add',
  1323. 'REQUEST_URI' => '/index.php?/posts/add',
  1324. 'PHP_SELF' => '',
  1325. 'URL' => '/index.php?/posts/add',
  1326. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1327. 'argv' => array('/posts/add'),
  1328. 'argc' => 1
  1329. ),
  1330. ),
  1331. array(
  1332. 'url' => 'posts/add',
  1333. 'base' => '/index.php?',
  1334. 'webroot' => '/app/webroot/'
  1335. )
  1336. ),
  1337. array(
  1338. 'IIS - No rewrite sub dir 2',
  1339. array(
  1340. 'App' => array(
  1341. 'base' => false,
  1342. 'baseUrl' => '/site/index.php',
  1343. 'dir' => 'app',
  1344. 'webroot' => 'webroot',
  1345. ),
  1346. 'SERVER' => array(
  1347. 'SCRIPT_NAME' => '/site/index.php',
  1348. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1349. 'QUERY_STRING' => '',
  1350. 'REQUEST_URI' => '/site/index.php',
  1351. 'URL' => '/site/index.php',
  1352. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1353. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1354. 'PHP_SELF' => '/site/index.php',
  1355. 'argv' => array(),
  1356. 'argc' => 0
  1357. ),
  1358. ),
  1359. array(
  1360. 'url' => '',
  1361. 'base' => '/site/index.php',
  1362. 'webroot' => '/site/app/webroot/'
  1363. ),
  1364. ),
  1365. array(
  1366. 'IIS - No rewrite sub dir 2 with path',
  1367. array(
  1368. 'App' => array(
  1369. 'base' => false,
  1370. 'baseUrl' => '/site/index.php',
  1371. 'dir' => 'app',
  1372. 'webroot' => 'webroot'
  1373. ),
  1374. 'GET' => array('/posts/add' => ''),
  1375. 'SERVER' => array(
  1376. 'SCRIPT_NAME' => '/site/index.php',
  1377. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1378. 'QUERY_STRING' => '/posts/add',
  1379. 'REQUEST_URI' => '/site/index.php/posts/add',
  1380. 'URL' => '/site/index.php/posts/add',
  1381. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1382. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1383. 'PHP_SELF' => '/site/index.php/posts/add',
  1384. 'argv' => array('/posts/add'),
  1385. 'argc' => 1
  1386. ),
  1387. ),
  1388. array(
  1389. 'url' => 'posts/add',
  1390. 'base' => '/site/index.php',
  1391. 'webroot' => '/site/app/webroot/'
  1392. )
  1393. ),
  1394. array(
  1395. 'Apache - No rewrite, document root set to webroot, requesting path',
  1396. array(
  1397. 'App' => array(
  1398. 'base' => false,
  1399. 'baseUrl' => '/index.php',
  1400. 'dir' => 'app',
  1401. 'webroot' => 'webroot'
  1402. ),
  1403. 'SERVER' => array(
  1404. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1405. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1406. 'QUERY_STRING' => '',
  1407. 'REQUEST_URI' => '/index.php/posts/index',
  1408. 'SCRIPT_NAME' => '/index.php',
  1409. 'PATH_INFO' => '/posts/index',
  1410. 'PHP_SELF' => '/index.php/posts/index',
  1411. ),
  1412. ),
  1413. array(
  1414. 'url' => 'posts/index',
  1415. 'base' => '/index.php',
  1416. 'webroot' => '/'
  1417. ),
  1418. ),
  1419. array(
  1420. 'Apache - No rewrite, document root set to webroot, requesting root',
  1421. array(
  1422. 'App' => array(
  1423. 'base' => false,
  1424. 'baseUrl' => '/index.php',
  1425. 'dir' => 'app',
  1426. 'webroot' => 'webroot'
  1427. ),
  1428. 'SERVER' => array(
  1429. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1430. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1431. 'QUERY_STRING' => '',
  1432. 'REQUEST_URI' => '/index.php',
  1433. 'SCRIPT_NAME' => '/index.php',
  1434. 'PATH_INFO' => '',
  1435. 'PHP_SELF' => '/index.php',
  1436. ),
  1437. ),
  1438. array(
  1439. 'url' => '',
  1440. 'base' => '/index.php',
  1441. 'webroot' => '/'
  1442. ),
  1443. ),
  1444. array(
  1445. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1446. array(
  1447. 'App' => array(
  1448. 'base' => false,
  1449. 'baseUrl' => '/site/index.php',
  1450. 'dir' => 'app',
  1451. 'webroot' => 'webroot'
  1452. ),
  1453. 'SERVER' => array(
  1454. 'SERVER_NAME' => 'localhost',
  1455. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1456. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1457. 'REQUEST_URI' => '/site/index.php/posts/index',
  1458. 'SCRIPT_NAME' => '/site/index.php',
  1459. 'PATH_INFO' => '/posts/index',
  1460. 'PHP_SELF' => '/site/index.php/posts/index',
  1461. ),
  1462. ),
  1463. array(
  1464. 'url' => 'posts/index',
  1465. 'base' => '/site/index.php',
  1466. 'webroot' => '/site/app/webroot/',
  1467. ),
  1468. ),
  1469. array(
  1470. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1471. array(
  1472. 'App' => array(
  1473. 'base' => false,
  1474. 'baseUrl' => '/site/index.php',
  1475. 'dir' => 'app',
  1476. 'webroot' => 'webroot'
  1477. ),
  1478. 'SERVER' => array(
  1479. 'SERVER_NAME' => 'localhost',
  1480. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1481. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1482. 'REQUEST_URI' => '/site/index.php/',
  1483. 'SCRIPT_NAME' => '/site/index.php',
  1484. 'PHP_SELF' => '/site/index.php/',
  1485. ),
  1486. ),
  1487. array(
  1488. 'url' => '',
  1489. 'base' => '/site/index.php',
  1490. 'webroot' => '/site/app/webroot/',
  1491. ),
  1492. ),
  1493. array(
  1494. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1495. array(
  1496. 'App' => array(
  1497. 'base' => false,
  1498. 'baseUrl' => '/site/index.php',
  1499. 'dir' => 'app',
  1500. 'webroot' => 'webroot'
  1501. ),
  1502. 'GET' => array('a' => 'b', 'c' => 'd'),
  1503. 'SERVER' => array(
  1504. 'SERVER_NAME' => 'localhost',
  1505. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1506. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1507. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1508. 'SCRIPT_NAME' => '/site/index.php',
  1509. 'PATH_INFO' => '/posts/index',
  1510. 'PHP_SELF' => '/site/index.php/posts/index',
  1511. 'QUERY_STRING' => 'a=b&c=d'
  1512. ),
  1513. ),
  1514. array(
  1515. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1516. 'url' => 'posts/index',
  1517. 'base' => '/site/index.php',
  1518. 'webroot' => '/site/app/webroot/',
  1519. ),
  1520. ),
  1521. array(
  1522. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1523. array(
  1524. 'App' => array(
  1525. 'base' => false,
  1526. 'baseUrl' => false,
  1527. 'dir' => 'app',
  1528. 'webroot' => 'webroot'
  1529. ),
  1530. 'SERVER' => array(
  1531. 'SERVER_NAME' => 'localhost',
  1532. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1533. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1534. 'REQUEST_URI' => '/site/',
  1535. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1536. 'PHP_SELF' => '/site/app/webroot/index.php',
  1537. ),
  1538. ),
  1539. array(
  1540. 'url' => '',
  1541. 'base' => '/site',
  1542. 'webroot' => '/site/',
  1543. ),
  1544. ),
  1545. array(
  1546. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  1547. array(
  1548. 'App' => array(
  1549. 'base' => false,
  1550. 'baseUrl' => false,
  1551. 'dir' => 'app',
  1552. 'webroot' => 'webroot'
  1553. ),
  1554. 'SERVER' => array(
  1555. 'SERVER_NAME' => 'localhost',
  1556. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1557. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1558. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1559. 'PHP_SELF' => '/site/app/webroot/index.php',
  1560. 'PATH_INFO' => null,
  1561. 'REQUEST_URI' => null,
  1562. ),
  1563. ),
  1564. array(
  1565. 'url' => '',
  1566. 'base' => '/site',
  1567. 'webroot' => '/site/',
  1568. ),
  1569. ),
  1570. array(
  1571. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1572. array(
  1573. 'App' => array(
  1574. 'base' => false,
  1575. 'baseUrl' => false,
  1576. 'dir' => 'app',
  1577. 'webroot' => 'webroot'
  1578. ),
  1579. 'SERVER' => array(
  1580. 'SERVER_NAME' => 'localhost',
  1581. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1582. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1583. 'SCRIPT_NAME' => '/index.php',
  1584. 'PHP_SELF' => '/index.php',
  1585. 'PATH_INFO' => null,
  1586. 'REQUEST_URI' => null,
  1587. ),
  1588. ),
  1589. array(
  1590. 'url' => '',
  1591. 'base' => '',
  1592. 'webroot' => '/',
  1593. ),
  1594. ),
  1595. array(
  1596. 'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
  1597. array(
  1598. 'App' => array(
  1599. 'base' => false,
  1600. 'baseUrl' => false,
  1601. 'dir' => 'app',
  1602. 'webroot' => 'webroot'
  1603. ),
  1604. 'SERVER' => array(
  1605. 'SERVER_NAME' => 'localhost',
  1606. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1607. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1608. 'REQUEST_URI' => '/site/posts/index',
  1609. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1610. 'PHP_SELF' => '/site/app/webroot/index.php',
  1611. ),
  1612. ),
  1613. array(
  1614. 'url' => 'posts/index',
  1615. 'base' => '/site',
  1616. 'webroot' => '/site/',
  1617. ),
  1618. ),
  1619. array(
  1620. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  1621. array(
  1622. 'App' => array(
  1623. 'base' => false,
  1624. 'baseUrl' => false,
  1625. 'dir' => 'app',
  1626. 'webroot' => 'webroot'
  1627. ),
  1628. 'GET' => array('/posts/add' => ''),
  1629. 'SERVER' => array(
  1630. 'SERVER_NAME' => 'localhost',
  1631. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1632. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1633. 'SCRIPT_NAME' => '/index.php',
  1634. 'QUERY_STRING' => '/posts/add&',
  1635. 'PHP_SELF' => '/index.php',
  1636. 'PATH_INFO' => null,
  1637. 'REQUEST_URI' => '/posts/add',
  1638. ),
  1639. ),
  1640. array(
  1641. 'url' => 'posts/add',
  1642. 'base' => '',
  1643. 'webroot' => '/',
  1644. 'urlParams' => array()
  1645. ),
  1646. ),
  1647. );
  1648. }
  1649. /**
  1650. * Test environment detection
  1651. *
  1652. * @dataProvider environmentGenerator
  1653. * @param $name
  1654. * @param $env
  1655. * @param $expected
  1656. * @return void
  1657. */
  1658. public function testEnvironmentDetection($name, $env, $expected) {
  1659. $_GET = array();
  1660. $this->_loadEnvironment($env);
  1661. $request = new CakeRequest();
  1662. $this->assertEquals($expected['url'], $request->url, "url error");
  1663. $this->assertEquals($expected['base'], $request->base, "base error");
  1664. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1665. if (isset($expected['urlParams'])) {
  1666. $this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch");
  1667. }
  1668. }
  1669. /**
  1670. * Test the query() method
  1671. *
  1672. * @return void
  1673. */
  1674. public function testQuery() {
  1675. $_GET = array();
  1676. $_GET['foo'] = 'bar';
  1677. $request = new CakeRequest();
  1678. $result = $request->query('foo');
  1679. $this->assertEquals('bar', $result);
  1680. $result = $request->query('imaginary');
  1681. $this->assertNull($result);
  1682. }
  1683. /**
  1684. * Test the query() method with arrays passed via $_GET
  1685. *
  1686. * @return void
  1687. */
  1688. public function testQueryWithArray() {
  1689. $_GET = array();
  1690. $_GET['test'] = array('foo', 'bar');
  1691. $request = new CakeRequest();
  1692. $result = $request->query('test');
  1693. $this->assertEquals(array('foo', 'bar'), $result);
  1694. $result = $request->query('test.1');
  1695. $this->assertEquals('bar', $result);
  1696. $result = $request->query('test.2');
  1697. $this->assertNull($result);
  1698. }
  1699. /**
  1700. * Test using param()
  1701. *
  1702. * @return void
  1703. */
  1704. public function testReadingParams() {
  1705. $request = new CakeRequest();
  1706. $request->addParams(array(
  1707. 'controller' => 'posts',
  1708. 'admin' => true,
  1709. 'truthy' => 1,
  1710. 'zero' => '0',
  1711. ));
  1712. $this->assertFalse($request->param('not_set'));
  1713. $this->assertTrue($request->param('admin'));
  1714. $this->assertEquals(1, $request->param('truthy'));
  1715. $this->assertEquals('posts', $request->param('controller'));
  1716. $this->assertEquals('0', $request->param('zero'));
  1717. }
  1718. /**
  1719. * Test the data() method reading
  1720. *
  1721. * @return void
  1722. */
  1723. public function testDataReading() {
  1724. $_POST['data'] = array(
  1725. 'Model' => array(
  1726. 'field' => 'value'
  1727. )
  1728. );
  1729. $request = new CakeRequest('posts/index');
  1730. $result = $request->data('Model');
  1731. $this->assertEquals($_POST['data']['Model'], $result);
  1732. $result = $request->data('Model.imaginary');
  1733. $this->assertNull($result);
  1734. }
  1735. /**
  1736. * Test writing with data()
  1737. *
  1738. * @return void
  1739. */
  1740. public function testDataWriting() {
  1741. $_POST['data'] = array(
  1742. 'Model' => array(
  1743. 'field' => 'value'
  1744. )
  1745. );
  1746. $request = new CakeRequest('posts/index');
  1747. $result = $request->data('Model.new_value', 'new value');
  1748. $this->assertSame($result, $request, 'Return was not $this');
  1749. $this->assertEquals('new value', $request->data['Model']['new_value']);
  1750. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1751. $this->assertEquals('New post', $request->data['Post']['title']);
  1752. $this->assertEquals('Mark', $request->data['Comment']['1']['author']);
  1753. }
  1754. /**
  1755. * Test writing falsey values.
  1756. *
  1757. * @return void
  1758. */
  1759. public function testDataWritingFalsey() {
  1760. $request = new CakeRequest('posts/index');
  1761. $request->data('Post.null', null);
  1762. $this->assertNull($request->data['Post']['null']);
  1763. $request->data('Post.false', false);
  1764. $this->assertFalse($request->data['Post']['false']);
  1765. $request->data('Post.zero', 0);
  1766. $this->assertSame(0, $request->data['Post']['zero']);
  1767. $request->data('Post.empty', '');
  1768. $this->assertSame('', $request->data['Post']['empty']);
  1769. }
  1770. /**
  1771. * Test accept language
  1772. *
  1773. * @return void
  1774. */
  1775. public function testAcceptLanguage() {
  1776. // Weird language
  1777. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
  1778. $result = CakeRequest::acceptLanguage();
  1779. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1780. // No qualifier
  1781. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1782. $result = CakeRequest::acceptLanguage();
  1783. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1784. // With qualifier
  1785. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
  1786. $result = CakeRequest::acceptLanguage();
  1787. $this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
  1788. // With spaces
  1789. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
  1790. $result = CakeRequest::acceptLanguage();
  1791. $this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
  1792. // Checking if requested
  1793. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1794. $result = CakeRequest::acceptLanguage();
  1795. $result = CakeRequest::acceptLanguage('en-ca');
  1796. $this->assertTrue($result);
  1797. $result = CakeRequest::acceptLanguage('en-CA');
  1798. $this->assertTrue($result);
  1799. $result = CakeRequest::acceptLanguage('en-us');
  1800. $this->assertFalse($result);
  1801. $result = CakeRequest::acceptLanguage('en-US');
  1802. $this->assertFalse($result);
  1803. }
  1804. /**
  1805. * Test the here() method
  1806. *
  1807. * @return void
  1808. */
  1809. public function testHere() {
  1810. Configure::write('App.base', '/base_path');
  1811. $_GET = array('test' => 'value');
  1812. $request = new CakeRequest('/posts/add/1/name:value');
  1813. $result = $request->here();
  1814. $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
  1815. $result = $request->here(false);
  1816. $this->assertEquals('/posts/add/1/name:value?test=value', $result);
  1817. $request = new CakeRequest('/posts/base_path/1/name:value');
  1818. $result = $request->here();
  1819. $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
  1820. $result = $request->here(false);
  1821. $this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
  1822. }
  1823. /**
  1824. * Test the input() method.
  1825. *
  1826. * @return void
  1827. */
  1828. public function testInput() {
  1829. $request = $this->getMock('CakeRequest', array('_readInput'));
  1830. $request->expects($this->once())->method('_readInput')
  1831. ->will($this->returnValue('I came from stdin'));
  1832. $result = $request->input();
  1833. $this->assertEquals('I came from stdin', $result);
  1834. }
  1835. /**
  1836. * Test input() decoding.
  1837. *
  1838. * @return void
  1839. */
  1840. public function testInputDecode() {
  1841. $request = $this->getMock('CakeRequest', array('_readInput'));
  1842. $request->expects($this->once())->method('_readInput')
  1843. ->will($this->returnValue('{"name":"value"}'));
  1844. $result = $request->input('json_decode');
  1845. $this->assertEquals(array('name' => 'value'), (array)$result);
  1846. }
  1847. /**
  1848. * Test input() decoding with additional arguments.
  1849. *
  1850. * @return void
  1851. */
  1852. public function testInputDecodeExtraParams() {
  1853. $xml = <<<XML
  1854. <?xml version="1.0" encoding="utf-8"?>
  1855. <post>
  1856. <title id="title">Test</title>
  1857. </post>
  1858. XML;
  1859. $request = $this->getMock('CakeRequest', array('_readInput'));
  1860. $request->expects($this->once())->method('_readInput')
  1861. ->will($this->returnValue($xml));
  1862. $result = $request->input('Xml::build', array('return' => 'domdocument'));
  1863. $this->assertInstanceOf('DOMDocument', $result);
  1864. $this->assertEquals(
  1865. 'Test',
  1866. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1867. );
  1868. }
  1869. /**
  1870. * Test is('requested') and isRequested()
  1871. *
  1872. * @return void
  1873. */
  1874. public function testIsRequested() {
  1875. $request = new CakeRequest('/posts/index');
  1876. $request->addParams(array(
  1877. 'controller' => 'posts',
  1878. 'action' => 'index',
  1879. 'plugin' => null,
  1880. 'requested' => 1
  1881. ));
  1882. $this->assertTrue($request->is('requested'));
  1883. $this->assertTrue($request->isRequested());
  1884. $request = new CakeRequest('/posts/index');
  1885. $request->addParams(array(
  1886. 'controller' => 'posts',
  1887. 'action' => 'index',
  1888. 'plugin' => null,
  1889. ));
  1890. $this->assertFalse($request->is('requested'));
  1891. $this->assertFalse($request->isRequested());
  1892. }
  1893. /**
  1894. * Test onlyAllow method
  1895. *
  1896. * @return void
  1897. */
  1898. public function testOnlyAllow() {
  1899. $_SERVER['REQUEST_METHOD'] = 'PUT';
  1900. $request = new CakeRequest('/posts/edit/1');
  1901. $this->assertTrue($request->onlyAllow(array('put')));
  1902. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  1903. $this->assertTrue($request->onlyAllow('post', 'delete'));
  1904. }
  1905. /**
  1906. * Test onlyAllow throwing exception
  1907. *
  1908. * @return void
  1909. */
  1910. public function testOnlyAllowException() {
  1911. $_SERVER['REQUEST_METHOD'] = 'PUT';
  1912. $request = new CakeRequest('/posts/edit/1');
  1913. try {
  1914. $request->onlyAllow('POST', 'DELETE');
  1915. $this->fail('An expected exception has not been raised.');
  1916. } catch (MethodNotAllowedException $e) {
  1917. $this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
  1918. }
  1919. $this->setExpectedException('MethodNotAllowedException');
  1920. $request->onlyAllow('POST');
  1921. }
  1922. /**
  1923. * loadEnvironment method
  1924. *
  1925. * @param array $env
  1926. * @return void
  1927. */
  1928. protected function _loadEnvironment($env) {
  1929. if (isset($env['App'])) {
  1930. Configure::write('App', $env['App']);
  1931. }
  1932. if (isset($env['GET'])) {
  1933. foreach ($env['GET'] as $key => $val) {
  1934. $_GET[$key] = $val;
  1935. }
  1936. }
  1937. if (isset($env['POST'])) {
  1938. foreach ($env['POST'] as $key => $val) {
  1939. $_POST[$key] = $val;
  1940. }
  1941. }
  1942. if (isset($env['SERVER'])) {
  1943. foreach ($env['SERVER'] as $key => $val) {
  1944. $_SERVER[$key] = $val;
  1945. }
  1946. }
  1947. }
  1948. }