CakeRequestTest.php 39 KB

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