comparison core/modules/locale/tests/src/Kernel/LocaleStringTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
1 <?php
2
3 namespace Drupal\Tests\locale\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\locale\StringInterface;
8
9 /**
10 * Tests the locale string storage, string objects and data API.
11 *
12 * @group locale
13 */
14 class LocaleStringTest extends KernelTestBase {
15
16 /**
17 * {@inheritdoc}
18 */
19 protected static $modules = [
20 'language',
21 'locale',
22 ];
23
24 /**
25 * The locale storage.
26 *
27 * @var \Drupal\locale\StringStorageInterface
28 */
29 protected $storage;
30
31 /**
32 * {@inheritdoc}
33 */
34 protected function setUp() {
35 parent::setUp();
36
37 // Add a default locale storage for all these tests.
38 $this->storage = $this->container->get('locale.storage');
39 // Create two languages: Spanish and German.
40 foreach (['es', 'de'] as $langcode) {
41 ConfigurableLanguage::createFromLangcode($langcode)->save();
42 }
43 $this->installSchema('locale', [
44 'locales_location',
45 'locales_source',
46 'locales_target',
47 ]);
48 }
49
50 /**
51 * Test CRUD API.
52 */
53 public function testStringCrudApi() {
54 // Create source string.
55 $source = $this->buildSourceString()->save();
56 $this->assertTrue($source->lid);
57
58 // Load strings by lid and source.
59 $string1 = $this->storage->findString(['lid' => $source->lid]);
60 $this->assertEquals($source, $string1);
61 $string2 = $this->storage->findString(['source' => $source->source, 'context' => $source->context]);
62 $this->assertEquals($source, $string2);
63 $string3 = $this->storage->findString(['source' => $source->source, 'context' => '']);
64 $this->assertFalse($string3);
65
66 // Check version handling and updating.
67 $this->assertEquals('none', $source->version);
68 $string = $this->storage->findTranslation(['lid' => $source->lid]);
69 $this->assertEquals(\Drupal::VERSION, $string->version);
70
71 // Create translation and find it by lid and source.
72 $langcode = 'es';
73 $translation = $this->createTranslation($source, $langcode);
74 $this->assertEquals(LOCALE_NOT_CUSTOMIZED, $translation->customized);
75 $string1 = $this->storage->findTranslation(['language' => $langcode, 'lid' => $source->lid]);
76 $this->assertEquals($translation->translation, $string1->translation);
77 $string2 = $this->storage->findTranslation(['language' => $langcode, 'source' => $source->source, 'context' => $source->context]);
78 $this->assertEquals($translation->translation, $string2->translation);
79 $translation
80 ->setCustomized()
81 ->save();
82 $translation = $this->storage->findTranslation(['language' => $langcode, 'lid' => $source->lid]);
83 $this->assertEquals(LOCALE_CUSTOMIZED, $translation->customized);
84
85 // Delete translation.
86 $translation->delete();
87 $deleted = $this->storage->findTranslation(['language' => $langcode, 'lid' => $source->lid]);
88 $this->assertNull($deleted->translation);
89
90 // Create some translations and then delete string and all of its
91 // translations.
92 $lid = $source->lid;
93 $this->createAllTranslations($source);
94 $search = $this->storage->getTranslations(['lid' => $source->lid]);
95 $this->assertCount(3, $search);
96
97 $source->delete();
98 $string = $this->storage->findString(['lid' => $lid]);
99 $this->assertFalse($string);
100 $deleted = $search = $this->storage->getTranslations(['lid' => $lid]);
101 $this->assertFalse($deleted);
102
103 // Tests that locations of different types and arbitrary lengths can be
104 // added to a source string. Too long locations will be cut off.
105 $source_string = $this->buildSourceString();
106 $source_string->addLocation('javascript', $this->randomString(8));
107 $source_string->addLocation('configuration', $this->randomString(50));
108 $source_string->addLocation('code', $this->randomString(100));
109 $source_string->addLocation('path', $location = $this->randomString(300));
110 $source_string->save();
111
112 $rows = $this->container->get('database')->select('locales_location')
113 ->fields('locales_location')
114 ->condition('sid', $source_string->lid)
115 ->execute()
116 ->fetchAllAssoc('type');
117 $this->assertCount(4, $rows);
118 $this->assertEquals(substr($location, 0, 255), $rows['path']->name);
119 }
120
121 /**
122 * Test Search API loading multiple objects.
123 */
124 public function testStringSearchApi() {
125 $language_count = 3;
126 // Strings 1 and 2 will have some common prefix.
127 // Source 1 will have all translations, not customized.
128 // Source 2 will have all translations, customized.
129 // Source 3 will have no translations.
130 $prefix = $this->randomMachineName(100);
131 $source1 = $this->buildSourceString(['source' => $prefix . $this->randomMachineName(100)])->save();
132 $source2 = $this->buildSourceString(['source' => $prefix . $this->randomMachineName(100)])->save();
133 $source3 = $this->buildSourceString()->save();
134
135 // Load all source strings.
136 $strings = $this->storage->getStrings([]);
137 $this->assertCount(3, $strings);
138 // Load all source strings matching a given string.
139 $filter_options['filters'] = ['source' => $prefix];
140 $strings = $this->storage->getStrings([], $filter_options);
141 $this->assertCount(2, $strings);
142
143 // Not customized translations.
144 $translate1 = $this->createAllTranslations($source1);
145 // Customized translations.
146 $this->createAllTranslations($source2, ['customized' => LOCALE_CUSTOMIZED]);
147 // Try quick search function with different field combinations.
148 $langcode = 'es';
149 $found = $this->storage->findTranslation(['language' => $langcode, 'source' => $source1->source, 'context' => $source1->context]);
150 $this->assertTrue($found && isset($found->language) && isset($found->translation) && !$found->isNew(), 'Translation not found searching by source and context.');
151 $this->assertEquals($translate1[$langcode]->translation, $found->translation);
152 // Now try a translation not found.
153 $found = $this->storage->findTranslation(['language' => $langcode, 'source' => $source3->source, 'context' => $source3->context]);
154 $this->assertTrue($found && $found->lid == $source3->lid && !isset($found->translation) && $found->isNew());
155
156 // Load all translations. For next queries we'll be loading only translated
157 // strings.
158 $translations = $this->storage->getTranslations(['translated' => TRUE]);
159 $this->assertCount(2 * $language_count, $translations);
160
161 // Load all customized translations.
162 $translations = $this->storage->getTranslations(['customized' => LOCALE_CUSTOMIZED, 'translated' => TRUE]);
163 $this->assertCount($language_count, $translations);
164
165 // Load all Spanish customized translations.
166 $translations = $this->storage->getTranslations(['language' => 'es', 'customized' => LOCALE_CUSTOMIZED, 'translated' => TRUE]);
167 $this->assertCount(1, $translations);
168
169 // Load all source strings without translation (1).
170 $translations = $this->storage->getStrings(['translated' => FALSE]);
171 $this->assertCount(1, $translations);
172
173 // Load Spanish translations using string filter.
174 $filter_options['filters'] = ['source' => $prefix];
175 $translations = $this->storage->getTranslations(['language' => 'es'], $filter_options);
176 $this->assertCount(2, $translations);
177 }
178
179 /**
180 * Creates random source string object.
181 *
182 * @param array $values
183 * The values array.
184 *
185 * @return \Drupal\locale\StringInterface
186 * A locale string.
187 */
188 protected function buildSourceString(array $values = []) {
189 return $this->storage->createString($values += [
190 'source' => $this->randomMachineName(100),
191 'context' => $this->randomMachineName(20),
192 ]);
193 }
194
195 /**
196 * Creates translations for source string and all languages.
197 *
198 * @param \Drupal\locale\StringInterface $source
199 * The source string.
200 * @param array $values
201 * The values array.
202 *
203 * @return array
204 * Translation list.
205 */
206 protected function createAllTranslations(StringInterface $source, array $values = []) {
207 $list = [];
208 /* @var $language_manager \Drupal\Core\Language\LanguageManagerInterface */
209 $language_manager = $this->container->get('language_manager');
210 foreach ($language_manager->getLanguages() as $language) {
211 $list[$language->getId()] = $this->createTranslation($source, $language->getId(), $values);
212 }
213 return $list;
214 }
215
216 /**
217 * Creates single translation for source string.
218 *
219 * @param \Drupal\locale\StringInterface $source
220 * The source string.
221 * @param string $langcode
222 * The language code.
223 * @param array $values
224 * The values array.
225 *
226 * @return \Drupal\locale\StringInterface
227 * The translated string object.
228 */
229 protected function createTranslation(StringInterface $source, $langcode, array $values = []) {
230 return $this->storage->createTranslation($values + [
231 'lid' => $source->lid,
232 'language' => $langcode,
233 'translation' => $this->randomMachineName(100),
234 ])->save();
235 }
236
237 }