CakeRequestTest.php 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311
  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 bool $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. * @return void
  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') . '/recipes/add';
  640. $result = $request->referer(true);
  641. $this->assertSame($result, '/recipes/add');
  642. }
  643. /**
  644. * Test referer() with a base path that duplicates the
  645. * first segment.
  646. *
  647. * @return void
  648. */
  649. public function testRefererBasePath() {
  650. $request = new CakeRequest('some/path');
  651. $request->url = 'users/login';
  652. $request->webroot = '/waves/';
  653. $request->base = '/waves';
  654. $request->here = '/waves/users/login';
  655. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/waves/waves/add';
  656. $result = $request->referer(true);
  657. $this->assertSame($result, '/waves/add');
  658. }
  659. /**
  660. * test the simple uses of is()
  661. *
  662. * @return void
  663. */
  664. public function testIsHttpMethods() {
  665. $request = new CakeRequest('some/path');
  666. $this->assertFalse($request->is('undefined-behavior'));
  667. $_SERVER['REQUEST_METHOD'] = 'GET';
  668. $this->assertTrue($request->is('get'));
  669. $_SERVER['REQUEST_METHOD'] = 'POST';
  670. $this->assertTrue($request->is('POST'));
  671. $_SERVER['REQUEST_METHOD'] = 'PUT';
  672. $this->assertTrue($request->is('put'));
  673. $this->assertFalse($request->is('get'));
  674. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  675. $this->assertTrue($request->is('delete'));
  676. $this->assertTrue($request->isDelete());
  677. $_SERVER['REQUEST_METHOD'] = 'delete';
  678. $this->assertFalse($request->is('delete'));
  679. }
  680. /**
  681. * Test is() with multiple types.
  682. *
  683. * @return void
  684. */
  685. public function testIsMultiple() {
  686. $request = new CakeRequest('some/path');
  687. $_SERVER['REQUEST_METHOD'] = 'GET';
  688. $this->assertTrue($request->is(array('get', 'post')));
  689. $_SERVER['REQUEST_METHOD'] = 'POST';
  690. $this->assertTrue($request->is(array('get', 'post')));
  691. $_SERVER['REQUEST_METHOD'] = 'PUT';
  692. $this->assertFalse($request->is(array('get', 'post')));
  693. }
  694. /**
  695. * Test isAll()
  696. *
  697. * @return void
  698. */
  699. public function testIsAll() {
  700. $request = new CakeRequest('some/path');
  701. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  702. $_SERVER['REQUEST_METHOD'] = 'GET';
  703. $this->assertTrue($request->isAll(array('ajax', 'get')));
  704. $this->assertFalse($request->isAll(array('post', 'get')));
  705. $this->assertFalse($request->isAll(array('ajax', 'post')));
  706. }
  707. /**
  708. * Test the method() method.
  709. *
  710. * @return void
  711. */
  712. public function testMethod() {
  713. $_SERVER['REQUEST_METHOD'] = 'delete';
  714. $request = new CakeRequest('some/path');
  715. $this->assertEquals('delete', $request->method());
  716. }
  717. /**
  718. * Test host retrieval.
  719. *
  720. * @return void
  721. */
  722. public function testHost() {
  723. $_SERVER['HTTP_HOST'] = 'localhost';
  724. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
  725. $request = new CakeRequest('some/path');
  726. $this->assertEquals('localhost', $request->host());
  727. $this->assertEquals('cakephp.org', $request->host(true));
  728. }
  729. /**
  730. * Test domain retrieval.
  731. *
  732. * @return void
  733. */
  734. public function testDomain() {
  735. $_SERVER['HTTP_HOST'] = 'something.example.com';
  736. $request = new CakeRequest('some/path');
  737. $this->assertEquals('example.com', $request->domain());
  738. $_SERVER['HTTP_HOST'] = 'something.example.co.uk';
  739. $this->assertEquals('example.co.uk', $request->domain(2));
  740. }
  741. /**
  742. * Test getting subdomains for a host.
  743. *
  744. * @return void
  745. */
  746. public function testSubdomain() {
  747. $_SERVER['HTTP_HOST'] = 'something.example.com';
  748. $request = new CakeRequest('some/path');
  749. $this->assertEquals(array('something'), $request->subdomains());
  750. $_SERVER['HTTP_HOST'] = 'www.something.example.com';
  751. $this->assertEquals(array('www', 'something'), $request->subdomains());
  752. $_SERVER['HTTP_HOST'] = 'www.something.example.co.uk';
  753. $this->assertEquals(array('www', 'something'), $request->subdomains(2));
  754. $_SERVER['HTTP_HOST'] = 'example.co.uk';
  755. $this->assertEquals(array(), $request->subdomains(2));
  756. }
  757. /**
  758. * Test ajax, flash and friends
  759. *
  760. * @return void
  761. */
  762. public function testisAjaxFlashAndFriends() {
  763. $request = new CakeRequest('some/path');
  764. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  765. $this->assertTrue($request->is('flash'));
  766. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
  767. $this->assertTrue($request->is('flash'));
  768. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  769. $this->assertTrue($request->is('ajax'));
  770. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
  771. $this->assertFalse($request->is('ajax'));
  772. $this->assertFalse($request->isAjax());
  773. $_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
  774. $this->assertTrue($request->is('mobile'));
  775. $this->assertTrue($request->isMobile());
  776. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b6pre) Gecko/20100902 Firefox/4.0b6pre Fennec/2.0b1pre';
  777. $this->assertTrue($request->is('mobile'));
  778. $this->assertTrue($request->isMobile());
  779. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; SAMSUNG; OMNIA7)';
  780. $this->assertTrue($request->is('mobile'));
  781. $this->assertTrue($request->isMobile());
  782. }
  783. /**
  784. * Test __call exceptions
  785. *
  786. * @expectedException CakeException
  787. * @return void
  788. */
  789. public function testMagicCallExceptionOnUnknownMethod() {
  790. $request = new CakeRequest('some/path');
  791. $request->IamABanana();
  792. }
  793. /**
  794. * Test is(ssl)
  795. *
  796. * @return void
  797. */
  798. public function testIsSsl() {
  799. $request = new CakeRequest('some/path');
  800. $_SERVER['HTTPS'] = 1;
  801. $this->assertTrue($request->is('ssl'));
  802. $_SERVER['HTTPS'] = 'on';
  803. $this->assertTrue($request->is('ssl'));
  804. $_SERVER['HTTPS'] = '1';
  805. $this->assertTrue($request->is('ssl'));
  806. $_SERVER['HTTPS'] = 'I am not empty';
  807. $this->assertTrue($request->is('ssl'));
  808. $_SERVER['HTTPS'] = 1;
  809. $this->assertTrue($request->is('ssl'));
  810. $_SERVER['HTTPS'] = 'off';
  811. $this->assertFalse($request->is('ssl'));
  812. $_SERVER['HTTPS'] = false;
  813. $this->assertFalse($request->is('ssl'));
  814. $_SERVER['HTTPS'] = '';
  815. $this->assertFalse($request->is('ssl'));
  816. }
  817. /**
  818. * Test getting request params with object properties.
  819. *
  820. * @return void
  821. */
  822. public function testMagicget() {
  823. $request = new CakeRequest('some/path');
  824. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  825. $this->assertEquals('posts', $request->controller);
  826. $this->assertEquals('view', $request->action);
  827. $this->assertEquals('blogs', $request->plugin);
  828. $this->assertNull($request->banana);
  829. }
  830. /**
  831. * Test isset()/empty() with overloaded properties.
  832. *
  833. * @return void
  834. */
  835. public function testMagicisset() {
  836. $request = new CakeRequest('some/path');
  837. $request->params = array(
  838. 'controller' => 'posts',
  839. 'action' => 'view',
  840. 'plugin' => 'blogs',
  841. 'named' => array()
  842. );
  843. $this->assertTrue(isset($request->controller));
  844. $this->assertFalse(isset($request->notthere));
  845. $this->assertFalse(empty($request->controller));
  846. $this->assertTrue(empty($request->named));
  847. }
  848. /**
  849. * Test the array access implementation
  850. *
  851. * @return void
  852. */
  853. public function testArrayAccess() {
  854. $request = new CakeRequest('some/path');
  855. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  856. $this->assertEquals('posts', $request['controller']);
  857. $request['slug'] = 'speedy-slug';
  858. $this->assertEquals('speedy-slug', $request->slug);
  859. $this->assertEquals('speedy-slug', $request['slug']);
  860. $this->assertTrue(isset($request['action']));
  861. $this->assertFalse(isset($request['wrong-param']));
  862. $this->assertTrue(isset($request['plugin']));
  863. unset($request['plugin']);
  864. $this->assertFalse(isset($request['plugin']));
  865. $this->assertNull($request['plugin']);
  866. $this->assertNull($request->plugin);
  867. $request = new CakeRequest('some/path?one=something&two=else');
  868. $this->assertTrue(isset($request['url']['one']));
  869. $request->data = array('Post' => array('title' => 'something'));
  870. $this->assertEquals('something', $request['data']['Post']['title']);
  871. }
  872. /**
  873. * Test adding detectors and having them work.
  874. *
  875. * @return void
  876. */
  877. public function testAddDetector() {
  878. $request = new CakeRequest('some/path');
  879. $request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
  880. $_SERVER['TEST_VAR'] = 'something';
  881. $this->assertTrue($request->is('compare'), 'Value match failed.');
  882. $_SERVER['TEST_VAR'] = 'wrong';
  883. $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
  884. $request->addDetector('compareCamelCase', array('env' => 'TEST_VAR', 'value' => 'foo'));
  885. $_SERVER['TEST_VAR'] = 'foo';
  886. $this->assertTrue($request->is('compareCamelCase'), 'Value match failed.');
  887. $this->assertTrue($request->is('comparecamelcase'), 'detectors should be case insensitive');
  888. $this->assertTrue($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  889. $_SERVER['TEST_VAR'] = 'not foo';
  890. $this->assertFalse($request->is('compareCamelCase'), 'Value match failed.');
  891. $this->assertFalse($request->is('comparecamelcase'), 'detectors should be case insensitive');
  892. $this->assertFalse($request->is('COMPARECAMELCASE'), 'detectors should be case insensitive');
  893. $request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
  894. $_SERVER['TEST_VAR'] = 'banana';
  895. $this->assertTrue($request->isBanana());
  896. $_SERVER['TEST_VAR'] = 'wrong value';
  897. $this->assertFalse($request->isBanana());
  898. $request->addDetector('mobile', array('options' => array('Imagination')));
  899. $_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
  900. $this->assertTrue($request->isMobile());
  901. $_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
  902. $this->assertTrue($request->isMobile());
  903. $request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, 'detectCallback')));
  904. $request->addDetector('index', array('param' => 'action', 'value' => 'index'));
  905. $request->params['action'] = 'index';
  906. $this->assertTrue($request->isIndex());
  907. $request->params['action'] = 'add';
  908. $this->assertFalse($request->isIndex());
  909. $request->return = true;
  910. $this->assertTrue($request->isCallMe());
  911. $request->return = false;
  912. $this->assertFalse($request->isCallMe());
  913. $request->addDetector('extension', array('param' => 'ext', 'options' => array('pdf', 'png', 'txt')));
  914. $request->params['ext'] = 'pdf';
  915. $this->assertTrue($request->is('extension'));
  916. $request->params['ext'] = 'exe';
  917. $this->assertFalse($request->isExtension());
  918. }
  919. /**
  920. * Helper function for testing callbacks.
  921. *
  922. * @param $request
  923. * @return bool
  924. */
  925. public function detectCallback($request) {
  926. return (bool)$request->return;
  927. }
  928. /**
  929. * Test getting headers
  930. *
  931. * @return void
  932. */
  933. public function testHeader() {
  934. $_SERVER['HTTP_HOST'] = 'localhost';
  935. $_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';
  936. $request = new CakeRequest('/', false);
  937. $this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
  938. $this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
  939. }
  940. /**
  941. * Test accepts() with and without parameters
  942. *
  943. * @return void
  944. */
  945. public function testAccepts() {
  946. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
  947. $request = new CakeRequest('/', false);
  948. $result = $request->accepts();
  949. $expected = array(
  950. 'text/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png', 'application/xml'
  951. );
  952. $this->assertEquals($expected, $result, 'Content types differ.');
  953. $result = $request->accepts('text/html');
  954. $this->assertTrue($result);
  955. $result = $request->accepts('image/gif');
  956. $this->assertFalse($result);
  957. }
  958. /**
  959. * Test that accept header types are trimmed for comparisons.
  960. *
  961. * @return void
  962. */
  963. public function testAcceptWithWhitespace() {
  964. $_SERVER['HTTP_ACCEPT'] = 'text/xml , text/html , text/plain,image/png';
  965. $request = new CakeRequest('/', false);
  966. $result = $request->accepts();
  967. $expected = array(
  968. 'text/xml', 'text/html', 'text/plain', 'image/png'
  969. );
  970. $this->assertEquals($expected, $result, 'Content types differ.');
  971. $this->assertTrue($request->accepts('text/html'));
  972. }
  973. /**
  974. * Content types from accepts() should respect the client's q preference values.
  975. *
  976. * @return void
  977. */
  978. public function testAcceptWithQvalueSorting() {
  979. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0';
  980. $request = new CakeRequest('/', false);
  981. $result = $request->accepts();
  982. $expected = array('application/xml', 'text/html', 'application/json');
  983. $this->assertEquals($expected, $result);
  984. }
  985. /**
  986. * Test the raw parsing of accept headers into the q value formatting.
  987. *
  988. * @return void
  989. */
  990. public function testParseAcceptWithQValue() {
  991. $_SERVER['HTTP_ACCEPT'] = 'text/html;q=0.8,application/json;q=0.7,application/xml;q=1.0,image/png';
  992. $request = new CakeRequest('/', false);
  993. $result = $request->parseAccept();
  994. $expected = array(
  995. '1.0' => array('application/xml', 'image/png'),
  996. '0.8' => array('text/html'),
  997. '0.7' => array('application/json'),
  998. );
  999. $this->assertEquals($expected, $result);
  1000. }
  1001. /**
  1002. * Test parsing accept with a confusing accept value.
  1003. *
  1004. * @return void
  1005. */
  1006. public function testParseAcceptNoQValues() {
  1007. $_SERVER['HTTP_ACCEPT'] = 'application/json, text/plain, */*';
  1008. $request = new CakeRequest('/', false);
  1009. $result = $request->parseAccept();
  1010. $expected = array(
  1011. '1.0' => array('application/json', 'text/plain', '*/*'),
  1012. );
  1013. $this->assertEquals($expected, $result);
  1014. }
  1015. /**
  1016. * Test parsing accept ignores index param
  1017. *
  1018. * @return void
  1019. */
  1020. public function testParseAcceptIgnoreAcceptExtensions() {
  1021. $_SERVER['HTTP_ACCEPT'] = 'application/json;level=1, text/plain, */*';
  1022. $request = new CakeRequest('/', false);
  1023. $result = $request->parseAccept();
  1024. $expected = array(
  1025. '1.0' => array('application/json', 'text/plain', '*/*'),
  1026. );
  1027. $this->assertEquals($expected, $result);
  1028. }
  1029. /**
  1030. * Test that parsing accept headers with invalid syntax works.
  1031. *
  1032. * The header used is missing a q value for application/xml.
  1033. *
  1034. * @return void
  1035. */
  1036. public function testParseAcceptInvalidSyntax() {
  1037. $_SERVER['HTTP_ACCEPT'] = 'text/html,application/xhtml+xml,application/xml;image/png,image/jpeg,image/*;q=0.9,*/*;q=0.8';
  1038. $request = new CakeRequest('/', false);
  1039. $result = $request->parseAccept();
  1040. $expected = array(
  1041. '1.0' => array('text/html', 'application/xhtml+xml', 'application/xml', 'image/jpeg'),
  1042. '0.9' => array('image/*'),
  1043. '0.8' => array('*/*'),
  1044. );
  1045. $this->assertEquals($expected, $result);
  1046. }
  1047. /**
  1048. * Test baseUrl and webroot with ModRewrite
  1049. *
  1050. * @return void
  1051. */
  1052. public function testBaseUrlAndWebrootWithModRewrite() {
  1053. Configure::write('App.baseUrl', false);
  1054. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  1055. $_SERVER['PHP_SELF'] = '/urlencode me/app/webroot/index.php';
  1056. $_SERVER['PATH_INFO'] = '/posts/view/1';
  1057. $request = new CakeRequest();
  1058. $this->assertEquals('/urlencode%20me', $request->base);
  1059. $this->assertEquals('/urlencode%20me/', $request->webroot);
  1060. $this->assertEquals('posts/view/1', $request->url);
  1061. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  1062. $_SERVER['PHP_SELF'] = '/1.2.x.x/app/webroot/index.php';
  1063. $_SERVER['PATH_INFO'] = '/posts/view/1';
  1064. $request = new CakeRequest();
  1065. $this->assertEquals('/1.2.x.x', $request->base);
  1066. $this->assertEquals('/1.2.x.x/', $request->webroot);
  1067. $this->assertEquals('posts/view/1', $request->url);
  1068. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
  1069. $_SERVER['PHP_SELF'] = '/index.php';
  1070. $_SERVER['PATH_INFO'] = '/posts/add';
  1071. $request = new CakeRequest();
  1072. $this->assertEquals('', $request->base);
  1073. $this->assertEquals('/', $request->webroot);
  1074. $this->assertEquals('posts/add', $request->url);
  1075. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  1076. $_SERVER['PHP_SELF'] = '/webroot/index.php';
  1077. $request = new CakeRequest();
  1078. $this->assertEquals('', $request->base);
  1079. $this->assertEquals('/', $request->webroot);
  1080. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  1081. $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
  1082. $request = new CakeRequest();
  1083. $this->assertEquals('', $request->base);
  1084. $this->assertEquals('/', $request->webroot);
  1085. Configure::write('App.dir', 'auth');
  1086. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  1087. $_SERVER['PHP_SELF'] = '/demos/auth/webroot/index.php';
  1088. $request = new CakeRequest();
  1089. $this->assertEquals('/demos/auth', $request->base);
  1090. $this->assertEquals('/demos/auth/', $request->webroot);
  1091. Configure::write('App.dir', 'code');
  1092. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  1093. $_SERVER['PHP_SELF'] = '/clients/PewterReport/code/webroot/index.php';
  1094. $request = new CakeRequest();
  1095. $this->assertEquals('/clients/PewterReport/code', $request->base);
  1096. $this->assertEquals('/clients/PewterReport/code/', $request->webroot);
  1097. }
  1098. /**
  1099. * Test baseUrl with ModRewrite alias
  1100. *
  1101. * @return void
  1102. */
  1103. public function testBaseUrlwithModRewriteAlias() {
  1104. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  1105. $_SERVER['PHP_SELF'] = '/control/index.php';
  1106. Configure::write('App.base', '/control');
  1107. $request = new CakeRequest();
  1108. $this->assertEquals('/control', $request->base);
  1109. $this->assertEquals('/control/', $request->webroot);
  1110. Configure::write('App.base', false);
  1111. Configure::write('App.dir', 'affiliate');
  1112. Configure::write('App.webroot', 'newaffiliate');
  1113. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  1114. $_SERVER['PHP_SELF'] = '/newaffiliate/index.php';
  1115. $request = new CakeRequest();
  1116. $this->assertEquals('/newaffiliate', $request->base);
  1117. $this->assertEquals('/newaffiliate/', $request->webroot);
  1118. }
  1119. /**
  1120. * Test base, webroot, URL and here parsing when there is URL rewriting but
  1121. * CakePHP gets called with index.php in URL nonetheless.
  1122. *
  1123. * Tests uri with
  1124. * - index.php/
  1125. * - index.php/
  1126. * - index.php/apples/
  1127. * - index.php/bananas/eat/tasty_banana
  1128. *
  1129. * @link https://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3318
  1130. * @return void
  1131. */
  1132. public function testBaseUrlWithModRewriteAndIndexPhp() {
  1133. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php';
  1134. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php';
  1135. unset($_SERVER['PATH_INFO']);
  1136. $request = new CakeRequest();
  1137. $this->assertEquals('/cakephp', $request->base);
  1138. $this->assertEquals('/cakephp/', $request->webroot);
  1139. $this->assertEquals('', $request->url);
  1140. $this->assertEquals('/cakephp/', $request->here);
  1141. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/';
  1142. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/';
  1143. $_SERVER['PATH_INFO'] = '/';
  1144. $request = new CakeRequest();
  1145. $this->assertEquals('/cakephp', $request->base);
  1146. $this->assertEquals('/cakephp/', $request->webroot);
  1147. $this->assertEquals('', $request->url);
  1148. $this->assertEquals('/cakephp/', $request->here);
  1149. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/apples';
  1150. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/apples';
  1151. $_SERVER['PATH_INFO'] = '/apples';
  1152. $request = new CakeRequest();
  1153. $this->assertEquals('/cakephp', $request->base);
  1154. $this->assertEquals('/cakephp/', $request->webroot);
  1155. $this->assertEquals('apples', $request->url);
  1156. $this->assertEquals('/cakephp/apples', $request->here);
  1157. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/melons/share/';
  1158. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/melons/share/';
  1159. $_SERVER['PATH_INFO'] = '/melons/share/';
  1160. $request = new CakeRequest();
  1161. $this->assertEquals('/cakephp', $request->base);
  1162. $this->assertEquals('/cakephp/', $request->webroot);
  1163. $this->assertEquals('melons/share/', $request->url);
  1164. $this->assertEquals('/cakephp/melons/share/', $request->here);
  1165. $_SERVER['REQUEST_URI'] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana';
  1166. $_SERVER['PHP_SELF'] = '/cakephp/app/webroot/index.php/bananas/eat/tasty_banana';
  1167. $_SERVER['PATH_INFO'] = '/bananas/eat/tasty_banana';
  1168. $request = new CakeRequest();
  1169. $this->assertEquals('/cakephp', $request->base);
  1170. $this->assertEquals('/cakephp/', $request->webroot);
  1171. $this->assertEquals('bananas/eat/tasty_banana', $request->url);
  1172. $this->assertEquals('/cakephp/bananas/eat/tasty_banana', $request->here);
  1173. }
  1174. /**
  1175. * Test base, webroot, and URL parsing when there is no URL rewriting
  1176. *
  1177. * @return void
  1178. */
  1179. public function testBaseUrlWithNoModRewrite() {
  1180. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
  1181. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
  1182. $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
  1183. $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
  1184. Configure::write('App', array(
  1185. 'dir' => APP_DIR,
  1186. 'webroot' => WEBROOT_DIR,
  1187. 'base' => false,
  1188. 'baseUrl' => '/cake/index.php'
  1189. ));
  1190. $request = new CakeRequest();
  1191. $this->assertEquals('/cake/index.php', $request->base);
  1192. $this->assertEquals('/cake/app/webroot/', $request->webroot);
  1193. $this->assertEquals('posts/index', $request->url);
  1194. }
  1195. /**
  1196. * Test baseUrl and webroot with baseUrl
  1197. *
  1198. * @return void
  1199. */
  1200. public function testBaseUrlAndWebrootWithBaseUrl() {
  1201. Configure::write('App.dir', 'app');
  1202. Configure::write('App.baseUrl', '/app/webroot/index.php');
  1203. $request = new CakeRequest();
  1204. $this->assertEquals('/app/webroot/index.php', $request->base);
  1205. $this->assertEquals('/app/webroot/', $request->webroot);
  1206. Configure::write('App.baseUrl', '/app/webroot/test.php');
  1207. $request = new CakeRequest();
  1208. $this->assertEquals('/app/webroot/test.php', $request->base);
  1209. $this->assertEquals('/app/webroot/', $request->webroot);
  1210. Configure::write('App.baseUrl', '/app/index.php');
  1211. $request = new CakeRequest();
  1212. $this->assertEquals('/app/index.php', $request->base);
  1213. $this->assertEquals('/app/webroot/', $request->webroot);
  1214. Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
  1215. $request = new CakeRequest();
  1216. $this->assertEquals('/CakeBB/app/webroot/index.php', $request->base);
  1217. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1218. Configure::write('App.baseUrl', '/CakeBB/app/index.php');
  1219. $request = new CakeRequest();
  1220. $this->assertEquals('/CakeBB/app/index.php', $request->base);
  1221. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1222. Configure::write('App.baseUrl', '/CakeBB/index.php');
  1223. $request = new CakeRequest();
  1224. $this->assertEquals('/CakeBB/index.php', $request->base);
  1225. $this->assertEquals('/CakeBB/app/webroot/', $request->webroot);
  1226. Configure::write('App.baseUrl', '/dbhauser/index.php');
  1227. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  1228. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  1229. $request = new CakeRequest();
  1230. $this->assertEquals('/dbhauser/index.php', $request->base);
  1231. $this->assertEquals('/dbhauser/app/webroot/', $request->webroot);
  1232. }
  1233. /**
  1234. * Test baseUrl with no rewrite and using the top level index.php.
  1235. *
  1236. * @return void
  1237. */
  1238. public function testBaseUrlNoRewriteTopLevelIndex() {
  1239. Configure::write('App.baseUrl', '/index.php');
  1240. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
  1241. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
  1242. $request = new CakeRequest();
  1243. $this->assertEquals('/index.php', $request->base);
  1244. $this->assertEquals('/app/webroot/', $request->webroot);
  1245. }
  1246. /**
  1247. * Check that a sub-directory containing app|webroot doesn't get mishandled when re-writing is off.
  1248. *
  1249. * @return void
  1250. */
  1251. public function testBaseUrlWithAppAndWebrootInDirname() {
  1252. Configure::write('App.baseUrl', '/approval/index.php');
  1253. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1254. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/approval/index.php';
  1255. $request = new CakeRequest();
  1256. $this->assertEquals('/approval/index.php', $request->base);
  1257. $this->assertEquals('/approval/app/webroot/', $request->webroot);
  1258. Configure::write('App.baseUrl', '/webrootable/index.php');
  1259. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/';
  1260. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/webrootable/index.php';
  1261. $request = new CakeRequest();
  1262. $this->assertEquals('/webrootable/index.php', $request->base);
  1263. $this->assertEquals('/webrootable/app/webroot/', $request->webroot);
  1264. }
  1265. /**
  1266. * Test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
  1267. *
  1268. * @return void
  1269. */
  1270. public function testBaseUrlNoRewriteWebrootIndex() {
  1271. Configure::write('App.baseUrl', '/index.php');
  1272. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
  1273. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
  1274. $request = new CakeRequest();
  1275. $this->assertEquals('/index.php', $request->base);
  1276. $this->assertEquals('/', $request->webroot);
  1277. }
  1278. /**
  1279. * Test that a request with a . in the main GET parameter is filtered out.
  1280. * PHP changes GET parameter keys containing dots to _.
  1281. *
  1282. * @return void
  1283. */
  1284. public function testGetParamsWithDot() {
  1285. $_GET = array();
  1286. $_GET['/posts/index/add_add'] = '';
  1287. $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
  1288. $_SERVER['REQUEST_URI'] = '/posts/index/add.add';
  1289. $request = new CakeRequest();
  1290. $this->assertEquals('', $request->base);
  1291. $this->assertEquals(array(), $request->query);
  1292. $_GET = array();
  1293. $_GET['/cake_dev/posts/index/add_add'] = '';
  1294. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1295. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/index/add.add';
  1296. $request = new CakeRequest();
  1297. $this->assertEquals('/cake_dev', $request->base);
  1298. $this->assertEquals(array(), $request->query);
  1299. }
  1300. /**
  1301. * Test that a request with urlencoded bits in the main GET parameter are filtered out.
  1302. *
  1303. * @return void
  1304. */
  1305. public function testGetParamWithUrlencodedElement() {
  1306. $_GET = array();
  1307. $_GET['/posts/add/∂∂'] = '';
  1308. $_SERVER['PHP_SELF'] = '/app/webroot/index.php';
  1309. $_SERVER['REQUEST_URI'] = '/posts/add/%E2%88%82%E2%88%82';
  1310. $request = new CakeRequest();
  1311. $this->assertEquals('', $request->base);
  1312. $this->assertEquals(array(), $request->query);
  1313. $_GET = array();
  1314. $_GET['/cake_dev/posts/add/∂∂'] = '';
  1315. $_SERVER['PHP_SELF'] = '/cake_dev/app/webroot/index.php';
  1316. $_SERVER['REQUEST_URI'] = '/cake_dev/posts/add/%E2%88%82%E2%88%82';
  1317. $request = new CakeRequest();
  1318. $this->assertEquals('/cake_dev', $request->base);
  1319. $this->assertEquals(array(), $request->query);
  1320. }
  1321. /**
  1322. * Generator for environment configurations
  1323. *
  1324. * @return array Environment array
  1325. */
  1326. public static function environmentGenerator() {
  1327. return array(
  1328. array(
  1329. 'IIS - No rewrite base path',
  1330. array(
  1331. 'App' => array(
  1332. 'base' => false,
  1333. 'baseUrl' => '/index.php',
  1334. 'dir' => 'app',
  1335. 'webroot' => 'webroot'
  1336. ),
  1337. 'SERVER' => array(
  1338. 'SCRIPT_NAME' => '/index.php',
  1339. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1340. 'QUERY_STRING' => '',
  1341. 'REQUEST_URI' => '/index.php',
  1342. 'URL' => '/index.php',
  1343. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  1344. 'ORIG_PATH_INFO' => '/index.php',
  1345. 'PATH_INFO' => '',
  1346. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  1347. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1348. 'PHP_SELF' => '/index.php',
  1349. ),
  1350. ),
  1351. array(
  1352. 'base' => '/index.php',
  1353. 'webroot' => '/app/webroot/',
  1354. 'url' => ''
  1355. ),
  1356. ),
  1357. array(
  1358. 'IIS - No rewrite with path, no PHP_SELF',
  1359. array(
  1360. 'App' => array(
  1361. 'base' => false,
  1362. 'baseUrl' => '/index.php?',
  1363. 'dir' => 'app',
  1364. 'webroot' => 'webroot'
  1365. ),
  1366. 'SERVER' => array(
  1367. 'QUERY_STRING' => '/posts/add',
  1368. 'REQUEST_URI' => '/index.php?/posts/add',
  1369. 'PHP_SELF' => '',
  1370. 'URL' => '/index.php?/posts/add',
  1371. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1372. 'argv' => array('/posts/add'),
  1373. 'argc' => 1
  1374. ),
  1375. ),
  1376. array(
  1377. 'url' => 'posts/add',
  1378. 'base' => '/index.php?',
  1379. 'webroot' => '/app/webroot/'
  1380. )
  1381. ),
  1382. array(
  1383. 'IIS - No rewrite sub dir 2',
  1384. array(
  1385. 'App' => array(
  1386. 'base' => false,
  1387. 'baseUrl' => '/site/index.php',
  1388. 'dir' => 'app',
  1389. 'webroot' => 'webroot',
  1390. ),
  1391. 'SERVER' => array(
  1392. 'SCRIPT_NAME' => '/site/index.php',
  1393. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1394. 'QUERY_STRING' => '',
  1395. 'REQUEST_URI' => '/site/index.php',
  1396. 'URL' => '/site/index.php',
  1397. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1398. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1399. 'PHP_SELF' => '/site/index.php',
  1400. 'argv' => array(),
  1401. 'argc' => 0
  1402. ),
  1403. ),
  1404. array(
  1405. 'url' => '',
  1406. 'base' => '/site/index.php',
  1407. 'webroot' => '/site/app/webroot/'
  1408. ),
  1409. ),
  1410. array(
  1411. 'IIS - No rewrite sub dir 2 with path',
  1412. array(
  1413. 'App' => array(
  1414. 'base' => false,
  1415. 'baseUrl' => '/site/index.php',
  1416. 'dir' => 'app',
  1417. 'webroot' => 'webroot'
  1418. ),
  1419. 'GET' => array('/posts/add' => ''),
  1420. 'SERVER' => array(
  1421. 'SCRIPT_NAME' => '/site/index.php',
  1422. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  1423. 'QUERY_STRING' => '/posts/add',
  1424. 'REQUEST_URI' => '/site/index.php/posts/add',
  1425. 'URL' => '/site/index.php/posts/add',
  1426. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  1427. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  1428. 'PHP_SELF' => '/site/index.php/posts/add',
  1429. 'argv' => array('/posts/add'),
  1430. 'argc' => 1
  1431. ),
  1432. ),
  1433. array(
  1434. 'url' => 'posts/add',
  1435. 'base' => '/site/index.php',
  1436. 'webroot' => '/site/app/webroot/'
  1437. )
  1438. ),
  1439. array(
  1440. 'Apache - No rewrite, document root set to webroot, requesting path',
  1441. array(
  1442. 'App' => array(
  1443. 'base' => false,
  1444. 'baseUrl' => '/index.php',
  1445. 'dir' => 'app',
  1446. 'webroot' => 'webroot'
  1447. ),
  1448. 'SERVER' => array(
  1449. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1450. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1451. 'QUERY_STRING' => '',
  1452. 'REQUEST_URI' => '/index.php/posts/index',
  1453. 'SCRIPT_NAME' => '/index.php',
  1454. 'PATH_INFO' => '/posts/index',
  1455. 'PHP_SELF' => '/index.php/posts/index',
  1456. ),
  1457. ),
  1458. array(
  1459. 'url' => 'posts/index',
  1460. 'base' => '/index.php',
  1461. 'webroot' => '/'
  1462. ),
  1463. ),
  1464. array(
  1465. 'Apache - No rewrite, document root set to webroot, requesting root',
  1466. array(
  1467. 'App' => array(
  1468. 'base' => false,
  1469. 'baseUrl' => '/index.php',
  1470. 'dir' => 'app',
  1471. 'webroot' => 'webroot'
  1472. ),
  1473. 'SERVER' => array(
  1474. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1475. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1476. 'QUERY_STRING' => '',
  1477. 'REQUEST_URI' => '/index.php',
  1478. 'SCRIPT_NAME' => '/index.php',
  1479. 'PATH_INFO' => '',
  1480. 'PHP_SELF' => '/index.php',
  1481. ),
  1482. ),
  1483. array(
  1484. 'url' => '',
  1485. 'base' => '/index.php',
  1486. 'webroot' => '/'
  1487. ),
  1488. ),
  1489. array(
  1490. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1491. array(
  1492. 'App' => array(
  1493. 'base' => false,
  1494. 'baseUrl' => '/site/index.php',
  1495. 'dir' => 'app',
  1496. 'webroot' => 'webroot'
  1497. ),
  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',
  1503. 'SCRIPT_NAME' => '/site/index.php',
  1504. 'PATH_INFO' => '/posts/index',
  1505. 'PHP_SELF' => '/site/index.php/posts/index',
  1506. ),
  1507. ),
  1508. array(
  1509. 'url' => 'posts/index',
  1510. 'base' => '/site/index.php',
  1511. 'webroot' => '/site/app/webroot/',
  1512. ),
  1513. ),
  1514. array(
  1515. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1516. array(
  1517. 'App' => array(
  1518. 'base' => false,
  1519. 'baseUrl' => '/site/index.php',
  1520. 'dir' => 'app',
  1521. 'webroot' => 'webroot'
  1522. ),
  1523. 'SERVER' => array(
  1524. 'SERVER_NAME' => 'localhost',
  1525. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1526. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1527. 'REQUEST_URI' => '/site/index.php/',
  1528. 'SCRIPT_NAME' => '/site/index.php',
  1529. 'PHP_SELF' => '/site/index.php/',
  1530. ),
  1531. ),
  1532. array(
  1533. 'url' => '',
  1534. 'base' => '/site/index.php',
  1535. 'webroot' => '/site/app/webroot/',
  1536. ),
  1537. ),
  1538. array(
  1539. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1540. array(
  1541. 'App' => array(
  1542. 'base' => false,
  1543. 'baseUrl' => '/site/index.php',
  1544. 'dir' => 'app',
  1545. 'webroot' => 'webroot'
  1546. ),
  1547. 'GET' => array('a' => 'b', 'c' => 'd'),
  1548. 'SERVER' => array(
  1549. 'SERVER_NAME' => 'localhost',
  1550. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1551. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1552. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1553. 'SCRIPT_NAME' => '/site/index.php',
  1554. 'PATH_INFO' => '/posts/index',
  1555. 'PHP_SELF' => '/site/index.php/posts/index',
  1556. 'QUERY_STRING' => 'a=b&c=d'
  1557. ),
  1558. ),
  1559. array(
  1560. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1561. 'url' => 'posts/index',
  1562. 'base' => '/site/index.php',
  1563. 'webroot' => '/site/app/webroot/',
  1564. ),
  1565. ),
  1566. array(
  1567. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1568. array(
  1569. 'App' => array(
  1570. 'base' => false,
  1571. 'baseUrl' => false,
  1572. 'dir' => 'app',
  1573. 'webroot' => 'webroot'
  1574. ),
  1575. 'SERVER' => array(
  1576. 'SERVER_NAME' => 'localhost',
  1577. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1578. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1579. 'REQUEST_URI' => '/site/',
  1580. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1581. 'PHP_SELF' => '/site/app/webroot/index.php',
  1582. ),
  1583. ),
  1584. array(
  1585. 'url' => '',
  1586. 'base' => '/site',
  1587. 'webroot' => '/site/',
  1588. ),
  1589. ),
  1590. array(
  1591. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/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. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1604. 'PHP_SELF' => '/site/app/webroot/index.php',
  1605. 'PATH_INFO' => null,
  1606. 'REQUEST_URI' => null,
  1607. ),
  1608. ),
  1609. array(
  1610. 'url' => '',
  1611. 'base' => '/site',
  1612. 'webroot' => '/site/',
  1613. ),
  1614. ),
  1615. array(
  1616. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1617. array(
  1618. 'App' => array(
  1619. 'base' => false,
  1620. 'baseUrl' => false,
  1621. 'dir' => 'app',
  1622. 'webroot' => 'webroot'
  1623. ),
  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. 'PHP_SELF' => '/index.php',
  1630. 'PATH_INFO' => null,
  1631. 'REQUEST_URI' => null,
  1632. ),
  1633. ),
  1634. array(
  1635. 'url' => '',
  1636. 'base' => '',
  1637. 'webroot' => '/',
  1638. ),
  1639. ),
  1640. array(
  1641. 'Apache - w/rewrite, document root set above top level cake dir, request root, absolute REQUEST_URI',
  1642. array(
  1643. 'App' => array(
  1644. 'base' => false,
  1645. 'baseUrl' => false,
  1646. 'dir' => 'app',
  1647. 'webroot' => 'webroot'
  1648. ),
  1649. 'SERVER' => array(
  1650. 'SERVER_NAME' => 'localhost',
  1651. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1652. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1653. 'REQUEST_URI' => '/site/posts/index',
  1654. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1655. 'PHP_SELF' => '/site/app/webroot/index.php',
  1656. ),
  1657. ),
  1658. array(
  1659. 'url' => 'posts/index',
  1660. 'base' => '/site',
  1661. 'webroot' => '/site/',
  1662. ),
  1663. ),
  1664. array(
  1665. 'Nginx - w/rewrite, document root set to webroot, request root, no PATH_INFO',
  1666. array(
  1667. 'App' => array(
  1668. 'base' => false,
  1669. 'baseUrl' => false,
  1670. 'dir' => 'app',
  1671. 'webroot' => 'webroot'
  1672. ),
  1673. 'GET' => array('/posts/add' => ''),
  1674. 'SERVER' => array(
  1675. 'SERVER_NAME' => 'localhost',
  1676. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1677. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1678. 'SCRIPT_NAME' => '/index.php',
  1679. 'QUERY_STRING' => '/posts/add&',
  1680. 'PHP_SELF' => '/index.php',
  1681. 'PATH_INFO' => null,
  1682. 'REQUEST_URI' => '/posts/add',
  1683. ),
  1684. ),
  1685. array(
  1686. 'url' => 'posts/add',
  1687. 'base' => '',
  1688. 'webroot' => '/',
  1689. 'urlParams' => array()
  1690. ),
  1691. ),
  1692. array(
  1693. 'Nginx - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO, base parameter set',
  1694. array(
  1695. 'App' => array(
  1696. 'base' => false,
  1697. 'baseUrl' => false,
  1698. 'dir' => 'app',
  1699. 'webroot' => 'webroot'
  1700. ),
  1701. 'GET' => array('/site/posts/add' => ''),
  1702. 'SERVER' => array(
  1703. 'SERVER_NAME' => 'localhost',
  1704. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1705. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1706. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1707. 'QUERY_STRING' => '/site/posts/add&',
  1708. 'PHP_SELF' => '/site/app/webroot/index.php',
  1709. 'PATH_INFO' => null,
  1710. 'REQUEST_URI' => '/site/posts/add',
  1711. ),
  1712. ),
  1713. array(
  1714. 'url' => 'posts/add',
  1715. 'base' => '/site',
  1716. 'webroot' => '/site/',
  1717. 'urlParams' => array()
  1718. ),
  1719. ),
  1720. );
  1721. }
  1722. /**
  1723. * Test environment detection
  1724. *
  1725. * @dataProvider environmentGenerator
  1726. * @param $name
  1727. * @param $env
  1728. * @param $expected
  1729. * @return void
  1730. */
  1731. public function testEnvironmentDetection($name, $env, $expected) {
  1732. $_GET = array();
  1733. $this->_loadEnvironment($env);
  1734. $request = new CakeRequest();
  1735. $this->assertEquals($expected['url'], $request->url, "url error");
  1736. $this->assertEquals($expected['base'], $request->base, "base error");
  1737. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1738. if (isset($expected['urlParams'])) {
  1739. $this->assertEquals($expected['urlParams'], $request->query, "GET param mismatch");
  1740. }
  1741. }
  1742. /**
  1743. * Test the query() method
  1744. *
  1745. * @return void
  1746. */
  1747. public function testQuery() {
  1748. $_GET = array();
  1749. $_GET['foo'] = 'bar';
  1750. $request = new CakeRequest();
  1751. $result = $request->query('foo');
  1752. $this->assertEquals('bar', $result);
  1753. $result = $request->query('imaginary');
  1754. $this->assertNull($result);
  1755. }
  1756. /**
  1757. * Test the query() method with arrays passed via $_GET
  1758. *
  1759. * @return void
  1760. */
  1761. public function testQueryWithArray() {
  1762. $_GET = array();
  1763. $_GET['test'] = array('foo', 'bar');
  1764. $request = new CakeRequest();
  1765. $result = $request->query('test');
  1766. $this->assertEquals(array('foo', 'bar'), $result);
  1767. $result = $request->query('test.1');
  1768. $this->assertEquals('bar', $result);
  1769. $result = $request->query('test.2');
  1770. $this->assertNull($result);
  1771. }
  1772. /**
  1773. * Test using param()
  1774. *
  1775. * @return void
  1776. */
  1777. public function testReadingParams() {
  1778. $request = new CakeRequest();
  1779. $request->addParams(array(
  1780. 'controller' => 'posts',
  1781. 'admin' => true,
  1782. 'truthy' => 1,
  1783. 'zero' => '0',
  1784. ));
  1785. $this->assertFalse($request->param('not_set'));
  1786. $this->assertTrue($request->param('admin'));
  1787. $this->assertEquals(1, $request->param('truthy'));
  1788. $this->assertEquals('posts', $request->param('controller'));
  1789. $this->assertEquals('0', $request->param('zero'));
  1790. }
  1791. /**
  1792. * Test the data() method reading
  1793. *
  1794. * @return void
  1795. */
  1796. public function testDataReading() {
  1797. $_POST['data'] = array(
  1798. 'Model' => array(
  1799. 'field' => 'value'
  1800. )
  1801. );
  1802. $request = new CakeRequest('posts/index');
  1803. $result = $request->data('Model');
  1804. $this->assertEquals($_POST['data']['Model'], $result);
  1805. $result = $request->data('Model.imaginary');
  1806. $this->assertNull($result);
  1807. }
  1808. /**
  1809. * Test writing with data()
  1810. *
  1811. * @return void
  1812. */
  1813. public function testDataWriting() {
  1814. $_POST['data'] = array(
  1815. 'Model' => array(
  1816. 'field' => 'value'
  1817. )
  1818. );
  1819. $request = new CakeRequest('posts/index');
  1820. $result = $request->data('Model.new_value', 'new value');
  1821. $this->assertSame($result, $request, 'Return was not $this');
  1822. $this->assertEquals('new value', $request->data['Model']['new_value']);
  1823. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1824. $this->assertEquals('New post', $request->data['Post']['title']);
  1825. $this->assertEquals('Mark', $request->data['Comment']['1']['author']);
  1826. }
  1827. /**
  1828. * Test writing falsey values.
  1829. *
  1830. * @return void
  1831. */
  1832. public function testDataWritingFalsey() {
  1833. $request = new CakeRequest('posts/index');
  1834. $request->data('Post.null', null);
  1835. $this->assertNull($request->data['Post']['null']);
  1836. $request->data('Post.false', false);
  1837. $this->assertFalse($request->data['Post']['false']);
  1838. $request->data('Post.zero', 0);
  1839. $this->assertSame(0, $request->data['Post']['zero']);
  1840. $request->data('Post.empty', '');
  1841. $this->assertSame('', $request->data['Post']['empty']);
  1842. }
  1843. /**
  1844. * Test accept language
  1845. *
  1846. * @return void
  1847. */
  1848. public function testAcceptLanguage() {
  1849. // Weird language
  1850. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
  1851. $result = CakeRequest::acceptLanguage();
  1852. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1853. // No qualifier
  1854. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1855. $result = CakeRequest::acceptLanguage();
  1856. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1857. // With qualifier
  1858. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-US,en;q=0.8,pt-BR;q=0.6,pt;q=0.4';
  1859. $result = CakeRequest::acceptLanguage();
  1860. $this->assertEquals(array('en-us', 'en', 'pt-br', 'pt'), $result, 'Languages do not match');
  1861. // With spaces
  1862. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'da, en-gb;q=0.8, en;q=0.7';
  1863. $result = CakeRequest::acceptLanguage();
  1864. $this->assertEquals(array('da', 'en-gb', 'en'), $result, 'Languages do not match');
  1865. // Checking if requested
  1866. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx,en_ca';
  1867. $result = CakeRequest::acceptLanguage();
  1868. $result = CakeRequest::acceptLanguage('en-ca');
  1869. $this->assertTrue($result);
  1870. $result = CakeRequest::acceptLanguage('en-CA');
  1871. $this->assertTrue($result);
  1872. $result = CakeRequest::acceptLanguage('en-us');
  1873. $this->assertFalse($result);
  1874. $result = CakeRequest::acceptLanguage('en-US');
  1875. $this->assertFalse($result);
  1876. }
  1877. /**
  1878. * Test the here() method
  1879. *
  1880. * @return void
  1881. */
  1882. public function testHere() {
  1883. Configure::write('App.base', '/base_path');
  1884. $_GET = array('test' => 'value');
  1885. $request = new CakeRequest('/posts/add/1/name:value');
  1886. $result = $request->here();
  1887. $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
  1888. $result = $request->here(false);
  1889. $this->assertEquals('/posts/add/1/name:value?test=value', $result);
  1890. $request = new CakeRequest('/posts/base_path/1/name:value');
  1891. $result = $request->here();
  1892. $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
  1893. $result = $request->here(false);
  1894. $this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
  1895. }
  1896. /**
  1897. * Test the here() with space in URL
  1898. *
  1899. * @return void
  1900. */
  1901. public function testHereWithSpaceInUrl() {
  1902. Configure::write('App.base', '');
  1903. $_GET = array('/admin/settings/settings/prefix/Access_Control' => '');
  1904. $request = new CakeRequest('/admin/settings/settings/prefix/Access%20Control');
  1905. $result = $request->here();
  1906. $this->assertEquals('/admin/settings/settings/prefix/Access%20Control', $result);
  1907. }
  1908. /**
  1909. * Test the input() method.
  1910. *
  1911. * @return void
  1912. */
  1913. public function testInput() {
  1914. $request = $this->getMock('CakeRequest', array('_readInput'));
  1915. $request->expects($this->once())->method('_readInput')
  1916. ->will($this->returnValue('I came from stdin'));
  1917. $result = $request->input();
  1918. $this->assertEquals('I came from stdin', $result);
  1919. }
  1920. /**
  1921. * Test input() decoding.
  1922. *
  1923. * @return void
  1924. */
  1925. public function testInputDecode() {
  1926. $request = $this->getMock('CakeRequest', array('_readInput'));
  1927. $request->expects($this->once())->method('_readInput')
  1928. ->will($this->returnValue('{"name":"value"}'));
  1929. $result = $request->input('json_decode');
  1930. $this->assertEquals(array('name' => 'value'), (array)$result);
  1931. }
  1932. /**
  1933. * Test input() decoding with additional arguments.
  1934. *
  1935. * @return void
  1936. */
  1937. public function testInputDecodeExtraParams() {
  1938. $xml = <<<XML
  1939. <?xml version="1.0" encoding="utf-8"?>
  1940. <post>
  1941. <title id="title">Test</title>
  1942. </post>
  1943. XML;
  1944. $request = $this->getMock('CakeRequest', array('_readInput'));
  1945. $request->expects($this->once())->method('_readInput')
  1946. ->will($this->returnValue($xml));
  1947. $result = $request->input('Xml::build', array('return' => 'domdocument'));
  1948. $this->assertInstanceOf('DOMDocument', $result);
  1949. $this->assertEquals(
  1950. 'Test',
  1951. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1952. );
  1953. }
  1954. /**
  1955. * Test is('requested') and isRequested()
  1956. *
  1957. * @return void
  1958. */
  1959. public function testIsRequested() {
  1960. $request = new CakeRequest('/posts/index');
  1961. $request->addParams(array(
  1962. 'controller' => 'posts',
  1963. 'action' => 'index',
  1964. 'plugin' => null,
  1965. 'requested' => 1
  1966. ));
  1967. $this->assertTrue($request->is('requested'));
  1968. $this->assertTrue($request->isRequested());
  1969. $request = new CakeRequest('/posts/index');
  1970. $request->addParams(array(
  1971. 'controller' => 'posts',
  1972. 'action' => 'index',
  1973. 'plugin' => null,
  1974. ));
  1975. $this->assertFalse($request->is('requested'));
  1976. $this->assertFalse($request->isRequested());
  1977. }
  1978. /**
  1979. * Test allowMethod method
  1980. *
  1981. * @return void
  1982. */
  1983. public function testAllowMethod() {
  1984. $_SERVER['REQUEST_METHOD'] = 'PUT';
  1985. $request = new CakeRequest('/posts/edit/1');
  1986. $this->assertTrue($request->allowMethod(array('put')));
  1987. // BC check
  1988. $this->assertTrue($request->onlyAllow(array('put')));
  1989. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  1990. $this->assertTrue($request->allowMethod('post', 'delete'));
  1991. // BC check
  1992. $this->assertTrue($request->onlyAllow('post', 'delete'));
  1993. }
  1994. /**
  1995. * Test allowMethod throwing exception
  1996. *
  1997. * @return void
  1998. */
  1999. public function testAllowMethodException() {
  2000. $_SERVER['REQUEST_METHOD'] = 'PUT';
  2001. $request = new CakeRequest('/posts/edit/1');
  2002. try {
  2003. $request->allowMethod('POST', 'DELETE');
  2004. $this->fail('An expected exception has not been raised.');
  2005. } catch (MethodNotAllowedException $e) {
  2006. $this->assertEquals(array('Allow' => 'POST, DELETE'), $e->responseHeader());
  2007. }
  2008. $this->setExpectedException('MethodNotAllowedException');
  2009. $request->allowMethod('POST');
  2010. }
  2011. /**
  2012. * loadEnvironment method
  2013. *
  2014. * @param array $env
  2015. * @return void
  2016. */
  2017. protected function _loadEnvironment($env) {
  2018. if (isset($env['App'])) {
  2019. Configure::write('App', $env['App']);
  2020. }
  2021. if (isset($env['GET'])) {
  2022. foreach ($env['GET'] as $key => $val) {
  2023. $_GET[$key] = $val;
  2024. }
  2025. }
  2026. if (isset($env['POST'])) {
  2027. foreach ($env['POST'] as $key => $val) {
  2028. $_POST[$key] = $val;
  2029. }
  2030. }
  2031. if (isset($env['SERVER'])) {
  2032. foreach ($env['SERVER'] as $key => $val) {
  2033. $_SERVER[$key] = $val;
  2034. }
  2035. }
  2036. }
  2037. }