CakeRequestTest.php 57 KB

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