3 namespace Drupal\simpletest\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Tests the internal browser of the testing framework.
12 class BrowserTest extends WebTestBase {
15 * A flag indicating whether a cookie has been set in a test.
19 protected static $cookieSet = FALSE;
26 public static $modules = ['block'];
31 protected function setUp() {
34 $this->drupalPlaceBlock('local_tasks_block');
38 * Test \Drupal\simpletest\WebTestBase::getAbsoluteUrl().
40 public function testGetAbsoluteUrl() {
43 $this->drupalGet($url);
44 $absolute = \Drupal::url('user.login', [], ['absolute' => TRUE]);
45 $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
46 $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
48 $this->drupalPostForm(NULL, [], t('Log in'));
49 $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
50 $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
52 $this->clickLink('Create new account');
53 $absolute = \Drupal::url('user.register', [], ['absolute' => TRUE]);
54 $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.');
55 $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.');
59 * Tests XPath escaping.
61 public function testXPathEscaping() {
65 <a href="link1">A "weird" link, just to bother the dumb "XPath 1.0"</a>
66 <a href="link2">A second "even more weird" link, in memory of George O'Malley</a>
67 <a href="link3">A \$third$ link, so weird it's worth $1 million</a>
68 <a href="link4">A fourth link, containing alternative \\1 regex backreferences \\2</a>
72 $this->setRawContent($testpage);
74 // Matches the first link.
75 $urls = $this->xpath('//a[text()=:text]', [':text' => 'A "weird" link, just to bother the dumb "XPath 1.0"']);
76 $this->assertEqual($urls[0]['href'], 'link1', 'Match with quotes.');
78 $urls = $this->xpath('//a[text()=:text]', [':text' => 'A second "even more weird" link, in memory of George O\'Malley']);
79 $this->assertEqual($urls[0]['href'], 'link2', 'Match with mixed single and double quotes.');
81 $urls = $this->xpath('//a[text()=:text]', [':text' => 'A $third$ link, so weird it\'s worth $1 million']);
82 $this->assertEqual($urls[0]['href'], 'link3', 'Match with a regular expression back reference symbol (dollar sign).');
84 $urls = $this->xpath('//a[text()=:text]', [':text' => 'A fourth link, containing alternative \\1 regex backreferences \\2']);
85 $this->assertEqual($urls[0]['href'], 'link4', 'Match with another regular expression back reference symbol (double backslash).');
89 * Tests that cookies set during a request are available for testing.
91 public function testCookies() {
92 // Check that the $this->cookies property is populated when a user logs in.
93 $user = $this->drupalCreateUser();
94 $edit = ['name' => $user->getUsername(), 'pass' => $user->pass_raw];
95 $this->drupalPostForm('<front>', $edit, t('Log in'));
96 $this->assertEqual(count($this->cookies), 1, 'A cookie is set when the user logs in.');
98 // Check that the name and value of the cookie match the request data.
99 $cookie_header = $this->drupalGetHeader('set-cookie', TRUE);
101 // The name and value are located at the start of the string, separated by
102 // an equals sign and ending in a semicolon.
103 preg_match('/^([^=]+)=([^;]+)/', $cookie_header, $matches);
105 $value = $matches[2];
107 $this->assertTrue(array_key_exists($name, $this->cookies), 'The cookie name is correct.');
108 $this->assertEqual($value, $this->cookies[$name]['value'], 'The cookie value is correct.');
110 // Set a flag indicating that a cookie has been set in this test.
111 // @see testCookieDoesNotBleed()
112 static::$cookieSet = TRUE;
116 * Tests that the cookies from a previous test do not bleed into a new test.
118 * @see static::testCookies()
120 public function testCookieDoesNotBleed() {
121 // In order for this test to be effective it should always run after the
122 // testCookies() test.
123 $this->assertTrue(static::$cookieSet, 'Tests have been executed in the expected order.');
124 $this->assertEqual(count($this->cookies), 0, 'No cookies are present at the start of a new test.');