1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Test\Integrity\App\Language;
use Magento\Framework\App\Utility\Files;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Setup\Module\I18n\Dictionary\Options\ResolverFactory;
use Magento\Setup\Module\I18n\Locale;
use Magento\Setup\Module\I18n\Pack\Writer\File\Csv;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class TranslationFilesTest extends TranslationFiles
{
/**
* Context
*
* @var \Magento\Setup\Module\I18n\Context
*/
protected $context;
/**
* Test default locale
*
* Check that all translation phrases in code are present in the locale files
*
* @param string $file
* @param array $phrases
*
* @dataProvider defaultLocaleDataProvider
*/
public function testDefaultLocale($file, $phrases)
{
$this->markTestSkipped('MAGETWO-26083');
$failures = $this->comparePhrase($phrases, $this->csvParser->getDataPairs($file));
$this->assertEmpty(
$failures,
$this->printMessage([$file => $failures])
);
}
/**
* @return array
* @throws \RuntimeException
*/
public function defaultLocaleDataProvider()
{
$parser = $this->prepareParser();
$optionResolverFactory = new ResolverFactory();
$optionResolver = $optionResolverFactory->create(BP, true);
$parser->parse($optionResolver->getOptions());
$defaultLocale = [];
foreach ($parser->getPhrases() as $key => $phrase) {
if (!$phrase->getContextType() || !$phrase->getContextValue()) {
throw new \RuntimeException(sprintf('Missed context in row #%d.', $key + 1));
}
foreach ($phrase->getContextValue() as $context) {
$phraseText = $this->eliminateSpecialChars($phrase->getPhrase());
$phraseTranslation = $this->eliminateSpecialChars($phrase->getTranslation());
$file = $this->buildFilePath($phrase, $context);
$defaultLocale[$file]['file'] = $file;
$defaultLocale[$file]['phrases'][$phraseText] = $phraseTranslation;
}
}
return $defaultLocale;
}
/**
* @param \Magento\Setup\Module\I18n\Dictionary\Phrase $phrase
* @param array $context
* @return string
*/
protected function buildFilePath($phrase, $context)
{
$path = $this->getContext()->buildPathToLocaleDirectoryByContext($phrase->getContextType(), $context);
return $path . Locale::DEFAULT_SYSTEM_LOCALE . '.' . Csv::FILE_EXTENSION;
}
/**
* @return \Magento\Setup\Module\I18n\Context
*/
protected function getContext()
{
if ($this->context === null) {
$this->context = new \Magento\Setup\Module\I18n\Context(new ComponentRegistrar());
}
return $this->context;
}
/**
* @return \Magento\Setup\Module\I18n\Parser\Contextual
*/
protected function prepareParser()
{
$filesCollector = new \Magento\Setup\Module\I18n\FilesCollector();
$phraseCollector = new \Magento\Setup\Module\I18n\Parser\Adapter\Php\Tokenizer\PhraseCollector(
new \Magento\Setup\Module\I18n\Parser\Adapter\Php\Tokenizer()
);
$adapters = [
'php' => new \Magento\Setup\Module\I18n\Parser\Adapter\Php($phraseCollector),
'js' => new \Magento\Setup\Module\I18n\Parser\Adapter\Js(),
'xml' => new \Magento\Setup\Module\I18n\Parser\Adapter\Xml(),
'html' => new \Magento\Setup\Module\I18n\Parser\Adapter\Html(),
];
$parserContextual = new \Magento\Setup\Module\I18n\Parser\Contextual(
$filesCollector,
new \Magento\Setup\Module\I18n\Factory(),
new \Magento\Setup\Module\I18n\Context(new ComponentRegistrar())
);
foreach ($adapters as $type => $adapter) {
$parserContextual->addAdapter($type, $adapter);
}
return $parserContextual;
}
/**
* @param string $text
* @return string
*/
protected function eliminateSpecialChars($text)
{
return preg_replace(['/\\\\\'/', '/\\\\\\\\/'], ['\'', '\\'], $text);
}
/**
* Test placeholders in translations.
* Compares count numeric placeholders in keys and translates.
*
* @param string $placePath
* @dataProvider getLocalePlacePath
*/
public function testPhrasePlaceHolders($placePath)
{
$this->markTestSkipped('MAGETWO-26083');
$files = $this->getCsvFiles($placePath);
$failures = [];
foreach ($files as $locale => $file) {
$fileData = $this->csvParser->getDataPairs($file);
foreach ($fileData as $key => $translate) {
preg_match_all('/%(\d+)/', $key, $keyMatches);
preg_match_all('/%(\d+)/', $translate, $translateMatches);
if (count(array_unique($keyMatches[1])) != count(array_unique($translateMatches[1]))) {
$failures[$locale][$key][] = $translate;
}
}
}
$this->assertEmpty(
$failures,
$this->printMessage(
$failures,
'Found discrepancy between keys and translations in count of numeric placeholders'
)
);
}
}