Completed
Pull Request — develop (#639)
by Narcotic
18:41 queued 12:22
created

LanguageControllerTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
/**
3
 * Basic functional test for /i18n/language.
4
 */
5
6
namespace Graviton\I18nBundle\Tests\Controller;
7
8
use Graviton\TestBundle\Test\RestTestCase;
9
10
/**
11
 * Basic functional test for /i18n/language.
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  https://opensource.org/licenses/MIT MIT License
15
 * @link     http://swisscom.ch
16
 */
17
class LanguageControllerTest extends RestTestCase
18
{
19
    /**
20
     * @const complete content type string expected on a resouce
21
     */
22
    const CONTENT_TYPE = 'application/json; charset=UTF-8; profile=http://localhost/schema/i18n/language/';
23
24
    /**
25
     * load fixtures
26
     *
27
     * @return void
28
     */
29
    public function setUp()
30
    {
31
        $this->loadFixtures(
32
            array(
33
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData',
34
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadTranslatableData'
35
            ),
36
            null,
37
            'doctrine_mongodb'
38
        );
39
    }
40
41
    /**
42
     * check if a list of all languages can be optained
43
     *
44
     * @return void
45
     */
46
    public function testFindAll()
47
    {
48
        $client = static::createRestClient();
49
        $client->request('GET', '/i18n/language/');
50
51
        $response = $client->getResponse();
52
        $results = $client->getResults();
53
54
        $this->assertResponseContentType(self::CONTENT_TYPE . 'collection', $response);
55
56
        // we assume that initially all systems will only know of the english lang
57
        $this->assertcount(1, $results);
58
59
        $this->assertEquals('en', $results[0]->id);
60
61
        $this->assertEquals('en', $response->headers->get('Content-Language'));
62
63
        $this->assertEquals('English', $results[0]->name->en);
64
    }
65
66
    /**
67
     * validate that multiple languages work as advertised
68
     *
69
     * @return void
70
     */
71
    public function testMultiLangFinding()
72
    {
73
        $this->loadFixtures(
74
            array(
75
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData',
76
                'Graviton\I18nBundle\DataFixtures\MongoDB\LoadMultiLanguageData',
77
            ),
78
            null,
79
            'doctrine_mongodb'
80
        );
81
82
        $client = static::createRestClient();
83
        $client->request('GET', '/i18n/language/');
84
85
        $response = $client->getResponse();
86
        $results = $client->getResults();
87
88
        $this->assertResponseContentType(self::CONTENT_TYPE . 'collection', $response);
89
90
        $this->assertcount(3, $results);
91
92
        $this->assertEquals('en', $response->headers->get('Content-Language'));
93
94
        $this->assertEquals('de', $results[0]->id);
95
        $this->assertEquals('German', $results[0]->name->en);
96
97
        $this->assertEquals('en', $results[1]->id);
98
        $this->assertEquals('English', $results[1]->name->en);
99
100
        $this->assertEquals('fr', $results[2]->id);
101
        $this->assertEquals('French', $results[2]->name->en);
102
    }
103
104
    /**
105
     * test add language and request both languages
106
     *
107
     * @return void
108
     */
109
    public function testAddAndUseNewLanguage()
110
    {
111
        $newLang = new \stdClass;
112
        $newLang->id = 'de';
113
        $newLang->name = new \stdClass;
114
        $newLang->name->en = 'German';
115
116
        $client = static::createRestClient();
117
        $client->post('/i18n/language/', $newLang);
118
        $response = $client->getResponse();
119
120
        $client = static::createRestClient();
121
        $client->request('GET', $response->headers->get('Location'));
122
        $response = $client->getResponse();
123
        $results = $client->getResults();
124
125
        $this->assertResponseContentType(self::CONTENT_TYPE . 'item', $response);
126
        $this->assertEquals('de', $results->id);
127
        $this->assertEquals('en', $response->headers->get('Content-Language'));
128
129
        $client = static::createRestClient();
130
        $client->request('GET', '/i18n/language/', [], [], array('HTTP_ACCEPT_LANGUAGE' => 'en,de'));
131
        $this->assertEquals('en, de', $client->getResponse()->headers->get('Content-Language'));
132
133
        $client = static::createRestClient();
134
        $client->request('GET', '/i18n/language/en', [], [], array('HTTP_ACCEPT_LANGUAGE' => 'en,de'));
135
        $results = $client->getResults();
136
137
        $this->assertEquals('English', $results->name->en);
138
        $this->assertEquals('Englisch', $results->name->de);
139
140
        $client = static::createRestClient();
141
        $client->request('GET', '/i18n/translatable/?locale=en&domain=i18n&original=German');
142
143
        $this->assertCount(1, $client->getResults());
144
        $this->assertEquals('i18n', $client->getResults()[0]->domain);
145
        $this->assertEquals('en', $client->getResults()[0]->locale);
146
        $this->assertEquals('German', $client->getResults()[0]->original);
147
148
        $client = static::createRestClient();
149
        $client->request('GET', '/i18n/translatable/?locale=de&domain=i18n&original=German');
150
        $this->assertCount(1, $client->getResults());
151
        $this->assertEquals('i18n', $client->getResults()[0]->domain);
152
        $this->assertEquals('de', $client->getResults()[0]->locale);
153
        $this->assertEquals('German', $client->getResults()[0]->original);
154
    }
155
156
    /**
157
     * test to add a language and alter a translatable via other service and check
158
     * if the catalogue gets updated accordingly
159
     *
160
     * @return void
161
     */
162
    public function testCacheInvalidation()
163
    {
164
        $newLang = new \stdClass;
165
        $newLang->id = 'es';
166
        $newLang->name = new \stdClass;
167
        $newLang->name->en = 'Spanish';
168
169
        $client = static::createRestClient();
170
        $client->post('/i18n/language/', $newLang);
171
        $response = $client->getResponse();
172
173
        $client = static::createRestClient();
174
        $client->request('GET', $response->headers->get('Location'));
175
        $response = $client->getResponse();
176
        $results = $client->getResults();
177
178
        $this->assertResponseContentType(self::CONTENT_TYPE . 'item', $response);
179
        $this->assertEquals('es', $results->id);
180
        $this->assertEquals('en', $response->headers->get('Content-Language'));
181
182
        // update description for new language
183
        $putLang = new \stdClass;
184
        $putLang->id = 'es';
185
        $putLang->name = new \stdClass;
186
        $putLang->name->en = 'Spanish';
187
        $putLang->name->es = 'Español';
188
189
        $client = static::createRestClient();
190
        $client->put('/i18n/language/es', $putLang, [], [], array('HTTP_ACCEPT_LANGUAGE' => 'es'));
191
192
        $client = static::createRestClient();
193
        $client->request(
194
            'GET',
195
            '/i18n/language/es',
196
            [],
197
            [],
198
            array('HTTP_ACCEPT_LANGUAGE' => 'es')
199
        );
200
        $response = $client->getResponse();
201
        $results = $client->getResults();
202
203
        $this->assertResponseContentType(self::CONTENT_TYPE . 'item', $response);
204
        $this->assertEquals('es', $results->id);
205
        $this->assertEquals('Español', $results->name->es);
206
        $this->assertEquals('en, es', $response->headers->get('Content-Language'));
207
208
        // now, do it again to see if subsequent changes get reflected properly (triggerfile check)
209
        $newPutLang = clone $putLang;
210
        $newPutLang->name->es = 'Espanyol'; // this is a catalan way to spell 'Spanish'
211
212
        $client = static::createRestClient();
213
        $client->put('/i18n/language/es', $newPutLang, [], [], array('HTTP_ACCEPT_LANGUAGE' => 'es'));
214
215
        $client = static::createRestClient();
216
        $client->request(
217
            'GET',
218
            '/i18n/language/es',
219
            [],
220
            [],
221
            array('HTTP_ACCEPT_LANGUAGE' => 'es')
222
        );
223
        $response = $client->getResponse();
224
        $results = $client->getResults();
225
226
        $this->assertResponseContentType(self::CONTENT_TYPE . 'item', $response);
227
        $this->assertEquals('es', $results->id);
228
        $this->assertEquals('Espanyol', $results->name->es);
229
        $this->assertEquals('en, es', $response->headers->get('Content-Language'));
230
    }
231
232
    /**
233
     * check that we do not return unknown languages
234
     *
235
     * @return void
236
     */
237
    public function testDontReturnUnknownLanguage()
238
    {
239
        $client = static::createRestClient();
240
241
        $client->request('GET', '/i18n/language/en', [], [], array('HTTP_ACCEPT_LANGUAGE' => 'en,de'));
242
243
        $response = $client->getResponse();
244
        $results = $client->getResults();
245
246
        $this->assertEquals('en', $response->headers->get('Content-Language'));
247
        $this->assertEquals('English', $results->name->en);
248
        $this->assertFalse(isset($results->name->de));
249
    }
250
251
    /**
252
     * check for language schema
253
     *
254
     * @return void
255
     */
256
    public function testSchema()
257
    {
258
        $client = static::createRestClient();
259
260
        $client->request('GET', '/schema/i18n/language/collection', [], [], ['HTTP_ACCEPT_LANGUAGE' => 'en,de']);
261
262
        $results = $client->getResults();
263
264
        $this->assertEquals('A Language available for i18n purposes.', $results->items->description);
265
        $this->assertEquals(array('id', 'name'), $results->items->required);
266
267
        $properties = $results->items->properties;
268
        $this->assertEquals('string', $properties->id->type);
269
        $this->assertEquals('Language Tag', $properties->id->title);
270
        $this->assertEquals('A RFC2616 language tag.', $properties->id->description);
271
272
        $this->assertEquals('object', $properties->name->type);
273
        $this->assertEquals('Language', $properties->name->title);
274
        $this->assertEquals('Common name of a language.', $properties->name->description);
275
        $this->assertEquals('string', $properties->name->properties->en->type);
276
    }
277
}
278