View source
<?php
namespace Drupal\simpletest\Tests;
use Drupal\Core\Url;
use Drupal\simpletest\WebTestBase;
class SimpleTestBrowserTest extends WebTestBase {
public static $modules = array(
'simpletest',
'test_page_test',
);
public function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser(array(
'administer unit tests',
)));
}
public function testInternalBrowser() {
$this
->drupalGet('test-page');
$this
->assertTrue($this
->drupalGetHeader('Date'), 'An HTTP header was received.');
$this
->assertTitle(t('Test page | @site-name', array(
'@site-name' => $this
->config('system.site')
->get('name'),
)));
$this
->assertNoTitle('Foo');
$old_user_id = $this->container
->get('current_user')
->id();
$user = $this
->drupalCreateUser();
$this
->drupalLogin($user);
$this
->assertNotEqual($old_user_id, $this->container
->get('current_user')
->id(), 'Current user service updated.');
$headers = $this
->drupalGetHeaders(TRUE);
$this
->assertEqual(count($headers), 2, 'There was one intermediate request.');
$this
->assertTrue(strpos($headers[0][':status'], '303') !== FALSE, 'Intermediate response code was 303.');
$this
->assertFalse(empty($headers[0]['location']), 'Intermediate request contained a Location header.');
$this
->assertEqual($this
->getUrl(), $headers[0]['location'], 'HTTP redirect was followed');
$this
->assertFalse($this
->drupalGetHeader('Location'), 'Headers from intermediate request were reset.');
$this
->assertResponse(200, 'Response code from intermediate request was reset.');
$this
->drupalLogout();
$this
->assertEqual(0, $this->container
->get('current_user')
->id(), 'Current user service updated.');
$this->maximumRedirects = 1;
$edit = array(
'name' => $user
->getUsername(),
'pass' => $user->pass_raw,
);
$this
->drupalPostForm('user/login', $edit, t('Log in'), array(
'query' => array(
'destination' => 'user/logout',
),
));
$headers = $this
->drupalGetHeaders(TRUE);
$this
->assertEqual(count($headers), 2, 'Simpletest stopped following redirects after the first one.');
unlink($this->siteDirectory . '/.htkey');
$this
->drupalGet(Url::fromUri('base:core/install.php', array(
'external' => TRUE,
'absolute' => TRUE,
))
->toString());
$this
->assertResponse(403, 'Cannot access install.php.');
}
public function testUserAgentValidation() {
global $base_url;
$this
->drupalLogout();
$system_path = $base_url . '/' . drupal_get_path('module', 'system');
$HTTP_path = $system_path . '/tests/http.php/user/login';
$https_path = $system_path . '/tests/https.php/user/login';
$this
->assertTrue(preg_match('/simpletest\\d+/', $this->databasePrefix, $matches), 'Database prefix contains simpletest prefix.');
$test_ua = drupal_generate_test_ua($matches[0]);
$this->additionalCurlOptions = array(
CURLOPT_USERAGENT => $test_ua,
);
$this
->drupalGet($HTTP_path);
$this
->assertResponse(200, 'Requesting http.php with a legitimate simpletest User-Agent returns OK.');
$this
->drupalGet($https_path);
$this
->assertResponse(200, 'Requesting https.php with a legitimate simpletest User-Agent returns OK.');
$this->additionalCurlOptions = array(
CURLOPT_USERAGENT => $test_ua . 'X',
);
$this
->drupalGet($HTTP_path);
$this
->assertResponse(403, 'Requesting http.php with a bad simpletest User-Agent fails.');
$this
->drupalGet($https_path);
$this
->assertResponse(403, 'Requesting https.php with a bad simpletest User-Agent fails.');
$this->additionalCurlOptions = array(
CURLOPT_USERAGENT => '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);
$this
->assertResponse(403, 'Requesting http.php with a normal User-Agent fails.');
$this
->drupalGet($https_path);
$this
->assertResponse(403, 'Requesting https.php with a normal User-Agent fails.');
}
public function testTestingThroughUI() {
$this
->drupalGet('admin/config/development/testing');
$this
->assertTrue(strpos($this->drupalSettings['simpleTest']['images'][0], 'core/misc/menu-collapsed.png') > 0, 'drupalSettings contains a link to core/misc/menu-collapsed.png.');
$tests = array(
'Drupal\\system\\Tests\\DrupalKernel\\DrupalKernelTest',
'Drupal\\Tests\\action\\Unit\\Menu\\ActionLocalTasksTest',
'Drupal\\Tests\\simpletest\\Functional\\BrowserTestBaseTest',
);
foreach ($tests as $test) {
$this
->drupalGet('admin/config/development/testing');
$edit = array(
"tests[{$test}]" => TRUE,
);
$this
->drupalPostForm(NULL, $edit, t('Run tests'));
$this
->assertText('0 fails, 0 exceptions');
}
}
}