CakeRequestTest.php 61 KB

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