CakeRequestTest.php 62 KB

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