Chris@0: drupalPlaceBlock('local_tasks_block'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test \Drupal\simpletest\WebTestBase::getAbsoluteUrl(). Chris@0: */ Chris@0: public function testGetAbsoluteUrl() { Chris@0: $url = 'user/login'; Chris@0: Chris@0: $this->drupalGet($url); Chris@18: $absolute = Url::fromRoute('user.login', [], ['absolute' => TRUE])->toString(); Chris@0: $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.'); Chris@0: $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.'); Chris@0: Chris@0: $this->drupalPostForm(NULL, [], t('Log in')); Chris@0: $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.'); Chris@0: $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.'); Chris@0: Chris@0: $this->clickLink('Create new account'); Chris@18: $absolute = Url::fromRoute('user.register', [], ['absolute' => TRUE])->toString(); Chris@0: $this->assertEqual($absolute, $this->url, 'Passed and requested URL are equal.'); Chris@0: $this->assertEqual($this->url, $this->getAbsoluteUrl($this->url), 'Requested and returned absolute URL are equal.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests XPath escaping. Chris@0: */ Chris@0: public function testXPathEscaping() { Chris@0: $testpage = <<< EOF Chris@0: Chris@0: Chris@0: A "weird" link, just to bother the dumb "XPath 1.0" Chris@0: A second "even more weird" link, in memory of George O'Malley Chris@0: A \$third$ link, so weird it's worth $1 million Chris@0: A fourth link, containing alternative \\1 regex backreferences \\2 Chris@0: Chris@0: Chris@0: EOF; Chris@0: $this->setRawContent($testpage); Chris@0: Chris@0: // Matches the first link. Chris@0: $urls = $this->xpath('//a[text()=:text]', [':text' => 'A "weird" link, just to bother the dumb "XPath 1.0"']); Chris@0: $this->assertEqual($urls[0]['href'], 'link1', 'Match with quotes.'); Chris@0: Chris@0: $urls = $this->xpath('//a[text()=:text]', [':text' => 'A second "even more weird" link, in memory of George O\'Malley']); Chris@0: $this->assertEqual($urls[0]['href'], 'link2', 'Match with mixed single and double quotes.'); Chris@0: Chris@0: $urls = $this->xpath('//a[text()=:text]', [':text' => 'A $third$ link, so weird it\'s worth $1 million']); Chris@0: $this->assertEqual($urls[0]['href'], 'link3', 'Match with a regular expression back reference symbol (dollar sign).'); Chris@0: Chris@0: $urls = $this->xpath('//a[text()=:text]', [':text' => 'A fourth link, containing alternative \\1 regex backreferences \\2']); Chris@0: $this->assertEqual($urls[0]['href'], 'link4', 'Match with another regular expression back reference symbol (double backslash).'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that cookies set during a request are available for testing. Chris@0: */ Chris@0: public function testCookies() { Chris@0: // Check that the $this->cookies property is populated when a user logs in. Chris@0: $user = $this->drupalCreateUser(); Chris@18: $edit = ['name' => $user->getAccountName(), 'pass' => $user->pass_raw]; Chris@0: $this->drupalPostForm('', $edit, t('Log in')); Chris@0: $this->assertEqual(count($this->cookies), 1, 'A cookie is set when the user logs in.'); Chris@0: Chris@0: // Check that the name and value of the cookie match the request data. Chris@0: $cookie_header = $this->drupalGetHeader('set-cookie', TRUE); Chris@0: Chris@0: // The name and value are located at the start of the string, separated by Chris@0: // an equals sign and ending in a semicolon. Chris@0: preg_match('/^([^=]+)=([^;]+)/', $cookie_header, $matches); Chris@0: $name = $matches[1]; Chris@0: $value = $matches[2]; Chris@0: Chris@0: $this->assertTrue(array_key_exists($name, $this->cookies), 'The cookie name is correct.'); Chris@0: $this->assertEqual($value, $this->cookies[$name]['value'], 'The cookie value is correct.'); Chris@0: Chris@0: // Set a flag indicating that a cookie has been set in this test. Chris@0: // @see testCookieDoesNotBleed() Chris@0: static::$cookieSet = TRUE; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that the cookies from a previous test do not bleed into a new test. Chris@0: * Chris@0: * @see static::testCookies() Chris@0: */ Chris@0: public function testCookieDoesNotBleed() { Chris@0: // In order for this test to be effective it should always run after the Chris@0: // testCookies() test. Chris@0: $this->assertTrue(static::$cookieSet, 'Tests have been executed in the expected order.'); Chris@0: $this->assertEqual(count($this->cookies), 0, 'No cookies are present at the start of a new test.'); Chris@0: } Chris@0: Chris@0: }