annotate core/modules/simpletest/src/Tests/BrowserTest.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\simpletest\Tests;
Chris@0 4
Chris@0 5 use Drupal\simpletest\WebTestBase;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests the internal browser of the testing framework.
Chris@0 9 *
Chris@0 10 * @group simpletest
Chris@0 11 */
Chris@0 12 class BrowserTest extends WebTestBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * A flag indicating whether a cookie has been set in a test.
Chris@0 16 *
Chris@0 17 * @var bool
Chris@0 18 */
Chris@0 19 protected static $cookieSet = FALSE;
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Modules to enable.
Chris@0 23 *
Chris@0 24 * @var string[]
Chris@0 25 */
Chris@0 26 public static $modules = ['block'];
Chris@0 27
Chris@0 28 /**
Chris@0 29 * {@inheritdoc}
Chris@0 30 */
Chris@0 31 protected function setUp() {
Chris@0 32 parent::setUp();
Chris@0 33
Chris@0 34 $this->drupalPlaceBlock('local_tasks_block');
Chris@0 35 }
Chris@0 36
Chris@0 37 /**
Chris@0 38 * Test \Drupal\simpletest\WebTestBase::getAbsoluteUrl().
Chris@0 39 */
Chris@0 40 public function testGetAbsoluteUrl() {
Chris@0 41 $url = 'user/login';
Chris@0 42
Chris@0 43 $this->drupalGet($url);
Chris@0 44 $absolute = \Drupal::url('user.login', [], ['absolute' => TRUE]);
Chris@0 45 $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
Chris@0 46 $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
Chris@0 47
Chris@0 48 $this->drupalPostForm(NULL, [], t('Log in'));
Chris@0 49 $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
Chris@0 50 $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
Chris@0 51
Chris@0 52 $this->clickLink('Create new account');
Chris@0 53 $absolute = \Drupal::url('user.register', [], ['absolute' => TRUE]);
Chris@0 54 $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
Chris@0 55 $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Tests XPath escaping.
Chris@0 60 */
Chris@0 61 public function testXPathEscaping() {
Chris@0 62 $testpage = <<< EOF
Chris@0 63 <html>
Chris@0 64 <body>
Chris@0 65 <a href="link1">A "weird" link, just to bother the dumb "XPath 1.0"</a>
Chris@0 66 <a href="link2">A second "even more weird" link, in memory of George O'Malley</a>
Chris@0 67 <a href="link3">A \$third$ link, so weird it's worth $1 million</a>
Chris@0 68 <a href="link4">A fourth link, containing alternative \\1 regex backreferences \\2</a>
Chris@0 69 </body>
Chris@0 70 </html>
Chris@0 71 EOF;
Chris@0 72 $this->setRawContent($testpage);
Chris@0 73
Chris@0 74 // Matches the first link.
Chris@0 75 $urls = $this->xpath('//a[text()=:text]', [':text' => 'A "weird" link, just to bother the dumb "XPath 1.0"']);
Chris@0 76 $this->assertEqual($urls[0]['href'], 'link1', 'Match with quotes.');
Chris@0 77
Chris@0 78 $urls = $this->xpath('//a[text()=:text]', [':text' => 'A second "even more weird" link, in memory of George O\'Malley']);
Chris@0 79 $this->assertEqual($urls[0]['href'], 'link2', 'Match with mixed single and double quotes.');
Chris@0 80
Chris@0 81 $urls = $this->xpath('//a[text()=:text]', [':text' => 'A $third$ link, so weird it\'s worth $1 million']);
Chris@0 82 $this->assertEqual($urls[0]['href'], 'link3', 'Match with a regular expression back reference symbol (dollar sign).');
Chris@0 83
Chris@0 84 $urls = $this->xpath('//a[text()=:text]', [':text' => 'A fourth link, containing alternative \\1 regex backreferences \\2']);
Chris@0 85 $this->assertEqual($urls[0]['href'], 'link4', 'Match with another regular expression back reference symbol (double backslash).');
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * Tests that cookies set during a request are available for testing.
Chris@0 90 */
Chris@0 91 public function testCookies() {
Chris@0 92 // Check that the $this->cookies property is populated when a user logs in.
Chris@0 93 $user = $this->drupalCreateUser();
Chris@0 94 $edit = ['name' => $user->getUsername(), 'pass' => $user->pass_raw];
Chris@0 95 $this->drupalPostForm('<front>', $edit, t('Log in'));
Chris@0 96 $this->assertEqual(count($this->cookies), 1, 'A cookie is set when the user logs in.');
Chris@0 97
Chris@0 98 // Check that the name and value of the cookie match the request data.
Chris@0 99 $cookie_header = $this->drupalGetHeader('set-cookie', TRUE);
Chris@0 100
Chris@0 101 // The name and value are located at the start of the string, separated by
Chris@0 102 // an equals sign and ending in a semicolon.
Chris@0 103 preg_match('/^([^=]+)=([^;]+)/', $cookie_header, $matches);
Chris@0 104 $name = $matches[1];
Chris@0 105 $value = $matches[2];
Chris@0 106
Chris@0 107 $this->assertTrue(array_key_exists($name, $this->cookies), 'The cookie name is correct.');
Chris@0 108 $this->assertEqual($value, $this->cookies[$name]['value'], 'The cookie value is correct.');
Chris@0 109
Chris@0 110 // Set a flag indicating that a cookie has been set in this test.
Chris@0 111 // @see testCookieDoesNotBleed()
Chris@0 112 static::$cookieSet = TRUE;
Chris@0 113 }
Chris@0 114
Chris@0 115 /**
Chris@0 116 * Tests that the cookies from a previous test do not bleed into a new test.
Chris@0 117 *
Chris@0 118 * @see static::testCookies()
Chris@0 119 */
Chris@0 120 public function testCookieDoesNotBleed() {
Chris@0 121 // In order for this test to be effective it should always run after the
Chris@0 122 // testCookies() test.
Chris@0 123 $this->assertTrue(static::$cookieSet, 'Tests have been executed in the expected order.');
Chris@0 124 $this->assertEqual(count($this->cookies), 0, 'No cookies are present at the start of a new test.');
Chris@0 125 }
Chris@0 126
Chris@0 127 }