PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/control/DirectorTest.php

http://github.com/silverstripe/sapphire
PHP | 611 lines | 440 code | 120 blank | 51 comment | 7 complexity | 1de82612d7c60b3eb03ed584665c9fc7 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, CC-BY-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. use SilverStripe\ORM\DataModel;
  3. /**
  4. * @package framework
  5. * @subpackage tests
  6. *
  7. * @todo test Director::alternateBaseFolder()
  8. */
  9. class DirectorTest extends SapphireTest {
  10. protected static $originalRequestURI;
  11. protected $originalProtocolHeaders = array();
  12. protected $originalGet = array();
  13. protected $originalSession = array();
  14. public function setUp() {
  15. parent::setUp();
  16. // Hold the original request URI once so it doesn't get overwritten
  17. if(!self::$originalRequestURI) {
  18. self::$originalRequestURI = $_SERVER['REQUEST_URI'];
  19. }
  20. $_SERVER['REQUEST_URI'] = 'http://www.mysite.com';
  21. $this->originalGet = $_GET;
  22. $this->originalSession = $_SESSION;
  23. $_SESSION = array();
  24. Config::inst()->update('Director', 'rules', array(
  25. 'DirectorTestRule/$Action/$ID/$OtherID' => 'DirectorTestRequest_Controller',
  26. 'en-nz/$Action/$ID/$OtherID' => array(
  27. 'Controller' => 'DirectorTestRequest_Controller',
  28. 'Locale' => 'en_NZ'
  29. )
  30. ));
  31. $headers = array(
  32. 'HTTP_X_FORWARDED_PROTOCOL', 'HTTPS', 'SSL'
  33. );
  34. foreach($headers as $header) {
  35. if(isset($_SERVER[$header])) {
  36. $this->originalProtocolHeaders[$header] = $_SERVER[$header];
  37. }
  38. }
  39. }
  40. public function tearDown() {
  41. // TODO Remove director rule, currently API doesnt allow this
  42. $_GET = $this->originalGet;
  43. $_SESSION = $this->originalSession;
  44. // Reinstate the original REQUEST_URI after it was modified by some tests
  45. $_SERVER['REQUEST_URI'] = self::$originalRequestURI;
  46. if($this->originalProtocolHeaders) {
  47. foreach($this->originalProtocolHeaders as $header => $value) {
  48. $_SERVER[$header] = $value;
  49. }
  50. }
  51. parent::tearDown();
  52. }
  53. public function testFileExists() {
  54. $tempFileName = 'DirectorTest_testFileExists.tmp';
  55. $tempFilePath = TEMP_FOLDER . '/' . $tempFileName;
  56. // create temp file
  57. file_put_contents($tempFilePath, '');
  58. $this->assertTrue(
  59. Director::fileExists($tempFilePath),
  60. 'File exist check with absolute path'
  61. );
  62. $this->assertTrue(
  63. Director::fileExists($tempFilePath . '?queryparams=1&foo[bar]=bar'),
  64. 'File exist check with query params ignored'
  65. );
  66. unlink($tempFilePath);
  67. }
  68. public function testAbsoluteURL() {
  69. $rootURL = Director::protocolAndHost();
  70. $_SERVER['REQUEST_URI'] = "$rootURL/mysite/sub-page/";
  71. Config::inst()->update('Director', 'alternate_base_url', '/mysite/');
  72. //test empty / local urls
  73. foreach(array('', './', '.') as $url) {
  74. $this->assertEquals("$rootURL/mysite/", Director::absoluteURL($url, Director::BASE));
  75. $this->assertEquals("$rootURL/", Director::absoluteURL($url, Director::ROOT));
  76. $this->assertEquals("$rootURL/mysite/sub-page/", Director::absoluteURL($url, Director::REQUEST));
  77. }
  78. // Test site root url
  79. $this->assertEquals("$rootURL/", Director::absoluteURL('/'));
  80. // Test Director::BASE
  81. $this->assertEquals($rootURL, Director::absoluteURL($rootURL, Director::BASE));
  82. $this->assertEquals('http://www.mytest.com', Director::absoluteURL('http://www.mytest.com', Director::BASE));
  83. $this->assertEquals("$rootURL/test", Director::absoluteURL("$rootURL/test", Director::BASE));
  84. $this->assertEquals("$rootURL/root", Director::absoluteURL("/root", Director::BASE));
  85. $this->assertEquals("$rootURL/root/url", Director::absoluteURL("/root/url", Director::BASE));
  86. // Test Director::ROOT
  87. $this->assertEquals($rootURL, Director::absoluteURL($rootURL, Director::ROOT));
  88. $this->assertEquals('http://www.mytest.com', Director::absoluteURL('http://www.mytest.com', Director::ROOT));
  89. $this->assertEquals("$rootURL/test", Director::absoluteURL("$rootURL/test", Director::ROOT));
  90. $this->assertEquals("$rootURL/root", Director::absoluteURL("/root", Director::ROOT));
  91. $this->assertEquals("$rootURL/root/url", Director::absoluteURL("/root/url", Director::ROOT));
  92. // Test Director::REQUEST
  93. $this->assertEquals($rootURL, Director::absoluteURL($rootURL, Director::REQUEST));
  94. $this->assertEquals('http://www.mytest.com', Director::absoluteURL('http://www.mytest.com', Director::REQUEST));
  95. $this->assertEquals("$rootURL/test", Director::absoluteURL("$rootURL/test", Director::REQUEST));
  96. $this->assertEquals("$rootURL/root", Director::absoluteURL("/root", Director::REQUEST));
  97. $this->assertEquals("$rootURL/root/url", Director::absoluteURL("/root/url", Director::REQUEST));
  98. // Test evaluating relative urls relative to base (default)
  99. $this->assertEquals("$rootURL/mysite/test", Director::absoluteURL("test"));
  100. $this->assertEquals("$rootURL/mysite/test/url", Director::absoluteURL("test/url"));
  101. $this->assertEquals("$rootURL/mysite/test", Director::absoluteURL("test", Director::BASE));
  102. $this->assertEquals("$rootURL/mysite/test/url", Director::absoluteURL("test/url", Director::BASE));
  103. // Test evaluting relative urls relative to root
  104. $this->assertEquals("$rootURL/test", Director::absoluteURL("test", Director::ROOT));
  105. $this->assertEquals("$rootURL/test/url", Director::absoluteURL("test/url", Director::ROOT));
  106. // Test relative to requested page
  107. $this->assertEquals("$rootURL/mysite/sub-page/test", Director::absoluteURL("test", Director::REQUEST));
  108. $this->assertEquals("$rootURL/mysite/sub-page/test/url", Director::absoluteURL("test/url", Director::REQUEST));
  109. // Test that javascript links are not left intact
  110. $this->assertStringStartsNotWith('javascript', Director::absoluteURL('javascript:alert("attack")'));
  111. $this->assertStringStartsNotWith('alert', Director::absoluteURL('javascript:alert("attack")'));
  112. $this->assertStringStartsNotWith('javascript', Director::absoluteURL('alert("attack")'));
  113. $this->assertStringStartsNotWith('alert', Director::absoluteURL('alert("attack")'));
  114. }
  115. public function testAlternativeBaseURL() {
  116. // Get original protocol and hostname
  117. $rootURL = Director::protocolAndHost();
  118. // relative base URLs - you should end them in a /
  119. Config::inst()->update('Director', 'alternate_base_url', '/relativebase/');
  120. $_SERVER['REQUEST_URI'] = "$rootURL/relativebase/sub-page/";
  121. $this->assertEquals('/relativebase/', Director::baseURL());
  122. $this->assertEquals($rootURL . '/relativebase/', Director::absoluteBaseURL());
  123. $this->assertEquals(
  124. $rootURL . '/relativebase/subfolder/test',
  125. Director::absoluteURL('subfolder/test')
  126. );
  127. // absolute base URLs - you should end them in a /
  128. Config::inst()->update('Director', 'alternate_base_url', 'http://www.example.org/');
  129. $_SERVER['REQUEST_URI'] = "http://www.example.org/sub-page/";
  130. $this->assertEquals('http://www.example.org/', Director::baseURL());
  131. $this->assertEquals('http://www.example.org/', Director::absoluteBaseURL());
  132. $this->assertEquals('http://www.example.org/sub-page/', Director::absoluteURL('', Director::REQUEST));
  133. $this->assertEquals('http://www.example.org/', Director::absoluteURL('', Director::BASE));
  134. $this->assertEquals('http://www.example.org/', Director::absoluteURL('', Director::ROOT));
  135. $this->assertEquals(
  136. 'http://www.example.org/sub-page/subfolder/test',
  137. Director::absoluteURL('subfolder/test', Director::REQUEST)
  138. );
  139. $this->assertEquals(
  140. 'http://www.example.org/subfolder/test',
  141. Director::absoluteURL('subfolder/test', Director::ROOT)
  142. );
  143. $this->assertEquals(
  144. 'http://www.example.org/subfolder/test',
  145. Director::absoluteURL('subfolder/test', Director::BASE)
  146. );
  147. // Setting it to false restores functionality
  148. Config::inst()->update('Director', 'alternate_base_url', false);
  149. $_SERVER['REQUEST_URI'] = $rootURL;
  150. $this->assertEquals(BASE_URL.'/', Director::baseURL());
  151. $this->assertEquals($rootURL.BASE_URL.'/', Director::absoluteBaseURL(BASE_URL));
  152. $this->assertEquals(
  153. $rootURL.BASE_URL . '/subfolder/test',
  154. Director::absoluteURL('subfolder/test')
  155. );
  156. }
  157. /**
  158. * Tests that {@link Director::is_absolute()} works under different environment types
  159. */
  160. public function testIsAbsolute() {
  161. $expected = array (
  162. 'C:/something' => true,
  163. 'd:\\' => true,
  164. 'e/' => false,
  165. 's:/directory' => true,
  166. '/var/www' => true,
  167. '\\Something' => true,
  168. 'something/c:' => false,
  169. 'folder' => false,
  170. 'a/c:/' => false
  171. );
  172. foreach($expected as $path => $result) {
  173. $this->assertEquals(Director::is_absolute($path), $result, "Test result for $path");
  174. }
  175. }
  176. public function testIsAbsoluteUrl() {
  177. $this->assertTrue(Director::is_absolute_url('http://test.com/testpage'));
  178. $this->assertTrue(Director::is_absolute_url('ftp://test.com'));
  179. $this->assertFalse(Director::is_absolute_url('test.com/testpage'));
  180. $this->assertFalse(Director::is_absolute_url('/relative'));
  181. $this->assertFalse(Director::is_absolute_url('relative'));
  182. $this->assertFalse(Director::is_absolute_url("/relative/?url=http://foo.com"));
  183. $this->assertFalse(Director::is_absolute_url("/relative/#http://foo.com"));
  184. $this->assertTrue(Director::is_absolute_url("https://test.com/?url=http://foo.com"));
  185. $this->assertTrue(Director::is_absolute_url("trickparseurl:http://test.com"));
  186. $this->assertTrue(Director::is_absolute_url('//test.com'));
  187. $this->assertTrue(Director::is_absolute_url('/////test.com'));
  188. $this->assertTrue(Director::is_absolute_url(' ///test.com'));
  189. $this->assertTrue(Director::is_absolute_url('http:test.com'));
  190. $this->assertTrue(Director::is_absolute_url('//http://test.com'));
  191. }
  192. public function testIsRelativeUrl() {
  193. $siteUrl = Director::absoluteBaseURL();
  194. $this->assertFalse(Director::is_relative_url('http://test.com'));
  195. $this->assertFalse(Director::is_relative_url('https://test.com'));
  196. $this->assertFalse(Director::is_relative_url(' https://test.com/testpage '));
  197. $this->assertTrue(Director::is_relative_url('test.com/testpage'));
  198. $this->assertFalse(Director::is_relative_url('ftp://test.com'));
  199. $this->assertTrue(Director::is_relative_url('/relative'));
  200. $this->assertTrue(Director::is_relative_url('relative'));
  201. $this->assertTrue(Director::is_relative_url('/relative/?url=http://test.com'));
  202. $this->assertTrue(Director::is_relative_url('/relative/#=http://test.com'));
  203. }
  204. public function testMakeRelative() {
  205. $siteUrl = Director::absoluteBaseURL();
  206. $siteUrlNoProtocol = preg_replace('/https?:\/\//', '', $siteUrl);
  207. $this->assertEquals(Director::makeRelative("$siteUrl"), '');
  208. $this->assertEquals(Director::makeRelative("https://$siteUrlNoProtocol"), '');
  209. $this->assertEquals(Director::makeRelative("http://$siteUrlNoProtocol"), '');
  210. $this->assertEquals(Director::makeRelative(" $siteUrl/testpage "), 'testpage');
  211. $this->assertEquals(Director::makeRelative("$siteUrlNoProtocol/testpage"), 'testpage');
  212. $this->assertEquals(Director::makeRelative('ftp://test.com'), 'ftp://test.com');
  213. $this->assertEquals(Director::makeRelative('http://test.com'), 'http://test.com');
  214. $this->assertEquals(Director::makeRelative('relative'), 'relative');
  215. $this->assertEquals(Director::makeRelative("$siteUrl/?url=http://test.com"), '?url=http://test.com');
  216. $this->assertEquals("test", Director::makeRelative("https://".$siteUrlNoProtocol."/test"));
  217. $this->assertEquals("test", Director::makeRelative("http://".$siteUrlNoProtocol."/test"));
  218. }
  219. /**
  220. * Mostly tested by {@link testIsRelativeUrl()},
  221. * just adding the host name matching aspect here.
  222. */
  223. public function testIsSiteUrl() {
  224. $this->assertFalse(Director::is_site_url("http://test.com"));
  225. $this->assertTrue(Director::is_site_url(Director::absoluteBaseURL()));
  226. $this->assertFalse(Director::is_site_url("http://test.com?url=" . Director::absoluteBaseURL()));
  227. $this->assertFalse(Director::is_site_url("http://test.com?url=" . urlencode(Director::absoluteBaseURL())));
  228. $this->assertFalse(Director::is_site_url("//test.com?url=" . Director::absoluteBaseURL()));
  229. }
  230. /**
  231. * Tests isDev, isTest, isLive set from querystring
  232. */
  233. public function testQueryIsEnvironment() {
  234. // Reset
  235. unset($_SESSION['isDev']);
  236. unset($_SESSION['isLive']);
  237. unset($_GET['isTest']);
  238. unset($_GET['isDev']);
  239. $_SESSION = $_SESSION ?: array();
  240. // Test isDev=1
  241. $_GET['isDev'] = '1';
  242. $this->assertTrue(Director::isDev());
  243. $this->assertFalse(Director::isTest());
  244. $this->assertFalse(Director::isLive());
  245. // Test persistence
  246. unset($_GET['isDev']);
  247. $this->assertTrue(Director::isDev());
  248. $this->assertFalse(Director::isTest());
  249. $this->assertFalse(Director::isLive());
  250. // Test change to isTest
  251. $_GET['isTest'] = '1';
  252. $this->assertFalse(Director::isDev());
  253. $this->assertTrue(Director::isTest());
  254. $this->assertFalse(Director::isLive());
  255. // Test persistence
  256. unset($_GET['isTest']);
  257. $this->assertFalse(Director::isDev());
  258. $this->assertTrue(Director::isTest());
  259. $this->assertFalse(Director::isLive());
  260. }
  261. public function testResetGlobalsAfterTestRequest() {
  262. $_GET = array('somekey' => 'getvalue');
  263. $_POST = array('somekey' => 'postvalue');
  264. $_COOKIE = array('somekey' => 'cookievalue');
  265. $cookies = Injector::inst()->createWithArgs(
  266. 'Cookie_Backend',
  267. array(array('somekey' => 'sometestcookievalue'))
  268. );
  269. $getresponse = Director::test('errorpage?somekey=sometestgetvalue', array('somekey' => 'sometestpostvalue'),
  270. null, null, null, null, $cookies);
  271. $this->assertEquals('getvalue', $_GET['somekey'],
  272. '$_GET reset to original value after Director::test()');
  273. $this->assertEquals('postvalue', $_POST['somekey'],
  274. '$_POST reset to original value after Director::test()');
  275. $this->assertEquals('cookievalue', $_COOKIE['somekey'],
  276. '$_COOKIE reset to original value after Director::test()');
  277. }
  278. public function testTestRequestCarriesGlobals() {
  279. $fixture = array('somekey' => 'sometestvalue');
  280. foreach(array('get', 'post') as $method) {
  281. foreach(array('return%sValue', 'returnRequestValue', 'returnCookieValue') as $testfunction) {
  282. $url = 'DirectorTestRequest_Controller/' . sprintf($testfunction, ucfirst($method))
  283. . '?' . http_build_query($fixture);
  284. $getresponse = Director::test(
  285. $url,
  286. $fixture,
  287. null,
  288. strtoupper($method),
  289. null,
  290. null,
  291. Injector::inst()->createWithArgs('Cookie_Backend', array($fixture))
  292. );
  293. $this->assertInstanceOf('SS_HTTPResponse', $getresponse, 'Director::test() returns SS_HTTPResponse');
  294. $this->assertEquals($fixture['somekey'], $getresponse->getBody(), 'Director::test() ' . $testfunction);
  295. }
  296. }
  297. }
  298. /**
  299. * Tests that additional parameters specified in the routing table are
  300. * saved in the request
  301. */
  302. public function testRouteParams() {
  303. Director::test('en-nz/myaction/myid/myotherid', null, null, null, null, null, null, $request);
  304. $this->assertEquals(
  305. array(
  306. 'Controller' => 'DirectorTestRequest_Controller',
  307. 'Action' => 'myaction',
  308. 'ID' => 'myid',
  309. 'OtherID' => 'myotherid',
  310. 'Locale' => 'en_NZ'
  311. ),
  312. $request->params()
  313. );
  314. }
  315. public function testForceSSLProtectsEntireSite() {
  316. $_SERVER['REQUEST_URI'] = Director::baseURL() . 'admin';
  317. $output = Director::forceSSL();
  318. $this->assertEquals($output, 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  319. $_SERVER['REQUEST_URI'] = Director::baseURL() . 'some-url';
  320. $output = Director::forceSSL();
  321. $this->assertEquals($output, 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  322. }
  323. public function testForceSSLOnTopLevelPagePattern() {
  324. $_SERVER['REQUEST_URI'] = Director::baseURL() . 'admin';
  325. $output = Director::forceSSL(array('/^admin/'));
  326. $this->assertEquals($output, 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  327. }
  328. public function testForceSSLOnSubPagesPattern() {
  329. $_SERVER['REQUEST_URI'] = Director::baseURL() . Config::inst()->get('SilverStripe\\Security\\Security', 'login_url');
  330. $output = Director::forceSSL(array('/^Security/'));
  331. $this->assertEquals($output, 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  332. }
  333. public function testForceSSLWithPatternDoesNotMatchOtherPages() {
  334. $_SERVER['REQUEST_URI'] = Director::baseURL() . 'normal-page';
  335. $output = Director::forceSSL(array('/^admin/'));
  336. $this->assertFalse($output);
  337. $_SERVER['REQUEST_URI'] = Director::baseURL() . 'just-another-page/sub-url';
  338. $output = Director::forceSSL(array('/^admin/', '/^Security/'));
  339. $this->assertFalse($output);
  340. }
  341. public function testForceSSLAlternateDomain() {
  342. Config::inst()->update('Director', 'alternate_base_url', '/');
  343. $_SERVER['REQUEST_URI'] = Director::baseURL() . 'admin';
  344. $output = Director::forceSSL(array('/^admin/'), 'secure.mysite.com');
  345. $this->assertEquals($output, 'https://secure.mysite.com/admin');
  346. }
  347. /**
  348. * @covers Director::extract_request_headers()
  349. */
  350. public function testExtractRequestHeaders() {
  351. $request = array(
  352. 'REDIRECT_STATUS' => '200',
  353. 'HTTP_HOST' => 'host',
  354. 'HTTP_USER_AGENT' => 'User Agent',
  355. 'HTTP_ACCEPT' => 'text/html',
  356. 'HTTP_ACCEPT_LANGUAGE' => 'en-us',
  357. 'HTTP_COOKIE' => 'MyCookie=1',
  358. 'SERVER_PROTOCOL' => 'HTTP/1.1',
  359. 'REQUEST_METHOD' => 'GET',
  360. 'REQUEST_URI' => '/',
  361. 'SCRIPT_NAME' => FRAMEWORK_DIR . '/main.php',
  362. 'CONTENT_TYPE' => 'text/xml',
  363. 'CONTENT_LENGTH' => 10
  364. );
  365. $headers = array(
  366. 'Host' => 'host',
  367. 'User-Agent' => 'User Agent',
  368. 'Accept' => 'text/html',
  369. 'Accept-Language' => 'en-us',
  370. 'Cookie' => 'MyCookie=1',
  371. 'Content-Type' => 'text/xml',
  372. 'Content-Length' => '10'
  373. );
  374. $this->assertEquals($headers, Director::extract_request_headers($request));
  375. }
  376. public function testUnmatchedRequestReturns404() {
  377. $this->assertEquals(404, Director::test('no-route')->getStatusCode());
  378. }
  379. public function testIsHttps() {
  380. if(!TRUSTED_PROXY) {
  381. $this->markTestSkipped('Test cannot be run without trusted proxy');
  382. }
  383. // nothing available
  384. $headers = array(
  385. 'HTTP_X_FORWARDED_PROTOCOL', 'HTTPS', 'SSL'
  386. );
  387. $origServer = $_SERVER;
  388. foreach($headers as $header) {
  389. if(isset($_SERVER[$header])) {
  390. unset($_SERVER['HTTP_X_FORWARDED_PROTOCOL']);
  391. }
  392. }
  393. $this->assertFalse(Director::is_https());
  394. $_SERVER['HTTP_X_FORWARDED_PROTOCOL'] = 'https';
  395. $this->assertTrue(Director::is_https());
  396. $_SERVER['HTTP_X_FORWARDED_PROTOCOL'] = 'http';
  397. $this->assertFalse(Director::is_https());
  398. $_SERVER['HTTP_X_FORWARDED_PROTOCOL'] = 'ftp';
  399. $this->assertFalse(Director::is_https());
  400. $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'https';
  401. $this->assertTrue(Director::is_https());
  402. $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'http';
  403. $this->assertFalse(Director::is_https());
  404. $_SERVER['HTTP_X_FORWARDED_PROTO'] = 'ftp';
  405. $this->assertFalse(Director::is_https());
  406. $_SERVER['HTTP_FRONT_END_HTTPS'] = 'On';
  407. $this->assertTrue(Director::is_https());
  408. $_SERVER['HTTP_FRONT_END_HTTPS'] = 'Off';
  409. $this->assertFalse(Director::is_https());
  410. // https via HTTPS
  411. $_SERVER['HTTPS'] = 'true';
  412. $this->assertTrue(Director::is_https());
  413. $_SERVER['HTTPS'] = '1';
  414. $this->assertTrue(Director::is_https());
  415. $_SERVER['HTTPS'] = 'off';
  416. $this->assertFalse(Director::is_https());
  417. // https via SSL
  418. $_SERVER['SSL'] = '';
  419. $this->assertTrue(Director::is_https());
  420. $_SERVER = $origServer;
  421. }
  422. public function testTestIgnoresHashes() {
  423. //test that hashes are ignored
  424. $url = "DirectorTestRequest_Controller/returnGetValue?somekey=key";
  425. $hash = "#test";
  426. $response = Director::test($url . $hash, null, null, null, null, null, null, $request);
  427. $this->assertFalse($response->isError());
  428. $this->assertEquals('key', $response->getBody());
  429. $this->assertEquals($request->getURL(true), $url);
  430. //test encoded hashes are accepted
  431. $url = "DirectorTestRequest_Controller/returnGetValue?somekey=test%23key";
  432. $response = Director::test($url, null, null, null, null, null, null, $request);
  433. $this->assertFalse($response->isError());
  434. $this->assertEquals('test#key', $response->getBody());
  435. $this->assertEquals($request->getURL(true), $url);
  436. }
  437. public function testRequestFilterInDirectorTest() {
  438. $filter = new TestRequestFilter;
  439. $processor = new RequestProcessor(array($filter));
  440. Injector::inst()->registerService($processor, 'RequestProcessor');
  441. $response = Director::test('some-dummy-url');
  442. $this->assertEquals(1, $filter->preCalls);
  443. $this->assertEquals(1, $filter->postCalls);
  444. $filter->failPost = true;
  445. $this->setExpectedException('SS_HTTPResponse_Exception');
  446. $response = Director::test('some-dummy-url');
  447. $this->assertEquals(2, $filter->preCalls);
  448. $this->assertEquals(2, $filter->postCalls);
  449. $filter->failPre = true;
  450. $response = Director::test('some-dummy-url');
  451. $this->assertEquals(3, $filter->preCalls);
  452. // preCall 'false' will trigger an exception and prevent post call execution
  453. $this->assertEquals(2, $filter->postCalls);
  454. }
  455. }
  456. class TestRequestFilter implements RequestFilter, TestOnly {
  457. public $preCalls = 0;
  458. public $postCalls = 0;
  459. public $failPre = false;
  460. public $failPost = false;
  461. public function preRequest(\SS_HTTPRequest $request, \Session $session, DataModel $model) {
  462. ++$this->preCalls;
  463. if ($this->failPre) {
  464. return false;
  465. }
  466. }
  467. public function postRequest(\SS_HTTPRequest $request, \SS_HTTPResponse $response, DataModel $model) {
  468. ++$this->postCalls;
  469. if ($this->failPost) {
  470. return false;
  471. }
  472. }
  473. public function reset() {
  474. $this->preCalls = 0;
  475. $this->postCalls = 0;
  476. }
  477. }
  478. class DirectorTestRequest_Controller extends Controller implements TestOnly {
  479. private static $allowed_actions = array(
  480. 'returnGetValue',
  481. 'returnPostValue',
  482. 'returnRequestValue',
  483. 'returnCookieValue',
  484. );
  485. public function returnGetValue($request) { return $_GET['somekey']; }
  486. public function returnPostValue($request) { return $_POST['somekey']; }
  487. public function returnRequestValue($request) { return $_REQUEST['somekey']; }
  488. public function returnCookieValue($request) { return $_COOKIE['somekey']; }
  489. }