PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/code/ryzom/tools/server/www/webtt/cake/tests/cases/libs/controller/components/request_handler.test.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 731 lines | 370 code | 95 blank | 266 comment | 2 complexity | cf9efc640de548e56d9d41ad162d5d4b MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * RequestHandlerComponentTest file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite 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://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.tests.cases.libs.controller.components
  17. * @since CakePHP(tm) v 1.2.0.5435
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. App::import('Controller', 'Controller', false);
  21. App::import('Component', array('RequestHandler'));
  22. Mock::generatePartial('RequestHandlerComponent', 'NoStopRequestHandler', array('_stop', '_header'));
  23. Mock::generatePartial('Controller', 'RequestHandlerMockController', array('header'));
  24. /**
  25. * RequestHandlerTestController class
  26. *
  27. * @package cake
  28. * @subpackage cake.tests.cases.libs.controller.components
  29. */
  30. class RequestHandlerTestController extends Controller {
  31. /**
  32. * name property
  33. *
  34. * @var string
  35. * @access public
  36. */
  37. var $name = 'RequestHandlerTest';
  38. /**
  39. * uses property
  40. *
  41. * @var mixed null
  42. * @access public
  43. */
  44. var $uses = null;
  45. /**
  46. * construct method
  47. *
  48. * @param array $params
  49. * @access private
  50. * @return void
  51. */
  52. function __construct($params = array()) {
  53. foreach ($params as $key => $val) {
  54. $this->{$key} = $val;
  55. }
  56. parent::__construct();
  57. }
  58. /**
  59. * test method for ajax redirection
  60. *
  61. * @return void
  62. */
  63. function destination() {
  64. $this->viewPath = 'posts';
  65. $this->render('index');
  66. }
  67. /**
  68. * test method for ajax redirection + parameter parsing
  69. *
  70. * @return void
  71. */
  72. function param_method($one = null, $two = null) {
  73. echo "one: $one two: $two";
  74. $this->autoRender = false;
  75. }
  76. /**
  77. * test method for testing layout rendering when isAjax()
  78. *
  79. * @return void
  80. */
  81. function ajax2_layout() {
  82. if ($this->autoLayout) {
  83. $this->layout = 'ajax2';
  84. }
  85. $this->destination();
  86. }
  87. }
  88. /**
  89. * RequestHandlerTestDisabledController class
  90. *
  91. * @package cake
  92. * @subpackage cake.tests.cases.libs.controller.components
  93. */
  94. class RequestHandlerTestDisabledController extends Controller {
  95. /**
  96. * uses property
  97. *
  98. * @var mixed null
  99. * @access public
  100. */
  101. var $uses = null;
  102. /**
  103. * construct method
  104. *
  105. * @param array $params
  106. * @access private
  107. * @return void
  108. */
  109. function __construct($params = array()) {
  110. foreach ($params as $key => $val) {
  111. $this->{$key} = $val;
  112. }
  113. parent::__construct();
  114. }
  115. /**
  116. * beforeFilter method
  117. *
  118. * @return void
  119. * @access public
  120. */
  121. function beforeFilter() {
  122. $this->RequestHandler->enabled = false;
  123. }
  124. }
  125. /**
  126. * RequestHandlerComponentTest class
  127. *
  128. * @package cake
  129. * @subpackage cake.tests.cases.libs.controller.components
  130. */
  131. class RequestHandlerComponentTest extends CakeTestCase {
  132. /**
  133. * Controller property
  134. *
  135. * @var RequestHandlerTestController
  136. * @access public
  137. */
  138. var $Controller;
  139. /**
  140. * RequestHandler property
  141. *
  142. * @var RequestHandlerComponent
  143. * @access public
  144. */
  145. var $RequestHandler;
  146. /**
  147. * startTest method
  148. *
  149. * @access public
  150. * @return void
  151. */
  152. function startTest() {
  153. $this->_init();
  154. }
  155. /**
  156. * init method
  157. *
  158. * @access protected
  159. * @return void
  160. */
  161. function _init() {
  162. $this->Controller = new RequestHandlerTestController(array('components' => array('RequestHandler')));
  163. $this->Controller->constructClasses();
  164. $this->RequestHandler =& $this->Controller->RequestHandler;
  165. }
  166. /**
  167. * endTest method
  168. *
  169. * @access public
  170. * @return void
  171. */
  172. function endTest() {
  173. unset($this->RequestHandler);
  174. unset($this->Controller);
  175. if (!headers_sent()) {
  176. header('Content-type: text/html'); //reset content type.
  177. }
  178. App::build();
  179. }
  180. /**
  181. * testInitializeCallback method
  182. *
  183. * @access public
  184. * @return void
  185. */
  186. function testInitializeCallback() {
  187. $this->assertNull($this->RequestHandler->ext);
  188. $this->_init();
  189. $this->Controller->params['url']['ext'] = 'rss';
  190. $this->RequestHandler->initialize($this->Controller);
  191. $this->assertEqual($this->RequestHandler->ext, 'rss');
  192. $settings = array(
  193. 'ajaxLayout' => 'test_ajax'
  194. );
  195. $this->RequestHandler->initialize($this->Controller, $settings);
  196. $this->assertEqual($this->RequestHandler->ajaxLayout, 'test_ajax');
  197. }
  198. /**
  199. * testDisabling method
  200. *
  201. * @access public
  202. * @return void
  203. */
  204. function testDisabling() {
  205. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  206. $this->_init();
  207. $this->Controller->Component->initialize($this->Controller);
  208. $this->Controller->beforeFilter();
  209. $this->Controller->Component->startup($this->Controller);
  210. $this->assertEqual($this->Controller->params, array('isAjax' => true));
  211. $this->Controller = new RequestHandlerTestDisabledController(array('components' => array('RequestHandler')));
  212. $this->Controller->constructClasses();
  213. $this->Controller->Component->initialize($this->Controller);
  214. $this->Controller->beforeFilter();
  215. $this->Controller->Component->startup($this->Controller);
  216. $this->assertEqual($this->Controller->params, array());
  217. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  218. }
  219. /**
  220. * testAutoResponseType method
  221. *
  222. * @access public
  223. * @return void
  224. */
  225. function testAutoResponseType() {
  226. $this->Controller->ext = '.thtml';
  227. $this->Controller->params['url']['ext'] = 'rss';
  228. $this->RequestHandler->initialize($this->Controller);
  229. $this->RequestHandler->startup($this->Controller);
  230. $this->assertEqual($this->Controller->ext, '.ctp');
  231. }
  232. /**
  233. * testAutoAjaxLayout method
  234. *
  235. * @access public
  236. * @return void
  237. */
  238. function testAutoAjaxLayout() {
  239. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  240. $this->RequestHandler->startup($this->Controller);
  241. $this->assertTrue($this->Controller->layout, $this->RequestHandler->ajaxLayout);
  242. $this->_init();
  243. $this->Controller->params['url']['ext'] = 'js';
  244. $this->RequestHandler->initialize($this->Controller);
  245. $this->RequestHandler->startup($this->Controller);
  246. $this->assertNotEqual($this->Controller->layout, 'ajax');
  247. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  248. }
  249. /**
  250. * testStartupCallback method
  251. *
  252. * @access public
  253. * @return void
  254. */
  255. function testStartupCallback() {
  256. $_SERVER['REQUEST_METHOD'] = 'PUT';
  257. $_SERVER['CONTENT_TYPE'] = 'application/xml';
  258. $this->RequestHandler->startup($this->Controller);
  259. $this->assertTrue(is_array($this->Controller->data));
  260. $this->assertFalse(is_object($this->Controller->data));
  261. }
  262. /**
  263. * testStartupCallback with charset.
  264. *
  265. * @return void
  266. */
  267. function testStartupCallbackCharset() {
  268. $_SERVER['REQUEST_METHOD'] = 'PUT';
  269. $_SERVER['CONTENT_TYPE'] = 'application/xml; charset=UTF-8';
  270. $this->RequestHandler->startup($this->Controller);
  271. $this->assertTrue(is_array($this->Controller->data));
  272. $this->assertFalse(is_object($this->Controller->data));
  273. }
  274. /**
  275. * testNonAjaxRedirect method
  276. *
  277. * @access public
  278. * @return void
  279. */
  280. function testNonAjaxRedirect() {
  281. $this->RequestHandler->initialize($this->Controller);
  282. $this->RequestHandler->startup($this->Controller);
  283. $this->assertNull($this->RequestHandler->beforeRedirect($this->Controller, '/'));
  284. }
  285. /**
  286. * testRenderAs method
  287. *
  288. * @access public
  289. * @return void
  290. */
  291. function testRenderAs() {
  292. $this->assertFalse(in_array('Xml', $this->Controller->helpers));
  293. $this->RequestHandler->renderAs($this->Controller, 'xml');
  294. $this->assertTrue(in_array('Xml', $this->Controller->helpers));
  295. $this->Controller->viewPath = 'request_handler_test\\xml';
  296. $this->RequestHandler->renderAs($this->Controller, 'js');
  297. $this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'js');
  298. }
  299. /**
  300. * test that respondAs works as expected.
  301. *
  302. * @return void
  303. */
  304. function testRespondAs() {
  305. $RequestHandler = new NoStopRequestHandler();
  306. $RequestHandler->expectAt(0, '_header', array('Content-Type: application/json'));
  307. $RequestHandler->expectAt(1, '_header', array('Content-Type: text/xml'));
  308. $result = $RequestHandler->respondAs('json');
  309. $this->assertTrue($result);
  310. $result = $RequestHandler->respondAs('text/xml');
  311. $this->assertTrue($result);
  312. }
  313. /**
  314. * test that attachment headers work with respondAs
  315. *
  316. * @return void
  317. */
  318. function testRespondAsWithAttachment() {
  319. $RequestHandler = new NoStopRequestHandler();
  320. $RequestHandler->expectAt(0, '_header', array('Content-Disposition: attachment; filename="myfile.xml"'));
  321. $RequestHandler->expectAt(1, '_header', array('Content-Type: text/xml'));
  322. $result = $RequestHandler->respondAs('xml', array('attachment' => 'myfile.xml'));
  323. $this->assertTrue($result);
  324. }
  325. /**
  326. * test that calling renderAs() more than once continues to work.
  327. *
  328. * @link #6466
  329. * @return void
  330. */
  331. function testRenderAsCalledTwice() {
  332. $this->RequestHandler->renderAs($this->Controller, 'xml');
  333. $this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'xml');
  334. $this->assertEqual($this->Controller->layoutPath, 'xml');
  335. $this->assertTrue(in_array('Xml', $this->Controller->helpers));
  336. $this->RequestHandler->renderAs($this->Controller, 'js');
  337. $this->assertEqual($this->Controller->viewPath, 'request_handler_test' . DS . 'js');
  338. $this->assertEqual($this->Controller->layoutPath, 'js');
  339. $this->assertTrue(in_array('Js', $this->Controller->helpers));
  340. }
  341. /**
  342. * testRequestClientTypes method
  343. *
  344. * @access public
  345. * @return void
  346. */
  347. function testRequestClientTypes() {
  348. $this->assertFalse($this->RequestHandler->isFlash());
  349. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  350. $this->assertTrue($this->RequestHandler->isFlash());
  351. unset($_SERVER['HTTP_USER_AGENT'], $_SERVER['HTTP_X_REQUESTED_WITH']);
  352. $this->assertFalse($this->RequestHandler->isAjax());
  353. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  354. $_SERVER['HTTP_X_PROTOTYPE_VERSION'] = '1.5';
  355. $this->assertTrue($this->RequestHandler->isAjax());
  356. $this->assertEqual($this->RequestHandler->getAjaxVersion(), '1.5');
  357. unset($_SERVER['HTTP_X_REQUESTED_WITH'], $_SERVER['HTTP_X_PROTOTYPE_VERSION']);
  358. $this->assertFalse($this->RequestHandler->isAjax());
  359. $this->assertFalse($this->RequestHandler->getAjaxVersion());
  360. }
  361. /**
  362. * Tests the detection of various Flash versions
  363. *
  364. * @access public
  365. * @return void
  366. */
  367. function testFlashDetection() {
  368. $_agent = env('HTTP_USER_AGENT');
  369. $_SERVER['HTTP_USER_AGENT'] = 'Shockwave Flash';
  370. $this->assertTrue($this->RequestHandler->isFlash());
  371. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash';
  372. $this->assertTrue($this->RequestHandler->isFlash());
  373. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 9';
  374. $this->assertTrue($this->RequestHandler->isFlash());
  375. $_SERVER['HTTP_USER_AGENT'] = 'Adobe Flash Player 10';
  376. $this->assertTrue($this->RequestHandler->isFlash());
  377. $_SERVER['HTTP_USER_AGENT'] = 'Shock Flash';
  378. $this->assertFalse($this->RequestHandler->isFlash());
  379. $_SERVER['HTTP_USER_AGENT'] = $_agent;
  380. }
  381. /**
  382. * testRequestContentTypes method
  383. *
  384. * @access public
  385. * @return void
  386. */
  387. function testRequestContentTypes() {
  388. $_SERVER['REQUEST_METHOD'] = 'GET';
  389. $this->assertNull($this->RequestHandler->requestedWith());
  390. $_SERVER['REQUEST_METHOD'] = 'POST';
  391. $_SERVER['CONTENT_TYPE'] = 'application/json';
  392. $this->assertEqual($this->RequestHandler->requestedWith(), 'json');
  393. $result = $this->RequestHandler->requestedWith(array('json', 'xml'));
  394. $this->assertEqual($result, 'json');
  395. $result =$this->RequestHandler->requestedWith(array('rss', 'atom'));
  396. $this->assertFalse($result);
  397. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  398. $this->_init();
  399. $this->assertTrue($this->RequestHandler->isXml());
  400. $this->assertFalse($this->RequestHandler->isAtom());
  401. $this->assertFalse($this->RequestHandler->isRSS());
  402. $_SERVER['HTTP_ACCEPT'] = 'application/atom+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  403. $this->_init();
  404. $this->assertTrue($this->RequestHandler->isAtom());
  405. $this->assertFalse($this->RequestHandler->isRSS());
  406. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  407. $this->_init();
  408. $this->assertFalse($this->RequestHandler->isAtom());
  409. $this->assertTrue($this->RequestHandler->isRSS());
  410. $this->assertFalse($this->RequestHandler->isWap());
  411. $_SERVER['HTTP_ACCEPT'] = 'text/vnd.wap.wml,text/html,text/plain,image/png,*/*';
  412. $this->_init();
  413. $this->assertTrue($this->RequestHandler->isWap());
  414. $_SERVER['HTTP_ACCEPT'] = 'application/rss+xml,text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  415. }
  416. /**
  417. * testResponseContentType method
  418. *
  419. * @access public
  420. * @return void
  421. */
  422. function testResponseContentType() {
  423. $this->assertNull($this->RequestHandler->responseType());
  424. $this->assertTrue($this->RequestHandler->respondAs('atom'));
  425. $this->assertEqual($this->RequestHandler->responseType(), 'atom');
  426. }
  427. /**
  428. * testMobileDeviceDetection method
  429. *
  430. * @access public
  431. * @return void
  432. */
  433. function testMobileDeviceDetection() {
  434. $this->assertFalse($this->RequestHandler->isMobile());
  435. $_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3';
  436. $this->assertTrue($this->RequestHandler->isMobile());
  437. $_SERVER['HTTP_USER_AGENT'] = 'Some imaginary UA';
  438. $this->RequestHandler->mobileUA []= 'imaginary';
  439. $this->assertTrue($this->RequestHandler->isMobile());
  440. array_pop($this->RequestHandler->mobileUA);
  441. }
  442. /**
  443. * testRequestProperties method
  444. *
  445. * @access public
  446. * @return void
  447. */
  448. function testRequestProperties() {
  449. $_SERVER['HTTPS'] = 'on';
  450. $this->assertTrue($this->RequestHandler->isSSL());
  451. unset($_SERVER['HTTPS']);
  452. $this->assertFalse($this->RequestHandler->isSSL());
  453. $_ENV['SCRIPT_URI'] = 'https://localhost/';
  454. $s = $_SERVER;
  455. $_SERVER = array();
  456. $this->assertTrue($this->RequestHandler->isSSL());
  457. $_SERVER = $s;
  458. }
  459. /**
  460. * testRequestMethod method
  461. *
  462. * @access public
  463. * @return void
  464. */
  465. function testRequestMethod() {
  466. $_SERVER['REQUEST_METHOD'] = 'GET';
  467. $this->assertTrue($this->RequestHandler->isGet());
  468. $this->assertFalse($this->RequestHandler->isPost());
  469. $this->assertFalse($this->RequestHandler->isPut());
  470. $this->assertFalse($this->RequestHandler->isDelete());
  471. $_SERVER['REQUEST_METHOD'] = 'POST';
  472. $this->assertFalse($this->RequestHandler->isGet());
  473. $this->assertTrue($this->RequestHandler->isPost());
  474. $this->assertFalse($this->RequestHandler->isPut());
  475. $this->assertFalse($this->RequestHandler->isDelete());
  476. $_SERVER['REQUEST_METHOD'] = 'PUT';
  477. $this->assertFalse($this->RequestHandler->isGet());
  478. $this->assertFalse($this->RequestHandler->isPost());
  479. $this->assertTrue($this->RequestHandler->isPut());
  480. $this->assertFalse($this->RequestHandler->isDelete());
  481. $_SERVER['REQUEST_METHOD'] = 'DELETE';
  482. $this->assertFalse($this->RequestHandler->isGet());
  483. $this->assertFalse($this->RequestHandler->isPost());
  484. $this->assertFalse($this->RequestHandler->isPut());
  485. $this->assertTrue($this->RequestHandler->isDelete());
  486. }
  487. /**
  488. * testClientContentPreference method
  489. *
  490. * @access public
  491. * @return void
  492. */
  493. function testClientContentPreference() {
  494. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*';
  495. $this->_init();
  496. $this->assertNotEqual($this->RequestHandler->prefers(), 'rss');
  497. $this->RequestHandler->ext = 'rss';
  498. $this->assertEqual($this->RequestHandler->prefers(), 'rss');
  499. $this->assertFalse($this->RequestHandler->prefers('xml'));
  500. $this->assertEqual($this->RequestHandler->prefers(array('js', 'xml', 'xhtml')), 'xml');
  501. $this->assertTrue($this->RequestHandler->accepts('xml'));
  502. $_SERVER['HTTP_ACCEPT'] = 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
  503. $this->_init();
  504. $this->assertEqual($this->RequestHandler->prefers(), 'xml');
  505. $this->assertEqual($this->RequestHandler->accepts(array('js', 'xml', 'html')), 'xml');
  506. $this->assertFalse($this->RequestHandler->accepts(array('gif', 'jpeg', 'foo')));
  507. $_SERVER['HTTP_ACCEPT'] = '*/*;q=0.5';
  508. $this->_init();
  509. $this->assertEqual($this->RequestHandler->prefers(), 'html');
  510. $this->assertFalse($this->RequestHandler->prefers('rss'));
  511. $this->assertFalse($this->RequestHandler->accepts('rss'));
  512. }
  513. /**
  514. * testCustomContent method
  515. *
  516. * @access public
  517. * @return void
  518. */
  519. function testCustomContent() {
  520. $_SERVER['HTTP_ACCEPT'] = 'text/x-mobile,text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5';
  521. $this->_init();
  522. $this->RequestHandler->setContent('mobile', 'text/x-mobile');
  523. $this->RequestHandler->startup($this->Controller);
  524. $this->assertEqual($this->RequestHandler->prefers(), 'mobile');
  525. $this->_init();
  526. $this->RequestHandler->setContent(array('mobile' => 'text/x-mobile'));
  527. $this->RequestHandler->startup($this->Controller);
  528. $this->assertEqual($this->RequestHandler->prefers(), 'mobile');
  529. }
  530. /**
  531. * testClientProperties method
  532. *
  533. * @access public
  534. * @return void
  535. */
  536. function testClientProperties() {
  537. $_SERVER['HTTP_HOST'] = 'localhost:80';
  538. $this->assertEqual($this->RequestHandler->getReferer(), 'localhost');
  539. $_SERVER['HTTP_HOST'] = null;
  540. $_SERVER['HTTP_X_FORWARDED_HOST'] = 'cakephp.org';
  541. $this->assertEqual($this->RequestHandler->getReferer(), 'cakephp.org');
  542. $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
  543. $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
  544. $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
  545. $this->assertEqual($this->RequestHandler->getClientIP(false), '192.168.1.5');
  546. $this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.2');
  547. unset($_SERVER['HTTP_X_FORWARDED_FOR']);
  548. $this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.2');
  549. unset($_SERVER['HTTP_CLIENT_IP']);
  550. $this->assertEqual($this->RequestHandler->getClientIP(), '192.168.1.3');
  551. $_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
  552. $this->assertEqual($this->RequestHandler->getClientIP(), '10.0.1.2');
  553. }
  554. /**
  555. * test that ajax requests involving redirects trigger requestAction instead.
  556. *
  557. * @return void
  558. */
  559. function testAjaxRedirectAsRequestAction() {
  560. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  561. $this->_init();
  562. App::build(array(
  563. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  564. ), true);
  565. $this->Controller->RequestHandler = new NoStopRequestHandler($this);
  566. $this->Controller->RequestHandler->expectOnce('_stop');
  567. ob_start();
  568. $this->Controller->RequestHandler->beforeRedirect(
  569. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'destination')
  570. );
  571. $result = ob_get_clean();
  572. $this->assertPattern('/posts index/', $result, 'RequestAction redirect failed.');
  573. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  574. App::build();
  575. }
  576. /**
  577. * test that ajax requests involving redirects don't force no layout
  578. * this would cause the ajax layout to not be rendered.
  579. *
  580. * @return void
  581. */
  582. function testAjaxRedirectAsRequestActionStillRenderingLayout() {
  583. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  584. $this->_init();
  585. App::build(array(
  586. 'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
  587. ), true);
  588. $this->Controller->RequestHandler = new NoStopRequestHandler($this);
  589. $this->Controller->RequestHandler->expectOnce('_stop');
  590. ob_start();
  591. $this->Controller->RequestHandler->beforeRedirect(
  592. $this->Controller, array('controller' => 'request_handler_test', 'action' => 'ajax2_layout')
  593. );
  594. $result = ob_get_clean();
  595. $this->assertPattern('/posts index/', $result, 'RequestAction redirect failed.');
  596. $this->assertPattern('/Ajax!/', $result, 'Layout was not rendered.');
  597. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  598. App::build();
  599. }
  600. /**
  601. * test that the beforeRedirect callback properly converts
  602. * array urls into their correct string ones, and adds base => false so
  603. * the correct urls are generated.
  604. *
  605. * @link http://cakephp.lighthouseapp.com/projects/42648-cakephp-1x/tickets/276
  606. * @return void
  607. */
  608. function testBeforeRedirectCallbackWithArrayUrl() {
  609. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  610. Router::setRequestInfo(array(
  611. array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'named' => array(), 'form' => array(), 'url' => array('url' => 'accounts/')),
  612. array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
  613. ));
  614. $RequestHandler =& new NoStopRequestHandler();
  615. ob_start();
  616. $RequestHandler->beforeRedirect(
  617. $this->Controller,
  618. array('controller' => 'request_handler_test', 'action' => 'param_method', 'first', 'second')
  619. );
  620. $result = ob_get_clean();
  621. $this->assertEqual($result, 'one: first two: second');
  622. }
  623. /**
  624. * assure that beforeRedirect with a status code will correctly set the status header
  625. *
  626. * @return void
  627. */
  628. function testBeforeRedirectCallingHeader() {
  629. $controller =& new RequestHandlerMockController();
  630. $RequestHandler =& new NoStopRequestHandler();
  631. $controller->expectOnce('header', array('HTTP/1.1 403 Forbidden'));
  632. ob_start();
  633. $RequestHandler->beforeRedirect($controller, 'request_handler_test/param_method/first/second', 403);
  634. $result = ob_get_clean();
  635. }
  636. }