CakeRequestTest.php 56 KB

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