fe99ce2f37c424c3680c17dd542616cf4d72ce98
[yaffs-website] / web / core / modules / locale / src / Tests / LocaleJavascriptTranslationTest.php
1 <?php
2
3 namespace Drupal\locale\Tests;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\simpletest\WebTestBase;
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 WebTestBase {
15
16   /**
17    * Modules to enable.
18    *
19    * @var array
20    */
21   public static $modules = ['locale', 'locale_test'];
22
23   public function testFileParsing() {
24     $filename = __DIR__ . '/../../tests/locale_test.js';
25
26     // Parse the file to look for source strings.
27     _locale_parse_js_file($filename);
28
29     // Get all of the source strings that were found.
30     $strings = $this->container
31       ->get('locale.storage')
32       ->getStrings([
33         'type' => 'javascript',
34         'name' => $filename,
35       ]);
36
37     $source_strings = [];
38     foreach ($strings as $string) {
39       $source_strings[$string->source] = $string->context;
40     }
41
42     $etx = LOCALE_PLURAL_DELIMITER;
43     // List of all strings that should be in the file.
44     $test_strings = [
45       'Standard Call t' => '',
46       'Whitespace Call t' => '',
47
48       'Single Quote t' => '',
49       "Single Quote \\'Escaped\\' t" => '',
50       'Single Quote Concat strings t' => '',
51
52       'Double Quote t' => '',
53       "Double Quote \\\"Escaped\\\" t" => '',
54       'Double Quote Concat strings t' => '',
55
56       'Context !key Args t' => 'Context string',
57
58       'Context Unquoted t' => 'Context string unquoted',
59       'Context Single Quoted t' => 'Context string single quoted',
60       'Context Double Quoted t' => 'Context string double quoted',
61
62       "Standard Call plural{$etx}Standard Call @count plural" => '',
63       "Whitespace Call plural{$etx}Whitespace Call @count plural" => '',
64
65       "Single Quote plural{$etx}Single Quote @count plural" => '',
66       "Single Quote \\'Escaped\\' plural{$etx}Single Quote \\'Escaped\\' @count plural" => '',
67
68       "Double Quote plural{$etx}Double Quote @count plural" => '',
69       "Double Quote \\\"Escaped\\\" plural{$etx}Double Quote \\\"Escaped\\\" @count plural" => '',
70
71       "Context !key Args plural{$etx}Context !key Args @count plural" => 'Context string',
72
73       "Context Unquoted plural{$etx}Context Unquoted @count plural" => 'Context string unquoted',
74       "Context Single Quoted plural{$etx}Context Single Quoted @count plural" => 'Context string single quoted',
75       "Context Double Quoted plural{$etx}Context Double Quoted @count plural" => 'Context string double quoted',
76     ];
77
78     // Assert that all strings were found properly.
79     foreach ($test_strings as $str => $context) {
80       $args = ['%source' => $str, '%context' => $context];
81
82       // Make sure that the string was found in the file.
83       $this->assertTrue(isset($source_strings[$str]), SafeMarkup::format('Found source string: %source', $args));
84
85       // Make sure that the proper context was matched.
86       $message = $context ? SafeMarkup::format('Context for %source is %context', $args) : SafeMarkup::format('Context for %source is blank', $args);
87       $this->assertTrue(isset($source_strings[$str]) && $source_strings[$str] === $context, $message);
88     }
89
90     $this->assertEqual(count($source_strings), count($test_strings), 'Found correct number of source strings.');
91   }
92
93   /**
94    * Assert translations JS is added before drupal.js, because it depends on it.
95    */
96   public function testLocaleTranslationJsDependencies() {
97     // User to add and remove language.
98     $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages', 'translate interface']);
99
100     // Add custom language.
101     $this->drupalLogin($admin_user);
102     // Code for the language.
103     $langcode = 'es';
104     // The English name for the language.
105     $name = $this->randomMachineName(16);
106     // The domain prefix.
107     $prefix = $langcode;
108     $edit = [
109       'predefined_langcode' => 'custom',
110       'langcode' => $langcode,
111       'label' => $name,
112       'direction' => LanguageInterface::DIRECTION_LTR,
113     ];
114     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
115
116     // Set path prefix.
117     $edit = ["prefix[$langcode]" => $prefix];
118     $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
119
120     // This forces locale.admin.js string sources to be imported, which contains
121     // the next translation.
122     $this->drupalGet($prefix . '/admin/config/regional/translate');
123
124     // Translate a string in locale.admin.js to our new language.
125     $strings = \Drupal::service('locale.storage')
126       ->getStrings([
127         'source' => 'Show description',
128         'type' => 'javascript',
129         'name' => 'core/modules/locale/locale.admin.js',
130       ]);
131     $string = $strings[0];
132
133     $this->drupalPostForm(NULL, ['string' => 'Show description'], t('Filter'));
134     $edit = ['strings[' . $string->lid . '][translations][0]' => $this->randomString(16)];
135     $this->drupalPostForm(NULL, $edit, t('Save translations'));
136
137     // Calculate the filename of the JS including the translations.
138     $js_translation_files = \Drupal::state()->get('locale.translation.javascript');
139     $js_filename = $prefix . '_' . $js_translation_files[$prefix] . '.js';
140
141     // Assert translations JS is included before drupal.js.
142     $this->assertTrue(strpos($this->content, $js_filename) < strpos($this->content, 'core/misc/drupal.js'), 'Translations are included before Drupal.t.');
143   }
144
145 }