SimpleTestBrowserTest.php 6.15 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\simpletest\Tests;

5
use Drupal\Core\Url;
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
use Drupal\simpletest\WebTestBase;

/**
 * Tests the Simpletest UI internal browser.
 *
 * @group simpletest
 */
class SimpleTestBrowserTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('simpletest', 'test_page_test');

22
  protected function setUp() {
23 24 25 26 27 28 29 30 31 32 33 34 35
    parent::setUp();
    // Create and log in an admin user.
    $this->drupalLogin($this->drupalCreateUser(array('administer unit tests')));
  }

  /**
   * Test the internal browsers functionality.
   */
  public function testInternalBrowser() {
    // Retrieve the test page and check its title and headers.
    $this->drupalGet('test-page');
    $this->assertTrue($this->drupalGetHeader('Date'), 'An HTTP header was received.');
    $this->assertTitle(t('Test page | @site-name', array(
36
      '@site-name' => $this->config('system.site')->get('name'),
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    )));
    $this->assertNoTitle('Foo');

    $old_user_id = $this->container->get('current_user')->id();
    $user = $this->drupalCreateUser();
    $this->drupalLogin($user);
    // Check that current user service updated.
    $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();
    // Check that current user service updated to anonymous user.
    $this->assertEqual(0, $this->container->get('current_user')->id(), 'Current user service updated.');

    // Test the maximum redirection option.
    $this->maximumRedirects = 1;
    $edit = array(
      'name' => $user->getUsername(),
      'pass' => $user->pass_raw
    );
63
    $this->drupalPostForm('user/login', $edit, t('Log in'), array(
64 65 66 67 68 69 70 71 72 73 74
      'query' => array('destination' => 'user/logout'),
    ));
    $headers = $this->drupalGetHeaders(TRUE);
    $this->assertEqual(count($headers), 2, 'Simpletest stopped following redirects after the first one.');

    // Remove the Simpletest private key file so we can test the protection
    // against requests that forge a valid testing user agent to gain access
    // to the installer.
    // @see drupal_valid_test_ua()
    // Not using File API; a potential error must trigger a PHP warning.
    unlink($this->siteDirectory . '/.htkey');
75
    $this->drupalGet(Url::fromUri('base:core/install.php', array('external' => TRUE, 'absolute' => TRUE))->toString());
76 77 78 79 80 81 82 83
    $this->assertResponse(403, 'Cannot access install.php.');
  }

  /**
   * Test validation of the User-Agent header we use to perform test requests.
   */
  public function testUserAgentValidation() {
    global $base_url;
84 85 86 87

    // Logout the user which was logged in during test-setup.
    $this->drupalLogout();

88
    $system_path = $base_url . '/' . drupal_get_path('module', 'system');
89 90
    $HTTP_path = $system_path . '/tests/http.php/user/login';
    $https_path = $system_path . '/tests/https.php/user/login';
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    // Generate a valid simpletest User-Agent to pass validation.
    $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);

    // Test pages only available for testing.
    $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.');

    // Now slightly modify the HMAC on the header, which should not validate.
    $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.');

    // Use a real User-Agent and verify that the special files http.php and
    // https.php can't be accessed.
    $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.');
  }

  /**
119
   * Tests that PHPUnit and KernelTestBase tests work through the UI.
120 121
   */
  public function testTestingThroughUI() {
122 123
    $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.');
124 125 126 127
    // We can not test WebTestBase tests here since they require a valid .htkey
    // to be created. However this scenario is covered by the testception of
    // \Drupal\simpletest\Tests\SimpleTestTest.

128
    $tests = array(
129
      // A KernelTestBase test.
130
      'Drupal\KernelTests\KernelTestBaseTest',
131 132 133 134
      // A PHPUnit unit test.
      'Drupal\Tests\action\Unit\Menu\ActionLocalTasksTest',
      // A PHPUnit functional test.
      'Drupal\Tests\simpletest\Functional\BrowserTestBaseTest',
135 136
    );

137 138 139 140 141 142 143 144
    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');
    }
145 146 147
  }

}