1212namespace ONGR \TranslationsBundle \Tests \Functional \Controller ;
1313
1414use ONGR \ElasticsearchBundle \Test \AbstractElasticsearchTestCase ;
15+ use ONGR \TranslationsBundle \Document \Message ;
16+ use org \bovigo \vfs \vfsStream ;
17+ use Symfony \Component \Yaml \Yaml ;
1518
1619/**
1720 * Tests rest controller actions.
1821 */
1922class ApiControllerTest extends AbstractElasticsearchTestCase
2023{
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+
2135 /**
2236 * {@inheritdoc}
2337 */
@@ -27,8 +41,11 @@ protected function getDataArray()
2741 'default ' => [
2842 'translation ' => [
2943 [
30- '_id ' => sha1 ('foo .key ' ),
44+ '_id ' => sha1 ('foofoo .key ' ),
3145 'key ' => 'foo.key ' ,
46+ 'domain ' => 'foo ' ,
47+ 'path ' => vfsStream::url (self ::STREAM ),
48+ 'format ' => 'yml ' ,
3249 'tags ' => [
3350 [
3451 'name ' => 'foo_tag ' ,
@@ -37,29 +54,32 @@ protected function getDataArray()
3754 'name ' => 'tuna_tag ' ,
3855 ],
3956 ],
40- 'messages ' =>
57+ 'messages ' => [
4158 [
42- [
43- 'locale ' => 'en ' ,
44- 'message ' => 'foo ' ,
45- ],
59+ 'locale ' => 'en ' ,
60+ 'message ' => 'foo ' ,
61+ 'status ' => Message::FRESH ,
4662 ],
63+ ],
4764 ],
4865 [
49- '_id ' => sha1 ('baz .key ' ),
66+ '_id ' => sha1 ('bazbaz .key ' ),
5067 'key ' => 'baz.key ' ,
68+ 'domain ' => 'baz ' ,
69+ 'path ' => vfsStream::url (self ::STREAM ),
70+ 'format ' => 'yml ' ,
5171 'tags ' => [
5272 [
5373 'name ' => 'baz_tag ' ,
5474 ],
5575 ],
56- 'messages ' =>
76+ 'messages ' => [
5777 [
58- [
59- 'locale ' => 'en ' ,
60- 'message ' => 'baz ' ,
61- ],
78+ 'locale ' => 'en ' ,
79+ 'message ' => 'baz ' ,
80+ 'status ' => Message::DIRTY ,
6281 ],
82+ ],
6383 ],
6484 ],
6585 ],
@@ -136,7 +156,7 @@ public function testActionStatusCode($method, $url, $statusCode, $content = '')
136156 public function testEditTagAction ()
137157 {
138158 $ client = self ::createClient ();
139- $ id = sha1 ('foo .key ' );
159+ $ id = sha1 ('foofoo .key ' );
140160
141161 $ requestContent = json_encode (
142162 [
@@ -176,7 +196,7 @@ public function testEditTagAction()
176196 public function testEditMessageAction ()
177197 {
178198 $ client = self ::createClient ();
179- $ id = sha1 ('foo .key ' );
199+ $ id = sha1 ('foofoo .key ' );
180200
181201 $ requestContent = json_encode (
182202 [
@@ -216,7 +236,7 @@ public function testEditMessageAction()
216236 public function testGetTagsAction ()
217237 {
218238 $ client = self ::createClient ();
219- $ id = sha1 ('foo .key ' );
239+ $ id = sha1 ('foofoo .key ' );
220240
221241 $ requestContent = json_encode (
222242 [
@@ -240,13 +260,13 @@ public function testGetTagsAction()
240260 [
241261 [
242262 'tags ' => [
243- ['name ' => 'foo_tag ' ],
244- ['name ' => 'tuna_tag ' ],
263+ ['name ' => 'baz_tag ' ],
245264 ],
246265 ],
247266 [
248267 'tags ' => [
249- ['name ' => 'baz_tag ' ],
268+ ['name ' => 'foo_tag ' ],
269+ ['name ' => 'tuna_tag ' ],
250270 ],
251271 ],
252272 ],
@@ -260,7 +280,7 @@ public function testGetTagsAction()
260280 public function testRemoveAction ()
261281 {
262282 $ client = self ::createClient ();
263- $ id = sha1 ('foo .key ' );
283+ $ id = sha1 ('foofoo .key ' );
264284
265285 $ requestContent = json_encode (
266286 [
@@ -299,7 +319,7 @@ public function testRemoveAction()
299319 public function testAddAction ()
300320 {
301321 $ client = self ::createClient ();
302- $ id = sha1 ('foo .key ' );
322+ $ id = sha1 ('foofoo .key ' );
303323
304324 $ requestContent = json_encode (
305325 [
@@ -336,4 +356,37 @@ public function testAddAction()
336356 $ this ->assertNotFalse ($ key , 'tag should exist ' );
337357 $ this ->assertEquals ('new_foo_tag ' , $ tags [$ key ], 'Tag should have name as defined in request. ' );
338358 }
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+ }
339392}
0 commit comments