Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / locale / tests / src / Functional / LocaleLocaleLookupTest.php
1 <?php
2
3 namespace Drupal\Tests\locale\Functional;
4
5 use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\Tests\BrowserTestBase;
8
9 /**
10  * Tests LocaleLookup.
11  *
12  * @group locale
13  */
14 class LocaleLocaleLookupTest extends BrowserTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['locale', 'locale_test'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     // Change the language default object to different values.
30     ConfigurableLanguage::createFromLangcode('fr')->save();
31     $this->config('system.site')->set('default_langcode', 'fr')->save();
32
33     $this->drupalLogin($this->rootUser);
34   }
35
36   /**
37    * Tests that there are no circular dependencies.
38    */
39   public function testCircularDependency() {
40     // Ensure that we can enable early_translation_test on a non-english site.
41     $this->drupalPostForm('admin/modules', ['modules[early_translation_test][enable]' => TRUE], t('Install'));
42     $this->assertResponse(200);
43   }
44
45   /**
46    * Test language fallback defaults.
47    */
48   public function testLanguageFallbackDefaults() {
49     $this->drupalGet('');
50     // Ensure state of fallback languages persisted by
51     // locale_test_language_fallback_candidates_locale_lookup_alter() is empty.
52     $this->assertEqual(\Drupal::state()->get('locale.test_language_fallback_candidates_locale_lookup_alter_candidates'), []);
53     // Make sure there is enough information provided for alter hooks.
54     $context = \Drupal::state()->get('locale.test_language_fallback_candidates_locale_lookup_alter_context');
55     $this->assertEqual($context['langcode'], 'fr');
56     $this->assertEqual($context['operation'], 'locale_lookup');
57   }
58
59   /**
60    * Test old plural style @count[number] fix.
61    *
62    * @dataProvider providerTestFixOldPluralStyle
63    */
64   public function testFixOldPluralStyle($translation_value, $expected) {
65     $string_storage = \Drupal::service('locale.storage');
66     $string = $string_storage->findString(['source' => 'Member for', 'context' => '']);
67     $lid = $string->getId();
68     $string_storage->createTranslation([
69       'lid' => $lid,
70       'language' => 'fr',
71       'translation' => $translation_value,
72     ])->save();
73     _locale_refresh_translations(['fr'], [$lid]);
74
75     // Check that 'count[2]' was fixed for render value.
76     $this->drupalGet('');
77     $this->assertSession()->pageTextContains($expected);
78
79     // Check that 'count[2]' was saved for source value.
80     $translation = $string_storage->findTranslation(['language' => 'fr', 'lid' => $lid])->translation;
81     $this->assertSame($translation_value, $translation, 'Source value not changed');
82     $this->assertNotFalse(strpos($translation, '@count[2]'), 'Source value contains @count[2]');
83   }
84
85   /**
86    * Provides data for testFixOldPluralStyle().
87    *
88    * @return array
89    *   An array of test data:
90    *     - translation value
91    *     - expected result
92    */
93   public function providerTestFixOldPluralStyle() {
94     return [
95       'non-plural translation' => ['@count[2] non-plural test', '@count[2] non-plural test'],
96       'plural translation' => ['@count[2] plural test' . PluralTranslatableMarkup::DELIMITER, '@count plural test'],
97     ];
98   }
99
100 }