Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / translation / Tests / Loader / XliffFileLoaderTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Translation\Tests\Loader;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Resource\FileResource;
16 use Symfony\Component\Translation\Loader\XliffFileLoader;
17
18 class XliffFileLoaderTest extends TestCase
19 {
20     public function testLoad()
21     {
22         $loader = new XliffFileLoader();
23         $resource = __DIR__.'/../fixtures/resources.xlf';
24         $catalogue = $loader->load($resource, 'en', 'domain1');
25
26         $this->assertEquals('en', $catalogue->getLocale());
27         $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
28         $this->assertSame(array(), libxml_get_errors());
29         $this->assertContainsOnly('string', $catalogue->all('domain1'));
30     }
31
32     public function testLoadWithInternalErrorsEnabled()
33     {
34         $internalErrors = libxml_use_internal_errors(true);
35
36         $this->assertSame(array(), libxml_get_errors());
37
38         $loader = new XliffFileLoader();
39         $resource = __DIR__.'/../fixtures/resources.xlf';
40         $catalogue = $loader->load($resource, 'en', 'domain1');
41
42         $this->assertEquals('en', $catalogue->getLocale());
43         $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
44         $this->assertSame(array(), libxml_get_errors());
45
46         libxml_clear_errors();
47         libxml_use_internal_errors($internalErrors);
48     }
49
50     public function testLoadWithExternalEntitiesDisabled()
51     {
52         $disableEntities = libxml_disable_entity_loader(true);
53
54         $loader = new XliffFileLoader();
55         $resource = __DIR__.'/../fixtures/resources.xlf';
56         $catalogue = $loader->load($resource, 'en', 'domain1');
57
58         libxml_disable_entity_loader($disableEntities);
59
60         $this->assertEquals('en', $catalogue->getLocale());
61         $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
62     }
63
64     public function testLoadWithResname()
65     {
66         $loader = new XliffFileLoader();
67         $catalogue = $loader->load(__DIR__.'/../fixtures/resname.xlf', 'en', 'domain1');
68
69         $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'), $catalogue->all('domain1'));
70     }
71
72     public function testIncompleteResource()
73     {
74         $loader = new XliffFileLoader();
75         $catalogue = $loader->load(__DIR__.'/../fixtures/resources.xlf', 'en', 'domain1');
76
77         $this->assertEquals(array('foo' => 'bar', 'extra' => 'extra', 'key' => '', 'test' => 'with'), $catalogue->all('domain1'));
78     }
79
80     public function testEncoding()
81     {
82         $loader = new XliffFileLoader();
83         $catalogue = $loader->load(__DIR__.'/../fixtures/encoding.xlf', 'en', 'domain1');
84
85         $this->assertEquals(utf8_decode('föö'), $catalogue->get('bar', 'domain1'));
86         $this->assertEquals(utf8_decode('bär'), $catalogue->get('foo', 'domain1'));
87         $this->assertEquals(array('notes' => array(array('content' => utf8_decode('bäz'))), 'id' => '1'), $catalogue->getMetadata('foo', 'domain1'));
88     }
89
90     public function testTargetAttributesAreStoredCorrectly()
91     {
92         $loader = new XliffFileLoader();
93         $catalogue = $loader->load(__DIR__.'/../fixtures/with-attributes.xlf', 'en', 'domain1');
94
95         $metadata = $catalogue->getMetadata('foo', 'domain1');
96         $this->assertEquals('translated', $metadata['target-attributes']['state']);
97     }
98
99     /**
100      * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
101      */
102     public function testLoadInvalidResource()
103     {
104         $loader = new XliffFileLoader();
105         $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1');
106     }
107
108     /**
109      * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
110      */
111     public function testLoadResourceDoesNotValidate()
112     {
113         $loader = new XliffFileLoader();
114         $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1');
115     }
116
117     /**
118      * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
119      */
120     public function testLoadNonExistingResource()
121     {
122         $loader = new XliffFileLoader();
123         $resource = __DIR__.'/../fixtures/non-existing.xlf';
124         $loader->load($resource, 'en', 'domain1');
125     }
126
127     /**
128      * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
129      */
130     public function testLoadThrowsAnExceptionIfFileNotLocal()
131     {
132         $loader = new XliffFileLoader();
133         $resource = 'http://example.com/resources.xlf';
134         $loader->load($resource, 'en', 'domain1');
135     }
136
137     /**
138      * @expectedException        \Symfony\Component\Translation\Exception\InvalidResourceException
139      * @expectedExceptionMessage Document types are not allowed.
140      */
141     public function testDocTypeIsNotAllowed()
142     {
143         $loader = new XliffFileLoader();
144         $loader->load(__DIR__.'/../fixtures/withdoctype.xlf', 'en', 'domain1');
145     }
146
147     public function testParseEmptyFile()
148     {
149         $loader = new XliffFileLoader();
150         $resource = __DIR__.'/../fixtures/empty.xlf';
151
152         if (method_exists($this, 'expectException')) {
153             $this->expectException('Symfony\Component\Translation\Exception\InvalidResourceException');
154             $this->expectExceptionMessage(sprintf('Unable to load "%s":', $resource));
155         } else {
156             $this->setExpectedException('Symfony\Component\Translation\Exception\InvalidResourceException', sprintf('Unable to load "%s":', $resource));
157         }
158
159         $loader->load($resource, 'en', 'domain1');
160     }
161
162     public function testLoadNotes()
163     {
164         $loader = new XliffFileLoader();
165         $catalogue = $loader->load(__DIR__.'/../fixtures/withnote.xlf', 'en', 'domain1');
166
167         $this->assertEquals(array('notes' => array(array('priority' => 1, 'content' => 'foo')), 'id' => '1'), $catalogue->getMetadata('foo', 'domain1'));
168         // message without target
169         $this->assertEquals(array('notes' => array(array('content' => 'bar', 'from' => 'foo')), 'id' => '2'), $catalogue->getMetadata('extra', 'domain1'));
170         // message with empty target
171         $this->assertEquals(array('notes' => array(array('content' => 'baz'), array('priority' => 2, 'from' => 'bar', 'content' => 'qux')), 'id' => '123'), $catalogue->getMetadata('key', 'domain1'));
172     }
173
174     public function testLoadVersion2()
175     {
176         $loader = new XliffFileLoader();
177         $resource = __DIR__.'/../fixtures/resources-2.0.xlf';
178         $catalogue = $loader->load($resource, 'en', 'domain1');
179
180         $this->assertEquals('en', $catalogue->getLocale());
181         $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
182         $this->assertSame(array(), libxml_get_errors());
183
184         $domains = $catalogue->all();
185         $this->assertCount(3, $domains['domain1']);
186         $this->assertContainsOnly('string', $catalogue->all('domain1'));
187
188         // target attributes
189         $this->assertEquals(array('target-attributes' => array('order' => 1)), $catalogue->getMetadata('bar', 'domain1'));
190     }
191
192     public function testLoadVersion2WithNoteMeta()
193     {
194         $loader = new XliffFileLoader();
195         $resource = __DIR__.'/../fixtures/resources-notes-meta.xlf';
196         $catalogue = $loader->load($resource, 'en', 'domain1');
197
198         $this->assertEquals('en', $catalogue->getLocale());
199         $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
200         $this->assertSame(array(), libxml_get_errors());
201
202         // test for "foo" metadata
203         $this->assertTrue($catalogue->defines('foo', 'domain1'));
204         $metadata = $catalogue->getMetadata('foo', 'domain1');
205         $this->assertNotEmpty($metadata);
206         $this->assertCount(3, $metadata['notes']);
207
208         $this->assertEquals('state', $metadata['notes'][0]['category']);
209         $this->assertEquals('new', $metadata['notes'][0]['content']);
210
211         $this->assertEquals('approved', $metadata['notes'][1]['category']);
212         $this->assertEquals('true', $metadata['notes'][1]['content']);
213
214         $this->assertEquals('section', $metadata['notes'][2]['category']);
215         $this->assertEquals('1', $metadata['notes'][2]['priority']);
216         $this->assertEquals('user login', $metadata['notes'][2]['content']);
217
218         // test for "baz" metadata
219         $this->assertTrue($catalogue->defines('baz', 'domain1'));
220         $metadata = $catalogue->getMetadata('baz', 'domain1');
221         $this->assertNotEmpty($metadata);
222         $this->assertCount(2, $metadata['notes']);
223
224         $this->assertEquals('x', $metadata['notes'][0]['id']);
225         $this->assertEquals('x_content', $metadata['notes'][0]['content']);
226
227         $this->assertEquals('target', $metadata['notes'][1]['appliesTo']);
228         $this->assertEquals('quality', $metadata['notes'][1]['category']);
229         $this->assertEquals('Fuzzy', $metadata['notes'][1]['content']);
230     }
231
232     public function testLoadVersion2WithMultiSegmentUnit()
233     {
234         $loader = new XliffFileLoader();
235         $resource = __DIR__.'/../fixtures/resources-2.0-multi-segment-unit.xlf';
236         $catalog = $loader->load($resource, 'en', 'domain1');
237
238         $this->assertSame('en', $catalog->getLocale());
239         $this->assertEquals(array(new FileResource($resource)), $catalog->getResources());
240         $this->assertFalse(libxml_get_last_error());
241
242         // test for "foo" metadata
243         $this->assertTrue($catalog->defines('foo', 'domain1'));
244         $metadata = $catalog->getMetadata('foo', 'domain1');
245         $this->assertNotEmpty($metadata);
246         $this->assertCount(1, $metadata['notes']);
247
248         $this->assertSame('processed', $metadata['notes'][0]['category']);
249         $this->assertSame('true', $metadata['notes'][0]['content']);
250
251         // test for "bar" metadata
252         $this->assertTrue($catalog->defines('bar', 'domain1'));
253         $metadata = $catalog->getMetadata('bar', 'domain1');
254         $this->assertNotEmpty($metadata);
255         $this->assertCount(1, $metadata['notes']);
256
257         $this->assertSame('processed', $metadata['notes'][0]['category']);
258         $this->assertSame('true', $metadata['notes'][0]['content']);
259     }
260 }