annotate core/modules/simpletest/src/Tests/BrowserTest.php @ 19:fa3358dc1485 tip

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