CakeRequestTest.php 63 KB

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