CakeRequestTest.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  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. }
  912. /**
  913. * Helper function for testing callbacks.
  914. *
  915. * @param $request
  916. * @return boolean
  917. */
  918. public function detectCallback($request) {
  919. return (bool)$request->return;
  920. }
  921. /**
  922. * Test getting headers
  923. *
  924. * @return void
  925. */
  926. public function testHeader() {
  927. $_SERVER['HTTP_HOST'] = 'localhost';
  928. $_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';
  929. $request = new CakeRequest('/', false);
  930. $this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
  931. $this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
  932. }
  933. /**
  934. * Test accepts() with and without parameters
  935. *
  936. * @return void
  937. */
  938. public function testAccepts() {
  939. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
  940. $request = new CakeRequest('/', false);
  941. $result = $request->accepts();
  942. $expected = array(
  943. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
  944. );
  945. $this->assertEquals($expected, $result, 'Content types differ.');
  946. $result = $request->accepts('text/html');
  947. $this->assertTrue($result);
  948. $result = $request->accepts('image/gif');
  949. $this->assertFalse($result);
  950. }
  951. /**
  952. * Test that accept header types are trimmed for comparisons.
  953. *
  954. * @return void
  955. */
  956. public function testAcceptWithWhitespace() {
  957. $_SERVER['HTTP_ACCEPT'] = 'text/xml , text/html , text/plain,image/png';
  958. $request = new CakeRequest('/', false);
  959. $result = $request->accepts();
  960. $expected = array(
  961. 'text/xml', 'text/html', 'text/plain', 'image/png'
  962. );
  963. $this->assertEquals($expected, $result, 'Content types differ.');
  964. $this->assertTrue($request->accepts('text/html'));
  965. }
  966. /**
  967. * Content types from accepts() should respect the client's q preference values.
  968. *
  969. * @return void
  970. */
  971. public function testAcceptWithQvalueSorting() {
  972. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
  973. $request = new CakeRequest('/', false);
  974. $result = $request->accepts();
  975. $expected = array('application/xml', 'text/html', 'application/json');
  976. $this->assertEquals($expected, $result);
  977. }
  978. /**
  979. * Test the raw parsing of accept headers into the q value formatting.
  980. *
  981. * @return void
  982. */
  983. public function testParseAcceptWithQValue() {
  984. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
  985. $request = new CakeRequest('/', false);
  986. $result = $request->parseAccept();
  987. $expected = array(
  988. '1.0' => array('application/xml', 'image/png'),
  989. '0.8' => array('text/html'),
  990. '0.7' => array('application/json'),
  991. );
  992. $this->assertEquals($expected, $result);
  993. }
  994. /**
  995. * Test parsing accept with a confusing accept value.
  996. *
  997. * @return void
  998. */
  999. public function testParseAcceptNoQValues() {
  1000. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*';
  1001. $request = new CakeRequest('/', false);
  1002. $result = $request->parseAccept();
  1003. $expected = array(
  1004. '1.0' => array('application/json', 'text/plain', '*/*'),
  1005. );
  1006. $this->assertEquals($expected, $result);
  1007. }
  1008. /**
  1009. * Test parsing accept ignores index param
  1010. *
  1011. * @return void
  1012. */
  1013. public function testParseAcceptIgnoreAcceptExtensions() {
  1014. $_SERVER['HTTP_ACCEPT'] = 'application/json;level=1, text/plain, */*';
  1015. $request = new CakeRequest('/', false);
  1016. $result = $request->parseAccept();
  1017. $expected = array(
  1018. '1.0' => array('application/json', 'text/plain', '*/*'),
  1019. );
  1020. $this->assertEquals($expected, $result);
  1021. }
  1022. /**
  1023. * Test that parsing accept headers with invalid syntax works.
  1024. *
  1025. * The header used is missing a q value for application/xml.
  1026. *
  1027. * @return void
  1028. */
  1029. public function testParseAcceptInvalidSyntax() {
  1030. $_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
  1031. $request = new CakeRequest('/', false);
  1032. $result = $request->parseAccept();
  1033. $expected = array(
  1034. '1.0' => array('text/html', 'application/xhtml+xml', 'application/xml', 'image/jpeg'),
  1035. '0.9' => array('image/*'),
  1036. '0.8' => array('*/*'),
  1037. );
  1038. $this->assertEquals($expected, $result);
  1039. }
  1040. /**
  1041. * Test baseUrl and webroot with ModRewrite
  1042. *
  1043. * @return void
  1044. */
  1045. public function testBaseUrlAndWebrootWithModRewrite() {
  1046. Configure::write('App.baseUrl', false);
  1047. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  1048. $_SERVER['PHP_SELF'] = '/urlencode me/app/webroot/index.php';
  1049. $_SERVER['PATH_INFO'] = '/posts/view/1';
  1050. $request = new CakeRequest();
  1051. $this->assertEquals('/urlencode%20me', $request->base);
  1052. $this->assertEquals('/urlencode%20me/', $request->webroot);
  1053. $this->assertEquals('posts/view/1', $request->url);
  1054. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  1055. $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
  1056. $_SERVER['PATH_INFO'] = '/posts/view/1';
  1057. $request = new CakeRequest();
  1058. $this->assertEquals('/1.2.x.x', $request->base);
  1059. $this->assertEquals('/1.2.x.x/', $request->webroot);
  1060. $this->assertEquals('posts/view/1', $request->url);
  1061. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
  1062. $_SERVER['PHP_SELF'] = '/index.php';
  1063. $_SERVER['PATH_INFO'] = '/posts/add';
  1064. $request = new CakeRequest();
  1065. $this->assertEquals('', $request->base);
  1066. $this->assertEquals('/', $request->webroot);
  1067. $this->assertEquals('posts/add', $request->url);
  1068. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  1069. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  1070. $request = new CakeRequest();
  1071. $this->assertEquals('', $request->base);
  1072. $this->assertEquals('/', $request->webroot);
  1073. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  1074. $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
  1075. $request = new CakeRequest();
  1076. $this->assertEquals('', $request->base);
  1077. $this->assertEquals('/', $request->webroot);
  1078. Configure::write('App.dir', 'auth');
  1079. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  1080. $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
  1081. $request = new CakeRequest();
  1082. $this->assertEquals('/demos/auth', $request->base);
  1083. $this->assertEquals('/demos/auth/', $request->webroot);
  1084. Configure::write('App.dir', 'code');
  1085. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  1086. $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
  1087. $request = new CakeRequest();
  1088. $this->assertEquals('/clients/PewterReport/code', $request->base);
  1089. $this->assertEquals('/clients/PewterReport/code/', $request->webroot);
  1090. }
  1091. /**
  1092. * Test baseUrl with ModRewrite alias
  1093. *
  1094. * @return void
  1095. */
  1096. public function testBaseUrlwithModRewriteAlias() {
  1097. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  1098. $_SERVER['PHP_SELF'] = '/control/index.php';
  1099. Configure::write('App.base', '/control');
  1100. $request = new CakeRequest();
  1101. $this->assertEquals('/control', $request->base);
  1102. $this->assertEquals('/control/', $request->webroot);
  1103. Configure::write('App.base', false);
  1104. Configure::write('App.dir', 'affiliate');
  1105. Configure::write('App.webroot', 'newaffiliate');
  1106. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  1107. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  1108. $request = new CakeRequest();
  1109. $this->assertEquals('/newaffiliate', $request->base);
  1110. $this->assertEquals('/newaffiliate/', $request->webroot);
  1111. }
  1112. /**
  1113. * Test base, webroot, URL and here parsing when there is URL rewriting but
  1114. * CakePHP gets called with index.php in URL nonetheless.
  1115. *
  1116. * Tests uri with
  1117. * - index.php/
  1118. * - index.php/
  1119. * - index.php/apples/
  1120. * - index.php/bananas/eat/tasty_banana
  1121. *
  1122. * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3318
  1123. */
  1124. public function testBaseUrlWithModRewriteAndIndexPhp() {
  1125. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php';
  1126. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php';
  1127. unset($_SERVER['PATH_INFO']);
  1128. $request = new CakeRequest();
  1129. $this->assertEquals('/cakephp', $request->base);
  1130. $this->assertEquals('/cakephp/', $request->webroot);
  1131. $this->assertEquals('', $request->url);
  1132. $this->assertEquals('/cakephp/', $request->here);
  1133. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/';
  1134. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/';
  1135. $_SERVER['PATH_INFO'] = '/';
  1136. $request = new CakeRequest();
  1137. $this->assertEquals('/cakephp', $request->base);
  1138. $this->assertEquals('/cakephp/', $request->webroot);
  1139. $this->assertEquals('', $request->url);
  1140. $this->assertEquals('/cakephp/', $request->here);
  1141. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/apples';
  1142. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/apples';
  1143. $_SERVER['PATH_INFO'] = '/apples';
  1144. $request = new CakeRequest();
  1145. $this->assertEquals('/cakephp', $request->base);
  1146. $this->assertEquals('/cakephp/', $request->webroot);
  1147. $this->assertEquals('apples', $request->url);
  1148. $this->assertEquals('/cakephp/apples', $request->here);
  1149. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/melons/share/';
  1150. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/melons/share/';
  1151. $_SERVER['PATH_INFO'] = '/melons/share/';
  1152. $request = new CakeRequest();
  1153. $this->assertEquals('/cakephp', $request->base);
  1154. $this->assertEquals('/cakephp/', $request->webroot);
  1155. $this->assertEquals('melons/share/', $request->url);
  1156. $this->assertEquals('/cakephp/melons/share/', $request->here);
  1157. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana';
  1158. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana';
  1159. $_SERVER['PATH_INFO'] = '/bananas/eat/tasty_banana';
  1160. $request = new CakeRequest();
  1161. $this->assertEquals('/cakephp', $request->base);
  1162. $this->assertEquals('/cakephp/', $request->webroot);
  1163. $this->assertEquals('bananas/eat/tasty_banana', $request->url);
  1164. $this->assertEquals('/cakephp/bananas/eat/tasty_banana', $request->here);
  1165. }
  1166. /**
  1167. * Test base, webroot, and URL parsing when there is no URL rewriting
  1168. *
  1169. * @return void
  1170. */
  1171. public function testBaseUrlWithNoModRewrite() {
  1172. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
  1173. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
  1174. $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
  1175. $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
  1176. Configure::write('App', array(
  1177. 'dir' => APP_DIR,
  1178. 'webroot' => WEBROOT_DIR,
  1179. 'base' => false,
  1180. 'baseUrl' => '/cake/index.php'
  1181. ));
  1182. $request = new CakeRequest();
  1183. $this->assertEquals('/cake/index.php', $request->base);
  1184. $this->assertEquals('/cake/app/webroot/', $request->webroot);
  1185. $this->assertEquals('posts/index', $request->url);
  1186. }
  1187. /**
  1188. * Test baseUrl and webroot with baseUrl
  1189. *
  1190. * @return void
  1191. */
  1192. public function testBaseUrlAndWebrootWithBaseUrl() {
  1193. Configure::write('App.dir', 'app');
  1194. Configure::write('App.baseUrl', '/app/webroot/index.php');
  1195. $request = new CakeRequest();
  1196. $this->assertEquals('/app/webroot/index.php', $request->base);
  1197. $this->assertEquals('/app/webroot/', $request->webroot);
  1198. Configure::write('App.baseUrl', '/app/webroot/test.php');
  1199. $request = new CakeRequest();
  1200. $this->assertEquals('/app/webroot/test.php', $request->base);
  1201. $this->assertEquals('/app/webroot/', $request->webroot);
  1202. Configure::write('App.baseUrl', '/app/index.php');
  1203. $request = new CakeRequest();
  1204. $this->assertEquals('/app/index.php', $request->base);
  1205. $this->assertEquals('/app/webroot/', $request->webroot);
  1206. Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
  1207. $request = new CakeRequest();
  1208. $this->assertEquals('/CakeBB/app/webroot/index.php', $request->base);
  1209. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1210. Configure::write('App.baseUrl', '/CakeBB/app/index.php');
  1211. $request = new CakeRequest();
  1212. $this->assertEquals('/CakeBB/app/index.php', $request->base);
  1213. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1214. Configure::write('App.baseUrl', '/CakeBB/index.php');
  1215. $request = new CakeRequest();
  1216. $this->assertEquals('/CakeBB/index.php', $request->base);
  1217. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1218. Configure::write('App.baseUrl', '/dbhauser/index.php');
  1219. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  1220. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  1221. $request = new CakeRequest();
  1222. $this->assertEquals('/dbhauser/index.php', $request->base);
  1223. $this->assertEquals('/dbhauser/app/webroot/', $request->webroot);
  1224. }
  1225. /**
  1226. * Test baseUrl with no rewrite and using the top level index.php.
  1227. *
  1228. * @return void
  1229. */
  1230. public function testBaseUrlNoRewriteTopLevelIndex() {
  1231. Configure::write('App.baseUrl', '/index.php');
  1232. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
  1233. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
  1234. $request = new CakeRequest();
  1235. $this->assertEquals('/index.php', $request->base);
  1236. $this->assertEquals('/app/webroot/', $request->webroot);
  1237. }
  1238. /**
  1239. * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
  1240. *
  1241. * @return void
  1242. */
  1243. public function testBaseUrlWithAppAndWebrootInDirname() {
  1244. Configure::write('App.baseUrl', '/approval/index.php');
  1245. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1246. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
  1247. $request = new CakeRequest();
  1248. $this->assertEquals('/approval/index.php', $request->base);
  1249. $this->assertEquals('/approval/app/webroot/', $request->webroot);
  1250. Configure::write('App.baseUrl', '/webrootable/index.php');
  1251. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1252. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
  1253. $request = new CakeRequest();
  1254. $this->assertEquals('/webrootable/index.php', $request->base);
  1255. $this->assertEquals('/webrootable/app/webroot/', $request->webroot);
  1256. }
  1257. /**
  1258. * Test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
  1259. *
  1260. * @return void
  1261. */
  1262. public function testBaseUrlNoRewriteWebrootIndex() {
  1263. Configure::write('App.baseUrl', '/index.php');
  1264. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
  1265. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
  1266. $request = new CakeRequest();
  1267. $this->assertEquals('/index.php', $request->base);
  1268. $this->assertEquals('/', $request->webroot);
  1269. }
  1270. /**
  1271. * Test that a request with a . in the main GET parameter is filtered out.
  1272. * PHP changes GET parameter keys containing dots to _.
  1273. *
  1274. * @return void
  1275. */
  1276. public function testGetParamsWithDot() {
  1277. $_GET = array();
  1278. $_GET['/posts/index/add_add'] = '';
  1279. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1280. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
  1281. $request = new CakeRequest();
  1282. $this->assertEquals(array(), $request->query);
  1283. }
  1284. /**
  1285. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  1286. *
  1287. * @return void
  1288. */
  1289. public function testGetParamWithUrlencodedElement() {
  1290. $_GET = array();
  1291. $_GET['/posts/add/∂∂'] = '';
  1292. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1293. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
  1294. $request = new CakeRequest();
  1295. $this->assertEquals(array(), $request->query);
  1296. }
  1297. /**
  1298. * Generator for environment configurations
  1299. *
  1300. * @return array Environment array
  1301. */
  1302. public static function environmentGenerator() {
  1303. return array(
  1304. array(
  1305. 'IIS - No rewrite base path',
  1306. array(
  1307. 'App' => array(
  1308. 'base' => false,
  1309. 'baseUrl' => '/index.php',
  1310. 'dir' => 'app',
  1311. 'webroot' => 'webroot'
  1312. ),
  1313. 'SERVER' => array(
  1314. 'SCRIPT_NAME' => '/index.php',
  1315. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1316. 'QUERY_STRING' => '',
  1317. 'REQUEST_URI' => '/index.php',
  1318. 'URL' => '/index.php',
  1319. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  1320. 'ORIG_PATH_INFO' => '/index.php',
  1321. 'PATH_INFO' => '',
  1322. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  1323. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1324. 'PHP_SELF' => '/index.php',
  1325. ),
  1326. ),
  1327. array(
  1328. 'base' => '/index.php',
  1329. 'webroot' => '/app/webroot/',
  1330. 'url' => ''
  1331. ),
  1332. ),
  1333. array(
  1334. 'IIS - No rewrite with path, no PHP_SELF',
  1335. array(
  1336. 'App' => array(
  1337. 'base' => false,
  1338. 'baseUrl' => '/index.php?',
  1339. 'dir' => 'app',
  1340. 'webroot' => 'webroot'
  1341. ),
  1342. 'SERVER' => array(
  1343. 'QUERY_STRING' => '/posts/add',
  1344. 'REQUEST_URI' => '/index.php?/posts/add',
  1345. 'PHP_SELF' => '',
  1346. 'URL' => '/index.php?/posts/add',
  1347. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1348. 'argv' => array('/posts/add'),
  1349. 'argc' => 1
  1350. ),
  1351. ),
  1352. array(
  1353. 'url' => 'posts/add',
  1354. 'base' => '/index.php?',
  1355. 'webroot' => '/app/webroot/'
  1356. )
  1357. ),
  1358. array(
  1359. 'IIS - No rewrite sub dir 2',
  1360. array(
  1361. 'App' => array(
  1362. 'base' => false,
  1363. 'baseUrl' => '/site/index.php',
  1364. 'dir' => 'app',
  1365. 'webroot' => 'webroot',
  1366. ),
  1367. 'SERVER' => array(
  1368. 'SCRIPT_NAME' => '/site/index.php',
  1369. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1370. 'QUERY_STRING' => '',
  1371. 'REQUEST_URI' => '/site/index.php',
  1372. 'URL' => '/site/index.php',
  1373. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1374. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1375. 'PHP_SELF' => '/site/index.php',
  1376. 'argv' => array(),
  1377. 'argc' => 0
  1378. ),
  1379. ),
  1380. array(
  1381. 'url' => '',
  1382. 'base' => '/site/index.php',
  1383. 'webroot' => '/site/app/webroot/'
  1384. ),
  1385. ),
  1386. array(
  1387. 'IIS - No rewrite sub dir 2 with path',
  1388. array(
  1389. 'App' => array(
  1390. 'base' => false,
  1391. 'baseUrl' => '/site/index.php',
  1392. 'dir' => 'app',
  1393. 'webroot' => 'webroot'
  1394. ),
  1395. 'GET' => array('/posts/add' => ''),
  1396. 'SERVER' => array(
  1397. 'SCRIPT_NAME' => '/site/index.php',
  1398. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1399. 'QUERY_STRING' => '/posts/add',
  1400. 'REQUEST_URI' => '/site/index.php/posts/add',
  1401. 'URL' => '/site/index.php/posts/add',
  1402. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1403. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1404. 'PHP_SELF' => '/site/index.php/posts/add',
  1405. 'argv' => array('/posts/add'),
  1406. 'argc' => 1
  1407. ),
  1408. ),
  1409. array(
  1410. 'url' => 'posts/add',
  1411. 'base' => '/site/index.php',
  1412. 'webroot' => '/site/app/webroot/'
  1413. )
  1414. ),
  1415. array(
  1416. 'Apache - No rewrite, document root set to webroot, requesting path',
  1417. array(
  1418. 'App' => array(
  1419. 'base' => false,
  1420. 'baseUrl' => '/index.php',
  1421. 'dir' => 'app',
  1422. 'webroot' => 'webroot'
  1423. ),
  1424. 'SERVER' => array(
  1425. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1426. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1427. 'QUERY_STRING' => '',
  1428. 'REQUEST_URI' => '/index.php/posts/index',
  1429. 'SCRIPT_NAME' => '/index.php',
  1430. 'PATH_INFO' => '/posts/index',
  1431. 'PHP_SELF' => '/index.php/posts/index',
  1432. ),
  1433. ),
  1434. array(
  1435. 'url' => 'posts/index',
  1436. 'base' => '/index.php',
  1437. 'webroot' => '/'
  1438. ),
  1439. ),
  1440. array(
  1441. 'Apache - No rewrite, document root set to webroot, requesting root',
  1442. array(
  1443. 'App' => array(
  1444. 'base' => false,
  1445. 'baseUrl' => '/index.php',
  1446. 'dir' => 'app',
  1447. 'webroot' => 'webroot'
  1448. ),
  1449. 'SERVER' => array(
  1450. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1451. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1452. 'QUERY_STRING' => '',
  1453. 'REQUEST_URI' => '/index.php',
  1454. 'SCRIPT_NAME' => '/index.php',
  1455. 'PATH_INFO' => '',
  1456. 'PHP_SELF' => '/index.php',
  1457. ),
  1458. ),
  1459. array(
  1460. 'url' => '',
  1461. 'base' => '/index.php',
  1462. 'webroot' => '/'
  1463. ),
  1464. ),
  1465. array(
  1466. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1467. array(
  1468. 'App' => array(
  1469. 'base' => false,
  1470. 'baseUrl' => '/site/index.php',
  1471. 'dir' => 'app',
  1472. 'webroot' => 'webroot'
  1473. ),
  1474. 'SERVER' => array(
  1475. 'SERVER_NAME' => 'localhost',
  1476. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1477. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1478. 'REQUEST_URI' => '/site/index.php/posts/index',
  1479. 'SCRIPT_NAME' => '/site/index.php',
  1480. 'PATH_INFO' => '/posts/index',
  1481. 'PHP_SELF' => '/site/index.php/posts/index',
  1482. ),
  1483. ),
  1484. array(
  1485. 'url' => 'posts/index',
  1486. 'base' => '/site/index.php',
  1487. 'webroot' => '/site/app/webroot/',
  1488. ),
  1489. ),
  1490. array(
  1491. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1492. array(
  1493. 'App' => array(
  1494. 'base' => false,
  1495. 'baseUrl' => '/site/index.php',
  1496. 'dir' => 'app',
  1497. 'webroot' => 'webroot'
  1498. ),
  1499. 'SERVER' => array(
  1500. 'SERVER_NAME' => 'localhost',
  1501. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1502. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1503. 'REQUEST_URI' => '/site/index.php/',
  1504. 'SCRIPT_NAME' => '/site/index.php',
  1505. 'PHP_SELF' => '/site/index.php/',
  1506. ),
  1507. ),
  1508. array(
  1509. 'url' => '',
  1510. 'base' => '/site/index.php',
  1511. 'webroot' => '/site/app/webroot/',
  1512. ),
  1513. ),
  1514. array(
  1515. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1516. array(
  1517. 'App' => array(
  1518. 'base' => false,
  1519. 'baseUrl' => '/site/index.php',
  1520. 'dir' => 'app',
  1521. 'webroot' => 'webroot'
  1522. ),
  1523. 'GET' => array('a' => 'b', 'c' => 'd'),
  1524. 'SERVER' => array(
  1525. 'SERVER_NAME' => 'localhost',
  1526. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1527. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1528. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1529. 'SCRIPT_NAME' => '/site/index.php',
  1530. 'PATH_INFO' => '/posts/index',
  1531. 'PHP_SELF' => '/site/index.php/posts/index',
  1532. 'QUERY_STRING' => 'a=b&c=d'
  1533. ),
  1534. ),
  1535. array(
  1536. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1537. 'url' => 'posts/index',
  1538. 'base' => '/site/index.php',
  1539. 'webroot' => '/site/app/webroot/',
  1540. ),
  1541. ),
  1542. array(
  1543. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1544. array(
  1545. 'App' => array(
  1546. 'base' => false,
  1547. 'baseUrl' => false,
  1548. 'dir' => 'app',
  1549. 'webroot' => 'webroot'
  1550. ),
  1551. 'SERVER' => array(
  1552. 'SERVER_NAME' => 'localhost',
  1553. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1554. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1555. 'REQUEST_URI' => '/site/',
  1556. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1557. 'PHP_SELF' => '/site/app/webroot/index.php',
  1558. ),
  1559. ),
  1560. array(
  1561. 'url' => '',
  1562. 'base' => '/site',
  1563. 'webroot' => '/site/',
  1564. ),
  1565. ),
  1566. array(
  1567. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  1568. array(
  1569. 'App' => array(
  1570. 'base' => false,
  1571. 'baseUrl' => false,
  1572. 'dir' => 'app',
  1573. 'webroot' => 'webroot'
  1574. ),
  1575. 'SERVER' => array(
  1576. 'SERVER_NAME' => 'localhost',
  1577. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1578. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1579. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1580. 'PHP_SELF' => '/site/app/webroot/index.php',
  1581. 'PATH_INFO' => null,
  1582. 'REQUEST_URI' => null,
  1583. ),
  1584. ),
  1585. array(
  1586. 'url' => '',
  1587. 'base' => '/site',
  1588. 'webroot' => '/site/',
  1589. ),
  1590. ),
  1591. array(
  1592. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1593. array(
  1594. 'App' => array(
  1595. 'base' => false,
  1596. 'baseUrl' => false,
  1597. 'dir' => 'app',
  1598. 'webroot' => 'webroot'
  1599. ),
  1600. 'SERVER' => array(
  1601. 'SERVER_NAME' => 'localhost',
  1602. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1603. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1604. 'SCRIPT_NAME' => '/index.php',
  1605. 'PHP_SELF' => '/index.php',
  1606. 'PATH_INFO' => null,
  1607. 'REQUEST_URI' => null,
  1608. ),
  1609. ),
  1610. array(
  1611. 'url' => '',
  1612. 'base' => '',
  1613. 'webroot' => '/',
  1614. ),
  1615. ),
  1616. array(
  1617. 'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
  1618. array(
  1619. 'App' => array(
  1620. 'base' => false,
  1621. 'baseUrl' => false,
  1622. 'dir' => 'app',
  1623. 'webroot' => 'webroot'
  1624. ),
  1625. 'SERVER' => array(
  1626. 'SERVER_NAME' => 'localhost',
  1627. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1628. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1629. 'REQUEST_URI' => '/site/posts/index',
  1630. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1631. 'PHP_SELF' => '/site/app/webroot/index.php',
  1632. ),
  1633. ),
  1634. array(
  1635. 'url' => 'posts/index',
  1636. 'base' => '/site',
  1637. 'webroot' => '/site/',
  1638. ),
  1639. ),
  1640. array(
  1641. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  1642. array(
  1643. 'App' => array(
  1644. 'base' => false,
  1645. 'baseUrl' => false,
  1646. 'dir' => 'app',
  1647. 'webroot' => 'webroot'
  1648. ),
  1649. 'GET' => array('/posts/add' => ''),
  1650. 'SERVER' => array(
  1651. 'SERVER_NAME' => 'localhost',
  1652. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1653. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1654. 'SCRIPT_NAME' => '/index.php',
  1655. 'QUERY_STRING' => '/posts/add&',
  1656. 'PHP_SELF' => '/index.php',
  1657. 'PATH_INFO' => null,
  1658. 'REQUEST_URI' => '/posts/add',
  1659. ),
  1660. ),
  1661. array(
  1662. 'url' => 'posts/add',
  1663. 'base' => '',
  1664. 'webroot' => '/',
  1665. 'urlParams' => array()
  1666. ),
  1667. ),
  1668. );
  1669. }
  1670. /**
  1671. * Test environment detection
  1672. *
  1673. * @dataProvider environmentGenerator
  1674. * @param $name
  1675. * @param $env
  1676. * @param $expected
  1677. * @return void
  1678. */
  1679. public function testEnvironmentDetection($name, $env, $expected) {
  1680. $_GET = array();
  1681. $this->_loadEnvironment($env);
  1682. $request = new CakeRequest();
  1683. $this->assertEquals($expected['url'], $request->url, "url error");
  1684. $this->assertEquals($expected['base'], $request->base, "base error");
  1685. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1686. if (isset($expected['urlParams'])) {
  1687. $this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch");
  1688. }
  1689. }
  1690. /**
  1691. * Test the query() method
  1692. *
  1693. * @return void
  1694. */
  1695. public function testQuery() {
  1696. $_GET = array();
  1697. $_GET['foo'] = 'bar';
  1698. $request = new CakeRequest();
  1699. $result = $request->query('foo');
  1700. $this->assertEquals('bar', $result);
  1701. $result = $request->query('imaginary');
  1702. $this->assertNull($result);
  1703. }
  1704. /**
  1705. * Test the query() method with arrays passed via $_GET
  1706. *
  1707. * @return void
  1708. */
  1709. public function testQueryWithArray() {
  1710. $_GET = array();
  1711. $_GET['test'] = array('foo', 'bar');
  1712. $request = new CakeRequest();
  1713. $result = $request->query('test');
  1714. $this->assertEquals(array('foo', 'bar'), $result);
  1715. $result = $request->query('test.1');
  1716. $this->assertEquals('bar', $result);
  1717. $result = $request->query('test.2');
  1718. $this->assertNull($result);
  1719. }
  1720. /**
  1721. * Test using param()
  1722. *
  1723. * @return void
  1724. */
  1725. public function testReadingParams() {
  1726. $request = new CakeRequest();
  1727. $request->addParams(array(
  1728. 'controller' => 'posts',
  1729. 'admin' => true,
  1730. 'truthy' => 1,
  1731. 'zero' => '0',
  1732. ));
  1733. $this->assertFalse($request->param('not_set'));
  1734. $this->assertTrue($request->param('admin'));
  1735. $this->assertEquals(1, $request->param('truthy'));
  1736. $this->assertEquals('posts', $request->param('controller'));
  1737. $this->assertEquals('0', $request->param('zero'));
  1738. }
  1739. /**
  1740. * Test the data() method reading
  1741. *
  1742. * @return void
  1743. */
  1744. public function testDataReading() {
  1745. $_POST['data'] = array(
  1746. 'Model' => array(
  1747. 'field' => 'value'
  1748. )
  1749. );
  1750. $request = new CakeRequest('posts/index');
  1751. $result = $request->data('Model');
  1752. $this->assertEquals($_POST['data']['Model'], $result);
  1753. $result = $request->data('Model.imaginary');
  1754. $this->assertNull($result);
  1755. }
  1756. /**
  1757. * Test writing with data()
  1758. *
  1759. * @return void
  1760. */
  1761. public function testDataWriting() {
  1762. $_POST['data'] = array(
  1763. 'Model' => array(
  1764. 'field' => 'value'
  1765. )
  1766. );
  1767. $request = new CakeRequest('posts/index');
  1768. $result = $request->data('Model.new_value', 'new value');
  1769. $this->assertSame($result, $request, 'Return was not $this');
  1770. $this->assertEquals('new value', $request->data['Model']['new_value']);
  1771. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1772. $this->assertEquals('New post', $request->data['Post']['title']);
  1773. $this->assertEquals('Mark', $request->data['Comment']['1']['author']);
  1774. }
  1775. /**
  1776. * Test writing falsey values.
  1777. *
  1778. * @return void
  1779. */
  1780. public function testDataWritingFalsey() {
  1781. $request = new CakeRequest('posts/index');
  1782. $request->data('Post.null', null);
  1783. $this->assertNull($request->data['Post']['null']);
  1784. $request->data('Post.false', false);
  1785. $this->assertFalse($request->data['Post']['false']);
  1786. $request->data('Post.zero', 0);
  1787. $this->assertSame(0, $request->data['Post']['zero']);
  1788. $request->data('Post.empty', '');
  1789. $this->assertSame('', $request->data['Post']['empty']);
  1790. }
  1791. /**
  1792. * Test accept language
  1793. *
  1794. * @return void
  1795. */
  1796. public function testAcceptLanguage() {
  1797. // Weird language
  1798. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
  1799. $result = CakeRequest::acceptLanguage();
  1800. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1801. // No qualifier
  1802. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1803. $result = CakeRequest::acceptLanguage();
  1804. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1805. // With qualifier
  1806. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
  1807. $result = CakeRequest::acceptLanguage();
  1808. $this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
  1809. // With spaces
  1810. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
  1811. $result = CakeRequest::acceptLanguage();
  1812. $this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
  1813. // Checking if requested
  1814. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1815. $result = CakeRequest::acceptLanguage();
  1816. $result = CakeRequest::acceptLanguage('en-ca');
  1817. $this->assertTrue($result);
  1818. $result = CakeRequest::acceptLanguage('en-CA');
  1819. $this->assertTrue($result);
  1820. $result = CakeRequest::acceptLanguage('en-us');
  1821. $this->assertFalse($result);
  1822. $result = CakeRequest::acceptLanguage('en-US');
  1823. $this->assertFalse($result);
  1824. }
  1825. /**
  1826. * Test the here() method
  1827. *
  1828. * @return void
  1829. */
  1830. public function testHere() {
  1831. Configure::write('App.base', '/base_path');
  1832. $_GET = array('test' => 'value');
  1833. $request = new CakeRequest('/posts/add/1/name:value');
  1834. $result = $request->here();
  1835. $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
  1836. $result = $request->here(false);
  1837. $this->assertEquals('/posts/add/1/name:value?test=value', $result);
  1838. $request = new CakeRequest('/posts/base_path/1/name:value');
  1839. $result = $request->here();
  1840. $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
  1841. $result = $request->here(false);
  1842. $this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
  1843. }
  1844. /**
  1845. * Test the input() method.
  1846. *
  1847. * @return void
  1848. */
  1849. public function testInput() {
  1850. $request = $this->getMock('CakeRequest', array('_readInput'));
  1851. $request->expects($this->once())->method('_readInput')
  1852. ->will($this->returnValue('I came from stdin'));
  1853. $result = $request->input();
  1854. $this->assertEquals('I came from stdin', $result);
  1855. }
  1856. /**
  1857. * Test input() decoding.
  1858. *
  1859. * @return void
  1860. */
  1861. public function testInputDecode() {
  1862. $request = $this->getMock('CakeRequest', array('_readInput'));
  1863. $request->expects($this->once())->method('_readInput')
  1864. ->will($this->returnValue('{"name":"value"}'));
  1865. $result = $request->input('json_decode');
  1866. $this->assertEquals(array('name' => 'value'), (array)$result);
  1867. }
  1868. /**
  1869. * Test input() decoding with additional arguments.
  1870. *
  1871. * @return void
  1872. */
  1873. public function testInputDecodeExtraParams() {
  1874. $xml = <<<XML
  1875. <?xml version="1.0" encoding="utf-8"?>
  1876. <post>
  1877. <title id="title">Test</title>
  1878. </post>
  1879. XML;
  1880. $request = $this->getMock('CakeRequest', array('_readInput'));
  1881. $request->expects($this->once())->method('_readInput')
  1882. ->will($this->returnValue($xml));
  1883. $result = $request->input('Xml::build', array('return' => 'domdocument'));
  1884. $this->assertInstanceOf('DOMDocument', $result);
  1885. $this->assertEquals(
  1886. 'Test',
  1887. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1888. );
  1889. }
  1890. /**
  1891. * Test is('requested') and isRequested()
  1892. *
  1893. * @return void
  1894. */
  1895. public function testIsRequested() {
  1896. $request = new CakeRequest('/posts/index');
  1897. $request->addParams(array(
  1898. 'controller' => 'posts',
  1899. 'action' => 'index',
  1900. 'plugin' => null,
  1901. 'requested' => 1
  1902. ));
  1903. $this->assertTrue($request->is('requested'));
  1904. $this->assertTrue($request->isRequested());
  1905. $request = new CakeRequest('/posts/index');
  1906. $request->addParams(array(
  1907. 'controller' => 'posts',
  1908. 'action' => 'index',
  1909. 'plugin' => null,
  1910. ));
  1911. $this->assertFalse($request->is('requested'));
  1912. $this->assertFalse($request->isRequested());
  1913. }
  1914. /**
  1915. * Test onlyAllow method
  1916. *
  1917. * @return void
  1918. */
  1919. public function testOnlyAllow() {
  1920. $_SERVER['REQUEST_METHOD'] = 'PUT';
  1921. $request = new CakeRequest('/posts/edit/1');
  1922. $this->assertTrue($request->onlyAllow(array('put')));
  1923. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  1924. $this->assertTrue($request->onlyAllow('post', 'delete'));
  1925. }
  1926. /**
  1927. * Test onlyAllow throwing exception
  1928. *
  1929. * @return void
  1930. */
  1931. public function testOnlyAllowException() {
  1932. $_SERVER['REQUEST_METHOD'] = 'PUT';
  1933. $request = new CakeRequest('/posts/edit/1');
  1934. try {
  1935. $request->onlyAllow('POST', 'DELETE');
  1936. $this->fail('An expected exception has not been raised.');
  1937. } catch (MethodNotAllowedException $e) {
  1938. $this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
  1939. }
  1940. $this->setExpectedException('MethodNotAllowedException');
  1941. $request->onlyAllow('POST');
  1942. }
  1943. /**
  1944. * loadEnvironment method
  1945. *
  1946. * @param array $env
  1947. * @return void
  1948. */
  1949. protected function _loadEnvironment($env) {
  1950. if (isset($env['App'])) {
  1951. Configure::write('App', $env['App']);
  1952. }
  1953. if (isset($env['GET'])) {
  1954. foreach ($env['GET'] as $key => $val) {
  1955. $_GET[$key] = $val;
  1956. }
  1957. }
  1958. if (isset($env['POST'])) {
  1959. foreach ($env['POST'] as $key => $val) {
  1960. $_POST[$key] = $val;
  1961. }
  1962. }
  1963. if (isset($env['SERVER'])) {
  1964. foreach ($env['SERVER'] as $key => $val) {
  1965. $_SERVER[$key] = $val;
  1966. }
  1967. }
  1968. }
  1969. }