Commit c8b4da18 authored by catch's avatar catch

Issue #2756059 by GoZ, klausi, Jo Fitzgerald, naveenvalecha, dawehner: Convert...

Issue #2756059 by GoZ, klausi, Jo Fitzgerald, naveenvalecha, dawehner: Convert web tests to browser tests for language module
parent 61c3d57c
<?php <?php
namespace Drupal\language\Tests; namespace Drupal\Tests\language\Functional;
use Drupal\simpletest\WebTestBase; use Drupal\Tests\BrowserTestBase;
/** /**
* Tests entity type without language support. * Tests entity type without language support.
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* *
* @group language * @group language
*/ */
class EntityTypeWithoutLanguageFormTest extends WebTestBase { class EntityTypeWithoutLanguageFormTest extends BrowserTestBase {
/** /**
* Modules to enable. * Modules to enable.
......
<?php <?php
namespace Drupal\language\Tests; namespace Drupal\Tests\language\Functional;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings; use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\simpletest\WebTestBase;
use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\BrowserTestBase;
/** /**
* Tests the features of the language configuration element field. * Tests the features of the language configuration element field.
* *
* @group language * @group language
*/ */
class LanguageConfigurationElementTest extends WebTestBase { class LanguageConfigurationElementTest extends BrowserTestBase {
/** /**
* Modules to enable. * Modules to enable.
......
<?php <?php
namespace Drupal\language\Tests; namespace Drupal\Tests\language\Functional;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase; use Drupal\Tests\BrowserTestBase;
/** /**
* Adds and configures languages to check negotiation changes. * Adds and configures languages to check negotiation changes.
* *
* @group language * @group language
*/ */
class LanguageConfigurationTest extends WebTestBase { class LanguageConfigurationTest extends BrowserTestBase {
/** /**
* Modules to enable. * Modules to enable.
...@@ -169,10 +169,7 @@ public function testLanguageConfigurationWeight() { ...@@ -169,10 +169,7 @@ public function testLanguageConfigurationWeight() {
$this->checkConfigurableLanguageWeight('after re-ordering'); $this->checkConfigurableLanguageWeight('after re-ordering');
// Remove predefined language. // Remove predefined language.
$edit = [ $this->drupalPostForm('admin/config/regional/language/delete/fr', [], 'Delete');
'confirm' => 1,
];
$this->drupalPostForm('admin/config/regional/language/delete/fr', $edit, 'Delete');
$this->checkConfigurableLanguageWeight('after deleting a language'); $this->checkConfigurableLanguageWeight('after deleting a language');
} }
......
<?php <?php
namespace Drupal\language\Tests; namespace Drupal\Tests\language\Functional;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
/** /**
* Adds and configures custom languages. * Adds and configures custom languages.
* *
* @group language * @group language
*/ */
class LanguageCustomLanguageConfigurationTest extends WebTestBase { class LanguageCustomLanguageConfigurationTest extends BrowserTestBase {
/** /**
* Modules to enable. * Modules to enable.
......
<?php <?php
namespace Drupal\language\Tests; namespace Drupal\Tests\language\Functional;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
/** /**
* Adds a new language and tests changing its status and the default language. * Adds a new language and tests changing its status and the default language.
* *
* @group language * @group language
*/ */
class LanguageListTest extends WebTestBase { class LanguageListTest extends BrowserTestBase {
/** /**
* Modules to enable. * Modules to enable.
...@@ -176,9 +176,20 @@ public function testLanguageList() { ...@@ -176,9 +176,20 @@ public function testLanguageList() {
$this->assertResponse(403, 'Can not delete locked language'); $this->assertResponse(403, 'Can not delete locked language');
// Ensure that NL cannot be set default when it's not available. // Ensure that NL cannot be set default when it's not available.
// First create the NL language.
$edit = [
'predefined_langcode' => 'nl',
];
$this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
// Load the form which has now the additional NL language option.
$this->drupalGet('admin/config/regional/language'); $this->drupalGet('admin/config/regional/language');
$extra_values = '&site_default_language=nl';
$this->drupalPostForm(NULL, [], t('Save configuration'), [], [], NULL, $extra_values); // Delete the NL language in the background.
$language_storage = $this->container->get('entity_type.manager')->getStorage('configurable_language');
$language_storage->load('nl')->delete();
$this->drupalPostForm(NULL, ['site_default_language' => 'nl'], 'Save configuration');
$this->assertText(t('Selected default language no longer exists.')); $this->assertText(t('Selected default language no longer exists.'));
$this->assertNoFieldChecked('edit-site-default-language-xx', 'The previous default language got deselected.'); $this->assertNoFieldChecked('edit-site-default-language-xx', 'The previous default language got deselected.');
} }
......
<?php <?php
namespace Drupal\language\Tests; namespace Drupal\Tests\language\Functional;
use Drupal\simpletest\WebTestBase; use Drupal\Tests\BrowserTestBase;
/** /**
* Adds a new language with translations and tests language list order. * Adds a new language with translations and tests language list order.
* *
* @group language * @group language
*/ */
class LanguageLocaleListTest extends WebTestBase { class LanguageLocaleListTest extends BrowserTestBase {
/** /**
* Modules to enable. * Modules to enable.
...@@ -57,9 +57,11 @@ public function testLanguageLocaleList() { ...@@ -57,9 +57,11 @@ public function testLanguageLocaleList() {
// Get language list displayed in select list. // Get language list displayed in select list.
$this->drupalGet('fr/admin/config/regional/language/add'); $this->drupalGet('fr/admin/config/regional/language/add');
$select = $this->xpath('//select[@id="edit-predefined-langcode"]'); $option_elements = $this->xpath('//select[@id="edit-predefined-langcode/option"]');
$select_element = (array) end($select); $options = [];
$options = $select_element['option']; foreach ($option_elements as $option_element) {
$options[] = $option_element->getText();
}
// Remove the 'Custom language...' option form the end. // Remove the 'Custom language...' option form the end.
array_pop($options); array_pop($options);
// Order language list. // Order language list.
......
<?php <?php
namespace Drupal\language\Tests; namespace Drupal\Tests\language\Functional;
use Drupal\simpletest\WebTestBase; use Drupal\Tests\BrowserTestBase;
/** /**
* Tests the content translation settings language selector options. * Tests the content translation settings language selector options.
* *
* @group language * @group language
*/ */
class LanguageSelectorTranslatableTest extends WebTestBase { class LanguageSelectorTranslatableTest extends BrowserTestBase {
/** /**
* Modules to enable. * Modules to enable.
...@@ -82,7 +82,7 @@ public function testLanguageStringSelector() { ...@@ -82,7 +82,7 @@ public function testLanguageStringSelector() {
$elements = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => 'edit-settings-user-user-settings-language-langcode', ':option' => 'en']); $elements = $this->xpath('//select[@id=:id]//option[@value=:option]', [':id' => 'edit-settings-user-user-settings-language-langcode', ':option' => 'en']);
// Check that the language text is translated. // Check that the language text is translated.
$this->assertEqual((string) $elements[0], $name_translation, 'Checking the option string English is translated to Spanish.'); $this->assertEqual($elements[0]->getText(), $name_translation, 'Checking the option string English is translated to Spanish.');
} }
} }
<?php <?php
namespace Drupal\language\Tests; namespace Drupal\Tests\language\Functional;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\menu_link_content\Entity\MenuLinkContent; use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\simpletest\WebTestBase; use Drupal\Tests\BrowserTestBase;
/** /**
* Functional tests for the language switching feature. * Functional tests for the language switching feature.
* *
* @group language * @group language
*/ */
class LanguageSwitchingTest extends WebTestBase { class LanguageSwitchingTest extends BrowserTestBase {
/** /**
* Modules to enable. * Modules to enable.
...@@ -73,22 +73,24 @@ protected function doTestLanguageBlockAuthenticated($block_label) { ...@@ -73,22 +73,24 @@ protected function doTestLanguageBlockAuthenticated($block_label) {
// Assert that each list item and anchor element has the appropriate data- // Assert that each list item and anchor element has the appropriate data-
// attributes. // attributes.
list($language_switcher) = $this->xpath('//div[@id=:id]', [':id' => 'block-test-language-block']); $language_switchers = $this->xpath('//div[@id=:id]/ul/li', [':id' => 'block-test-language-block']);
$list_items = []; $list_items = [];
$anchors = []; $anchors = [];
$labels = []; $labels = [];
foreach ($language_switcher->ul->li as $list_item) { foreach ($language_switchers as $list_item) {
$classes = explode(" ", (string) $list_item['class']); $classes = explode(" ", $list_item->getAttribute('class'));
list($langcode) = array_intersect($classes, ['en', 'fr']); list($langcode) = array_intersect($classes, ['en', 'fr']);
$list_items[] = [ $list_items[] = [
'langcode_class' => $langcode, '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[] = [ $anchors[] = [
'hreflang' => (string) $list_item->a['hreflang'], 'hreflang' => $link->getAttribute('hreflang'),
'data-drupal-link-system-path' => (string) $list_item->a['data-drupal-link-system-path'], 'data-drupal-link-system-path' => $link->getAttribute('data-drupal-link-system-path'),
]; ];
$labels[] = (string) $list_item->a; $labels[] = $link->getText();
} }
$expected_list_items = [ $expected_list_items = [
0 => ['langcode_class' => 'en', 'data-drupal-link-system-path' => 'user/2'], 0 => ['langcode_class' => 'en', 'data-drupal-link-system-path' => 'user/2'],
...@@ -124,7 +126,7 @@ protected function doTestLanguageBlockAnonymous($block_label) { ...@@ -124,7 +126,7 @@ protected function doTestLanguageBlockAnonymous($block_label) {
$this->assertText($block_label, 'Language switcher block found.'); $this->assertText($block_label, 'Language switcher block found.');
// Assert that only the current language is marked as active. // Assert that only the current language is marked as active.
list($language_switcher) = $this->xpath('//div[@id=:id]', [':id' => 'block-test-language-block']); $language_switchers = $this->xpath('//div[@id=:id]/ul/li', [':id' => 'block-test-language-block']);
$links = [ $links = [
'active' => [], 'active' => [],
'inactive' => [], 'inactive' => [],
...@@ -134,8 +136,8 @@ protected function doTestLanguageBlockAnonymous($block_label) { ...@@ -134,8 +136,8 @@ protected function doTestLanguageBlockAnonymous($block_label) {
'inactive' => [], 'inactive' => [],
]; ];
$labels = []; $labels = [];
foreach ($language_switcher->ul->li as $link) { foreach ($language_switchers as $list_item) {
$classes = explode(" ", (string) $link['class']); $classes = explode(" ", $list_item->getAttribute('class'));
list($langcode) = array_intersect($classes, ['en', 'fr']); list($langcode) = array_intersect($classes, ['en', 'fr']);
if (in_array('is-active', $classes)) { if (in_array('is-active', $classes)) {
$links['active'][] = $langcode; $links['active'][] = $langcode;
...@@ -143,14 +145,16 @@ protected function doTestLanguageBlockAnonymous($block_label) { ...@@ -143,14 +145,16 @@ protected function doTestLanguageBlockAnonymous($block_label) {
else { else {
$links['inactive'][] = $langcode; $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)) { if (in_array('is-active', $anchor_classes)) {
$anchors['active'][] = $langcode; $anchors['active'][] = $langcode;
} }
else { else {
$anchors['inactive'][] = $langcode; $anchors['inactive'][] = $langcode;
} }
$labels[] = (string) $link->a; $labels[] = $link->getText();
} }
$this->assertIdentical($links, ['active' => ['en'], 'inactive' => ['fr']], 'Only the current language list item is marked as active on the language switcher block.'); $this->assertIdentical($links, ['active' => ['en'], 'inactive' => ['fr']], 'Only the current language list item is marked as active on the language switcher block.');
$this->assertIdentical($anchors, ['active' => ['en'], 'inactive' => ['fr']], 'Only the current language anchor is marked as active on the language switcher block.'); $this->assertIdentical($anchors, ['active' => ['en'], 'inactive' => ['fr']], 'Only the current language anchor is marked as active on the language switcher block.');
...@@ -208,7 +212,7 @@ public function testLanguageBlockWithDomain() { ...@@ -208,7 +212,7 @@ public function testLanguageBlockWithDomain() {
':hreflang' => 'en', ':hreflang' => 'en',
]); ]);
$english_url = $generator->generateFromRoute('entity.user.canonical', ['user' => 2], ['language' => $languages['en']]); $english_url = $generator->generateFromRoute('entity.user.canonical', ['user' => 2], ['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 // Verify the Italian URL is correct
list($italian_link) = $this->xpath('//div[@id=:id]/ul/li/a[@hreflang=:hreflang]', [ list($italian_link) = $this->xpath('//div[@id=:id]/ul/li/a[@hreflang=:hreflang]', [
...@@ -216,7 +220,7 @@ public function testLanguageBlockWithDomain() { ...@@ -216,7 +220,7 @@ public function testLanguageBlockWithDomain() {
':hreflang' => 'it', ':hreflang' => 'it',
]); ]);
$italian_url = $generator->generateFromRoute('entity.user.canonical', ['user' => 2], ['language' => $languages['it']]); $italian_url = $generator->generateFromRoute('entity.user.canonical', ['user' => 2], ['language' => $languages['it']]);
$this->assertEqual($italian_url, (string) $italian_link['href']); $this->assertEqual($italian_url, $italian_link->getAttribute('href'));
} }
/** /**
......
<?php <?php
namespace Drupal\language\Tests; namespace Drupal\Tests\language\Functional;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage; use Drupal\language\Entity\ConfigurableLanguage;
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected; use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSelected;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession; use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationSession;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser; use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUser;
use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin; use Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin;
use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
* *
* @group language * @group language
*/ */
class LanguageUILanguageNegotiationTest extends WebTestBase { class LanguageUILanguageNegotiationTest extends BrowserTestBase {
/** /**
* Modules to enable. * Modules to enable.
...@@ -69,9 +69,9 @@ public function testUILanguageNegotiation() { ...@@ -69,9 +69,9 @@ public function testUILanguageNegotiation() {
// For testing path prefix. // For testing path prefix.
$langcode = 'zh-hans'; $langcode = 'zh-hans';
// For setting browser language preference to 'vi'. // For setting browser language preference to 'vi'.
$http_header_browser_fallback = ["Accept-Language: $langcode_browser_fallback;q=1"]; $http_header_browser_fallback = ["Accept-Language" => "$langcode_browser_fallback;q=1"];
// For setting browser language preference to some unknown. // For setting browser language preference to some unknown.
$http_header_blah = ["Accept-Language: blah;q=1"]; $http_header_blah = ["Accept-Language" => "blah;q=1"];
// Setup the site languages by installing two languages. // Setup the site languages by installing two languages.
// Set the default language in order for the translated string to be registered // Set the default language in order for the translated string to be registered
...@@ -107,7 +107,7 @@ public function testUILanguageNegotiation() { ...@@ -107,7 +107,7 @@ public function testUILanguageNegotiation() {
]; ];
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textarea = current($this->xpath('//textarea')); $textarea = current($this->xpath('//textarea'));
$lid = (string) $textarea[0]['name']; $lid = $textarea->getAttribute('name');
$edit = [ $edit = [
$lid => $language_browser_fallback_string, $lid => $language_browser_fallback_string,
]; ];
...@@ -119,7 +119,7 @@ public function testUILanguageNegotiation() { ...@@ -119,7 +119,7 @@ public function testUILanguageNegotiation() {
]; ];
$this->drupalPostForm('admin/config/regional/translate', $search, t('Filter')); $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
$textarea = current($this->xpath('//textarea')); $textarea = current($this->xpath('//textarea'));
$lid = (string) $textarea[0]['name']; $lid = $textarea->getAttribute('name');
$edit = [ $edit = [
$lid => $language_string, $lid => $language_string,
]; ];
...@@ -398,7 +398,7 @@ public function testUrlLanguageFallback() { ...@@ -398,7 +398,7 @@ public function testUrlLanguageFallback() {
// Access the front page without specifying any valid URL language prefix // Access the front page without specifying any valid URL language prefix
// and having as browser language preference a non-default language. // and having as browser language preference a non-default language.
$http_header = ["Accept-Language: $langcode_browser_fallback;q=1"]; $http_header = ["Accept-Language" => "$langcode_browser_fallback;q=1"];
$language = new Language(['id' => '']); $language = new Language(['id' => '']);
$this->drupalGet('', ['language' => $language], $http_header); $this->drupalGet('', ['language' => $language], $http_header);
...@@ -406,11 +406,11 @@ public function testUrlLanguageFallback() { ...@@ -406,11 +406,11 @@ public function testUrlLanguageFallback() {
// language. // language.
$args = [':id' => 'block-test-language-block', ':url' => \Drupal::url('<front>') . $langcode_browser_fallback]; $args = [':id' => 'block-test-language-block', ':url' => \Drupal::url('<front>') . $langcode_browser_fallback];
$fields = $this->xpath('//div[@id=:id]//a[@class="language-link is-active" and starts-with(@href, :url)]', $args); $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. // Check that URLs are rewritten using the given browser language.
$fields = $this->xpath('//div[@class="site-name"]/a[@rel="home" and @href=:url]', $args); $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.');
} }
/** /**
......
<?php <?php
namespace Drupal\language\Tests; namespace Drupal\Tests\language\Functional;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl; use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\simpletest\WebTestBase; use Drupal\Tests\BrowserTestBase;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
/** /**
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* *
* @group language * @group language
*/ */
class LanguageUrlRewritingTest extends WebTestBase { class LanguageUrlRewritingTest extends BrowserTestBase {
/** /**