CakeRequestTest.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.tests.cases.libs
  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 CakeRequestTestCase extends CakeTestCase {
  23. /**
  24. * setup callback
  25. *
  26. * @return void
  27. */
  28. function setUp() {
  29. parent::setUp();
  30. $this->_server = $_SERVER;
  31. $this->_get = $_GET;
  32. $this->_post = $_POST;
  33. $this->_files = $_FILES;
  34. $this->_app = Configure::read('App');
  35. $this->_case = null;
  36. if (isset($_GET['case'])) {
  37. $this->_case = $_GET['case'];
  38. unset($_GET['case']);
  39. }
  40. Configure::write('App.baseUrl', false);
  41. }
  42. /**
  43. * tearDown-
  44. *
  45. * @return void
  46. */
  47. function tearDown() {
  48. parent::tearDown();
  49. $_SERVER = $this->_server;
  50. $_GET = $this->_get;
  51. $_POST = $this->_post;
  52. $_FILES = $this->_files;
  53. if (!empty($this->_case)) {
  54. $_GET['case'] = $this->_case;
  55. }
  56. Configure::write('App', $this->_app);
  57. }
  58. /**
  59. * test that the autoparse = false constructor works.
  60. *
  61. * @return void
  62. */
  63. function testNoAutoParseConstruction() {
  64. $_GET = array(
  65. 'one' => 'param'
  66. );
  67. $request = new CakeRequest(null, false);
  68. $this->assertFalse(isset($request->query['one']));
  69. }
  70. /**
  71. * test construction
  72. *
  73. * @return void
  74. */
  75. function testConstructionGetParsing() {
  76. $_GET = array(
  77. 'one' => 'param',
  78. 'two' => 'banana'
  79. );
  80. $request = new CakeRequest('some/path');
  81. $this->assertEqual($request->query, $_GET);
  82. $_GET = array(
  83. 'one' => 'param',
  84. 'two' => 'banana',
  85. );
  86. $request = new CakeRequest('some/path');
  87. $this->assertEqual($request->query, $_GET);
  88. $this->assertEqual($request->url, 'some/path');
  89. }
  90. /**
  91. * Test that querystring args provided in the url string are parsed.
  92. *
  93. * @return void
  94. */
  95. function testQueryStringParsingFromInputUrl() {
  96. $_GET = array();
  97. $request = new CakeRequest('some/path?one=something&two=else');
  98. $expected = array('one' => 'something', 'two' => 'else');
  99. $this->assertEqual($request->query, $expected);
  100. $this->assertEquals('some/path?one=something&two=else', $request->url);
  101. }
  102. /**
  103. * test addParams() method
  104. *
  105. * @return void
  106. */
  107. function testAddParams() {
  108. $request = new CakeRequest('some/path');
  109. $request->params = array('controller' => 'posts', 'action' => 'view');
  110. $result = $request->addParams(array('plugin' => null, 'action' => 'index'));
  111. $this->assertIdentical($result, $request, 'Method did not return itself. %s');
  112. $this->assertEqual($request->controller, 'posts');
  113. $this->assertEqual($request->action, 'index');
  114. $this->assertEqual($request->plugin, null);
  115. }
  116. /**
  117. * test splicing in paths.
  118. *
  119. * @return void
  120. */
  121. function testAddPaths() {
  122. $request = new CakeRequest('some/path');
  123. $request->webroot = '/some/path/going/here/';
  124. $result = $request->addPaths(array(
  125. 'random' => '/something', 'webroot' => '/', 'here' => '/', 'base' => '/base_dir'
  126. ));
  127. $this->assertIdentical($result, $request, 'Method did not return itself. %s');
  128. $this->assertEqual($request->webroot, '/');
  129. $this->assertEqual($request->base, '/base_dir');
  130. $this->assertEqual($request->here, '/');
  131. $this->assertFalse(isset($request->random));
  132. }
  133. /**
  134. * test parsing POST data into the object.
  135. *
  136. * @return void
  137. */
  138. function testPostParsing() {
  139. $_POST = array('data' => array(
  140. 'Article' => array('title')
  141. ));
  142. $request = new CakeRequest('some/path');
  143. $this->assertEqual($request->data, $_POST['data']);
  144. $_POST = array('one' => 1, 'two' => 'three');
  145. $request = new CakeRequest('some/path');
  146. $this->assertEquals($_POST, $request->data);
  147. }
  148. /**
  149. * test parsing of FILES array
  150. *
  151. * @return void
  152. */
  153. function testFILESParsing() {
  154. $_FILES = array('data' => array('name' => array(
  155. 'File' => array(
  156. array('data' => 'cake_sqlserver_patch.patch'),
  157. array('data' => 'controller.diff'),
  158. array('data' => ''),
  159. array('data' => ''),
  160. ),
  161. 'Post' => array('attachment' => 'jquery-1.2.1.js'),
  162. ),
  163. 'type' => array(
  164. 'File' => array(
  165. array('data' => ''),
  166. array('data' => ''),
  167. array('data' => ''),
  168. array('data' => ''),
  169. ),
  170. 'Post' => array('attachment' => 'application/x-javascript'),
  171. ),
  172. 'tmp_name' => array(
  173. 'File' => array(
  174. array('data' => '/private/var/tmp/phpy05Ywj'),
  175. array('data' => '/private/var/tmp/php7MBztY'),
  176. array('data' => ''),
  177. array('data' => ''),
  178. ),
  179. 'Post' => array('attachment' => '/private/var/tmp/phpEwlrIo'),
  180. ),
  181. 'error' => array(
  182. 'File' => array(
  183. array('data' => 0),
  184. array('data' => 0),
  185. array('data' => 4),
  186. array('data' => 4)
  187. ),
  188. 'Post' => array('attachment' => 0)
  189. ),
  190. 'size' => array(
  191. 'File' => array(
  192. array('data' => 6271),
  193. array('data' => 350),
  194. array('data' => 0),
  195. array('data' => 0),
  196. ),
  197. 'Post' => array('attachment' => 80469)
  198. ),
  199. ));
  200. $request = new CakeRequest('some/path');
  201. $expected = array(
  202. 'File' => array(
  203. array('data' => array(
  204. 'name' => 'cake_sqlserver_patch.patch',
  205. 'type' => '',
  206. 'tmp_name' => '/private/var/tmp/phpy05Ywj',
  207. 'error' => 0,
  208. 'size' => 6271,
  209. )),
  210. array(
  211. 'data' => array(
  212. 'name' => 'controller.diff',
  213. 'type' => '',
  214. 'tmp_name' => '/private/var/tmp/php7MBztY',
  215. 'error' => 0,
  216. 'size' => 350,
  217. )),
  218. array('data' => array(
  219. 'name' => '',
  220. 'type' => '',
  221. 'tmp_name' => '',
  222. 'error' => 4,
  223. 'size' => 0,
  224. )),
  225. array('data' => array(
  226. 'name' => '',
  227. 'type' => '',
  228. 'tmp_name' => '',
  229. 'error' => 4,
  230. 'size' => 0,
  231. )),
  232. ),
  233. 'Post' => array('attachment' => array(
  234. 'name' => 'jquery-1.2.1.js',
  235. 'type' => 'application/x-javascript',
  236. 'tmp_name' => '/private/var/tmp/phpEwlrIo',
  237. 'error' => 0,
  238. 'size' => 80469,
  239. ))
  240. );
  241. $this->assertEqual($request->data, $expected);
  242. $_FILES = array(
  243. 'data' => array(
  244. 'name' => array(
  245. 'Document' => array(
  246. 1 => array(
  247. 'birth_cert' => 'born on.txt',
  248. 'passport' => 'passport.txt',
  249. 'drivers_license' => 'ugly pic.jpg'
  250. ),
  251. 2 => array(
  252. 'birth_cert' => 'aunt betty.txt',
  253. 'passport' => 'betty-passport.txt',
  254. 'drivers_license' => 'betty-photo.jpg'
  255. ),
  256. ),
  257. ),
  258. 'type' => array(
  259. 'Document' => array(
  260. 1 => array(
  261. 'birth_cert' => 'application/octet-stream',
  262. 'passport' => 'application/octet-stream',
  263. 'drivers_license' => 'application/octet-stream',
  264. ),
  265. 2 => array(
  266. 'birth_cert' => 'application/octet-stream',
  267. 'passport' => 'application/octet-stream',
  268. 'drivers_license' => 'application/octet-stream',
  269. )
  270. )
  271. ),
  272. 'tmp_name' => array(
  273. 'Document' => array(
  274. 1 => array(
  275. 'birth_cert' => '/private/var/tmp/phpbsUWfH',
  276. 'passport' => '/private/var/tmp/php7f5zLt',
  277. 'drivers_license' => '/private/var/tmp/phpMXpZgT',
  278. ),
  279. 2 => array(
  280. 'birth_cert' => '/private/var/tmp/php5kHZt0',
  281. 'passport' => '/private/var/tmp/phpnYkOuM',
  282. 'drivers_license' => '/private/var/tmp/php9Rq0P3',
  283. )
  284. )
  285. ),
  286. 'error' => array(
  287. 'Document' => array(
  288. 1 => array(
  289. 'birth_cert' => 0,
  290. 'passport' => 0,
  291. 'drivers_license' => 0,
  292. ),
  293. 2 => array(
  294. 'birth_cert' => 0,
  295. 'passport' => 0,
  296. 'drivers_license' => 0,
  297. )
  298. )
  299. ),
  300. 'size' => array(
  301. 'Document' => array(
  302. 1 => array(
  303. 'birth_cert' => 123,
  304. 'passport' => 458,
  305. 'drivers_license' => 875,
  306. ),
  307. 2 => array(
  308. 'birth_cert' => 876,
  309. 'passport' => 976,
  310. 'drivers_license' => 9783,
  311. )
  312. )
  313. )
  314. )
  315. );
  316. $request = new CakeRequest('some/path');
  317. $expected = array(
  318. 'Document' => array(
  319. 1 => array(
  320. 'birth_cert' => array(
  321. 'name' => 'born on.txt',
  322. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  323. 'error' => 0,
  324. 'size' => 123,
  325. 'type' => 'application/octet-stream',
  326. ),
  327. 'passport' => array(
  328. 'name' => 'passport.txt',
  329. 'tmp_name' => '/private/var/tmp/php7f5zLt',
  330. 'error' => 0,
  331. 'size' => 458,
  332. 'type' => 'application/octet-stream',
  333. ),
  334. 'drivers_license' => array(
  335. 'name' => 'ugly pic.jpg',
  336. 'tmp_name' => '/private/var/tmp/phpMXpZgT',
  337. 'error' => 0,
  338. 'size' => 875,
  339. 'type' => 'application/octet-stream',
  340. ),
  341. ),
  342. 2 => array(
  343. 'birth_cert' => array(
  344. 'name' => 'aunt betty.txt',
  345. 'tmp_name' => '/private/var/tmp/php5kHZt0',
  346. 'error' => 0,
  347. 'size' => 876,
  348. 'type' => 'application/octet-stream',
  349. ),
  350. 'passport' => array(
  351. 'name' => 'betty-passport.txt',
  352. 'tmp_name' => '/private/var/tmp/phpnYkOuM',
  353. 'error' => 0,
  354. 'size' => 976,
  355. 'type' => 'application/octet-stream',
  356. ),
  357. 'drivers_license' => array(
  358. 'name' => 'betty-photo.jpg',
  359. 'tmp_name' => '/private/var/tmp/php9Rq0P3',
  360. 'error' => 0,
  361. 'size' => 9783,
  362. 'type' => 'application/octet-stream',
  363. ),
  364. ),
  365. )
  366. );
  367. $this->assertEqual($request->data, $expected);
  368. $_FILES = array(
  369. 'data' => array(
  370. 'name' => array('birth_cert' => 'born on.txt'),
  371. 'type' => array('birth_cert' => 'application/octet-stream'),
  372. 'tmp_name' => array('birth_cert' => '/private/var/tmp/phpbsUWfH'),
  373. 'error' => array('birth_cert' => 0),
  374. 'size' => array('birth_cert' => 123)
  375. )
  376. );
  377. $request = new CakeRequest('some/path');
  378. $expected = array(
  379. 'birth_cert' => array(
  380. 'name' => 'born on.txt',
  381. 'type' => 'application/octet-stream',
  382. 'tmp_name' => '/private/var/tmp/phpbsUWfH',
  383. 'error' => 0,
  384. 'size' => 123
  385. )
  386. );
  387. $this->assertEqual($request->data, $expected);
  388. $_FILES = array(
  389. 'something' => array(
  390. 'name' => 'something.txt',
  391. 'type' => 'text/plain',
  392. 'tmp_name' => '/some/file',
  393. 'error' => 0,
  394. 'size' => 123
  395. )
  396. );
  397. $request = new CakeRequest('some/path');
  398. $this->assertEqual($request->params['form'], $_FILES);
  399. }
  400. /**
  401. * test method overrides coming in from POST data.
  402. *
  403. * @return void
  404. */
  405. function testMethodOverrides() {
  406. $_POST = array('_method' => 'POST');
  407. $request = new CakeRequest('some/path');
  408. $this->assertEqual(env('REQUEST_METHOD'), 'POST');
  409. $_POST = array('_method' => 'DELETE');
  410. $request = new CakeRequest('some/path');
  411. $this->assertEqual(env('REQUEST_METHOD'), 'DELETE');
  412. $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';
  413. $request = new CakeRequest('some/path');
  414. $this->assertEqual(env('REQUEST_METHOD'), 'PUT');
  415. }
  416. /**
  417. * test the clientIp method.
  418. *
  419. * @return void
  420. */
  421. function testclientIp() {
  422. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
  423. $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
  424. $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
  425. $request = new CakeRequest('some/path');
  426. $this->assertEqual($request->clientIp(false), '192.168.1.5');
  427. $this->assertEqual($request->clientIp(), '192.168.1.2');
  428. unset($_SERVER['HTTP_X_FORWARDED_FOR']);
  429. $this->assertEqual($request->clientIp(), '192.168.1.2');
  430. unset($_SERVER['HTTP_CLIENT_IP']);
  431. $this->assertEqual($request->clientIp(), '192.168.1.3');
  432. $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
  433. $this->assertEqual($request->clientIp(), '10.0.1.2');
  434. }
  435. /**
  436. * test the referer function.
  437. *
  438. * @return void
  439. */
  440. function testReferer() {
  441. $request = new CakeRequest('some/path');
  442. $request->webroot = '/';
  443. $_SERVER['HTTP_REFERER'] = 'http://cakephp.org';
  444. $result = $request->referer();
  445. $this->assertIdentical($result, 'http://cakephp.org');
  446. $_SERVER['HTTP_REFERER'] = '';
  447. $result = $request->referer();
  448. $this->assertIdentical($result, '/');
  449. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  450. $result = $request->referer(true);
  451. $this->assertIdentical($result, '/some/path');
  452. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  453. $result = $request->referer(false);
  454. $this->assertIdentical($result, FULL_BASE_URL . '/some/path');
  455. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/some/path';
  456. $result = $request->referer(true);
  457. $this->assertIdentical($result, '/some/path');
  458. $_SERVER['HTTP_REFERER'] = FULL_BASE_URL . '/recipes/add';
  459. $result = $request->referer(true);
  460. $this->assertIdentical($result, '/recipes/add');
  461. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
  462. $result = $request->referer();
  463. $this->assertIdentical($result, 'cakephp.org');
  464. }
  465. /**
  466. * test the simple uses of is()
  467. *
  468. * @return void
  469. */
  470. function testIsHttpMethods() {
  471. $request = new CakeRequest('some/path');
  472. $this->assertFalse($request->is('undefined-behavior'));
  473. $_SERVER['REQUEST_METHOD'] = 'GET';
  474. $this->assertTrue($request->is('get'));
  475. $_SERVER['REQUEST_METHOD'] = 'POST';
  476. $this->assertTrue($request->is('POST'));
  477. $_SERVER['REQUEST_METHOD'] = 'PUT';
  478. $this->assertTrue($request->is('put'));
  479. $this->assertFalse($request->is('get'));
  480. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  481. $this->assertTrue($request->is('delete'));
  482. $this->assertTrue($request->isDelete());
  483. $_SERVER['REQUEST_METHOD'] = 'delete';
  484. $this->assertFalse($request->is('delete'));
  485. }
  486. /**
  487. * test the method() method.
  488. *
  489. * @return void
  490. */
  491. function testMethod() {
  492. $_SERVER['REQUEST_METHOD'] = 'delete';
  493. $request = new CakeRequest('some/path');
  494. $this->assertEquals('delete', $request->method());
  495. }
  496. /**
  497. * test host retrieval.
  498. *
  499. * @return void
  500. */
  501. function testHost() {
  502. $_SERVER['HTTP_HOST'] = 'localhost';
  503. $request = new CakeRequest('some/path');
  504. $this->assertEquals('localhost', $request->host());
  505. }
  506. /**
  507. * test domain retrieval.
  508. *
  509. * @return void
  510. */
  511. function testDomain() {
  512. $_SERVER['HTTP_HOST'] = 'something.example.com';
  513. $request = new CakeRequest('some/path');
  514. $this->assertEquals('example.com', $request->domain());
  515. $_SERVER['HTTP_HOST'] = 'something.example.co.uk';
  516. $this->assertEquals('example.co.uk', $request->domain(2));
  517. }
  518. /**
  519. * test getting subdomains for a host.
  520. *
  521. * @return void
  522. */
  523. function testSubdomain() {
  524. $_SERVER['HTTP_HOST'] = 'something.example.com';
  525. $request = new CakeRequest('some/path');
  526. $this->assertEquals(array('something'), $request->subdomains());
  527. $_SERVER['HTTP_HOST'] = 'www.something.example.com';
  528. $this->assertEquals(array('www', 'something'), $request->subdomains());
  529. $_SERVER['HTTP_HOST'] = 'www.something.example.co.uk';
  530. $this->assertEquals(array('www', 'something'), $request->subdomains(2));
  531. $_SERVER['HTTP_HOST'] = 'example.co.uk';
  532. $this->assertEquals(array(), $request->subdomains(2));
  533. }
  534. /**
  535. * test ajax, flash and friends
  536. *
  537. * @return void
  538. */
  539. function testisAjaxFlashAndFriends() {
  540. $request = new CakeRequest('some/path');
  541. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  542. $this->assertTrue($request->is('flash'));
  543. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
  544. $this->assertTrue($request->is('flash'));
  545. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  546. $this->assertTrue($request->is('ajax'));
  547. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHTTPREQUEST';
  548. $this->assertFalse($request->is('ajax'));
  549. $this->assertFalse($request->isAjax());
  550. $_SERVER['HTTP_USER_AGENT'] = 'Android 2.0';
  551. $this->assertTrue($request->is('mobile'));
  552. $this->assertTrue($request->isMobile());
  553. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:2.0b6pre) Gecko/20100902 Firefox/4.0b6pre Fennec/2.0b1pre';
  554. $this->assertTrue($request->is('mobile'));
  555. $this->assertTrue($request->isMobile());
  556. }
  557. /**
  558. * test __call expcetions
  559. *
  560. * @expectedException CakeException
  561. * @return void
  562. */
  563. function test__callExceptionOnUnknownMethod() {
  564. $request = new CakeRequest('some/path');
  565. $request->IamABanana();
  566. }
  567. /**
  568. * test is(ssl)
  569. *
  570. * @return void
  571. */
  572. function testIsSsl() {
  573. $request = new CakeRequest('some/path');
  574. $_SERVER['HTTPS'] = 1;
  575. $this->assertTrue($request->is('ssl'));
  576. $_SERVER['HTTPS'] = 'on';
  577. $this->assertTrue($request->is('ssl'));
  578. $_SERVER['HTTPS'] = '1';
  579. $this->assertTrue($request->is('ssl'));
  580. $_SERVER['HTTPS'] = 'I am not empty';
  581. $this->assertTrue($request->is('ssl'));
  582. $_SERVER['HTTPS'] = 1;
  583. $this->assertTrue($request->is('ssl'));
  584. $_SERVER['HTTPS'] = 'off';
  585. $this->assertFalse($request->is('ssl'));
  586. $_SERVER['HTTPS'] = false;
  587. $this->assertFalse($request->is('ssl'));
  588. $_SERVER['HTTPS'] = '';
  589. $this->assertFalse($request->is('ssl'));
  590. }
  591. /**
  592. * test getting request params with object properties.
  593. *
  594. * @return void
  595. */
  596. function test__get() {
  597. $request = new CakeRequest('some/path');
  598. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  599. $this->assertEqual($request->controller, 'posts');
  600. $this->assertEqual($request->action, 'view');
  601. $this->assertEqual($request->plugin, 'blogs');
  602. $this->assertIdentical($request->banana, null);
  603. }
  604. /**
  605. * test the array access implementation
  606. *
  607. * @return void
  608. */
  609. function testArrayAccess() {
  610. $request = new CakeRequest('some/path');
  611. $request->params = array('controller' => 'posts', 'action' => 'view', 'plugin' => 'blogs');
  612. $this->assertEqual($request['controller'], 'posts');
  613. $request['slug'] = 'speedy-slug';
  614. $this->assertEqual($request->slug, 'speedy-slug');
  615. $this->assertEqual($request['slug'], 'speedy-slug');
  616. $this->assertTrue(isset($request['action']));
  617. $this->assertFalse(isset($request['wrong-param']));
  618. $this->assertTrue(isset($request['plugin']));
  619. unset($request['plugin']);
  620. $this->assertFalse(isset($request['plugin']));
  621. $this->assertNull($request['plugin']);
  622. $this->assertNull($request->plugin);
  623. $request = new CakeRequest('some/path?one=something&two=else');
  624. $this->assertTrue(isset($request['url']['one']));
  625. $request->data = array('Post' => array('title' => 'something'));
  626. $this->assertEqual($request['data']['Post']['title'], 'something');
  627. }
  628. /**
  629. * test adding detectors and having them work.
  630. *
  631. * @return void
  632. */
  633. function testAddDetector() {
  634. $request = new CakeRequest('some/path');
  635. $request->addDetector('compare', array('env' => 'TEST_VAR', 'value' => 'something'));
  636. $_SERVER['TEST_VAR'] = 'something';
  637. $this->assertTrue($request->is('compare'), 'Value match failed.');
  638. $_SERVER['TEST_VAR'] = 'wrong';
  639. $this->assertFalse($request->is('compare'), 'Value mis-match failed.');
  640. $request->addDetector('banana', array('env' => 'TEST_VAR', 'pattern' => '/^ban.*$/'));
  641. $_SERVER['TEST_VAR'] = 'banana';
  642. $this->assertTrue($request->isBanana());
  643. $_SERVER['TEST_VAR'] = 'wrong value';
  644. $this->assertFalse($request->isBanana());
  645. $request->addDetector('mobile', array('options' => array('Imagination')));
  646. $_SERVER['HTTP_USER_AGENT'] = 'Imagination land';
  647. $this->assertTrue($request->isMobile());
  648. $_SERVER['HTTP_USER_AGENT'] = 'iPhone 3.0';
  649. $this->assertTrue($request->isMobile());
  650. $request->addDetector('callme', array('env' => 'TEST_VAR', 'callback' => array($this, '_detectCallback')));
  651. $request->return = true;
  652. $this->assertTrue($request->isCallMe());
  653. $request->return = false;
  654. $this->assertFalse($request->isCallMe());
  655. }
  656. /**
  657. * helper function for testing callbacks.
  658. *
  659. * @return void
  660. */
  661. function _detectCallback($request) {
  662. return $request->return == true;
  663. }
  664. /**
  665. * test getting headers
  666. *
  667. * @return void
  668. */
  669. function testHeader() {
  670. $_SERVER['HTTP_HOST'] = 'localhost';
  671. $_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';
  672. $request = new CakeRequest('/', false);
  673. $this->assertEquals($_SERVER['HTTP_HOST'], $request->header('host'));
  674. $this->assertEquals($_SERVER['HTTP_USER_AGENT'], $request->header('User-Agent'));
  675. }
  676. /**
  677. * test accepts() with and without parameters
  678. *
  679. * @return void
  680. */
  681. function testAccepts() {
  682. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml;q=0.9,application/xhtml+xml,text/html,text/plain,image/png';
  683. $request = new CakeRequest('/', false);
  684. $result = $request->accepts();
  685. $expected = array(
  686. 'text/xml', 'application/xml', 'application/xhtml+xml', 'text/html', 'text/plain', 'image/png'
  687. );
  688. $this->assertEquals($expected, $result, 'Content types differ.');
  689. $result = $request->accepts('text/html');
  690. $this->assertTrue($result);
  691. $result = $request->accepts('image/gif');
  692. $this->assertFalse($result);
  693. }
  694. /**
  695. * testBaseUrlAndWebrootWithModRewrite method
  696. *
  697. * @return void
  698. */
  699. public function testBaseUrlAndWebrootWithModRewrite() {
  700. Configure::write('App.baseUrl', false);
  701. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  702. $_SERVER['SCRIPT_NAME'] = '/1.2.x.x/app/webroot/index.php';
  703. $_SERVER['PATH_INFO'] = '/posts/view/1';
  704. $request = new CakeRequest();
  705. $this->assertEqual($request->base, '/1.2.x.x');
  706. $this->assertEqual($request->webroot, '/1.2.x.x/');
  707. $this->assertEqual($request->url, 'posts/view/1');
  708. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/app/webroot';
  709. $_SERVER['SCRIPT_NAME'] = '/index.php';
  710. $_SERVER['PATH_INFO'] = '/posts/add';
  711. $request = new CakeRequest();
  712. $this->assertEqual($request->base, '');
  713. $this->assertEqual($request->webroot, '/');
  714. $this->assertEqual($request->url, 'posts/add');
  715. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches/1.2.x.x/test/';
  716. $_SERVER['SCRIPT_NAME'] = '/webroot/index.php';
  717. $request = new CakeRequest();
  718. $this->assertEqual('', $request->base);
  719. $this->assertEqual('/', $request->webroot);
  720. $_SERVER['DOCUMENT_ROOT'] = '/some/apps/where';
  721. $_SERVER['SCRIPT_NAME'] = '/app/webroot/index.php';
  722. $request = new CakeRequest();
  723. $this->assertEqual($request->base, '');
  724. $this->assertEqual($request->webroot, '/');
  725. Configure::write('App.dir', 'auth');
  726. $_SERVER['DOCUMENT_ROOT'] = '/cake/repo/branches';
  727. $_SERVER['SCRIPT_NAME'] = '/demos/auth/webroot/index.php';
  728. $request = new CakeRequest();
  729. $this->assertEqual($request->base, '/demos/auth');
  730. $this->assertEqual($request->webroot, '/demos/auth/');
  731. Configure::write('App.dir', 'code');
  732. $_SERVER['DOCUMENT_ROOT'] = '/Library/WebServer/Documents';
  733. $_SERVER['SCRIPT_NAME'] = '/clients/PewterReport/code/webroot/index.php';
  734. $request = new CakeRequest();
  735. $this->assertEqual($request->base, '/clients/PewterReport/code');
  736. $this->assertEqual($request->webroot, '/clients/PewterReport/code/');
  737. }
  738. /**
  739. * testBaseUrlwithModRewriteAlias method
  740. *
  741. * @return void
  742. */
  743. public function testBaseUrlwithModRewriteAlias() {
  744. $_SERVER['DOCUMENT_ROOT'] = '/home/aplusnur/public_html';
  745. $_SERVER['SCRIPT_NAME'] = '/control/index.php';
  746. Configure::write('App.base', '/control');
  747. $request = new CakeRequest();
  748. $this->assertEqual($request->base, '/control');
  749. $this->assertEqual($request->webroot, '/control/');
  750. Configure::write('App.base', false);
  751. Configure::write('App.dir', 'affiliate');
  752. Configure::write('App.webroot', 'newaffiliate');
  753. $_SERVER['DOCUMENT_ROOT'] = '/var/www/abtravaff/html';
  754. $_SERVER['SCRIPT_NAME'] = '/newaffiliate/index.php';
  755. $request = new CakeRequest();
  756. $this->assertEqual($request->base, '/newaffiliate');
  757. $this->assertEqual($request->webroot, '/newaffiliate/');
  758. }
  759. /**
  760. * test base, webroot, and url parsing when there is no url rewriting
  761. *
  762. * @return void
  763. */
  764. function testBaseUrlWithNoModRewrite() {
  765. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites';
  766. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake/index.php';
  767. $_SERVER['PHP_SELF'] = '/cake/index.php/posts/index';
  768. $_SERVER['REQUEST_URI'] = '/cake/index.php/posts/index';
  769. Configure::write('App', array(
  770. 'dir' => APP_DIR,
  771. 'webroot' => WEBROOT_DIR,
  772. 'base' => false,
  773. 'baseUrl' => '/cake/index.php'
  774. ));
  775. $request = new CakeRequest();
  776. $this->assertEqual($request->base, '/cake/index.php');
  777. $this->assertEqual($request->webroot, '/cake/app/webroot/');
  778. $this->assertEqual($request->url, 'posts/index');
  779. }
  780. /**
  781. * testBaseUrlAndWebrootWithBaseUrl method
  782. *
  783. * @return void
  784. */
  785. public function testBaseUrlAndWebrootWithBaseUrl() {
  786. Configure::write('App.dir', 'app');
  787. Configure::write('App.baseUrl', '/app/webroot/index.php');
  788. $request = new CakeRequest();
  789. $this->assertEqual($request->base, '/app/webroot/index.php');
  790. $this->assertEqual($request->webroot, '/app/webroot/');
  791. Configure::write('App.baseUrl', '/app/webroot/test.php');
  792. $request = new CakeRequest();
  793. $this->assertEqual($request->base, '/app/webroot/test.php');
  794. $this->assertEqual($request->webroot, '/app/webroot/');
  795. Configure::write('App.baseUrl', '/app/index.php');
  796. $request = new CakeRequest();
  797. $this->assertEqual($request->base, '/app/index.php');
  798. $this->assertEqual($request->webroot, '/app/webroot/');
  799. Configure::write('App.baseUrl', '/CakeBB/app/webroot/index.php');
  800. $request = new CakeRequest();
  801. $this->assertEqual($request->base, '/CakeBB/app/webroot/index.php');
  802. $this->assertEqual($request->webroot, '/CakeBB/app/webroot/');
  803. Configure::write('App.baseUrl', '/CakeBB/app/index.php');
  804. $request = new CakeRequest();
  805. $this->assertEqual($request->base, '/CakeBB/app/index.php');
  806. $this->assertEqual($request->webroot, '/CakeBB/app/webroot/');
  807. Configure::write('App.baseUrl', '/CakeBB/index.php');
  808. $request = new CakeRequest();
  809. $this->assertEqual($request->base, '/CakeBB/index.php');
  810. $this->assertEqual($request->webroot, '/CakeBB/app/webroot/');
  811. Configure::write('App.baseUrl', '/dbhauser/index.php');
  812. $_SERVER['DOCUMENT_ROOT'] = '/kunden/homepages/4/d181710652/htdocs/joomla';
  813. $_SERVER['SCRIPT_FILENAME'] = '/kunden/homepages/4/d181710652/htdocs/joomla/dbhauser/index.php';
  814. $request = new CakeRequest();
  815. $this->assertEqual($request->base, '/dbhauser/index.php');
  816. $this->assertEqual($request->webroot, '/dbhauser/app/webroot/');
  817. }
  818. /**
  819. * test baseUrl with no rewrite and using the top level index.php.
  820. *
  821. * @return void
  822. */
  823. function testBaseUrlNoRewriteTopLevelIndex() {
  824. Configure::write('App.baseUrl', '/index.php');
  825. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev';
  826. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/index.php';
  827. $request = new CakeRequest();
  828. $this->assertEqual('/index.php', $request->base);
  829. $this->assertEqual('/app/webroot/', $request->webroot);
  830. }
  831. /**
  832. * test baseUrl with no rewrite, and using the app/webroot/index.php file as is normal with virtual hosts.
  833. *
  834. * @return void
  835. */
  836. function testBaseUrlNoRewriteWebrootIndex() {
  837. Configure::write('App.baseUrl', '/index.php');
  838. $_SERVER['DOCUMENT_ROOT'] = '/Users/markstory/Sites/cake_dev/app/webroot';
  839. $_SERVER['SCRIPT_FILENAME'] = '/Users/markstory/Sites/cake_dev/app/webroot/index.php';
  840. $request = new CakeRequest();
  841. $this->assertEqual('/index.php', $request->base);
  842. $this->assertEqual('/', $request->webroot);
  843. }
  844. /**
  845. * generator for environment configurations
  846. *
  847. * @return void
  848. */
  849. public static function environmentGenerator() {
  850. return array(
  851. array(
  852. 'IIS - No rewrite base path',
  853. array(
  854. 'App' => array(
  855. 'base' => false,
  856. 'baseUrl' => '/index.php',
  857. 'dir' => 'app',
  858. 'webroot' => 'webroot'
  859. ),
  860. 'SERVER' => array(
  861. 'SCRIPT_NAME' => '/index.php',
  862. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  863. 'QUERY_STRING' => '',
  864. 'REQUEST_URI' => '/index.php',
  865. 'URL' => '/index.php',
  866. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\index.php',
  867. 'ORIG_PATH_INFO' => '/index.php',
  868. 'PATH_INFO' => '',
  869. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\index.php',
  870. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  871. 'PHP_SELF' => '/index.php',
  872. ),
  873. ),
  874. array(
  875. 'base' => '/index.php',
  876. 'webroot' => '/app/webroot/',
  877. 'url' => ''
  878. ),
  879. ),
  880. array(
  881. 'IIS - No rewrite with path, no PHP_SELF',
  882. array(
  883. 'App' => array(
  884. 'base' => false,
  885. 'baseUrl' => '/index.php?',
  886. 'dir' => 'app',
  887. 'webroot' => 'webroot'
  888. ),
  889. 'SERVER' => array(
  890. 'QUERY_STRING' => '/posts/add',
  891. 'REQUEST_URI' => '/index.php?/posts/add',
  892. 'PHP_SELF' => '',
  893. 'URL' => '/index.php?/posts/add',
  894. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  895. 'argv' => array('/posts/add'),
  896. 'argc' => 1
  897. ),
  898. ),
  899. array(
  900. 'url' => 'posts/add',
  901. 'base' => '/index.php?',
  902. 'webroot' => '/app/webroot/'
  903. )
  904. ),
  905. array(
  906. 'IIS - No rewrite sub dir 2',
  907. array(
  908. 'App' => array(
  909. 'base' => false,
  910. 'baseUrl' => '/site/index.php',
  911. 'dir' => 'app',
  912. 'webroot' => 'webroot',
  913. ),
  914. 'SERVER' => array(
  915. 'SCRIPT_NAME' => '/site/index.php',
  916. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  917. 'QUERY_STRING' => '',
  918. 'REQUEST_URI' => '/site/index.php',
  919. 'URL' => '/site/index.php',
  920. 'SCRIPT_FILENAME' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  921. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  922. 'PHP_SELF' => '/site/index.php',
  923. 'argv' => array(),
  924. 'argc' => 0
  925. ),
  926. ),
  927. array(
  928. 'url' => '',
  929. 'base' => '/site/index.php',
  930. 'webroot' => '/site/app/webroot/'
  931. ),
  932. ),
  933. array(
  934. 'IIS - No rewrite sub dir 2 with path',
  935. array(
  936. 'App' => array(
  937. 'base' => false,
  938. 'baseUrl' => '/site/index.php',
  939. 'dir' => 'app',
  940. 'webroot' => 'webroot'
  941. ),
  942. 'GET' => array('/posts/add' => ''),
  943. 'SERVER' => array(
  944. 'SCRIPT_NAME' => '/site/index.php',
  945. 'PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot',
  946. 'QUERY_STRING' => '/posts/add',
  947. 'REQUEST_URI' => '/site/index.php/posts/add',
  948. 'URL' => '/site/index.php/posts/add',
  949. 'ORIG_PATH_TRANSLATED' => 'C:\\Inetpub\\wwwroot\\site\\index.php',
  950. 'DOCUMENT_ROOT' => 'C:\\Inetpub\\wwwroot',
  951. 'PHP_SELF' => '/site/index.php/posts/add',
  952. 'argv' => array('/posts/add'),
  953. 'argc' => 1
  954. ),
  955. ),
  956. array(
  957. 'url' => 'posts/add',
  958. 'base' => '/site/index.php',
  959. 'webroot' => '/site/app/webroot/'
  960. )
  961. ),
  962. array(
  963. 'Apache - No rewrite, document root set to webroot, requesting path',
  964. array(
  965. 'App' => array(
  966. 'base' => false,
  967. 'baseUrl' => '/index.php',
  968. 'dir' => 'app',
  969. 'webroot' => 'webroot'
  970. ),
  971. 'SERVER' => array(
  972. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  973. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  974. 'QUERY_STRING' => '',
  975. 'REQUEST_URI' => '/index.php/posts/index',
  976. 'SCRIPT_NAME' => '/index.php',
  977. 'PATH_INFO' => '/posts/index',
  978. 'PHP_SELF' => '/index.php/posts/index',
  979. ),
  980. ),
  981. array(
  982. 'url' => 'posts/index',
  983. 'base' => '/index.php',
  984. 'webroot' => '/'
  985. ),
  986. ),
  987. array(
  988. 'Apache - No rewrite, document root set to webroot, requesting root',
  989. array(
  990. 'App' => array(
  991. 'base' => false,
  992. 'baseUrl' => '/index.php',
  993. 'dir' => 'app',
  994. 'webroot' => 'webroot'
  995. ),
  996. 'SERVER' => array(
  997. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  998. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  999. 'QUERY_STRING' => '',
  1000. 'REQUEST_URI' => '/index.php',
  1001. 'SCRIPT_NAME' => '/index.php',
  1002. 'PATH_INFO' => '',
  1003. 'PHP_SELF' => '/index.php',
  1004. ),
  1005. ),
  1006. array(
  1007. 'url' => '',
  1008. 'base' => '/index.php',
  1009. 'webroot' => '/'
  1010. ),
  1011. ),
  1012. array(
  1013. 'Apache - No rewrite, document root set above top level cake dir, requesting path',
  1014. array(
  1015. 'App' => array(
  1016. 'base' => false,
  1017. 'baseUrl' => '/site/index.php',
  1018. 'dir' => 'app',
  1019. 'webroot' => 'webroot'
  1020. ),
  1021. 'SERVER' => array(
  1022. 'SERVER_NAME' => 'localhost',
  1023. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1024. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1025. 'REQUEST_URI' => '/site/index.php/posts/index',
  1026. 'SCRIPT_NAME' => '/site/index.php',
  1027. 'PATH_INFO' => '/posts/index',
  1028. 'PHP_SELF' => '/site/index.php/posts/index',
  1029. ),
  1030. ),
  1031. array(
  1032. 'url' => 'posts/index',
  1033. 'base' => '/site/index.php',
  1034. 'webroot' => '/site/app/webroot/',
  1035. ),
  1036. ),
  1037. array(
  1038. 'Apache - No rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1039. array(
  1040. 'App' => array(
  1041. 'base' => false,
  1042. 'baseUrl' => '/site/index.php',
  1043. 'dir' => 'app',
  1044. 'webroot' => 'webroot'
  1045. ),
  1046. 'SERVER' => array(
  1047. 'SERVER_NAME' => 'localhost',
  1048. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1049. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1050. 'REQUEST_URI' => '/site/index.php/',
  1051. 'SCRIPT_NAME' => '/site/index.php',
  1052. 'PHP_SELF' => '/site/index.php/',
  1053. ),
  1054. ),
  1055. array(
  1056. 'url' => '',
  1057. 'base' => '/site/index.php',
  1058. 'webroot' => '/site/app/webroot/',
  1059. ),
  1060. ),
  1061. array(
  1062. 'Apache - No rewrite, document root set above top level cake dir, request path, with GET',
  1063. array(
  1064. 'App' => array(
  1065. 'base' => false,
  1066. 'baseUrl' => '/site/index.php',
  1067. 'dir' => 'app',
  1068. 'webroot' => 'webroot'
  1069. ),
  1070. 'GET' => array('a' => 'b', 'c' => 'd'),
  1071. 'SERVER' => array(
  1072. 'SERVER_NAME' => 'localhost',
  1073. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1074. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1075. 'REQUEST_URI' => '/site/index.php/posts/index?a=b&c=d',
  1076. 'SCRIPT_NAME' => '/site/index.php',
  1077. 'PATH_INFO' => '/posts/index',
  1078. 'PHP_SELF' => '/site/index.php/posts/index',
  1079. 'QUERY_STRING' => 'a=b&c=d'
  1080. ),
  1081. ),
  1082. array(
  1083. 'urlParams' => array('a' => 'b', 'c' => 'd'),
  1084. 'url' => 'posts/index',
  1085. 'base' => '/site/index.php',
  1086. 'webroot' => '/site/app/webroot/',
  1087. ),
  1088. ),
  1089. array(
  1090. 'Apache - w/rewrite, document root set above top level cake dir, request root, no PATH_INFO',
  1091. array(
  1092. 'App' => array(
  1093. 'base' => false,
  1094. 'baseUrl' => false,
  1095. 'dir' => 'app',
  1096. 'webroot' => 'webroot'
  1097. ),
  1098. 'SERVER' => array(
  1099. 'SERVER_NAME' => 'localhost',
  1100. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1101. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1102. 'REQUEST_URI' => '/site/',
  1103. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1104. 'PHP_SELF' => '/site/app/webroot/index.php',
  1105. ),
  1106. ),
  1107. array(
  1108. 'url' => '',
  1109. 'base' => '/site',
  1110. 'webroot' => '/site/',
  1111. ),
  1112. ),
  1113. array(
  1114. 'Apache - w/rewrite, document root above top level cake dir, request root, no PATH_INFO/REQUEST_URI',
  1115. array(
  1116. 'App' => array(
  1117. 'base' => false,
  1118. 'baseUrl' => false,
  1119. 'dir' => 'app',
  1120. 'webroot' => 'webroot'
  1121. ),
  1122. 'SERVER' => array(
  1123. 'SERVER_NAME' => 'localhost',
  1124. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents',
  1125. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/index.php',
  1126. 'SCRIPT_NAME' => '/site/app/webroot/index.php',
  1127. 'PHP_SELF' => '/site/app/webroot/index.php',
  1128. 'PATH_INFO' => null,
  1129. 'REQUEST_URI' => null,
  1130. ),
  1131. ),
  1132. array(
  1133. 'url' => '',
  1134. 'base' => '/site',
  1135. 'webroot' => '/site/',
  1136. ),
  1137. ),
  1138. array(
  1139. 'Apache - w/rewrite, document root set to webroot, request root, no PATH_INFO/REQUEST_URI',
  1140. array(
  1141. 'App' => array(
  1142. 'base' => false,
  1143. 'baseUrl' => false,
  1144. 'dir' => 'app',
  1145. 'webroot' => 'webroot'
  1146. ),
  1147. 'SERVER' => array(
  1148. 'SERVER_NAME' => 'localhost',
  1149. 'DOCUMENT_ROOT' => '/Library/WebServer/Documents/site/app/webroot',
  1150. 'SCRIPT_FILENAME' => '/Library/WebServer/Documents/site/app/webroot/index.php',
  1151. 'SCRIPT_NAME' => '/index.php',
  1152. 'PHP_SELF' => '/index.php',
  1153. 'PATH_INFO' => null,
  1154. 'REQUEST_URI' => null,
  1155. ),
  1156. ),
  1157. array(
  1158. 'url' => '',
  1159. 'base' => '',
  1160. 'webroot' => '/',
  1161. ),
  1162. ),
  1163. );
  1164. }
  1165. /**
  1166. * testEnvironmentDetection method
  1167. *
  1168. * @dataProvider environmentGenerator
  1169. * @return void
  1170. */
  1171. public function testEnvironmentDetection($name, $env, $expected) {
  1172. $_GET = array();
  1173. $this->__loadEnvironment($env);
  1174. $request = new CakeRequest();
  1175. $this->assertEquals($expected['url'], $request->url, "url error");
  1176. $this->assertEquals($expected['base'], $request->base, "base error");
  1177. $this->assertEquals($expected['webroot'], $request->webroot, "webroot error");
  1178. if (isset($expected['urlParams'])) {
  1179. $this->assertEqual($_GET, $expected['urlParams'], "GET param mismatch");
  1180. }
  1181. }
  1182. /**
  1183. * test the data() method reading
  1184. *
  1185. * @return void
  1186. */
  1187. function testDataReading() {
  1188. $_POST['data'] = array(
  1189. 'Model' => array(
  1190. 'field' => 'value'
  1191. )
  1192. );
  1193. $request = new CakeRequest('posts/index');
  1194. $result = $request->data('Model');
  1195. $this->assertEquals($_POST['data']['Model'], $result);
  1196. $result = $request->data('Model.imaginary');
  1197. $this->assertNull($result);
  1198. }
  1199. /**
  1200. * test writing with data()
  1201. *
  1202. * @return void
  1203. */
  1204. function testDataWriting() {
  1205. $_POST['data'] = array(
  1206. 'Model' => array(
  1207. 'field' => 'value'
  1208. )
  1209. );
  1210. $request = new CakeRequest('posts/index');
  1211. $result = $request->data('Model.new_value', 'new value');
  1212. $this->assertSame($result, $request, 'Return was not $this');
  1213. $this->assertEquals($request->data['Model']['new_value'], 'new value');
  1214. $request->data('Post.title', 'New post')->data('Comment.1.author', 'Mark');
  1215. $this->assertEquals($request->data['Post']['title'], 'New post');
  1216. $this->assertEquals($request->data['Comment']['1']['author'], 'Mark');
  1217. }
  1218. /**
  1219. * test writing falsey values.
  1220. *
  1221. * @return void
  1222. */
  1223. function testDataWritingFalsey() {
  1224. $request = new CakeRequest('posts/index');
  1225. $request->data('Post.null', null);
  1226. $this->assertNull($request->data['Post']['null']);
  1227. $request->data('Post.false', false);
  1228. $this->assertFalse($request->data['Post']['false']);
  1229. $request->data('Post.zero', 0);
  1230. $this->assertSame(0, $request->data['Post']['zero']);
  1231. $request->data('Post.empty', '');
  1232. $this->assertSame('', $request->data['Post']['empty']);
  1233. }
  1234. /**
  1235. * test accept language
  1236. *
  1237. * @return void
  1238. */
  1239. function testAcceptLanguage() {
  1240. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'inexistent,en-ca';
  1241. $result = CakeRequest::acceptLanguage();
  1242. $this->assertEquals(array('inexistent', 'en-ca'), $result, 'Languages do not match');
  1243. $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'es_mx;en_ca';
  1244. $result = CakeRequest::acceptLanguage();
  1245. $this->assertEquals(array('es-mx', 'en-ca'), $result, 'Languages do not match');
  1246. $result = CakeRequest::acceptLanguage('en-ca');
  1247. $this->assertTrue($result);
  1248. $result = CakeRequest::acceptLanguage('en-us');
  1249. $this->assertFalse($result);
  1250. }
  1251. /**
  1252. * test the here() method
  1253. *
  1254. * @return void
  1255. */
  1256. function testHere() {
  1257. Configure::write('App.base', '/base_path');
  1258. $_GET = array('test' => 'value');
  1259. $request = new CakeRequest('/posts/add/1/name:value');
  1260. $result = $request->here();
  1261. $this->assertEquals('/base_path/posts/add/1/name:value?test=value', $result);
  1262. $result = $request->here(false);
  1263. $this->assertEquals('/posts/add/1/name:value?test=value', $result);
  1264. $request = new CakeRequest('/posts/base_path/1/name:value');
  1265. $result = $request->here();
  1266. $this->assertEquals('/base_path/posts/base_path/1/name:value?test=value', $result);
  1267. $result = $request->here(false);
  1268. $this->assertEquals('/posts/base_path/1/name:value?test=value', $result);
  1269. }
  1270. /**
  1271. * Test the input() method.
  1272. *
  1273. * @return void
  1274. */
  1275. function testInput() {
  1276. $request = $this->getMock('CakeRequest', array('_readStdin'));
  1277. $request->expects($this->once())->method('_readStdin')
  1278. ->will($this->returnValue('I came from stdin'));
  1279. $result = $request->input();
  1280. $this->assertEquals('I came from stdin', $result);
  1281. }
  1282. /**
  1283. * Test input() decoding.
  1284. *
  1285. * @return void
  1286. */
  1287. function testInputDecode() {
  1288. $request = $this->getMock('CakeRequest', array('_readStdin'));
  1289. $request->expects($this->once())->method('_readStdin')
  1290. ->will($this->returnValue('{"name":"value"}'));
  1291. $result = $request->input('json_decode');
  1292. $this->assertEquals(array('name' => 'value'), (array)$result);
  1293. }
  1294. /**
  1295. * Test input() decoding with additional arguments.
  1296. *
  1297. * @return void
  1298. */
  1299. function testInputDecodeExtraParams() {
  1300. $xml = <<<XML
  1301. <?xml version="1.0" encoding="utf-8"?>
  1302. <post>
  1303. <title id="title">Test</title>
  1304. </post>
  1305. XML;
  1306. $request = $this->getMock('CakeRequest', array('_readStdin'));
  1307. $request->expects($this->once())->method('_readStdin')
  1308. ->will($this->returnValue($xml));
  1309. $result = $request->input('Xml::build', array('return' => 'domdocument'));
  1310. $this->assertInstanceOf('DOMDocument', $result);
  1311. $this->assertEquals(
  1312. 'Test',
  1313. $result->getElementsByTagName('title')->item(0)->childNodes->item(0)->wholeText
  1314. );
  1315. }
  1316. /**
  1317. * loadEnvironment method
  1318. *
  1319. * @param mixed $env
  1320. * @return void
  1321. * @access private
  1322. */
  1323. function __loadEnvironment($env) {
  1324. if (isset($env['App'])) {
  1325. Configure::write('App', $env['App']);
  1326. }
  1327. if (isset($env['GET'])) {
  1328. foreach ($env['GET'] as $key => $val) {
  1329. $_GET[$key] = $val;
  1330. }
  1331. }
  1332. if (isset($env['POST'])) {
  1333. foreach ($env['POST'] as $key => $val) {
  1334. $_POST[$key] = $val;
  1335. }
  1336. }
  1337. if (isset($env['SERVER'])) {
  1338. foreach ($env['SERVER'] as $key => $val) {
  1339. $_SERVER[$key] = $val;
  1340. }
  1341. }
  1342. }
  1343. }