comparison core/modules/simpletest/src/Tests/BrowserTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\simpletest\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8 * Tests the internal browser of the testing framework.
9 *
10 * @group simpletest
11 */
12 class BrowserTest extends WebTestBase {
13
14 /**
15 * A flag indicating whether a cookie has been set in a test.
16 *
17 * @var bool
18 */
19 protected static $cookieSet = FALSE;
20
21 /**
22 * Modules to enable.
23 *
24 * @var string[]
25 */
26 public static $modules = ['block'];
27
28 /**
29 * {@inheritdoc}
30 */
31 protected function setUp() {
32 parent::setUp();
33
34 $this->drupalPlaceBlock('local_tasks_block');
35 }
36
37 /**
38 * Test \Drupal\simpletest\WebTestBase::getAbsoluteUrl().
39 */
40 public function testGetAbsoluteUrl() {
41 $url = 'user/login';
42
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.');
47
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.');
51
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.');
56 }
57
58 /**
59 * Tests XPath escaping.
60 */
61 public function testXPathEscaping() {
62 $testpage = <<< EOF
63 <html>
64 <body>
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>
69 </body>
70 </html>
71 EOF;
72 $this->setRawContent($testpage);
73
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.');
77
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.');
80
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).');
83
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).');
86 }
87
88 /**
89 * Tests that cookies set during a request are available for testing.
90 */
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.');
97
98 // Check that the name and value of the cookie match the request data.
99 $cookie_header = $this->drupalGetHeader('set-cookie', TRUE);
100
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);
104 $name = $matches[1];
105 $value = $matches[2];
106
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.');
109
110 // Set a flag indicating that a cookie has been set in this test.
111 // @see testCookieDoesNotBleed()
112 static::$cookieSet = TRUE;
113 }
114
115 /**
116 * Tests that the cookies from a previous test do not bleed into a new test.
117 *
118 * @see static::testCookies()
119 */
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.');
125 }
126
127 }