12
12
namespace ONGR \TranslationsBundle \Tests \Functional \Controller ;
13
13
14
14
use ONGR \ElasticsearchBundle \Test \AbstractElasticsearchTestCase ;
15
+ use ONGR \TranslationsBundle \Document \Message ;
16
+ use org \bovigo \vfs \vfsStream ;
17
+ use Symfony \Component \Yaml \Yaml ;
15
18
16
19
/**
17
20
* Tests rest controller actions.
18
21
*/
19
22
class ApiControllerTest extends AbstractElasticsearchTestCase
20
23
{
24
+ const STREAM = 'translations_ctrl_api_test ' ;
25
+
26
+ /**
27
+ * {@inheritdoc}
28
+ */
29
+ protected function setUp ()
30
+ {
31
+ parent ::setUp ();
32
+ vfsStream::setup (self ::STREAM );
33
+ }
34
+
21
35
/**
22
36
* {@inheritdoc}
23
37
*/
@@ -27,8 +41,11 @@ protected function getDataArray()
27
41
'default ' => [
28
42
'translation ' => [
29
43
[
30
- '_id ' => sha1 ('foo .key ' ),
44
+ '_id ' => sha1 ('foofoo .key ' ),
31
45
'key ' => 'foo.key ' ,
46
+ 'domain ' => 'foo ' ,
47
+ 'path ' => vfsStream::url (self ::STREAM ),
48
+ 'format ' => 'yml ' ,
32
49
'tags ' => [
33
50
[
34
51
'name ' => 'foo_tag ' ,
@@ -37,29 +54,32 @@ protected function getDataArray()
37
54
'name ' => 'tuna_tag ' ,
38
55
],
39
56
],
40
- 'messages ' =>
57
+ 'messages ' => [
41
58
[
42
- [
43
- 'locale ' => 'en ' ,
44
- 'message ' => 'foo ' ,
45
- ],
59
+ 'locale ' => 'en ' ,
60
+ 'message ' => 'foo ' ,
61
+ 'status ' => Message::FRESH ,
46
62
],
63
+ ],
47
64
],
48
65
[
49
- '_id ' => sha1 ('baz .key ' ),
66
+ '_id ' => sha1 ('bazbaz .key ' ),
50
67
'key ' => 'baz.key ' ,
68
+ 'domain ' => 'baz ' ,
69
+ 'path ' => vfsStream::url (self ::STREAM ),
70
+ 'format ' => 'yml ' ,
51
71
'tags ' => [
52
72
[
53
73
'name ' => 'baz_tag ' ,
54
74
],
55
75
],
56
- 'messages ' =>
76
+ 'messages ' => [
57
77
[
58
- [
59
- 'locale ' => 'en ' ,
60
- 'message ' => 'baz ' ,
61
- ],
78
+ 'locale ' => 'en ' ,
79
+ 'message ' => 'baz ' ,
80
+ 'status ' => Message::DIRTY ,
62
81
],
82
+ ],
63
83
],
64
84
],
65
85
],
@@ -136,7 +156,7 @@ public function testActionStatusCode($method, $url, $statusCode, $content = '')
136
156
public function testEditTagAction ()
137
157
{
138
158
$ client = self ::createClient ();
139
- $ id = sha1 ('foo .key ' );
159
+ $ id = sha1 ('foofoo .key ' );
140
160
141
161
$ requestContent = json_encode (
142
162
[
@@ -176,7 +196,7 @@ public function testEditTagAction()
176
196
public function testEditMessageAction ()
177
197
{
178
198
$ client = self ::createClient ();
179
- $ id = sha1 ('foo .key ' );
199
+ $ id = sha1 ('foofoo .key ' );
180
200
181
201
$ requestContent = json_encode (
182
202
[
@@ -216,7 +236,7 @@ public function testEditMessageAction()
216
236
public function testGetTagsAction ()
217
237
{
218
238
$ client = self ::createClient ();
219
- $ id = sha1 ('foo .key ' );
239
+ $ id = sha1 ('foofoo .key ' );
220
240
221
241
$ requestContent = json_encode (
222
242
[
@@ -240,13 +260,13 @@ public function testGetTagsAction()
240
260
[
241
261
[
242
262
'tags ' => [
243
- ['name ' => 'foo_tag ' ],
244
- ['name ' => 'tuna_tag ' ],
263
+ ['name ' => 'baz_tag ' ],
245
264
],
246
265
],
247
266
[
248
267
'tags ' => [
249
- ['name ' => 'baz_tag ' ],
268
+ ['name ' => 'foo_tag ' ],
269
+ ['name ' => 'tuna_tag ' ],
250
270
],
251
271
],
252
272
],
@@ -260,7 +280,7 @@ public function testGetTagsAction()
260
280
public function testRemoveAction ()
261
281
{
262
282
$ client = self ::createClient ();
263
- $ id = sha1 ('foo .key ' );
283
+ $ id = sha1 ('foofoo .key ' );
264
284
265
285
$ requestContent = json_encode (
266
286
[
@@ -299,7 +319,7 @@ public function testRemoveAction()
299
319
public function testAddAction ()
300
320
{
301
321
$ client = self ::createClient ();
302
- $ id = sha1 ('foo .key ' );
322
+ $ id = sha1 ('foofoo .key ' );
303
323
304
324
$ requestContent = json_encode (
305
325
[
@@ -336,4 +356,37 @@ public function testAddAction()
336
356
$ this ->assertNotFalse ($ key , 'tag should exist ' );
337
357
$ this ->assertEquals ('new_foo_tag ' , $ tags [$ key ], 'Tag should have name as defined in request. ' );
338
358
}
359
+
360
+ /**
361
+ * Tests export action.
362
+ */
363
+ public function testExportAction ()
364
+ {
365
+ $ client = self ::createClient ();
366
+ $ currentDir = getcwd ();
367
+ $ webDir = $ currentDir . DIRECTORY_SEPARATOR . 'web ' ;
368
+
369
+ if (!is_dir ($ webDir )) {
370
+ mkdir ($ webDir );
371
+ }
372
+
373
+ chdir ($ webDir );
374
+
375
+ $ client ->request ('post ' , '/translate/_api/export ' );
376
+ $ path = vfsStream::url (self ::STREAM . DIRECTORY_SEPARATOR . 'baz.en.yml ' );
377
+
378
+ $ this ->assertFileExists ($ path , 'Translation file should exist ' );
379
+ $ dumpedData = Yaml::parse (file_get_contents ($ path ));
380
+
381
+ $ this ->assertEquals (['baz.key ' => 'baz ' ], $ dumpedData , 'Translations should be the same. ' );
382
+ $ document = $ this
383
+ ->getManager ('default ' , false )
384
+ ->getRepository ('ONGRTranslationsBundle:Translation ' )
385
+ ->find (sha1 ('bazbaz.key ' ));
386
+
387
+ $ this ->assertEquals (Message::FRESH , $ document ->getMessages ()[0 ]->getStatus (), 'Message should be refreshed ' );
388
+ $ this ->assertEquals ($ currentDir , getcwd ());
389
+
390
+ rmdir ($ webDir );
391
+ }
339
392
}
0 commit comments