From 52a5a05fc0fc461c5155f9d1daa06c0d2b67d782 Mon Sep 17 00:00:00 2001 From: GoZ Date: Mon, 13 Mar 2017 10:56:30 +0100 Subject: [PATCH] Issue #2756059 by klausi, Jo Fitzgerald, GoZ, naveenvalecha: Convert web tests to browser tests for language module --- .../EntityTypeWithoutLanguageFormTest.php | 6 ++-- .../LanguageConfigurationElementTest.php | 6 ++-- .../src/Functional}/LanguageConfigurationTest.php | 11 +++---- .../LanguageCustomLanguageConfigurationTest.php | 6 ++-- .../src/Functional}/LanguageListTest.php | 21 +++++++++--- .../src/Functional}/LanguageLocaleListTest.php | 14 ++++---- .../LanguageSelectorTranslatableTest.php | 8 ++--- .../src/Functional}/LanguageSwitchingTest.php | 38 ++++++++++++---------- .../src/Functional}/LanguageTourTest.php | 2 +- .../LanguageUILanguageNegotiationTest.php | 20 ++++++------ .../src/Functional}/LanguageUrlRewritingTest.php | 11 +++---- 11 files changed, 78 insertions(+), 65 deletions(-) rename core/modules/language/{src/Tests => tests/src/Functional}/EntityTypeWithoutLanguageFormTest.php (87%) rename core/modules/language/{src/Tests => tests/src/Functional}/LanguageConfigurationElementTest.php (98%) rename core/modules/language/{src/Tests => tests/src/Functional}/LanguageConfigurationTest.php (98%) rename core/modules/language/{src/Tests => tests/src/Functional}/LanguageCustomLanguageConfigurationTest.php (96%) rename core/modules/language/{src/Tests => tests/src/Functional}/LanguageListTest.php (93%) rename core/modules/language/{src/Tests => tests/src/Functional}/LanguageLocaleListTest.php (84%) rename core/modules/language/{src/Tests => tests/src/Functional}/LanguageSelectorTranslatableTest.php (88%) rename core/modules/language/{src/Tests => tests/src/Functional}/LanguageSwitchingTest.php (94%) rename core/modules/language/{src/Tests => tests/src/Functional}/LanguageTourTest.php (96%) rename core/modules/language/{src/Tests => tests/src/Functional}/LanguageUILanguageNegotiationTest.php (97%) rename core/modules/language/{src/Tests => tests/src/Functional}/LanguageUrlRewritingTest.php (96%) diff --git a/core/modules/language/src/Tests/EntityTypeWithoutLanguageFormTest.php b/core/modules/language/tests/src/Functional/EntityTypeWithoutLanguageFormTest.php similarity index 87% rename from core/modules/language/src/Tests/EntityTypeWithoutLanguageFormTest.php rename to core/modules/language/tests/src/Functional/EntityTypeWithoutLanguageFormTest.php index 97ccf2f..f7b6191 100644 --- a/core/modules/language/src/Tests/EntityTypeWithoutLanguageFormTest.php +++ b/core/modules/language/tests/src/Functional/EntityTypeWithoutLanguageFormTest.php @@ -1,8 +1,8 @@ checkConfigurableLanguageWeight('after re-ordering'); // Remove predefined language. - $edit = [ - 'confirm' => 1, - ]; - $this->drupalPostForm('admin/config/regional/language/delete/fr', $edit, 'Delete'); + $this->drupalPostForm('admin/config/regional/language/delete/fr', [], 'Delete'); $this->checkConfigurableLanguageWeight('after deleting a language'); } diff --git a/core/modules/language/src/Tests/LanguageCustomLanguageConfigurationTest.php b/core/modules/language/tests/src/Functional/LanguageCustomLanguageConfigurationTest.php similarity index 96% rename from core/modules/language/src/Tests/LanguageCustomLanguageConfigurationTest.php rename to core/modules/language/tests/src/Functional/LanguageCustomLanguageConfigurationTest.php index 2eb02df..a0510be 100644 --- a/core/modules/language/src/Tests/LanguageCustomLanguageConfigurationTest.php +++ b/core/modules/language/tests/src/Functional/LanguageCustomLanguageConfigurationTest.php @@ -1,17 +1,17 @@ assertResponse(403, 'Can not delete locked language'); // 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'); - $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->assertNoFieldChecked('edit-site-default-language-xx', 'The previous default language got deselected.'); } diff --git a/core/modules/language/src/Tests/LanguageLocaleListTest.php b/core/modules/language/tests/src/Functional/LanguageLocaleListTest.php similarity index 84% rename from core/modules/language/src/Tests/LanguageLocaleListTest.php rename to core/modules/language/tests/src/Functional/LanguageLocaleListTest.php index 5f96d89..a39a112 100644 --- a/core/modules/language/src/Tests/LanguageLocaleListTest.php +++ b/core/modules/language/tests/src/Functional/LanguageLocaleListTest.php @@ -1,15 +1,15 @@ drupalGet('fr/admin/config/regional/language/add'); - $select = $this->xpath('//select[@id="edit-predefined-langcode"]'); - $select_element = (array) end($select); - $options = $select_element['option']; + $option_elements = $this->xpath('//select[@id="edit-predefined-langcode/option"]'); + $options = []; + foreach ($option_elements as $option_element) { + $options[] = $option_element->getText(); + } // Remove the 'Custom language...' option form the end. array_pop($options); // Order language list. diff --git a/core/modules/language/src/Tests/LanguageSelectorTranslatableTest.php b/core/modules/language/tests/src/Functional/LanguageSelectorTranslatableTest.php similarity index 88% rename from core/modules/language/src/Tests/LanguageSelectorTranslatableTest.php rename to core/modules/language/tests/src/Functional/LanguageSelectorTranslatableTest.php index d13626c..3d027c3 100644 --- a/core/modules/language/src/Tests/LanguageSelectorTranslatableTest.php +++ b/core/modules/language/tests/src/Functional/LanguageSelectorTranslatableTest.php @@ -1,15 +1,15 @@ xpath('//select[@id=:id]//option[@value=:option]', [':id' => 'edit-settings-user-user-settings-language-langcode', ':option' => 'en']); // 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.'); } } diff --git a/core/modules/language/src/Tests/LanguageSwitchingTest.php b/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php similarity index 94% rename from core/modules/language/src/Tests/LanguageSwitchingTest.php rename to core/modules/language/tests/src/Functional/LanguageSwitchingTest.php index 4001521..1303082 100644 --- a/core/modules/language/src/Tests/LanguageSwitchingTest.php +++ b/core/modules/language/tests/src/Functional/LanguageSwitchingTest.php @@ -1,19 +1,19 @@ 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 = []; $anchors = []; $labels = []; - 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, ['en', 'fr']); $list_items[] = [ '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[] = [ - '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 = [ 0 => ['langcode_class' => 'en', 'data-drupal-link-system-path' => 'user/2'], @@ -124,7 +126,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]', [':id' => 'block-test-language-block']); + $language_switchers = $this->xpath('//div[@id=:id]/ul/li', [':id' => 'block-test-language-block']); $links = [ 'active' => [], 'inactive' => [], @@ -134,8 +136,8 @@ protected function doTestLanguageBlockAnonymous($block_label) { 'inactive' => [], ]; $labels = []; - 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, ['en', 'fr']); if (in_array('is-active', $classes)) { $links['active'][] = $langcode; @@ -143,14 +145,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, ['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.'); @@ -208,7 +212,7 @@ public function testLanguageBlockWithDomain() { ':hreflang' => '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 list($italian_link) = $this->xpath('//div[@id=:id]/ul/li/a[@hreflang=:hreflang]', [ @@ -216,7 +220,7 @@ public function testLanguageBlockWithDomain() { ':hreflang' => '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')); } /** diff --git a/core/modules/language/src/Tests/LanguageTourTest.php b/core/modules/language/tests/src/Functional/LanguageTourTest.php similarity index 96% rename from core/modules/language/src/Tests/LanguageTourTest.php rename to core/modules/language/tests/src/Functional/LanguageTourTest.php index b11006e..b0dc5d7 100644 --- a/core/modules/language/src/Tests/LanguageTourTest.php +++ b/core/modules/language/tests/src/Functional/LanguageTourTest.php @@ -1,6 +1,6 @@ "$langcode_browser_fallback;q=1"]; // 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. // Set the default language in order for the translated string to be registered @@ -107,7 +107,7 @@ public 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 = [ $lid => $language_browser_fallback_string, ]; @@ -119,7 +119,7 @@ public 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 = [ $lid => $language_string, ]; @@ -398,7 +398,7 @@ public 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 = ["Accept-Language: $langcode_browser_fallback;q=1"]; + $http_header = ["Accept-Language" => "$langcode_browser_fallback;q=1"]; $language = new Language(['id' => '']); $this->drupalGet('', ['language' => $language], $http_header); @@ -406,11 +406,11 @@ public function testUrlLanguageFallback() { // language. $args = [':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/modules/language/src/Tests/LanguageUrlRewritingTest.php b/core/modules/language/tests/src/Functional/LanguageUrlRewritingTest.php similarity index 96% rename from core/modules/language/src/Tests/LanguageUrlRewritingTest.php rename to core/modules/language/tests/src/Functional/LanguageUrlRewritingTest.php index 65ae64c..7021aa0 100644 --- a/core/modules/language/src/Tests/LanguageUrlRewritingTest.php +++ b/core/modules/language/tests/src/Functional/LanguageUrlRewritingTest.php @@ -1,12 +1,12 @@ getId()]) ? $prefixes[$language->getId()] : $this->randomMachineName(); - if ($this->assertNotEqual($stored_prefix, $prefix, $message1)) { - $prefix = $stored_prefix; - } + $this->assertNotEqual($stored_prefix, $prefix, $message1); + $prefix = $stored_prefix; $this->drupalGet("$prefix/$path"); $this->assertResponse(404, $message2); -- 2.8.1