-
Notifications
You must be signed in to change notification settings - Fork 993
/
LSMessageSourceTest.php
110 lines (100 loc) · 3.76 KB
/
LSMessageSourceTest.php
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
<?php
namespace ls\tests;
use App;
/**
* Test core : LSMessageSource
*/
class LSMessageSourceTest extends TestBaseClass
{
public static function setupBeforeClass(): void
{
parent::setUpBeforeClass();
self::$testHelper->resetCache();
}
public function testUpdatedString()
{
$sourcemessage = "sourcemessage" . App()->securityManager->generateRandomString(42);
$translatedmessage = "translatedmessage" . App()->securityManager->generateRandomString(42);
$translatedYes = "Yes" . App()->securityManager->generateRandomString(42);
/* Prefill fr cache and check po only*/
$yesPoFr = gT('Yes', 'unescaped', 'fr');
$this->assertNotEquals('Yes', $yesPoFr, 'Translation from po file seems broken');
/* Add the string to bet tested in translation DB */
$randomId = $this->addTranslation($sourcemessage, $translatedmessage);
$this->assertNotEmpty($randomId, 'Unable to create source_message row in DB');
$yesId = $this->addTranslation('Yes', $translatedYes);
$this->assertNotEmpty($randomId, 'Unable to create Yes source_message row in DB');
/* Check with not existing string */
if (!empty($randomId)) {
/* get current */
$translatedDeGt = gT($sourcemessage, 'unescaped', 'de');
$translatedFrGt = gT($sourcemessage, 'unescaped', 'fr');
/* Delete added translation */
$this->deleteTranslation($randomId);
/* result */
$this->assertEquals($translatedmessage, $translatedDeGt, 'Message translation by DB is broken');
$this->assertEquals($sourcemessage, $translatedFrGt, 'Yii Message caching seems broken');
}
/* Check with Yes : replace string */
if (!empty($yesId)) {
$translatedDeYesGt = gT('Yes', 'unescaped', 'de');
$translatedFrYesGt = gT('Yes', 'unescaped', 'fr');
/* Delete added translation */
$this->deleteTranslation($yesId);
/* result */
$this->assertEquals($translatedYes, $translatedDeYesGt, 'Message translation by DB is broken');
$this->assertEquals($yesPoFr, $translatedFrYesGt, 'Yii Message caching seems broken');
}
}
/**
* Create de and fr translation in DB and return id of source message
* @param $source
* @param $translated
* @return integer|null
*/
private function addTranslation($source, $translated)
{
App()->db->createCommand()->insert('{{source_message}}', array(
'category' => '',
'message' => $source,
));
$result = App()->db->createCommand()
->select("id")
->from("{{source_message}}")
->where(
'message=:message',
array(':message' => $source)
)->queryRow();
if (empty($result['id'])) {
return null;
}
App()->db->createCommand()->insert('{{message}}', array(
'id' => intval($result['id']),
'language' => 'de',
'translation' => $translated,
));
App()->db->createCommand()->insert('{{message}}', array(
'id' => intval($result['id']),
'language' => 'fr',
'translation' => $translated,
));
return $result['id'];
}
/**
* Delete existing trabnsklation by source id
* @param $integer
*/
private function deleteTranslation($sourceId)
{
App()->db->createCommand()->delete(
'{{source_message}}',
'id = :id',
array(":id" => $sourceId)
);
App()->db->createCommand()->delete(
'{{message}}',
'id = :id',
array(":id" => $sourceId)
);
}
}