CakeRequestTest.php 56 KB

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