CakeRequestTest.php 41 KB

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