Commit | Line | Data |
---|---|---|
04f32c6f TO |
1 | <?php |
2 | ||
3 | namespace E2E\Core; | |
4 | ||
5 | /** | |
6 | * Class LocalizedDataTest | |
7 | * @package E2E\Core | |
8 | * @group e2e | |
9 | */ | |
10 | class LocalizedDataTest extends \CiviEndToEndTestCase { | |
11 | ||
12 | /** | |
13 | * Smoke test to check that "civicrm_data*.mysql" files contain | |
14 | * translated strings. | |
b750d1c9 TO |
15 | * |
16 | * Note: As currently written, this test relies on the output of setup.sh/GenCode. | |
17 | * Consequently, if you're running locally while iterating on the code, you may find | |
18 | * the following command helps with your dev-test loop: | |
19 | * | |
20 | * $ env CIVICRM_LOCALES=en_US,fr_FR,de_DE ./bin/setup.sh -g \ | |
21 | * && phpunit6 tests/phpunit/E2E/Core/LocalizedDataTest.php | |
04f32c6f TO |
22 | */ |
23 | public function testLocalizedData() { | |
3b1d224b TO |
24 | $getSql = $this->getSqlFunc(); |
25 | ||
04f32c6f TO |
26 | $sqls = [ |
27 | 'de_DE' => $getSql('de_DE'), | |
28 | 'fr_FR' => $getSql('fr_FR'), | |
29 | ]; | |
30 | $pats = [ | |
31 | 'de_DE' => '/new_organization.*Neue Organisation/i', | |
32 | 'fr_FR' => '/new_organization.*Nouvelle organisation/i', | |
33 | ]; | |
34 | ||
35 | $match = function($sqlLocale, $patLocale) use ($pats, $sqls) { | |
36 | return (bool) preg_match($pats[$patLocale], $sqls[$sqlLocale]); | |
37 | }; | |
38 | ||
39 | $this->assertTrue($match('de_DE', 'de_DE'), 'The German SQL should match the German pattern.'); | |
40 | $this->assertTrue($match('fr_FR', 'fr_FR'), 'The French SQL should match the French pattern.'); | |
41 | $this->assertFalse($match('de_DE', 'fr_FR'), 'The German SQL should not match the French pattern.'); | |
42 | $this->assertFalse($match('fr_FR', 'de_DE'), 'The French SQL should not match the German pattern.'); | |
43 | } | |
44 | ||
3b1d224b TO |
45 | /** |
46 | * @return callable | |
47 | * The SQL loader -- function(string $locale): string | |
48 | */ | |
49 | private function getSqlFunc() { | |
50 | // Some deployment styles use stored files, and some generate SQL programmatically. | |
51 | // This heuristic discerns the style by UF name, although a better heuristic might be to check | |
52 | // for composer at CMS root. This works in a pinch. | |
53 | $uf = CIVICRM_UF; | |
54 | $installerTypes = [ | |
55 | 'Drupal' => [$this, '_getSqlFile'], | |
56 | 'Drupal8' => [$this, '_getSqlLive'], | |
57 | 'WordPress' => [$this, '_getSqlFile'], | |
58 | 'Backdrop' => [$this, '_getSqlFile'], | |
59 | 'Joomla' => [$this, '_getSqlFile'], | |
60 | ]; | |
61 | if (isset($installerTypes[$uf])) { | |
62 | return $installerTypes[$uf]; | |
63 | } | |
64 | else { | |
65 | throw new \RuntimeException("Failed to determine installation type for $uf"); | |
66 | } | |
67 | } | |
68 | ||
69 | private function _getSqlFile($locale) { | |
70 | $path = \Civi::paths()->getPath("[civicrm.root]/sql/civicrm_data.{$locale}.mysql"); | |
71 | $this->assertFileExists($path); | |
72 | return file_get_contents($path); | |
73 | } | |
74 | ||
75 | private function _getSqlLive($locale) { | |
76 | $schema = new \CRM_Core_CodeGen_Schema(\Civi\Test::codeGen()); | |
77 | $files = $schema->generateLocaleDataSql($locale); | |
78 | foreach ($files as $file => $content) { | |
79 | if (preg_match(';^civicrm_data\.;', $file)) { | |
80 | return $content; | |
81 | } | |
82 | } | |
83 | throw new \Exception("Faield to generate $locale"); | |
84 | } | |
85 | ||
04f32c6f | 86 | } |