CakeRequestTest.php 56 KB

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