PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Test/Case/Network/CakeRequestTest.php

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