CakeRequestTest.php 54 KB

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