| Total Complexity | 46 |
| Total Lines | 1007 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like DirectorTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DirectorTest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class DirectorTest extends SapphireTest |
||
| 23 | { |
||
| 24 | protected static $extra_controllers = [ |
||
| 25 | TestController::class, |
||
| 26 | ]; |
||
| 27 | |||
| 28 | protected function setUp() |
||
| 29 | { |
||
| 30 | parent::setUp(); |
||
| 31 | Director::config()->set('alternate_base_url', 'http://www.mysite.com:9090/'); |
||
| 32 | |||
| 33 | // Ensure redirects enabled on all environments and global state doesn't affect the tests |
||
| 34 | CanonicalURLMiddleware::singleton() |
||
| 35 | ->setForceSSLDomain(null) |
||
| 36 | ->setForceSSLPatterns([]) |
||
| 37 | ->setEnabledEnvs(true); |
||
| 38 | $this->expectedRedirect = null; |
||
| 39 | } |
||
| 40 | |||
| 41 | protected function getExtraRoutes() |
||
| 42 | { |
||
| 43 | $rules = parent::getExtraRoutes(); |
||
| 44 | |||
| 45 | $rules['DirectorTestRule/$Action/$ID/$OtherID'] = TestController::class; |
||
| 46 | $rules['en-nz/$Action/$ID/$OtherID'] = [ |
||
| 47 | 'Controller' => TestController::class, |
||
| 48 | 'Locale' => 'en_NZ', |
||
| 49 | ]; |
||
| 50 | return $rules; |
||
| 51 | } |
||
| 52 | |||
| 53 | protected function setUpRoutes() |
||
| 57 | } |
||
| 58 | |||
| 59 | public function testFileExists() |
||
| 78 | } |
||
| 79 | |||
| 80 | public function testAbsoluteURL() |
||
| 213 | } |
||
| 214 | |||
| 215 | public function testAlternativeBaseURL() |
||
| 216 | { |
||
| 217 | // relative base URLs - you should end them in a / |
||
| 218 | Director::config()->set('alternate_base_url', '/relativebase/'); |
||
| 219 | $_SERVER['HTTP_HOST'] = 'www.somesite.com'; |
||
| 220 | $_SERVER['REQUEST_URI'] = "/relativebase/sub-page/"; |
||
| 221 | |||
| 222 | $this->assertEquals('/relativebase/', Director::baseURL()); |
||
| 223 | $this->assertEquals('http://www.somesite.com/relativebase/', Director::absoluteBaseURL()); |
||
| 224 | $this->assertEquals( |
||
| 225 | 'http://www.somesite.com/relativebase/subfolder/test', |
||
| 226 | Director::absoluteURL('subfolder/test') |
||
| 227 | ); |
||
| 228 | |||
| 229 | // absolute base URLS with subdirectory - You should end them in a / |
||
| 230 | Director::config()->set('alternate_base_url', 'http://www.example.org/relativebase/'); |
||
| 231 | $_SERVER['REQUEST_URI'] = "http://www.example.org/relativebase/sub-page/"; |
||
| 232 | $this->assertEquals('/relativebase/', Director::baseURL()); // Non-absolute url |
||
| 233 | $this->assertEquals('http://www.example.org/relativebase/', Director::absoluteBaseURL()); |
||
| 234 | $this->assertEquals( |
||
| 235 | 'http://www.example.org/relativebase/sub-page/', |
||
| 236 | Director::absoluteURL('', Director::REQUEST) |
||
| 237 | ); |
||
| 238 | $this->assertEquals( |
||
| 239 | 'http://www.example.org/relativebase/', |
||
| 240 | Director::absoluteURL('', Director::BASE) |
||
| 241 | ); |
||
| 242 | $this->assertEquals('http://www.example.org/', Director::absoluteURL('', Director::ROOT)); |
||
| 243 | $this->assertEquals( |
||
| 244 | 'http://www.example.org/relativebase/sub-page/subfolder/test', |
||
| 245 | Director::absoluteURL('subfolder/test', Director::REQUEST) |
||
| 246 | ); |
||
| 247 | $this->assertEquals( |
||
| 248 | 'http://www.example.org/subfolder/test', |
||
| 249 | Director::absoluteURL('subfolder/test', Director::ROOT) |
||
| 250 | ); |
||
| 251 | $this->assertEquals( |
||
| 252 | 'http://www.example.org/relativebase/subfolder/test', |
||
| 253 | Director::absoluteURL('subfolder/test', Director::BASE) |
||
| 254 | ); |
||
| 255 | |||
| 256 | // absolute base URLs - you should end them in a / |
||
| 257 | Director::config()->set('alternate_base_url', 'http://www.example.org/'); |
||
| 258 | $_SERVER['REQUEST_URI'] = "http://www.example.org/sub-page/"; |
||
| 259 | $this->assertEquals('/', Director::baseURL()); // Non-absolute url |
||
| 260 | $this->assertEquals('http://www.example.org/', Director::absoluteBaseURL()); |
||
| 261 | $this->assertEquals('http://www.example.org/sub-page/', Director::absoluteURL('', Director::REQUEST)); |
||
| 262 | $this->assertEquals('http://www.example.org/', Director::absoluteURL('', Director::BASE)); |
||
| 263 | $this->assertEquals('http://www.example.org/', Director::absoluteURL('', Director::ROOT)); |
||
| 264 | $this->assertEquals( |
||
| 265 | 'http://www.example.org/sub-page/subfolder/test', |
||
| 266 | Director::absoluteURL('subfolder/test', Director::REQUEST) |
||
| 267 | ); |
||
| 268 | $this->assertEquals( |
||
| 269 | 'http://www.example.org/subfolder/test', |
||
| 270 | Director::absoluteURL('subfolder/test', Director::ROOT) |
||
| 271 | ); |
||
| 272 | $this->assertEquals( |
||
| 273 | 'http://www.example.org/subfolder/test', |
||
| 274 | Director::absoluteURL('subfolder/test', Director::BASE) |
||
| 275 | ); |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Tests that {@link Director::is_absolute()} works under different environment types |
||
| 280 | */ |
||
| 281 | public function testIsAbsolute() |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | public function testIsAbsoluteUrl() |
||
| 301 | { |
||
| 302 | $this->assertTrue(Director::is_absolute_url('http://test.com/testpage')); |
||
| 303 | $this->assertTrue(Director::is_absolute_url('ftp://test.com')); |
||
| 304 | $this->assertFalse(Director::is_absolute_url('test.com/testpage')); |
||
| 305 | $this->assertFalse(Director::is_absolute_url('/relative')); |
||
| 306 | $this->assertFalse(Director::is_absolute_url('relative')); |
||
| 307 | $this->assertFalse(Director::is_absolute_url("/relative/?url=http://foo.com")); |
||
| 308 | $this->assertFalse(Director::is_absolute_url("/relative/#http://foo.com")); |
||
| 309 | $this->assertTrue(Director::is_absolute_url("https://test.com/?url=http://foo.com")); |
||
| 310 | $this->assertTrue(Director::is_absolute_url("trickparseurl:http://test.com")); |
||
| 311 | $this->assertTrue(Director::is_absolute_url('//test.com')); |
||
| 312 | $this->assertTrue(Director::is_absolute_url('/////test.com')); |
||
| 313 | $this->assertTrue(Director::is_absolute_url(' ///test.com')); |
||
| 314 | $this->assertTrue(Director::is_absolute_url('http:test.com')); |
||
| 315 | $this->assertTrue(Director::is_absolute_url('//http://test.com')); |
||
| 316 | } |
||
| 317 | |||
| 318 | public function testHostPortParts() |
||
| 319 | { |
||
| 320 | $this->assertEquals('www.mysite.com:9090', Director::host()); |
||
| 321 | $this->assertEquals('www.mysite.com', Director::hostName()); |
||
| 322 | $this->assertEquals('9090', Director::port()); |
||
| 323 | } |
||
| 324 | |||
| 325 | public function testIsRelativeUrl() |
||
| 326 | { |
||
| 327 | $this->assertFalse(Director::is_relative_url('http://test.com')); |
||
| 328 | $this->assertFalse(Director::is_relative_url('https://test.com')); |
||
| 329 | $this->assertFalse(Director::is_relative_url(' https://test.com/testpage ')); |
||
| 330 | $this->assertTrue(Director::is_relative_url('test.com/testpage')); |
||
| 331 | $this->assertFalse(Director::is_relative_url('ftp://test.com')); |
||
| 332 | $this->assertTrue(Director::is_relative_url('/relative')); |
||
| 333 | $this->assertTrue(Director::is_relative_url('relative')); |
||
| 334 | $this->assertTrue(Director::is_relative_url('/relative/?url=http://test.com')); |
||
| 335 | $this->assertTrue(Director::is_relative_url('/relative/#=http://test.com')); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @return array |
||
| 340 | */ |
||
| 341 | public function providerMakeRelative() |
||
| 342 | { |
||
| 343 | return [ |
||
| 344 | // Resilience to slash position |
||
| 345 | [ |
||
| 346 | 'http://www.mysite.com:9090/base/folder', |
||
| 347 | 'http://www.mysite.com:9090/base/folder', |
||
| 348 | '' |
||
| 349 | ], |
||
| 350 | [ |
||
| 351 | 'http://www.mysite.com:9090/base/folder', |
||
| 352 | 'http://www.mysite.com:9090/base/folder/', |
||
| 353 | '' |
||
| 354 | ], |
||
| 355 | [ |
||
| 356 | 'http://www.mysite.com:9090/base/folder/', |
||
| 357 | 'http://www.mysite.com:9090/base/folder', |
||
| 358 | '' |
||
| 359 | ], |
||
| 360 | [ |
||
| 361 | 'http://www.mysite.com:9090/', |
||
| 362 | 'http://www.mysite.com:9090/', |
||
| 363 | '' |
||
| 364 | ], |
||
| 365 | [ |
||
| 366 | 'http://www.mysite.com:9090/', |
||
| 367 | 'http://www.mysite.com', |
||
| 368 | '' |
||
| 369 | ], |
||
| 370 | [ |
||
| 371 | 'http://www.mysite.com', |
||
| 372 | 'http://www.mysite.com:9090/', |
||
| 373 | '' |
||
| 374 | ], |
||
| 375 | [ |
||
| 376 | 'http://www.mysite.com:9090/base/folder', |
||
| 377 | 'http://www.mysite.com:9090/base/folder/page', |
||
| 378 | 'page' |
||
| 379 | ], |
||
| 380 | [ |
||
| 381 | 'http://www.mysite.com:9090/', |
||
| 382 | 'http://www.mysite.com:9090/page/', |
||
| 383 | 'page/' |
||
| 384 | ], |
||
| 385 | // Parsing protocol safely |
||
| 386 | [ |
||
| 387 | 'http://www.mysite.com:9090/base/folder', |
||
| 388 | 'https://www.mysite.com:9090/base/folder', |
||
| 389 | '' |
||
| 390 | ], |
||
| 391 | [ |
||
| 392 | 'https://www.mysite.com:9090/base/folder', |
||
| 393 | 'http://www.mysite.com:9090/base/folder/testpage', |
||
| 394 | 'testpage' |
||
| 395 | ], |
||
| 396 | [ |
||
| 397 | 'http://www.mysite.com:9090/base/folder', |
||
| 398 | '//www.mysite.com:9090/base/folder/testpage', |
||
| 399 | 'testpage' |
||
| 400 | ], |
||
| 401 | // Dirty input |
||
| 402 | [ |
||
| 403 | 'http://www.mysite.com:9090/base/folder', |
||
| 404 | ' https://www.mysite.com:9090/base/folder/testpage ', |
||
| 405 | 'testpage' |
||
| 406 | ], |
||
| 407 | [ |
||
| 408 | 'http://www.mysite.com:9090/base/folder', |
||
| 409 | '//www.mysite.com:9090/base//folder/testpage//subpage', |
||
| 410 | 'testpage/subpage' |
||
| 411 | ], |
||
| 412 | // Non-http protocol isn't modified |
||
| 413 | [ |
||
| 414 | 'http://www.mysite.com:9090/base/folder', |
||
| 415 | 'ftp://test.com', |
||
| 416 | 'ftp://test.com' |
||
| 417 | ], |
||
| 418 | // Alternate hostnames are redirected |
||
| 419 | [ |
||
| 420 | 'https://www.mysite.com:9090/base/folder', |
||
| 421 | 'http://mysite.com:9090/base/folder/testpage', |
||
| 422 | 'testpage' |
||
| 423 | ], |
||
| 424 | [ |
||
| 425 | 'http://www.otherdomain.com/base/folder', |
||
| 426 | '//www.mysite.com:9090/base/folder/testpage', |
||
| 427 | 'testpage' |
||
| 428 | ], |
||
| 429 | // Base folder is found |
||
| 430 | [ |
||
| 431 | 'http://www.mysite.com:9090/base/folder', |
||
| 432 | BASE_PATH . '/some/file.txt', |
||
| 433 | 'some/file.txt', |
||
| 434 | ], |
||
| 435 | // public folder is found |
||
| 436 | [ |
||
| 437 | 'http://www.mysite.com:9090/base/folder', |
||
| 438 | PUBLIC_PATH . '/some/file.txt', |
||
| 439 | 'some/file.txt', |
||
| 440 | ], |
||
| 441 | // querystring is protected |
||
| 442 | [ |
||
| 443 | 'http://www.mysite.com:9090/base/folder', |
||
| 444 | '//www.mysite.com:9090/base//folder/testpage//subpage?args=hello', |
||
| 445 | 'testpage/subpage?args=hello' |
||
| 446 | ], |
||
| 447 | [ |
||
| 448 | 'http://www.mysite.com:9090/base/folder', |
||
| 449 | '//www.mysite.com:9090/base//folder/?args=hello', |
||
| 450 | '?args=hello' |
||
| 451 | ], |
||
| 452 | ]; |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @dataProvider providerMakeRelative |
||
| 457 | * @param string $baseURL Site base URL |
||
| 458 | * @param string $requestURL Request URL |
||
| 459 | * @param string $relativeURL Expected relative URL |
||
| 460 | */ |
||
| 461 | public function testMakeRelative($baseURL, $requestURL, $relativeURL) |
||
| 469 | ); |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Mostly tested by {@link testIsRelativeUrl()}, |
||
| 474 | * just adding the host name matching aspect here. |
||
| 475 | */ |
||
| 476 | public function testIsSiteUrl() |
||
| 487 | } |
||
| 488 | |||
| 489 | public function testResetGlobalsAfterTestRequest() |
||
| 490 | { |
||
| 491 | $_GET = array('somekey' => 'getvalue'); |
||
| 492 | $_POST = array('somekey' => 'postvalue'); |
||
| 493 | $_COOKIE = array('somekey' => 'cookievalue'); |
||
| 494 | |||
| 495 | $cookies = Injector::inst()->createWithArgs( |
||
| 496 | Cookie_Backend::class, |
||
| 497 | array(array('somekey' => 'sometestcookievalue')) |
||
| 498 | ); |
||
| 499 | |||
| 500 | Director::test( |
||
| 501 | 'errorpage?somekey=sometestgetvalue', |
||
| 502 | array('somekey' => 'sometestpostvalue'), |
||
| 503 | null, |
||
| 504 | null, |
||
| 505 | null, |
||
| 506 | null, |
||
| 507 | $cookies |
||
| 508 | ); |
||
| 509 | |||
| 510 | $this->assertEquals( |
||
| 511 | 'getvalue', |
||
| 512 | $_GET['somekey'], |
||
| 513 | '$_GET reset to original value after Director::test()' |
||
| 514 | ); |
||
| 515 | $this->assertEquals( |
||
| 516 | 'postvalue', |
||
| 517 | $_POST['somekey'], |
||
| 518 | '$_POST reset to original value after Director::test()' |
||
| 519 | ); |
||
| 520 | $this->assertEquals( |
||
| 521 | 'cookievalue', |
||
| 522 | $_COOKIE['somekey'], |
||
| 523 | '$_COOKIE reset to original value after Director::test()' |
||
| 524 | ); |
||
| 525 | } |
||
| 526 | |||
| 527 | public function providerTestTestRequestCarriesGlobals() |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @dataProvider providerTestTestRequestCarriesGlobals |
||
| 543 | * @param $url |
||
| 544 | * @param $fixture |
||
| 545 | * @param $method |
||
| 546 | */ |
||
| 547 | public function testTestRequestCarriesGlobals($url, $fixture, $method) |
||
| 548 | { |
||
| 549 | $getresponse = Director::test( |
||
| 550 | $url, |
||
| 551 | $fixture, |
||
| 552 | null, |
||
| 553 | strtoupper($method), |
||
| 554 | null, |
||
| 555 | null, |
||
| 556 | Injector::inst()->createWithArgs(Cookie_Backend::class, array($fixture)) |
||
| 557 | ); |
||
| 558 | |||
| 559 | $this->assertInstanceOf(HTTPResponse::class, $getresponse, 'Director::test() returns HTTPResponse'); |
||
| 560 | $this->assertEquals($fixture['somekey'], $getresponse->getBody(), "Director::test({$url}, {$method})"); |
||
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Tests that additional parameters specified in the routing table are |
||
| 565 | * saved in the request |
||
| 566 | */ |
||
| 567 | public function testRouteParams() |
||
| 568 | { |
||
| 569 | /** @var HTTPRequest $request */ |
||
| 570 | Director::test('en-nz/myaction/myid/myotherid', null, null, null, null, null, null, $request); |
||
| 571 | |||
| 572 | $this->assertEquals( |
||
| 573 | array( |
||
| 574 | 'Controller' => TestController::class, |
||
| 575 | 'Action' => 'myaction', |
||
| 576 | 'ID' => 'myid', |
||
| 577 | 'OtherID' => 'myotherid', |
||
| 578 | 'Locale' => 'en_NZ' |
||
| 579 | ), |
||
| 580 | $request->params() |
||
| 581 | ); |
||
| 582 | } |
||
| 583 | |||
| 584 | public function testForceWWW() |
||
| 585 | { |
||
| 586 | $this->expectExceptionRedirect('http://www.mysite.com:9090/some-url'); |
||
| 587 | Director::mockRequest(function ($request) { |
||
| 588 | Injector::inst()->registerService($request, HTTPRequest::class); |
||
| 589 | Director::forceWWW(); |
||
| 590 | }, 'http://mysite.com:9090/some-url'); |
||
| 591 | } |
||
| 592 | |||
| 593 | public function testPromisedForceWWW() |
||
| 594 | { |
||
| 595 | Director::forceWWW(); |
||
| 596 | |||
| 597 | // Flag is set but not redirected yet |
||
| 598 | $middleware = CanonicalURLMiddleware::singleton(); |
||
| 599 | $this->assertTrue($middleware->getForceWWW()); |
||
| 600 | |||
| 601 | // Middleware forces the redirection eventually |
||
| 602 | /** @var HTTPResponse $response */ |
||
| 603 | $response = Director::mockRequest(function ($request) use ($middleware) { |
||
| 604 | return $middleware->process($request, function ($request) { |
||
|
|
|||
| 605 | return null; |
||
| 606 | }); |
||
| 607 | }, 'http://mysite.com:9090/some-url'); |
||
| 608 | |||
| 609 | // Middleware returns non-exception redirect |
||
| 610 | $this->assertEquals('http://www.mysite.com:9090/some-url', $response->getHeader('Location')); |
||
| 611 | $this->assertEquals(301, $response->getStatusCode()); |
||
| 612 | } |
||
| 613 | |||
| 614 | public function testForceSSLProtectsEntireSite() |
||
| 615 | { |
||
| 616 | $this->expectExceptionRedirect('https://www.mysite.com:9090/some-url'); |
||
| 617 | Director::mockRequest(function ($request) { |
||
| 618 | Injector::inst()->registerService($request, HTTPRequest::class); |
||
| 619 | Director::forceSSL(); |
||
| 620 | }, 'http://www.mysite.com:9090/some-url'); |
||
| 621 | } |
||
| 622 | |||
| 623 | public function testPromisedForceSSL() |
||
| 624 | { |
||
| 625 | Director::forceSSL(); |
||
| 626 | |||
| 627 | // Flag is set but not redirected yet |
||
| 628 | $middleware = CanonicalURLMiddleware::singleton(); |
||
| 629 | $this->assertTrue($middleware->getForceSSL()); |
||
| 630 | |||
| 631 | // Middleware forces the redirection eventually |
||
| 632 | /** @var HTTPResponse $response */ |
||
| 633 | $response = Director::mockRequest(function ($request) use ($middleware) { |
||
| 634 | return $middleware->process($request, function ($request) { |
||
| 635 | return null; |
||
| 636 | }); |
||
| 637 | }, 'http://www.mysite.com:9090/some-url'); |
||
| 638 | |||
| 639 | // Middleware returns non-exception redirect |
||
| 640 | $this->assertInstanceOf(HTTPResponse::class, $response); |
||
| 641 | $this->assertEquals('https://www.mysite.com:9090/some-url', $response->getHeader('Location')); |
||
| 642 | $this->assertEquals(301, $response->getStatusCode()); |
||
| 643 | } |
||
| 644 | |||
| 645 | public function testForceSSLOnTopLevelPagePattern() |
||
| 646 | { |
||
| 647 | // Expect admin to trigger redirect |
||
| 648 | $this->expectExceptionRedirect('https://www.mysite.com:9090/admin'); |
||
| 649 | Director::mockRequest(function (HTTPRequest $request) { |
||
| 650 | Injector::inst()->registerService($request, HTTPRequest::class); |
||
| 651 | Director::forceSSL(array('/^admin/')); |
||
| 652 | }, 'http://www.mysite.com:9090/admin'); |
||
| 653 | } |
||
| 654 | |||
| 655 | public function testForceSSLOnSubPagesPattern() |
||
| 656 | { |
||
| 657 | // Expect to redirect to security login page |
||
| 658 | $this->expectExceptionRedirect('https://www.mysite.com:9090/Security/login'); |
||
| 659 | Director::mockRequest(function (HTTPRequest $request) { |
||
| 660 | Injector::inst()->registerService($request, HTTPRequest::class); |
||
| 661 | Director::forceSSL(array('/^Security/')); |
||
| 662 | }, 'http://www.mysite.com:9090/Security/login'); |
||
| 663 | } |
||
| 664 | |||
| 665 | public function testForceSSLWithPatternDoesNotMatchOtherPages() |
||
| 666 | { |
||
| 667 | // Not on same url should not trigger redirect |
||
| 668 | $response = Director::mockRequest(function (HTTPRequest $request) { |
||
| 669 | Injector::inst()->registerService($request, HTTPRequest::class); |
||
| 670 | Director::forceSSL(array('/^admin/')); |
||
| 671 | }, 'http://www.mysite.com:9090/normal-page'); |
||
| 672 | $this->assertNull($response, 'Non-matching patterns do not trigger redirect'); |
||
| 673 | |||
| 674 | // nested url should not triger redirect either |
||
| 675 | $response = Director::mockRequest(function (HTTPRequest $request) { |
||
| 676 | Injector::inst()->registerService($request, HTTPRequest::class); |
||
| 677 | Director::forceSSL(array('/^admin/', '/^Security/')); |
||
| 678 | }, 'http://www.mysite.com:9090/just-another-page/sub-url'); |
||
| 679 | $this->assertNull($response, 'Non-matching patterns do not trigger redirect'); |
||
| 680 | } |
||
| 681 | |||
| 682 | public function testForceSSLAlternateDomain() |
||
| 690 | } |
||
| 691 | |||
| 692 | public function testForceSSLAlternateDomainWithPort() |
||
| 693 | { |
||
| 694 | // Ensure that forceSSL throws the appropriate exception |
||
| 695 | $this->expectExceptionRedirect('https://secure.mysite.com:81/admin'); |
||
| 696 | Director::mockRequest(function (HTTPRequest $request) { |
||
| 697 | Injector::inst()->registerService($request, HTTPRequest::class); |
||
| 698 | return Director::forceSSL(array('/^admin/'), 'secure.mysite.com:81'); |
||
| 699 | }, 'http://www.mysite.com:9090/admin'); |
||
| 700 | } |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Test that combined forceWWW and forceSSL combine safely |
||
| 704 | */ |
||
| 705 | public function testForceSSLandForceWWW() |
||
| 706 | { |
||
| 707 | Director::forceWWW(); |
||
| 708 | Director::forceSSL(); |
||
| 709 | |||
| 710 | // Flag is set but not redirected yet |
||
| 711 | $middleware = CanonicalURLMiddleware::singleton(); |
||
| 712 | $this->assertTrue($middleware->getForceWWW()); |
||
| 713 | $this->assertTrue($middleware->getForceSSL()); |
||
| 714 | |||
| 715 | // Middleware forces the redirection eventually |
||
| 716 | /** @var HTTPResponse $response */ |
||
| 717 | $response = Director::mockRequest(function ($request) use ($middleware) { |
||
| 718 | return $middleware->process($request, function ($request) { |
||
| 719 | return null; |
||
| 720 | }); |
||
| 721 | }, 'http://mysite.com:9090/some-url'); |
||
| 722 | |||
| 723 | // Middleware returns non-exception redirect |
||
| 724 | $this->assertEquals('https://www.mysite.com:9090/some-url', $response->getHeader('Location')); |
||
| 725 | $this->assertEquals(301, $response->getStatusCode()); |
||
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Set url to redirect to |
||
| 730 | * |
||
| 731 | * @var string |
||
| 732 | */ |
||
| 733 | protected $expectedRedirect = null; |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Expects this test to throw a HTTPResponse_Exception with the given redirect |
||
| 737 | * |
||
| 738 | * @param string $url |
||
| 739 | */ |
||
| 740 | protected function expectExceptionRedirect($url) |
||
| 741 | { |
||
| 742 | $this->expectedRedirect = $url; |
||
| 743 | } |
||
| 744 | |||
| 745 | protected function runTest() |
||
| 746 | { |
||
| 747 | try { |
||
| 748 | $result = parent::runTest(); |
||
| 749 | if ($this->expectedRedirect) { |
||
| 750 | $this->fail("Expected to redirect to {$this->expectedRedirect} but no redirect found"); |
||
| 751 | } |
||
| 752 | return $result; |
||
| 753 | } catch (HTTPResponse_Exception $exception) { |
||
| 754 | // Check URL |
||
| 755 | if ($this->expectedRedirect) { |
||
| 756 | $url = $exception->getResponse()->getHeader('Location'); |
||
| 757 | $this->assertEquals($this->expectedRedirect, $url, "Expected to redirect to {$this->expectedRedirect}"); |
||
| 758 | return null; |
||
| 759 | } else { |
||
| 760 | throw $exception; |
||
| 761 | } |
||
| 762 | } |
||
| 763 | } |
||
| 764 | |||
| 765 | public function testUnmatchedRequestReturns404() |
||
| 766 | { |
||
| 767 | // Remove non-tested rules |
||
| 768 | $this->assertEquals(404, Director::test('no-route')->getStatusCode()); |
||
| 769 | } |
||
| 770 | |||
| 771 | public function testIsHttps() |
||
| 772 | { |
||
| 773 | // Trust all IPs for this test |
||
| 774 | /** @var TrustedProxyMiddleware $trustedProxyMiddleware */ |
||
| 775 | $trustedProxyMiddleware |
||
| 776 | = Injector::inst()->get(TrustedProxyMiddleware::class); |
||
| 777 | $trustedProxyMiddleware->setTrustedProxyIPs('*'); |
||
| 778 | |||
| 779 | // Clear alternate_base_url for this test |
||
| 780 | Director::config()->remove('alternate_base_url'); |
||
| 781 | |||
| 782 | // nothing available |
||
| 783 | $headers = array( |
||
| 784 | 'HTTP_X_FORWARDED_PROTOCOL', 'HTTPS', 'SSL' |
||
| 785 | ); |
||
| 786 | foreach ($headers as $header) { |
||
| 787 | if (isset($_SERVER[$header])) { |
||
| 788 | unset($_SERVER['HTTP_X_FORWARDED_PROTOCOL']); |
||
| 789 | } |
||
| 790 | } |
||
| 791 | |||
| 792 | $this->assertEquals( |
||
| 793 | 'no', |
||
| 794 | Director::test('TestController/returnIsSSL')->getBody() |
||
| 795 | ); |
||
| 796 | |||
| 797 | $this->assertEquals( |
||
| 798 | 'yes', |
||
| 799 | Director::test( |
||
| 800 | 'TestController/returnIsSSL', |
||
| 801 | null, |
||
| 802 | null, |
||
| 803 | null, |
||
| 804 | null, |
||
| 805 | [ 'X-Forwarded-Protocol' => 'https' ] |
||
| 806 | )->getBody() |
||
| 807 | ); |
||
| 808 | |||
| 809 | $this->assertEquals( |
||
| 810 | 'no', |
||
| 811 | Director::test( |
||
| 812 | 'TestController/returnIsSSL', |
||
| 813 | null, |
||
| 814 | null, |
||
| 815 | null, |
||
| 816 | null, |
||
| 817 | [ 'X-Forwarded-Protocol' => 'http' ] |
||
| 818 | )->getBody() |
||
| 819 | ); |
||
| 820 | |||
| 821 | $this->assertEquals( |
||
| 822 | 'no', |
||
| 823 | Director::test( |
||
| 824 | 'TestController/returnIsSSL', |
||
| 825 | null, |
||
| 826 | null, |
||
| 827 | null, |
||
| 828 | null, |
||
| 829 | [ 'X-Forwarded-Protocol' => 'ftp' ] |
||
| 830 | )->getBody() |
||
| 831 | ); |
||
| 832 | |||
| 833 | // https via HTTPS |
||
| 834 | $_SERVER['HTTPS'] = 'true'; |
||
| 835 | $this->assertEquals( |
||
| 836 | 'yes', |
||
| 837 | Director::test('TestController/returnIsSSL')->getBody() |
||
| 838 | ); |
||
| 839 | |||
| 840 | $_SERVER['HTTPS'] = '1'; |
||
| 841 | $this->assertEquals( |
||
| 842 | 'yes', |
||
| 843 | Director::test('TestController/returnIsSSL')->getBody() |
||
| 844 | ); |
||
| 845 | |||
| 846 | $_SERVER['HTTPS'] = 'off'; |
||
| 847 | $this->assertEquals( |
||
| 848 | 'no', |
||
| 849 | Director::test('TestController/returnIsSSL')->getBody() |
||
| 850 | ); |
||
| 851 | |||
| 852 | // https via SSL |
||
| 853 | $_SERVER['SSL'] = ''; |
||
| 854 | $this->assertEquals( |
||
| 855 | 'yes', |
||
| 856 | Director::test('TestController/returnIsSSL')->getBody() |
||
| 857 | ); |
||
| 858 | } |
||
| 859 | |||
| 860 | public function testTestIgnoresHashes() |
||
| 877 | } |
||
| 878 | |||
| 879 | public function testRequestFilterInDirectorTest() |
||
| 880 | { |
||
| 881 | $filter = new DirectorTest\TestRequestFilter; |
||
| 882 | |||
| 883 | $processor = new RequestProcessor(array($filter)); |
||
| 884 | |||
| 885 | Injector::inst()->registerService($processor, RequestProcessor::class); |
||
| 886 | $response = Director::test('some-dummy-url'); |
||
| 887 | $this->assertEquals(404, $response->getStatusCode()); |
||
| 888 | |||
| 889 | $this->assertEquals(1, $filter->preCalls); |
||
| 890 | $this->assertEquals(1, $filter->postCalls); |
||
| 891 | |||
| 892 | $filter->failPost = true; |
||
| 893 | |||
| 894 | $response = Director::test('some-dummy-url'); |
||
| 895 | $this->assertEquals(500, $response->getStatusCode()); |
||
| 896 | $this->assertEquals(_t(Director::class . '.REQUEST_ABORTED', 'Request aborted'), $response->getBody()); |
||
| 897 | |||
| 898 | $this->assertEquals(2, $filter->preCalls); |
||
| 899 | $this->assertEquals(2, $filter->postCalls); |
||
| 900 | |||
| 901 | $filter->failPre = true; |
||
| 902 | |||
| 903 | $response = Director::test('some-dummy-url'); |
||
| 904 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 905 | $this->assertEquals(_t(Director::class . '.INVALID_REQUEST', 'Invalid request'), $response->getBody()); |
||
| 906 | |||
| 907 | $this->assertEquals(3, $filter->preCalls); |
||
| 908 | |||
| 909 | // preCall 'true' will trigger an exception and prevent post call execution |
||
| 910 | $this->assertEquals(2, $filter->postCalls); |
||
| 911 | } |
||
| 912 | |||
| 913 | public function testGlobalMiddleware() |
||
| 914 | { |
||
| 915 | $middleware = new DirectorTest\TestMiddleware; |
||
| 916 | Director::singleton()->setMiddlewares([$middleware]); |
||
| 917 | |||
| 918 | $response = Director::test('some-dummy-url'); |
||
| 919 | $this->assertEquals(404, $response->getStatusCode()); |
||
| 920 | |||
| 921 | // Both triggered |
||
| 922 | $this->assertEquals(1, $middleware->preCalls); |
||
| 923 | $this->assertEquals(1, $middleware->postCalls); |
||
| 924 | |||
| 925 | $middleware->failPost = true; |
||
| 926 | |||
| 927 | $response = Director::test('some-dummy-url'); |
||
| 928 | $this->assertEquals(500, $response->getStatusCode()); |
||
| 929 | |||
| 930 | // Both triggered |
||
| 931 | $this->assertEquals(2, $middleware->preCalls); |
||
| 932 | $this->assertEquals(2, $middleware->postCalls); |
||
| 933 | |||
| 934 | $middleware->failPre = true; |
||
| 935 | |||
| 936 | $response = Director::test('some-dummy-url'); |
||
| 937 | $this->assertEquals(400, $response->getStatusCode()); |
||
| 938 | |||
| 939 | // Pre triggered, post not |
||
| 940 | $this->assertEquals(3, $middleware->preCalls); |
||
| 941 | $this->assertEquals(2, $middleware->postCalls); |
||
| 942 | } |
||
| 943 | |||
| 944 | public function testRouteSpecificMiddleware() |
||
| 945 | { |
||
| 946 | // Inject adapter in place of controller |
||
| 947 | $specificMiddleware = new DirectorTest\TestMiddleware; |
||
| 948 | Injector::inst()->registerService($specificMiddleware, 'SpecificMiddleware'); |
||
| 949 | |||
| 950 | // Register adapter as factory for creating this controller |
||
| 951 | Config::modify()->merge( |
||
| 952 | Injector::class, |
||
| 953 | 'ControllerWithMiddleware', |
||
| 954 | [ |
||
| 955 | 'class' => RequestHandlerMiddlewareAdapter::class, |
||
| 956 | 'constructor' => [ |
||
| 957 | '%$' . TestController::class |
||
| 958 | ], |
||
| 959 | 'properties' => [ |
||
| 960 | 'Middlewares' => [ |
||
| 961 | '%$SpecificMiddleware', |
||
| 962 | ], |
||
| 963 | ], |
||
| 964 | ] |
||
| 965 | ); |
||
| 966 | |||
| 967 | // Global middleware |
||
| 968 | $middleware = new DirectorTest\TestMiddleware; |
||
| 969 | Director::singleton()->setMiddlewares([ $middleware ]); |
||
| 970 | |||
| 971 | // URL rules, one of which has a specific middleware |
||
| 972 | Config::modify()->set( |
||
| 973 | Director::class, |
||
| 974 | 'rules', |
||
| 975 | [ |
||
| 976 | 'url-one' => TestController::class, |
||
| 977 | 'url-two' => [ |
||
| 978 | 'Controller' => 'ControllerWithMiddleware', |
||
| 979 | ], |
||
| 980 | ] |
||
| 981 | ); |
||
| 982 | |||
| 983 | // URL without a route-specific middleware |
||
| 984 | Director::test('url-one'); |
||
| 985 | |||
| 986 | // Only the global middleware triggered |
||
| 987 | $this->assertEquals(1, $middleware->preCalls); |
||
| 988 | $this->assertEquals(0, $specificMiddleware->postCalls); |
||
| 989 | |||
| 990 | Director::test('url-two'); |
||
| 991 | |||
| 992 | // Both triggered on the url with the specific middleware applied |
||
| 993 | $this->assertEquals(2, $middleware->preCalls); |
||
| 994 | $this->assertEquals(1, $specificMiddleware->postCalls); |
||
| 995 | } |
||
| 996 | |||
| 997 | /** |
||
| 998 | * If using phpdbg it returns itself instead of "cli" from php_sapi_name() |
||
| 999 | */ |
||
| 1000 | public function testIsCli() |
||
| 1003 | } |
||
| 1004 | |||
| 1005 | public function testMockRequest() |
||
| 1006 | { |
||
| 1007 | Director::config()->set('alternate_base_url', 'http://www.mysite.com:9090/some-subdir/'); |
||
| 1008 | |||
| 1009 | // Can handle root-relative $url |
||
| 1010 | Director::mockRequest(function (HTTPRequest $request) { |
||
| 1011 | $this->assertEquals('some-page/nested', $request->getURL()); |
||
| 1012 | $this->assertEquals(1, $request->getVar('query')); |
||
| 1029 | } |
||
| 1030 | } |
||
| 1031 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.