CakeRequestTest.php 52 KB

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