BrowserTestBaseUserAgentTest.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace Drupal\FunctionalTests;
  3. use Drupal\Tests\BrowserTestBase;
  4. /**
  5. * Tests BrowserTestBase functionality.
  6. *
  7. * @group browsertestbase
  8. */
  9. class BrowserTestBaseUserAgentTest extends BrowserTestBase {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. protected $defaultTheme = 'stark';
  14. /**
  15. * The user agent string to use.
  16. *
  17. * @var string
  18. */
  19. protected $agent;
  20. /**
  21. * Test validation of the User-Agent header we use to perform test requests.
  22. */
  23. public function testUserAgentValidation() {
  24. $assert_session = $this->assertSession();
  25. $system_path = $this->buildUrl(drupal_get_path('module', 'system'));
  26. $http_path = $system_path . '/tests/http.php/user/login';
  27. $https_path = $system_path . '/tests/https.php/user/login';
  28. // Generate a valid simpletest User-Agent to pass validation.
  29. $this->assertNotFalse(preg_match('/test\d+/', $this->databasePrefix, $matches), 'Database prefix contains test prefix.');
  30. $this->agent = drupal_generate_test_ua($matches[0]);
  31. // Test pages only available for testing.
  32. $this->drupalGet($http_path);
  33. $assert_session->statusCodeEquals(200);
  34. $this->drupalGet($https_path);
  35. $assert_session->statusCodeEquals(200);
  36. // Now slightly modify the HMAC on the header, which should not validate.
  37. $this->agent = 'X';
  38. $this->drupalGet($http_path);
  39. $assert_session->statusCodeEquals(403);
  40. $this->drupalGet($https_path);
  41. $assert_session->statusCodeEquals(403);
  42. // Use a real User-Agent and verify that the special files http.php and
  43. // https.php can't be accessed.
  44. $this->agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
  45. $this->drupalGet($http_path);
  46. $assert_session->statusCodeEquals(403);
  47. $this->drupalGet($https_path);
  48. $assert_session->statusCodeEquals(403);
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function prepareRequest() {
  54. $session = $this->getSession();
  55. if ($this->agent) {
  56. $session->setCookie('SIMPLETEST_USER_AGENT', $this->agent);
  57. }
  58. else {
  59. $session->setCookie('SIMPLETEST_USER_AGENT', drupal_generate_test_ua($this->databasePrefix));
  60. }
  61. }
  62. }