Mercurial > hg > isophonics-drupal-site
comparison core/modules/locale/tests/src/Functional/LocaleJavascriptTranslationTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\locale\Functional; | |
4 | |
5 use Drupal\Core\Language\LanguageInterface; | |
6 use Drupal\Tests\BrowserTestBase; | |
7 use Drupal\Component\Utility\SafeMarkup; | |
8 | |
9 /** | |
10 * Tests parsing js files for translatable strings. | |
11 * | |
12 * @group locale | |
13 */ | |
14 class LocaleJavascriptTranslationTest extends BrowserTestBase { | |
15 | |
16 /** | |
17 * Modules to enable. | |
18 * | |
19 * @var array | |
20 */ | |
21 public static $modules = ['locale', 'locale_test']; | |
22 | |
23 public function testFileParsing() { | |
24 | |
25 // This test is for ensuring that the regular expression in | |
26 // _locale_parse_js_file() finds translatable source strings in all valid | |
27 // JavaScript syntax regardless of the coding style used, especially with | |
28 // respect to optional whitespace, line breaks, etc. | |
29 // - We test locale_test.es6.js, because that is the one that contains a | |
30 // variety of whitespace styles. | |
31 // - We also test the transpiled locale_test.js as an extra double-check | |
32 // that JavaScript transpilation doesn't change what | |
33 // _locale_parse_js_file() finds. | |
34 $files[] = __DIR__ . '/../../locale_test.es6.js'; | |
35 $files[] = __DIR__ . '/../../locale_test.js'; | |
36 | |
37 foreach ($files as $filename) { | |
38 // Parse the file to look for source strings. | |
39 _locale_parse_js_file($filename); | |
40 | |
41 // Get all of the source strings that were found. | |
42 $strings = $this->container | |
43 ->get('locale.storage') | |
44 ->getStrings([ | |
45 'type' => 'javascript', | |
46 'name' => $filename, | |
47 ]); | |
48 | |
49 $source_strings = []; | |
50 foreach ($strings as $string) { | |
51 $source_strings[$string->source] = $string->context; | |
52 } | |
53 | |
54 $etx = LOCALE_PLURAL_DELIMITER; | |
55 // List of all strings that should be in the file. | |
56 $test_strings = [ | |
57 'Standard Call t' => '', | |
58 'Whitespace Call t' => '', | |
59 | |
60 'Single Quote t' => '', | |
61 "Single Quote \\'Escaped\\' t" => '', | |
62 'Single Quote Concat strings t' => '', | |
63 | |
64 'Double Quote t' => '', | |
65 "Double Quote \\\"Escaped\\\" t" => '', | |
66 'Double Quote Concat strings t' => '', | |
67 | |
68 'Context !key Args t' => 'Context string', | |
69 | |
70 'Context Unquoted t' => 'Context string unquoted', | |
71 'Context Single Quoted t' => 'Context string single quoted', | |
72 'Context Double Quoted t' => 'Context string double quoted', | |
73 | |
74 "Standard Call plural{$etx}Standard Call @count plural" => '', | |
75 "Whitespace Call plural{$etx}Whitespace Call @count plural" => '', | |
76 | |
77 "Single Quote plural{$etx}Single Quote @count plural" => '', | |
78 "Single Quote \\'Escaped\\' plural{$etx}Single Quote \\'Escaped\\' @count plural" => '', | |
79 | |
80 "Double Quote plural{$etx}Double Quote @count plural" => '', | |
81 "Double Quote \\\"Escaped\\\" plural{$etx}Double Quote \\\"Escaped\\\" @count plural" => '', | |
82 | |
83 "Context !key Args plural{$etx}Context !key Args @count plural" => 'Context string', | |
84 | |
85 "Context Unquoted plural{$etx}Context Unquoted @count plural" => 'Context string unquoted', | |
86 "Context Single Quoted plural{$etx}Context Single Quoted @count plural" => 'Context string single quoted', | |
87 "Context Double Quoted plural{$etx}Context Double Quoted @count plural" => 'Context string double quoted', | |
88 ]; | |
89 | |
90 // Assert that all strings were found properly. | |
91 foreach ($test_strings as $str => $context) { | |
92 $args = ['%source' => $str, '%context' => $context]; | |
93 | |
94 // Make sure that the string was found in the file. | |
95 $this->assertTrue(isset($source_strings[$str]), SafeMarkup::format('Found source string: %source', $args)); | |
96 | |
97 // Make sure that the proper context was matched. | |
98 $message = $context ? SafeMarkup::format('Context for %source is %context', $args) : SafeMarkup::format('Context for %source is blank', $args); | |
99 $this->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, $message); | |
100 } | |
101 | |
102 $this->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.'); | |
103 } | |
104 } | |
105 | |
106 /** | |
107 * Assert translations JS is added before drupal.js, because it depends on it. | |
108 */ | |
109 public function testLocaleTranslationJsDependencies() { | |
110 // User to add and remove language. | |
111 $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface']); | |
112 | |
113 // Add custom language. | |
114 $this->drupalLogin($admin_user); | |
115 // Code for the language. | |
116 $langcode = 'es'; | |
117 // The English name for the language. | |
118 $name = $this->randomMachineName(16); | |
119 // The domain prefix. | |
120 $prefix = $langcode; | |
121 $edit = [ | |
122 'predefined_langcode' => 'custom', | |
123 'langcode' => $langcode, | |
124 'label' => $name, | |
125 'direction' => LanguageInterface::DIRECTION_LTR, | |
126 ]; | |
127 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); | |
128 | |
129 // Set path prefix. | |
130 $edit = ["prefix[$langcode]" => $prefix]; | |
131 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); | |
132 | |
133 // This forces locale.admin.js string sources to be imported, which contains | |
134 // the next translation. | |
135 $this->drupalGet($prefix . '/admin/config/regional/translate'); | |
136 | |
137 // Translate a string in locale.admin.js to our new language. | |
138 $strings = \Drupal::service('locale.storage') | |
139 ->getStrings([ | |
140 'source' => 'Show description', | |
141 'type' => 'javascript', | |
142 'name' => 'core/modules/locale/locale.admin.js', | |
143 ]); | |
144 $string = $strings[0]; | |
145 | |
146 $this->drupalPostForm(NULL, ['string' => 'Show description'], t('Filter')); | |
147 $edit = ['strings[' . $string->lid . '][translations][0]' => $this->randomString(16)]; | |
148 $this->drupalPostForm(NULL, $edit, t('Save translations')); | |
149 | |
150 // Calculate the filename of the JS including the translations. | |
151 $js_translation_files = \Drupal::state()->get('locale.translation.javascript'); | |
152 $js_filename = $prefix . '_' . $js_translation_files[$prefix] . '.js'; | |
153 | |
154 $content = $this->getSession()->getPage()->getContent(); | |
155 // Assert translations JS is included before drupal.js. | |
156 $this->assertTrue(strpos($content, $js_filename) < strpos($content, 'core/misc/drupal.js'), 'Translations are included before Drupal.t.'); | |
157 } | |
158 | |
159 } |