CakeRequestTest.php 58 KB

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