CakeRequestTest.php 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  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'] = '/app/webroot/index.php';
  1285. $_SERVER['REQUEST_URI'] = '/posts/index/add.add';
  1286. $request = new CakeRequest();
  1287. $this->assertEquals('', $request->base);
  1288. $this->assertEquals(array(), $request->query);
  1289. $_GET = array();
  1290. $_GET['/cake_dev/posts/index/add_add'] = '';
  1291. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1292. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
  1293. $request = new CakeRequest();
  1294. $this->assertEquals('/cake_dev', $request->base);
  1295. $this->assertEquals(array(), $request->query);
  1296. }
  1297. /**
  1298. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  1299. *
  1300. * @return void
  1301. */
  1302. public function testGetParamWithUrlencodedElement() {
  1303. $_GET = array();
  1304. $_GET['/posts/add/∂∂'] = '';
  1305. $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
  1306. $_SERVER['REQUEST_URI'] = '/posts/add/%E2%88%82%E2%88%82';
  1307. $request = new CakeRequest();
  1308. $this->assertEquals('', $request->base);
  1309. $this->assertEquals(array(), $request->query);
  1310. $_GET = array();
  1311. $_GET['/cake_dev/posts/add/∂∂'] = '';
  1312. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1313. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
  1314. $request = new CakeRequest();
  1315. $this->assertEquals('/cake_dev', $request->base);
  1316. $this->assertEquals(array(), $request->query);
  1317. }
  1318. /**
  1319. * Generator for environment configurations
  1320. *
  1321. * @return array Environment array
  1322. */
  1323. public static function environmentGenerator() {
  1324. return array(
  1325. array(
  1326. 'IIS - No rewrite base path',
  1327. array(
  1328. 'App' => array(
  1329. 'base' => false,
  1330. 'baseUrl' => '/index.php',
  1331. 'dir' => 'app',
  1332. 'webroot' => 'webroot'
  1333. ),
  1334. 'SERVER' => array(
  1335. 'SCRIPT_NAME' => '/index.php',
  1336. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1337. 'QUERY_STRING' => '',
  1338. 'REQUEST_URI' => '/index.php',
  1339. 'URL' => '/index.php',
  1340. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  1341. 'ORIG_PATH_INFO' => '/index.php',
  1342. 'PATH_INFO' => '',
  1343. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  1344. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1345. 'PHP_SELF' => '/index.php',
  1346. ),
  1347. ),
  1348. array(
  1349. 'base' => '/index.php',
  1350. 'webroot' => '/app/webroot/',
  1351. 'url' => ''
  1352. ),
  1353. ),
  1354. array(
  1355. 'IIS - No rewrite with path, no PHP_SELF',
  1356. array(
  1357. 'App' => array(
  1358. 'base' => false,
  1359. 'baseUrl' => '/index.php?',
  1360. 'dir' => 'app',
  1361. 'webroot' => 'webroot'
  1362. ),
  1363. 'SERVER' => array(
  1364. 'QUERY_STRING' => '/posts/add',
  1365. 'REQUEST_URI' => '/index.php?/posts/add',
  1366. 'PHP_SELF' => '',
  1367. 'URL' => '/index.php?/posts/add',
  1368. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1369. 'argv' => array('/posts/add'),
  1370. 'argc' => 1
  1371. ),
  1372. ),
  1373. array(
  1374. 'url' => 'posts/add',
  1375. 'base' => '/index.php?',
  1376. 'webroot' => '/app/webroot/'
  1377. )
  1378. ),
  1379. array(
  1380. 'IIS - No rewrite sub dir 2',
  1381. array(
  1382. 'App' => array(
  1383. 'base' => false,
  1384. 'baseUrl' => '/site/index.php',
  1385. 'dir' => 'app',
  1386. 'webroot' => 'webroot',
  1387. ),
  1388. 'SERVER' => array(
  1389. 'SCRIPT_NAME' => '/site/index.php',
  1390. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1391. 'QUERY_STRING' => '',
  1392. 'REQUEST_URI' => '/site/index.php',
  1393. 'URL' => '/site/index.php',
  1394. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1395. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1396. 'PHP_SELF' => '/site/index.php',
  1397. 'argv' => array(),
  1398. 'argc' => 0
  1399. ),
  1400. ),
  1401. array(
  1402. 'url' => '',
  1403. 'base' => '/site/index.php',
  1404. 'webroot' => '/site/app/webroot/'
  1405. ),
  1406. ),
  1407. array(
  1408. 'IIS - No rewrite sub dir 2 with path',
  1409. array(
  1410. 'App' => array(
  1411. 'base' => false,
  1412. 'baseUrl' => '/site/index.php',
  1413. 'dir' => 'app',
  1414. 'webroot' => 'webroot'
  1415. ),
  1416. 'GET' => array('/posts/add' => ''),
  1417. 'SERVER' => array(
  1418. 'SCRIPT_NAME' => '/site/index.php',
  1419. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1420. 'QUERY_STRING' => '/posts/add',
  1421. 'REQUEST_URI' => '/site/index.php/posts/add',
  1422. 'URL' => '/site/index.php/posts/add',
  1423. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1424. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1425. 'PHP_SELF' => '/site/index.php/posts/add',
  1426. 'argv' => array('/posts/add'),
  1427. 'argc' => 1
  1428. ),
  1429. ),
  1430. array(
  1431. 'url' => 'posts/add',
  1432. 'base' => '/site/index.php',
  1433. 'webroot' => '/site/app/webroot/'
  1434. )
  1435. ),
  1436. array(
  1437. 'Apache - No rewrite, document root set to webroot, requesting path',
  1438. array(
  1439. 'App' => array(
  1440. 'base' => false,
  1441. 'baseUrl' => '/index.php',
  1442. 'dir' => 'app',
  1443. 'webroot' => 'webroot'
  1444. ),
  1445. 'SERVER' => array(
  1446. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1447. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1448. 'QUERY_STRING' => '',
  1449. 'REQUEST_URI' => '/index.php/posts/index',
  1450. 'SCRIPT_NAME' => '/index.php',
  1451. 'PATH_INFO' => '/posts/index',
  1452. 'PHP_SELF' => '/index.php/posts/index',
  1453. ),
  1454. ),
  1455. array(
  1456. 'url' => 'posts/index',
  1457. 'base' => '/index.php',
  1458. 'webroot' => '/'
  1459. ),
  1460. ),
  1461. array(
  1462. 'Apache - No rewrite, document root set to webroot, requesting root',
  1463. array(
  1464. 'App' => array(
  1465. 'base' => false,
  1466. 'baseUrl' => '/index.php',
  1467. 'dir' => 'app',
  1468. 'webroot' => 'webroot'
  1469. ),
  1470. 'SERVER' => array(
  1471. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1472. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1473. 'QUERY_STRING' => '',
  1474. 'REQUEST_URI' => '/index.php',
  1475. 'SCRIPT_NAME' => '/index.php',
  1476. 'PATH_INFO' => '',
  1477. 'PHP_SELF' => '/index.php',
  1478. ),
  1479. ),
  1480. array(
  1481. 'url' => '',
  1482. 'base' => '/index.php',
  1483. 'webroot' => '/'
  1484. ),
  1485. ),
  1486. array(
  1487. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1488. array(
  1489. 'App' => array(
  1490. 'base' => false,
  1491. 'baseUrl' => '/site/index.php',
  1492. 'dir' => 'app',
  1493. 'webroot' => 'webroot'
  1494. ),
  1495. 'SERVER' => array(
  1496. 'SERVER_NAME' => 'localhost',
  1497. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1498. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1499. 'REQUEST_URI' => '/site/index.php/posts/index',
  1500. 'SCRIPT_NAME' => '/site/index.php',
  1501. 'PATH_INFO' => '/posts/index',
  1502. 'PHP_SELF' => '/site/index.php/posts/index',
  1503. ),
  1504. ),
  1505. array(
  1506. 'url' => 'posts/index',
  1507. 'base' => '/site/index.php',
  1508. 'webroot' => '/site/app/webroot/',
  1509. ),
  1510. ),
  1511. array(
  1512. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1513. array(
  1514. 'App' => array(
  1515. 'base' => false,
  1516. 'baseUrl' => '/site/index.php',
  1517. 'dir' => 'app',
  1518. 'webroot' => 'webroot'
  1519. ),
  1520. 'SERVER' => array(
  1521. 'SERVER_NAME' => 'localhost',
  1522. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1523. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1524. 'REQUEST_URI' => '/site/index.php/',
  1525. 'SCRIPT_NAME' => '/site/index.php',
  1526. 'PHP_SELF' => '/site/index.php/',
  1527. ),
  1528. ),
  1529. array(
  1530. 'url' => '',
  1531. 'base' => '/site/index.php',
  1532. 'webroot' => '/site/app/webroot/',
  1533. ),
  1534. ),
  1535. array(
  1536. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1537. array(
  1538. 'App' => array(
  1539. 'base' => false,
  1540. 'baseUrl' => '/site/index.php',
  1541. 'dir' => 'app',
  1542. 'webroot' => 'webroot'
  1543. ),
  1544. 'GET' => array('a' => 'b', 'c' => 'd'),
  1545. 'SERVER' => array(
  1546. 'SERVER_NAME' => 'localhost',
  1547. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1548. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1549. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1550. 'SCRIPT_NAME' => '/site/index.php',
  1551. 'PATH_INFO' => '/posts/index',
  1552. 'PHP_SELF' => '/site/index.php/posts/index',
  1553. 'QUERY_STRING' => 'a=b&c=d'
  1554. ),
  1555. ),
  1556. array(
  1557. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1558. 'url' => 'posts/index',
  1559. 'base' => '/site/index.php',
  1560. 'webroot' => '/site/app/webroot/',
  1561. ),
  1562. ),
  1563. array(
  1564. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1565. array(
  1566. 'App' => array(
  1567. 'base' => false,
  1568. 'baseUrl' => false,
  1569. 'dir' => 'app',
  1570. 'webroot' => 'webroot'
  1571. ),
  1572. 'SERVER' => array(
  1573. 'SERVER_NAME' => 'localhost',
  1574. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1575. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1576. 'REQUEST_URI' => '/site/',
  1577. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1578. 'PHP_SELF' => '/site/app/webroot/index.php',
  1579. ),
  1580. ),
  1581. array(
  1582. 'url' => '',
  1583. 'base' => '/site',
  1584. 'webroot' => '/site/',
  1585. ),
  1586. ),
  1587. array(
  1588. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  1589. array(
  1590. 'App' => array(
  1591. 'base' => false,
  1592. 'baseUrl' => false,
  1593. 'dir' => 'app',
  1594. 'webroot' => 'webroot'
  1595. ),
  1596. 'SERVER' => array(
  1597. 'SERVER_NAME' => 'localhost',
  1598. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1599. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1600. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1601. 'PHP_SELF' => '/site/app/webroot/index.php',
  1602. 'PATH_INFO' => null,
  1603. 'REQUEST_URI' => null,
  1604. ),
  1605. ),
  1606. array(
  1607. 'url' => '',
  1608. 'base' => '/site',
  1609. 'webroot' => '/site/',
  1610. ),
  1611. ),
  1612. array(
  1613. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1614. array(
  1615. 'App' => array(
  1616. 'base' => false,
  1617. 'baseUrl' => false,
  1618. 'dir' => 'app',
  1619. 'webroot' => 'webroot'
  1620. ),
  1621. 'SERVER' => array(
  1622. 'SERVER_NAME' => 'localhost',
  1623. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1624. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1625. 'SCRIPT_NAME' => '/index.php',
  1626. 'PHP_SELF' => '/index.php',
  1627. 'PATH_INFO' => null,
  1628. 'REQUEST_URI' => null,
  1629. ),
  1630. ),
  1631. array(
  1632. 'url' => '',
  1633. 'base' => '',
  1634. 'webroot' => '/',
  1635. ),
  1636. ),
  1637. array(
  1638. 'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
  1639. array(
  1640. 'App' => array(
  1641. 'base' => false,
  1642. 'baseUrl' => false,
  1643. 'dir' => 'app',
  1644. 'webroot' => 'webroot'
  1645. ),
  1646. 'SERVER' => array(
  1647. 'SERVER_NAME' => 'localhost',
  1648. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1649. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1650. 'REQUEST_URI' => '/site/posts/index',
  1651. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1652. 'PHP_SELF' => '/site/app/webroot/index.php',
  1653. ),
  1654. ),
  1655. array(
  1656. 'url' => 'posts/index',
  1657. 'base' => '/site',
  1658. 'webroot' => '/site/',
  1659. ),
  1660. ),
  1661. array(
  1662. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  1663. array(
  1664. 'App' => array(
  1665. 'base' => false,
  1666. 'baseUrl' => false,
  1667. 'dir' => 'app',
  1668. 'webroot' => 'webroot'
  1669. ),
  1670. 'GET' => array('/posts/add' => ''),
  1671. 'SERVER' => array(
  1672. 'SERVER_NAME' => 'localhost',
  1673. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1674. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1675. 'SCRIPT_NAME' => '/index.php',
  1676. 'QUERY_STRING' => '/posts/add&',
  1677. 'PHP_SELF' => '/index.php',
  1678. 'PATH_INFO' => null,
  1679. 'REQUEST_URI' => '/posts/add',
  1680. ),
  1681. ),
  1682. array(
  1683. 'url' => 'posts/add',
  1684. 'base' => '',
  1685. 'webroot' => '/',
  1686. 'urlParams' => array()
  1687. ),
  1688. ),
  1689. array(
  1690. 'Nginx - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO, base parameter set',
  1691. array(
  1692. 'App' => array(
  1693. 'base' => false,
  1694. 'baseUrl' => false,
  1695. 'dir' => 'app',
  1696. 'webroot' => 'webroot'
  1697. ),
  1698. 'GET' => array('/site/posts/add' => ''),
  1699. 'SERVER' => array(
  1700. 'SERVER_NAME' => 'localhost',
  1701. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1702. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1703. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1704. 'QUERY_STRING' => '/site/posts/add&',
  1705. 'PHP_SELF' => '/site/app/webroot/index.php',
  1706. 'PATH_INFO' => null,
  1707. 'REQUEST_URI' => '/site/posts/add',
  1708. ),
  1709. ),
  1710. array(
  1711. 'url' => 'posts/add',
  1712. 'base' => '/site',
  1713. 'webroot' => '/site/',
  1714. 'urlParams' => array()
  1715. ),
  1716. ),
  1717. );
  1718. }
  1719. /**
  1720. * Test environment detection
  1721. *
  1722. * @dataProvider environmentGenerator
  1723. * @param $name
  1724. * @param $env
  1725. * @param $expected
  1726. * @return void
  1727. */
  1728. public function testEnvironmentDetection($name, $env, $expected) {
  1729. $_GET = array();
  1730. $this->_loadEnvironment($env);
  1731. $request = new CakeRequest();
  1732. $this->assertEquals($expected['url'], $request->url, "url error");
  1733. $this->assertEquals($expected['base'], $request->base, "base error");
  1734. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1735. if (isset($expected['urlParams'])) {
  1736. $this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch");
  1737. }
  1738. }
  1739. /**
  1740. * Test the query() method
  1741. *
  1742. * @return void
  1743. */
  1744. public function testQuery() {
  1745. $_GET = array();
  1746. $_GET['foo'] = 'bar';
  1747. $request = new CakeRequest();
  1748. $result = $request->query('foo');
  1749. $this->assertEquals('bar', $result);
  1750. $result = $request->query('imaginary');
  1751. $this->assertNull($result);
  1752. }
  1753. /**
  1754. * Test the query() method with arrays passed via $_GET
  1755. *
  1756. * @return void
  1757. */
  1758. public function testQueryWithArray() {
  1759. $_GET = array();
  1760. $_GET['test'] = array('foo', 'bar');
  1761. $request = new CakeRequest();
  1762. $result = $request->query('test');
  1763. $this->assertEquals(array('foo', 'bar'), $result);
  1764. $result = $request->query('test.1');
  1765. $this->assertEquals('bar', $result);
  1766. $result = $request->query('test.2');
  1767. $this->assertNull($result);
  1768. }
  1769. /**
  1770. * Test using param()
  1771. *
  1772. * @return void
  1773. */
  1774. public function testReadingParams() {
  1775. $request = new CakeRequest();
  1776. $request->addParams(array(
  1777. 'controller' => 'posts',
  1778. 'admin' => true,
  1779. 'truthy' => 1,
  1780. 'zero' => '0',
  1781. ));
  1782. $this->assertFalse($request->param('not_set'));
  1783. $this->assertTrue($request->param('admin'));
  1784. $this->assertEquals(1, $request->param('truthy'));
  1785. $this->assertEquals('posts', $request->param('controller'));
  1786. $this->assertEquals('0', $request->param('zero'));
  1787. }
  1788. /**
  1789. * Test the data() method reading
  1790. *
  1791. * @return void
  1792. */
  1793. public function testDataReading() {
  1794. $_POST['data'] = array(
  1795. 'Model' => array(
  1796. 'field' => 'value'
  1797. )
  1798. );
  1799. $request = new CakeRequest('posts/index');
  1800. $result = $request->data('Model');
  1801. $this->assertEquals($_POST['data']['Model'], $result);
  1802. $result = $request->data('Model.imaginary');
  1803. $this->assertNull($result);
  1804. }
  1805. /**
  1806. * Test writing with data()
  1807. *
  1808. * @return void
  1809. */
  1810. public function testDataWriting() {
  1811. $_POST['data'] = array(
  1812. 'Model' => array(
  1813. 'field' => 'value'
  1814. )
  1815. );
  1816. $request = new CakeRequest('posts/index');
  1817. $result = $request->data('Model.new_value', 'new value');
  1818. $this->assertSame($result, $request, 'Return was not $this');
  1819. $this->assertEquals('new value', $request->data['Model']['new_value']);
  1820. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1821. $this->assertEquals('New post', $request->data['Post']['title']);
  1822. $this->assertEquals('Mark', $request->data['Comment']['1']['author']);
  1823. }
  1824. /**
  1825. * Test writing falsey values.
  1826. *
  1827. * @return void
  1828. */
  1829. public function testDataWritingFalsey() {
  1830. $request = new CakeRequest('posts/index');
  1831. $request->data('Post.null', null);
  1832. $this->assertNull($request->data['Post']['null']);
  1833. $request->data('Post.false', false);
  1834. $this->assertFalse($request->data['Post']['false']);
  1835. $request->data('Post.zero', 0);
  1836. $this->assertSame(0, $request->data['Post']['zero']);
  1837. $request->data('Post.empty', '');
  1838. $this->assertSame('', $request->data['Post']['empty']);
  1839. }
  1840. /**
  1841. * Test accept language
  1842. *
  1843. * @return void
  1844. */
  1845. public function testAcceptLanguage() {
  1846. // Weird language
  1847. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
  1848. $result = CakeRequest::acceptLanguage();
  1849. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1850. // No qualifier
  1851. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1852. $result = CakeRequest::acceptLanguage();
  1853. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1854. // With qualifier
  1855. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
  1856. $result = CakeRequest::acceptLanguage();
  1857. $this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
  1858. // With spaces
  1859. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
  1860. $result = CakeRequest::acceptLanguage();
  1861. $this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
  1862. // Checking if requested
  1863. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1864. $result = CakeRequest::acceptLanguage();
  1865. $result = CakeRequest::acceptLanguage('en-ca');
  1866. $this->assertTrue($result);
  1867. $result = CakeRequest::acceptLanguage('en-CA');
  1868. $this->assertTrue($result);
  1869. $result = CakeRequest::acceptLanguage('en-us');
  1870. $this->assertFalse($result);
  1871. $result = CakeRequest::acceptLanguage('en-US');
  1872. $this->assertFalse($result);
  1873. }
  1874. /**
  1875. * Test the here() method
  1876. *
  1877. * @return void
  1878. */
  1879. public function testHere() {
  1880. Configure::write('App.base', '/base_path');
  1881. $_GET = array('test' => 'value');
  1882. $request = new CakeRequest('/posts/add/1/name:value');
  1883. $result = $request->here();
  1884. $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
  1885. $result = $request->here(false);
  1886. $this->assertEquals('/posts/add/1/name:value?test=value', $result);
  1887. $request = new CakeRequest('/posts/base_path/1/name:value');
  1888. $result = $request->here();
  1889. $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
  1890. $result = $request->here(false);
  1891. $this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
  1892. }
  1893. /**
  1894. * Test the input() method.
  1895. *
  1896. * @return void
  1897. */
  1898. public function testInput() {
  1899. $request = $this->getMock('CakeRequest', array('_readInput'));
  1900. $request->expects($this->once())->method('_readInput')
  1901. ->will($this->returnValue('I came from stdin'));
  1902. $result = $request->input();
  1903. $this->assertEquals('I came from stdin', $result);
  1904. }
  1905. /**
  1906. * Test input() decoding.
  1907. *
  1908. * @return void
  1909. */
  1910. public function testInputDecode() {
  1911. $request = $this->getMock('CakeRequest', array('_readInput'));
  1912. $request->expects($this->once())->method('_readInput')
  1913. ->will($this->returnValue('{"name":"value"}'));
  1914. $result = $request->input('json_decode');
  1915. $this->assertEquals(array('name' => 'value'), (array)$result);
  1916. }
  1917. /**
  1918. * Test input() decoding with additional arguments.
  1919. *
  1920. * @return void
  1921. */
  1922. public function testInputDecodeExtraParams() {
  1923. $xml = <<<XML
  1924. <?xml version="1.0" encoding="utf-8"?>
  1925. <post>
  1926. <title id="title">Test</title>
  1927. </post>
  1928. XML;
  1929. $request = $this->getMock('CakeRequest', array('_readInput'));
  1930. $request->expects($this->once())->method('_readInput')
  1931. ->will($this->returnValue($xml));
  1932. $result = $request->input('Xml::build', array('return' => 'domdocument'));
  1933. $this->assertInstanceOf('DOMDocument', $result);
  1934. $this->assertEquals(
  1935. 'Test',
  1936. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1937. );
  1938. }
  1939. /**
  1940. * Test is('requested') and isRequested()
  1941. *
  1942. * @return void
  1943. */
  1944. public function testIsRequested() {
  1945. $request = new CakeRequest('/posts/index');
  1946. $request->addParams(array(
  1947. 'controller' => 'posts',
  1948. 'action' => 'index',
  1949. 'plugin' => null,
  1950. 'requested' => 1
  1951. ));
  1952. $this->assertTrue($request->is('requested'));
  1953. $this->assertTrue($request->isRequested());
  1954. $request = new CakeRequest('/posts/index');
  1955. $request->addParams(array(
  1956. 'controller' => 'posts',
  1957. 'action' => 'index',
  1958. 'plugin' => null,
  1959. ));
  1960. $this->assertFalse($request->is('requested'));
  1961. $this->assertFalse($request->isRequested());
  1962. }
  1963. /**
  1964. * Test allowMethod method
  1965. *
  1966. * @return void
  1967. */
  1968. public function testAllowMethod() {
  1969. $_SERVER['REQUEST_METHOD'] = 'PUT';
  1970. $request = new CakeRequest('/posts/edit/1');
  1971. $this->assertTrue($request->allowMethod(array('put')));
  1972. // BC check
  1973. $this->assertTrue($request->onlyAllow(array('put')));
  1974. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  1975. $this->assertTrue($request->allowMethod('post', 'delete'));
  1976. // BC check
  1977. $this->assertTrue($request->onlyAllow('post', 'delete'));
  1978. }
  1979. /**
  1980. * Test allowMethod throwing exception
  1981. *
  1982. * @return void
  1983. */
  1984. public function testAllowMethodException() {
  1985. $_SERVER['REQUEST_METHOD'] = 'PUT';
  1986. $request = new CakeRequest('/posts/edit/1');
  1987. try {
  1988. $request->allowMethod('POST', 'DELETE');
  1989. $this->fail('An expected exception has not been raised.');
  1990. } catch (MethodNotAllowedException $e) {
  1991. $this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
  1992. }
  1993. $this->setExpectedException('MethodNotAllowedException');
  1994. $request->allowMethod('POST');
  1995. }
  1996. /**
  1997. * loadEnvironment method
  1998. *
  1999. * @param array $env
  2000. * @return void
  2001. */
  2002. protected function _loadEnvironment($env) {
  2003. if (isset($env['App'])) {
  2004. Configure::write('App', $env['App']);
  2005. }
  2006. if (isset($env['GET'])) {
  2007. foreach ($env['GET'] as $key => $val) {
  2008. $_GET[$key] = $val;
  2009. }
  2010. }
  2011. if (isset($env['POST'])) {
  2012. foreach ($env['POST'] as $key => $val) {
  2013. $_POST[$key] = $val;
  2014. }
  2015. }
  2016. if (isset($env['SERVER'])) {
  2017. foreach ($env['SERVER'] as $key => $val) {
  2018. $_SERVER[$key] = $val;
  2019. }
  2020. }
  2021. }
  2022. }