CakeRequestTest.php 62 KB

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