diff --git a/core/modules/language/tests/src/Functional/LanguageNegotiationInfoTest.php b/core/modules/language/tests/src/Functional/LanguageNegotiationInfoTest.php index 9e81989..4c274e5 100644 --- a/core/modules/language/tests/src/Functional/LanguageNegotiationInfoTest.php +++ b/core/modules/language/tests/src/Functional/LanguageNegotiationInfoTest.php @@ -105,8 +105,7 @@ function testInfoAlterations() { $this->assertFalse(isset($negotiation[$interface_method_id]), 'Interface language negotiation method removed from the stored settings.'); $this->drupalGet('admin/config/regional/language/detection'); - // Interface language negotiation method unavailable.. - $this->assertNoFieldByName($form_field, NULL); + $this->assertNoFieldByName($form_field, NULL, 'Interface language negotiation method unavailable.'); // Check that type-specific language negotiation methods can be assigned // only to the corresponding language types. diff --git a/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php b/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php index 11adb12..299f48c 100644 --- a/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php +++ b/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php @@ -73,22 +73,24 @@ protected function doTestLanguageBlockAuthenticated($block_label) { // Assert that each list item and anchor element has the appropriate data- // attributes. - list($language_switcher) = $this->xpath('//div[@id=:id]', array(':id' => 'block-test-language-block')); + $language_switchers = $this->xpath('//div[@id=:id]/ul/li', array(':id' => 'block-test-language-block')); $list_items = array(); $anchors = array(); $labels = array(); - foreach ($language_switcher->ul->li as $list_item) { - $classes = explode(" ", (string) $list_item['class']); + foreach ($language_switchers as $list_item) { + $classes = explode(" ", $list_item->getAttribute('class')); list($langcode) = array_intersect($classes, array('en', 'fr')); $list_items[] = array( 'langcode_class' => $langcode, - 'data-drupal-link-system-path' => (string) $list_item['data-drupal-link-system-path'], + 'data-drupal-link-system-path' => $list_item->getAttribute('data-drupal-link-system-path'), ); + + $link = $list_item->find('xpath', 'a'); $anchors[] = array( - 'hreflang' => (string) $list_item->a['hreflang'], - 'data-drupal-link-system-path' => (string) $list_item->a['data-drupal-link-system-path'], + 'hreflang' => $link->getAttribute('hreflang'), + 'data-drupal-link-system-path' => $link->getAttribute('data-drupal-link-system-path'), ); - $labels[] = (string) $list_item->a; + $labels[] = $link->getText(); } $expected_list_items = array( 0 => array('langcode_class' => 'en', 'data-drupal-link-system-path' => 'user/2'), @@ -123,7 +125,7 @@ protected function doTestLanguageBlockAnonymous($block_label) { $this->assertText($block_label, 'Language switcher block found.'); // Assert that only the current language is marked as active. - list($language_switcher) = $this->xpath('//div[@id=:id]', array(':id' => 'block-test-language-block')); + $language_switchers = $this->xpath('//div[@id=:id]/ul/li', array(':id' => 'block-test-language-block')); $links = array( 'active' => array(), 'inactive' => array(), @@ -133,8 +135,8 @@ protected function doTestLanguageBlockAnonymous($block_label) { 'inactive' => array(), ); $labels = array(); - foreach ($language_switcher->ul->li as $link) { - $classes = explode(" ", (string) $link['class']); + foreach ($language_switchers as $list_item) { + $classes = explode(" ", $list_item->getAttribute('class')); list($langcode) = array_intersect($classes, array('en', 'fr')); if (in_array('is-active', $classes)) { $links['active'][] = $langcode; @@ -142,14 +144,16 @@ protected function doTestLanguageBlockAnonymous($block_label) { else { $links['inactive'][] = $langcode; } - $anchor_classes = explode(" ", (string) $link->a['class']); + + $link = $list_item->find('xpath', 'a'); + $anchor_classes = explode(" ", $link->getAttribute('class')); if (in_array('is-active', $anchor_classes)) { $anchors['active'][] = $langcode; } else { $anchors['inactive'][] = $langcode; } - $labels[] = (string) $link->a; + $labels[] = $link->getText(); } $this->assertIdentical($links, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language list item is marked as active on the language switcher block.'); $this->assertIdentical($anchors, array('active' => array('en'), 'inactive' => array('fr')), 'Only the current language anchor is marked as active on the language switcher block.'); @@ -207,7 +211,7 @@ function testLanguageBlockWithDomain() { ':hreflang' => 'en', )); $english_url = $generator->generateFromRoute('entity.user.canonical', array('user' => 2), array('language' => $languages['en'])); - $this->assertEqual($english_url, (string) $english_link['href']); + $this->assertEqual($english_url, $english_link->getAttribute('href')); // Verify the Italian URL is correct list($italian_link) = $this->xpath('//div[@id=:id]/ul/li/a[@hreflang=:hreflang]', array( @@ -215,7 +219,7 @@ function testLanguageBlockWithDomain() { ':hreflang' => 'it', )); $italian_url = $generator->generateFromRoute('entity.user.canonical', array('user' => 2), array('language' => $languages['it'])); - $this->assertEqual($italian_url, (string) $italian_link['href']); + $this->assertEqual($italian_url, $italian_link->getAttribute('href')); } /** diff --git a/core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php b/core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php index 30452aa..40780c8 100644 --- a/core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php +++ b/core/modules/language/tests/src/Functional/LanguageUILanguageNegotiationTest.php @@ -69,9 +69,9 @@ function testUILanguageNegotiation() { // For testing path prefix. $langcode = 'zh-hans'; // For setting browser language preference to 'vi'. - $http_header_browser_fallback = array("Accept-Language: $langcode_browser_fallback;q=1"); + $http_header_browser_fallback = array("Accept-Language" => "$langcode_browser_fallback;q=1"); // For setting browser language preference to some unknown. - $http_header_blah = array("Accept-Language: blah;q=1"); + $http_header_blah = array("Accept-Language" => "blah;q=1"); // Setup the site languages by installing two languages. // Set the default language in order for the translated string to be registered @@ -107,7 +107,7 @@ function testUILanguageNegotiation() { ); $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); $textarea = current($this->xpath('//textarea')); - $lid = (string) $textarea[0]['name']; + $lid = $textarea->getAttribute('name'); $edit = array( $lid => $language_browser_fallback_string, ); @@ -119,7 +119,7 @@ function testUILanguageNegotiation() { ); $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); $textarea = current($this->xpath('//textarea')); - $lid = (string) $textarea[0]['name']; + $lid = $textarea->getAttribute('name'); $edit = array( $lid => $language_string, ); @@ -398,7 +398,7 @@ function testUrlLanguageFallback() { // Access the front page without specifying any valid URL language prefix // and having as browser language preference a non-default language. - $http_header = array("Accept-Language: $langcode_browser_fallback;q=1"); + $http_header = array("Accept-Language" => "$langcode_browser_fallback;q=1"); $language = new Language(array('id' => '')); $this->drupalGet('', array('language' => $language), $http_header); @@ -406,11 +406,11 @@ function testUrlLanguageFallback() { // language. $args = array(':id' => 'block-test-language-block', ':url' => \Drupal::url('') . $langcode_browser_fallback); $fields = $this->xpath('//div[@id=:id]//a[@class="language-link is-active" and starts-with(@href, :url)]', $args); - $this->assertTrue($fields[0] == $languages[$langcode_browser_fallback]->getName(), 'The browser language is the URL active language'); + $this->assertSame($fields[0]->getText(), $languages[$langcode_browser_fallback]->getName(), 'The browser language is the URL active language'); // Check that URLs are rewritten using the given browser language. $fields = $this->xpath('//div[@class="site-name"]/a[@rel="home" and @href=:url]', $args); - $this->assertTrue($fields[0] == 'Drupal', 'URLs are rewritten using the browser language.'); + $this->assertSame($fields[0]->getText(), 'Drupal', 'URLs are rewritten using the browser language.'); } /** diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index 18e1b25..7b13fe7 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -7,6 +7,7 @@ use Behat\Mink\Mink; use Behat\Mink\Session; use Drupal\Component\FileCache\FileCacheFactory; +use Drupal\Component\Serialization\Json; use Drupal\Component\Serialization\Yaml; use Drupal\Component\Utility\Html; use Drupal\Component\Utility\SafeMarkup; @@ -29,7 +30,6 @@ use Drupal\simpletest\NodeCreationTrait; use Drupal\user\Entity\Role; use Drupal\user\Entity\User; -use Drupal\Tests\ConfigTestTrait; use Symfony\Component\CssSelector\CssSelectorConverter; use Symfony\Component\HttpFoundation\Request; @@ -626,17 +626,24 @@ protected function buildUrl($path, array $options = array()) { * Drupal path or URL to load into Mink controlled browser. * @param array $options * (optional) Options to be forwarded to the url generator. + * @param string[] $headers + * An array containing additional HTTP request headers, the array keys are + * the header names and the array values the header values. * * @return string * The retrieved HTML string, also available as $this->getRawContent() */ - protected function drupalGet($path, array $options = array()) { + protected function drupalGet($path, array $options = array(), array $headers = array()) { $options['absolute'] = TRUE; $url = $this->buildUrl($path, $options); $session = $this->getSession(); $this->prepareRequest(); + foreach ($headers as $header_name => $header_value) { + $this->getSession()->setRequestHeader($header_name, $header_value); + } + $session->visit($url); $out = $session->getPage()->getContent(); @@ -1770,6 +1777,20 @@ protected function getUrl() { } /** + * Gets the JavaScript drupalSettings variable for the currently-loaded page. + * + * @return array + * The JSON decoded drupalSettings value from the current page. + */ + protected function getDrupalSettings() { + $html = $this->getSession()->getPage()->getHtml(); + if (preg_match('@@', $html, $matches)) { + return Json::decode($matches[1]); + } + return []; + } + + /** * {@inheritdoc} */ public static function assertEquals($expected, $actual, $message = '', $delta = 0.0, $maxDepth = 10, $canonicalize = FALSE, $ignoreCase = FALSE) {