CakeRequestTest.php 53 KB

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