Mercurial > hg > isophonics-drupal-site
comparison core/modules/locale/tests/src/Functional/LocaleStringTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\locale\Functional; | |
4 | |
5 use Drupal\language\Entity\ConfigurableLanguage; | |
6 use Drupal\Tests\BrowserTestBase; | |
7 | |
8 /** | |
9 * Tests the locale string storage, string objects and data API. | |
10 * | |
11 * @group locale | |
12 */ | |
13 class LocaleStringTest extends BrowserTestBase { | |
14 | |
15 /** | |
16 * Modules to enable. | |
17 * | |
18 * @var array | |
19 */ | |
20 public static $modules = ['locale']; | |
21 | |
22 /** | |
23 * The locale storage. | |
24 * | |
25 * @var \Drupal\locale\StringStorageInterface | |
26 */ | |
27 protected $storage; | |
28 | |
29 /** | |
30 * {@inheritdoc} | |
31 */ | |
32 protected function setUp() { | |
33 parent::setUp(); | |
34 // Add a default locale storage for all these tests. | |
35 $this->storage = $this->container->get('locale.storage'); | |
36 // Create two languages: Spanish and German. | |
37 foreach (['es', 'de'] as $langcode) { | |
38 ConfigurableLanguage::createFromLangcode($langcode)->save(); | |
39 } | |
40 } | |
41 | |
42 /** | |
43 * Test CRUD API. | |
44 */ | |
45 public function testStringCRUDAPI() { | |
46 // Create source string. | |
47 $source = $this->buildSourceString(); | |
48 $source->save(); | |
49 $this->assertTrue($source->lid, format_string('Successfully created string %string', ['%string' => $source->source])); | |
50 | |
51 // Load strings by lid and source. | |
52 $string1 = $this->storage->findString(['lid' => $source->lid]); | |
53 $this->assertEqual($source, $string1, 'Successfully retrieved string by identifier.'); | |
54 $string2 = $this->storage->findString(['source' => $source->source, 'context' => $source->context]); | |
55 $this->assertEqual($source, $string2, 'Successfully retrieved string by source and context.'); | |
56 $string3 = $this->storage->findString(['source' => $source->source, 'context' => '']); | |
57 $this->assertFalse($string3, 'Cannot retrieve string with wrong context.'); | |
58 | |
59 // Check version handling and updating. | |
60 $this->assertEqual($source->version, 'none', 'String originally created without version.'); | |
61 $string = $this->storage->findTranslation(['lid' => $source->lid]); | |
62 $this->assertEqual($string->version, \Drupal::VERSION, 'Checked and updated string version to Drupal version.'); | |
63 | |
64 // Create translation and find it by lid and source. | |
65 $langcode = 'es'; | |
66 $translation = $this->createTranslation($source, $langcode); | |
67 $this->assertEqual($translation->customized, LOCALE_NOT_CUSTOMIZED, 'Translation created as not customized by default.'); | |
68 $string1 = $this->storage->findTranslation(['language' => $langcode, 'lid' => $source->lid]); | |
69 $this->assertEqual($string1->translation, $translation->translation, 'Successfully loaded translation by string identifier.'); | |
70 $string2 = $this->storage->findTranslation(['language' => $langcode, 'source' => $source->source, 'context' => $source->context]); | |
71 $this->assertEqual($string2->translation, $translation->translation, 'Successfully loaded translation by source and context.'); | |
72 $translation | |
73 ->setCustomized() | |
74 ->save(); | |
75 $translation = $this->storage->findTranslation(['language' => $langcode, 'lid' => $source->lid]); | |
76 $this->assertEqual($translation->customized, LOCALE_CUSTOMIZED, 'Translation successfully marked as customized.'); | |
77 | |
78 // Delete translation. | |
79 $translation->delete(); | |
80 $deleted = $this->storage->findTranslation(['language' => $langcode, 'lid' => $source->lid]); | |
81 $this->assertFalse(isset($deleted->translation), 'Successfully deleted translation string.'); | |
82 | |
83 // Create some translations and then delete string and all of its | |
84 // translations. | |
85 $lid = $source->lid; | |
86 $this->createAllTranslations($source); | |
87 $search = $this->storage->getTranslations(['lid' => $source->lid]); | |
88 $this->assertEqual(count($search), 3, 'Created and retrieved all translations for our source string.'); | |
89 | |
90 $source->delete(); | |
91 $string = $this->storage->findString(['lid' => $lid]); | |
92 $this->assertFalse($string, 'Successfully deleted source string.'); | |
93 $deleted = $search = $this->storage->getTranslations(['lid' => $lid]); | |
94 $this->assertFalse($deleted, 'Successfully deleted all translation strings.'); | |
95 | |
96 // Tests that locations of different types and arbitrary lengths can be | |
97 // added to a source string. Too long locations will be cut off. | |
98 $source_string = $this->buildSourceString(); | |
99 $source_string->addLocation('javascript', $this->randomString(8)); | |
100 $source_string->addLocation('configuration', $this->randomString(50)); | |
101 $source_string->addLocation('code', $this->randomString(100)); | |
102 $source_string->addLocation('path', $location = $this->randomString(300)); | |
103 $source_string->save(); | |
104 | |
105 $rows = db_query('SELECT * FROM {locales_location} WHERE sid = :sid', [':sid' => $source_string->lid])->fetchAllAssoc('type'); | |
106 $this->assertEqual(count($rows), 4, '4 source locations have been persisted.'); | |
107 $this->assertEqual($rows['path']->name, substr($location, 0, 255), 'Too long location has been limited to 255 characters.'); | |
108 } | |
109 | |
110 /** | |
111 * Test Search API loading multiple objects. | |
112 */ | |
113 public function testStringSearchAPI() { | |
114 $language_count = 3; | |
115 // Strings 1 and 2 will have some common prefix. | |
116 // Source 1 will have all translations, not customized. | |
117 // Source 2 will have all translations, customized. | |
118 // Source 3 will have no translations. | |
119 $prefix = $this->randomMachineName(100); | |
120 $source1 = $this->buildSourceString(['source' => $prefix . $this->randomMachineName(100)])->save(); | |
121 $source2 = $this->buildSourceString(['source' => $prefix . $this->randomMachineName(100)])->save(); | |
122 $source3 = $this->buildSourceString()->save(); | |
123 // Load all source strings. | |
124 $strings = $this->storage->getStrings([]); | |
125 $this->assertEqual(count($strings), 3, 'Found 3 source strings in the database.'); | |
126 // Load all source strings matching a given string. | |
127 $filter_options['filters'] = ['source' => $prefix]; | |
128 $strings = $this->storage->getStrings([], $filter_options); | |
129 $this->assertEqual(count($strings), 2, 'Found 2 strings using some string filter.'); | |
130 | |
131 // Not customized translations. | |
132 $translate1 = $this->createAllTranslations($source1); | |
133 // Customized translations. | |
134 $this->createAllTranslations($source2, ['customized' => LOCALE_CUSTOMIZED]); | |
135 // Try quick search function with different field combinations. | |
136 $langcode = 'es'; | |
137 $found = $this->storage->findTranslation(['language' => $langcode, 'source' => $source1->source, 'context' => $source1->context]); | |
138 $this->assertTrue($found && isset($found->language) && isset($found->translation) && !$found->isNew(), 'Translation found searching by source and context.'); | |
139 $this->assertEqual($found->translation, $translate1[$langcode]->translation, 'Found the right translation.'); | |
140 // Now try a translation not found. | |
141 $found = $this->storage->findTranslation(['language' => $langcode, 'source' => $source3->source, 'context' => $source3->context]); | |
142 $this->assertTrue($found && $found->lid == $source3->lid && !isset($found->translation) && $found->isNew(), 'Translation not found but source string found.'); | |
143 | |
144 // Load all translations. For next queries we'll be loading only translated | |
145 // strings. | |
146 $translations = $this->storage->getTranslations(['translated' => TRUE]); | |
147 $this->assertEqual(count($translations), 2 * $language_count, 'Created and retrieved all translations for source strings.'); | |
148 | |
149 // Load all customized translations. | |
150 $translations = $this->storage->getTranslations(['customized' => LOCALE_CUSTOMIZED, 'translated' => TRUE]); | |
151 $this->assertEqual(count($translations), $language_count, 'Retrieved all customized translations for source strings.'); | |
152 | |
153 // Load all Spanish customized translations. | |
154 $translations = $this->storage->getTranslations(['language' => 'es', 'customized' => LOCALE_CUSTOMIZED, 'translated' => TRUE]); | |
155 $this->assertEqual(count($translations), 1, 'Found only Spanish and customized translations.'); | |
156 | |
157 // Load all source strings without translation (1). | |
158 $translations = $this->storage->getStrings(['translated' => FALSE]); | |
159 $this->assertEqual(count($translations), 1, 'Found 1 source string without translations.'); | |
160 | |
161 // Load Spanish translations using string filter. | |
162 $filter_options['filters'] = ['source' => $prefix]; | |
163 $translations = $this->storage->getTranslations(['language' => 'es'], $filter_options); | |
164 $this->assertEqual(count($translations), 2, 'Found 2 translations using some string filter.'); | |
165 | |
166 } | |
167 | |
168 /** | |
169 * Creates random source string object. | |
170 * | |
171 * @return \Drupal\locale\StringInterface | |
172 * A locale string. | |
173 */ | |
174 public function buildSourceString($values = []) { | |
175 return $this->storage->createString($values += [ | |
176 'source' => $this->randomMachineName(100), | |
177 'context' => $this->randomMachineName(20), | |
178 ]); | |
179 } | |
180 | |
181 /** | |
182 * Creates translations for source string and all languages. | |
183 */ | |
184 public function createAllTranslations($source, $values = []) { | |
185 $list = []; | |
186 /* @var $language_manager \Drupal\Core\Language\LanguageManagerInterface */ | |
187 $language_manager = $this->container->get('language_manager'); | |
188 foreach ($language_manager->getLanguages() as $language) { | |
189 $list[$language->getId()] = $this->createTranslation($source, $language->getId(), $values); | |
190 } | |
191 return $list; | |
192 } | |
193 | |
194 /** | |
195 * Creates single translation for source string. | |
196 */ | |
197 public function createTranslation($source, $langcode, $values = []) { | |
198 return $this->storage->createTranslation($values + [ | |
199 'lid' => $source->lid, | |
200 'language' => $langcode, | |
201 'translation' => $this->randomMachineName(100), | |
202 ])->save(); | |
203 } | |
204 | |
205 } |