BrowserTestBaseUserAgentTest.php in Drupal 10
File
core/tests/Drupal/FunctionalTests/BrowserTestBaseUserAgentTest.php
View source
<?php
namespace Drupal\FunctionalTests;
use Drupal\Tests\BrowserTestBase;
class BrowserTestBaseUserAgentTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected $agent;
public function testUserAgentValidation() {
$assert_session = $this
->assertSession();
$system_path = $this
->buildUrl(\Drupal::service('extension.list.module')
->getPath('system'));
$http_path = $system_path . '/tests/http.php/user/login';
$https_path = $system_path . '/tests/https.php/user/login';
$this
->assertNotFalse(preg_match('/test\\d+/', $this->databasePrefix, $matches), 'Database prefix contains test prefix.');
$this->agent = drupal_generate_test_ua($matches[0]);
$this
->drupalGet($http_path);
$assert_session
->statusCodeEquals(200);
$this
->drupalGet($https_path);
$assert_session
->statusCodeEquals(200);
$this->agent = 'X';
$this
->drupalGet($http_path);
$assert_session
->statusCodeEquals(403);
$this
->drupalGet($https_path);
$assert_session
->statusCodeEquals(403);
$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';
$this
->drupalGet($http_path);
$assert_session
->statusCodeEquals(403);
$this
->drupalGet($https_path);
$assert_session
->statusCodeEquals(403);
}
protected function prepareRequest() {
$session = $this
->getSession();
if ($this->agent) {
$session
->setCookie('SIMPLETEST_USER_AGENT', $this->agent);
}
else {
$session
->setCookie('SIMPLETEST_USER_AGENT', drupal_generate_test_ua($this->databasePrefix));
}
}
}