CakeRequestTest.php 57 KB

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