|
1
|
|
|
<?php |
|
2
|
|
|
namespace tests; |
|
3
|
|
|
|
|
4
|
|
|
use tests\models\PostRequired; |
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\InvalidConfigException; |
|
7
|
|
|
use tests\models\PostAbridge as Post; |
|
8
|
|
|
use omgdef\multilingual\MultilingualBehavior; |
|
9
|
|
|
|
|
10
|
|
|
class MultilingualBehaviorAbridgeTest extends DatabaseTestCase |
|
11
|
|
|
{ |
|
12
|
|
|
public function testConfiguration() |
|
13
|
|
|
{ |
|
14
|
|
|
$post = new Post(); |
|
15
|
|
|
$post->detachBehavior('ml'); |
|
16
|
|
|
|
|
17
|
|
|
try { |
|
18
|
|
|
$post->attachBehavior('ml', [ |
|
19
|
|
|
'class' => MultilingualBehavior::className(), |
|
20
|
|
|
'languages' => [], |
|
21
|
|
|
]); |
|
22
|
|
|
$this->fail("Expected exception not thrown"); |
|
23
|
|
|
} catch (InvalidConfigException $e) { |
|
24
|
|
|
$this->assertEquals(101, $e->getCode()); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
try { |
|
28
|
|
|
$post->detachBehavior('ml'); |
|
29
|
|
|
$post->attachBehavior('ml', [ |
|
30
|
|
|
'class' => MultilingualBehavior::className(), |
|
31
|
|
|
'languages' => 'Some value', |
|
32
|
|
|
]); |
|
33
|
|
|
$this->fail("Expected exception not thrown"); |
|
34
|
|
|
} catch (InvalidConfigException $e) { |
|
35
|
|
|
$this->assertEquals(101, $e->getCode()); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
try { |
|
39
|
|
|
$post->detachBehavior('ml'); |
|
40
|
|
|
$post->attachBehavior('ml', [ |
|
41
|
|
|
'class' => MultilingualBehavior::className(), |
|
42
|
|
|
'languages' => [ |
|
43
|
|
|
'ru' => 'Russian', |
|
44
|
|
|
'en-US' => 'English', |
|
45
|
|
|
], |
|
46
|
|
|
'langForeignKey' => 'post_id', |
|
47
|
|
|
'tableName' => "{{%postLang}}", |
|
48
|
|
|
]); |
|
49
|
|
|
$this->fail("Expected exception not thrown"); |
|
50
|
|
|
} catch (InvalidConfigException $e) { |
|
51
|
|
|
$this->assertEquals(103, $e->getCode()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
|
|
$post->detachBehavior('ml'); |
|
56
|
|
|
$post->attachBehavior('ml', [ |
|
57
|
|
|
'class' => MultilingualBehavior::className(), |
|
58
|
|
|
'languages' => [ |
|
59
|
|
|
'ru' => 'Russian', |
|
60
|
|
|
'en-US' => 'English', |
|
61
|
|
|
], |
|
62
|
|
|
'attributes' => [ |
|
63
|
|
|
'title', 'body', |
|
64
|
|
|
] |
|
65
|
|
|
]); |
|
66
|
|
|
$this->fail("Expected exception not thrown"); |
|
67
|
|
|
} catch (InvalidConfigException $e) { |
|
68
|
|
|
$this->assertEquals(105, $e->getCode()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$post->detachBehavior('ml'); |
|
72
|
|
|
$post->attachBehavior('ml', [ |
|
73
|
|
|
'class' => MultilingualBehavior::className(), |
|
74
|
|
|
'languages' => [ |
|
75
|
|
|
'ru' => 'Russian', |
|
76
|
|
|
'en-US' => 'English', |
|
77
|
|
|
], |
|
78
|
|
|
'langForeignKey' => 'post_id', |
|
79
|
|
|
'attributes' => [ |
|
80
|
|
|
'title', 'body', |
|
81
|
|
|
] |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertNotNull($post->defaultLanguage); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
View Code Duplication |
public function testFindPosts() |
|
88
|
|
|
{ |
|
89
|
|
|
$data = []; |
|
90
|
|
|
$models = Post::find()->multilingual()->all(); |
|
91
|
|
|
foreach ($models as $model) { |
|
92
|
|
|
$this->assertEquals($model->title, $model->title_ru); |
|
93
|
|
|
$this->assertEquals($model->body, $model->body_ru); |
|
94
|
|
|
$this->assertNotNull($model->title_en); |
|
95
|
|
|
$this->assertNotNull($model->body_en); |
|
96
|
|
|
$data[] = $model->toArray([], ['translations']); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->assertEquals(require(__DIR__ . '/data/test-find-posts.php'), $data); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
View Code Duplication |
public function testCreatePost() |
|
103
|
|
|
{ |
|
104
|
|
|
$post = new Post([ |
|
105
|
|
|
'title' => 'New post title', |
|
106
|
|
|
'body' => 'New post body', |
|
107
|
|
|
]); |
|
108
|
|
|
|
|
109
|
|
|
$this->assertTrue($post->save()); |
|
110
|
|
|
|
|
111
|
|
|
$dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
|
112
|
|
|
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-create-post.xml'); |
|
113
|
|
|
|
|
114
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
public function testCreatePostSetTranslations() |
|
118
|
|
|
{ |
|
119
|
|
|
$post = new Post(); |
|
120
|
|
|
$data = [ |
|
121
|
|
|
'title' => 'New post title', |
|
122
|
|
|
'body' => 'New post body', |
|
123
|
|
|
'title_en' => 'New post title en', |
|
124
|
|
|
'body_en' => 'New post body en', |
|
125
|
|
|
'title_ru' => 'New post title ru', //this value should be overwritten by default language value |
|
126
|
|
|
'body_ru' => 'New post body ru', |
|
127
|
|
|
]; |
|
128
|
|
|
$formName = $post->formName(); |
|
129
|
|
|
if (!empty($formName)) { |
|
130
|
|
|
$data = [$formName => $data]; |
|
131
|
|
|
} |
|
132
|
|
|
$post->load($data); |
|
133
|
|
|
|
|
134
|
|
|
$this->assertTrue($post->save()); |
|
135
|
|
|
$dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
|
136
|
|
|
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-create-post-set-translations.xml'); |
|
137
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
View Code Duplication |
public function testUpdateNotPopulatedPost() |
|
141
|
|
|
{ |
|
142
|
|
|
$post = Post::findOne(2); |
|
143
|
|
|
$post->setAttributes([ |
|
144
|
|
|
'title' => 'Updated post title 2', |
|
145
|
|
|
'body' => 'Updated post body 2', |
|
146
|
|
|
'title_en' => 'Updated post title 2 en', |
|
147
|
|
|
'body_en' => 'Updated post title 2 en', |
|
148
|
|
|
]); |
|
149
|
|
|
|
|
150
|
|
|
$this->assertTrue($post->save()); |
|
151
|
|
|
$dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
|
152
|
|
|
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-update-not-populated-post.xml'); |
|
153
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
View Code Duplication |
public function testUpdatePopulatedPost() |
|
157
|
|
|
{ |
|
158
|
|
|
$post = Post::find()->multilingual()->where(['id' => 2])->one(); |
|
159
|
|
|
$post->setAttributes([ |
|
160
|
|
|
'title' => 'Updated post title 2', |
|
161
|
|
|
'body' => 'Updated post body 2', |
|
162
|
|
|
'title_en' => 'Updated post title 2 en', |
|
163
|
|
|
'body_en' => 'Updated post body 2 en', |
|
164
|
|
|
]); |
|
165
|
|
|
|
|
166
|
|
|
$this->assertTrue($post->save()); |
|
167
|
|
|
$dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
|
168
|
|
|
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-update-populated-post.xml'); |
|
169
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function testLocalized() |
|
173
|
|
|
{ |
|
174
|
|
|
$post = Post::find()->localized()->where(['id' => 2])->one(); |
|
175
|
|
|
$this->assertEquals(require(__DIR__ . '/data/test-localized-en.php'), [ |
|
176
|
|
|
'id' => $post->id, |
|
177
|
|
|
'title' => $post->title, |
|
178
|
|
|
'body' => $post->body, |
|
179
|
|
|
]); |
|
180
|
|
|
|
|
181
|
|
|
$post = Post::find()->localized('ru')->where(['id' => 2])->one(); |
|
182
|
|
|
$this->assertEquals(require(__DIR__ . '/data/test-localized-ru.php'), [ |
|
183
|
|
|
'id' => $post->id, |
|
184
|
|
|
'title' => $post->title, |
|
185
|
|
|
'body' => $post->body, |
|
186
|
|
|
]); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
View Code Duplication |
public function testDeletePost() |
|
190
|
|
|
{ |
|
191
|
|
|
$post = Post::findOne(2); |
|
192
|
|
|
$this->assertEquals(1, $post->delete()); |
|
193
|
|
|
$dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
|
194
|
|
|
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-delete-post.xml'); |
|
195
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public function testLocalizedAndMultilingual() |
|
199
|
|
|
{ |
|
200
|
|
|
$post = Post::find()->localized()->multilingual()->limit(1)->one(); |
|
201
|
|
|
$this->assertTrue($post->isRelationPopulated('translations')); |
|
202
|
|
|
$this->assertFalse($post->isRelationPopulated('translation')); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
public function testRequired() |
|
206
|
|
|
{ |
|
207
|
|
|
$post = new PostRequired([ |
|
208
|
|
|
'title' => 'rus', |
|
209
|
|
|
'body' => 'rus', |
|
210
|
|
|
]); |
|
211
|
|
|
|
|
212
|
|
|
$post->validate(); |
|
213
|
|
|
$this->assertArrayNotHasKey('title_ru', $post->errors); |
|
214
|
|
|
$this->assertArrayNotHasKey('body_ru', $post->errors); |
|
215
|
|
|
$this->assertArrayHasKey('title_en', $post->errors); |
|
216
|
|
|
$this->assertArrayHasKey('body_en', $post->errors); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|