1
|
|
|
<?php |
2
|
|
|
namespace tests; |
3
|
|
|
|
4
|
|
|
use tests\models\PostRequired; |
5
|
|
|
use Yii; |
6
|
|
|
use tests\models\Post; |
7
|
|
|
use omgdef\multilingual\MultilingualBehavior; |
8
|
|
|
|
9
|
|
|
class MultilingualBehaviorTest extends DatabaseTestCase |
10
|
|
|
{ |
11
|
|
View Code Duplication |
public function testFindPosts() |
12
|
|
|
{ |
13
|
|
|
$data = []; |
14
|
|
|
$models = Post::find()->multilingual()->all(); |
15
|
|
|
foreach ($models as $model) { |
16
|
|
|
$this->assertEquals($model->title, $model->title_ru); |
17
|
|
|
$this->assertEquals($model->body, $model->body_ru); |
18
|
|
|
$this->assertNotNull($model->title_en_us); |
19
|
|
|
$this->assertNotNull($model->body_en_us); |
20
|
|
|
$data[] = $model->toArray([], ['translations']); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
$this->assertEquals(require(__DIR__ . '/data/test-find-posts-na.php'), $data); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
View Code Duplication |
public function testCreatePost() |
27
|
|
|
{ |
28
|
|
|
$post = new Post([ |
29
|
|
|
'title' => 'New post title', |
30
|
|
|
'body' => 'New post body', |
31
|
|
|
]); |
32
|
|
|
|
33
|
|
|
$this->assertTrue($post->save()); |
34
|
|
|
|
35
|
|
|
$dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
36
|
|
|
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-create-post-na.xml'); |
37
|
|
|
|
38
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
View Code Duplication |
public function testCreatePostSetTranslations() |
42
|
|
|
{ |
43
|
|
|
$post = new Post(); |
44
|
|
|
$data = [ |
45
|
|
|
'title' => 'New post title', |
46
|
|
|
'body' => 'New post body', |
47
|
|
|
'title_en_us' => 'New post title en', |
48
|
|
|
'body_en_us' => 'New post body en', |
49
|
|
|
'title_ru' => 'New post title ru', //this value should be overwritten by default language value |
50
|
|
|
'body_ru' => 'New post body ru', |
51
|
|
|
]; |
52
|
|
|
$formName = $post->formName(); |
53
|
|
|
if (!empty($formName)) { |
54
|
|
|
$data = [$formName => $data]; |
55
|
|
|
} |
56
|
|
|
$post->load($data); |
57
|
|
|
|
58
|
|
|
$this->assertTrue($post->save()); |
59
|
|
|
$dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
60
|
|
|
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-create-post-set-translations-na.xml'); |
61
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function testUpdateNotPopulatedPost() |
65
|
|
|
{ |
66
|
|
|
$post = Post::findOne(2); |
67
|
|
|
$post->setAttributes([ |
68
|
|
|
'title' => 'Updated post title 2', |
69
|
|
|
'body' => 'Updated post body 2', |
70
|
|
|
'title_en_us' => 'Updated post title 2 en', |
71
|
|
|
'body_en_us' => 'Updated post title 2 en', |
72
|
|
|
]); |
73
|
|
|
|
74
|
|
|
$this->assertTrue($post->save()); |
75
|
|
|
$dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
76
|
|
|
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-update-not-populated-post-na.xml'); |
77
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function testUpdatePopulatedPost() |
81
|
|
|
{ |
82
|
|
|
$post = Post::find()->multilingual()->where(['id' => 2])->one(); |
83
|
|
|
$post->setAttributes([ |
84
|
|
|
'title' => 'Updated post title 2', |
85
|
|
|
'body' => 'Updated post body 2', |
86
|
|
|
'title_en_us' => 'Updated post title 2 en', |
87
|
|
|
'body_en_us' => 'Updated post body 2 en', |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
$this->assertTrue($post->save()); |
91
|
|
|
$dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
92
|
|
|
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-update-populated-post-na.xml'); |
93
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testLocalized() |
97
|
|
|
{ |
98
|
|
|
$post = Post::find()->localized(null, false)->where(['id' => 2])->one(); |
99
|
|
|
$this->assertEquals(require(__DIR__ . '/data/test-localized-en.php'), [ |
100
|
|
|
'id' => $post->id, |
101
|
|
|
'title' => $post->title, |
102
|
|
|
'body' => $post->body, |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
$post = Post::find()->localized('ru', false)->where(['id' => 2])->one(); |
106
|
|
|
$this->assertEquals(require(__DIR__ . '/data/test-localized-ru.php'), [ |
107
|
|
|
'id' => $post->id, |
108
|
|
|
'title' => $post->title, |
109
|
|
|
'body' => $post->body, |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
public function testDeletePost() |
114
|
|
|
{ |
115
|
|
|
$post = Post::findOne(2); |
116
|
|
|
$this->assertEquals(1, $post->delete()); |
117
|
|
|
$dataSet = $this->getConnection()->createDataSet(['post', 'postLang']); |
118
|
|
|
$expectedDataSet = $this->createFlatXMLDataSet(__DIR__ . '/data/test-delete-post-na.xml'); |
119
|
|
|
$this->assertDataSetsEqual($expectedDataSet, $dataSet); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritdoc |
124
|
|
|
*/ |
125
|
|
|
public function getDataSet() |
126
|
|
|
{ |
127
|
|
|
return $this->createFlatXMLDataSet(__DIR__ . '/data/test-na.xml'); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|