4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Translation\Tests\Loader;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Resource\FileResource;
16 use Symfony\Component\Translation\Loader\XliffFileLoader;
18 class XliffFileLoaderTest extends TestCase
20 public function testLoad()
22 $loader = new XliffFileLoader();
23 $resource = __DIR__.'/../fixtures/resources.xlf';
24 $catalogue = $loader->load($resource, 'en', 'domain1');
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'));
32 public function testLoadWithInternalErrorsEnabled()
34 $internalErrors = libxml_use_internal_errors(true);
36 $this->assertSame(array(), libxml_get_errors());
38 $loader = new XliffFileLoader();
39 $resource = __DIR__.'/../fixtures/resources.xlf';
40 $catalogue = $loader->load($resource, 'en', 'domain1');
42 $this->assertEquals('en', $catalogue->getLocale());
43 $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
44 $this->assertSame(array(), libxml_get_errors());
46 libxml_clear_errors();
47 libxml_use_internal_errors($internalErrors);
50 public function testLoadWithExternalEntitiesDisabled()
52 $disableEntities = libxml_disable_entity_loader(true);
54 $loader = new XliffFileLoader();
55 $resource = __DIR__.'/../fixtures/resources.xlf';
56 $catalogue = $loader->load($resource, 'en', 'domain1');
58 libxml_disable_entity_loader($disableEntities);
60 $this->assertEquals('en', $catalogue->getLocale());
61 $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
64 public function testLoadWithResname()
66 $loader = new XliffFileLoader();
67 $catalogue = $loader->load(__DIR__.'/../fixtures/resname.xlf', 'en', 'domain1');
69 $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'), $catalogue->all('domain1'));
72 public function testIncompleteResource()
74 $loader = new XliffFileLoader();
75 $catalogue = $loader->load(__DIR__.'/../fixtures/resources.xlf', 'en', 'domain1');
77 $this->assertEquals(array('foo' => 'bar', 'extra' => 'extra', 'key' => '', 'test' => 'with'), $catalogue->all('domain1'));
80 public function testEncoding()
82 $loader = new XliffFileLoader();
83 $catalogue = $loader->load(__DIR__.'/../fixtures/encoding.xlf', 'en', 'domain1');
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'));
90 public function testTargetAttributesAreStoredCorrectly()
92 $loader = new XliffFileLoader();
93 $catalogue = $loader->load(__DIR__.'/../fixtures/with-attributes.xlf', 'en', 'domain1');
95 $metadata = $catalogue->getMetadata('foo', 'domain1');
96 $this->assertEquals('translated', $metadata['target-attributes']['state']);
100 * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
102 public function testLoadInvalidResource()
104 $loader = new XliffFileLoader();
105 $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1');
109 * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
111 public function testLoadResourceDoesNotValidate()
113 $loader = new XliffFileLoader();
114 $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1');
118 * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
120 public function testLoadNonExistingResource()
122 $loader = new XliffFileLoader();
123 $resource = __DIR__.'/../fixtures/non-existing.xlf';
124 $loader->load($resource, 'en', 'domain1');
128 * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
130 public function testLoadThrowsAnExceptionIfFileNotLocal()
132 $loader = new XliffFileLoader();
133 $resource = 'http://example.com/resources.xlf';
134 $loader->load($resource, 'en', 'domain1');
138 * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
139 * @expectedExceptionMessage Document types are not allowed.
141 public function testDocTypeIsNotAllowed()
143 $loader = new XliffFileLoader();
144 $loader->load(__DIR__.'/../fixtures/withdoctype.xlf', 'en', 'domain1');
147 public function testParseEmptyFile()
149 $loader = new XliffFileLoader();
150 $resource = __DIR__.'/../fixtures/empty.xlf';
152 if (method_exists($this, 'expectException')) {
153 $this->expectException('Symfony\Component\Translation\Exception\InvalidResourceException');
154 $this->expectExceptionMessage(sprintf('Unable to load "%s":', $resource));
156 $this->setExpectedException('Symfony\Component\Translation\Exception\InvalidResourceException', sprintf('Unable to load "%s":', $resource));
159 $loader->load($resource, 'en', 'domain1');
162 public function testLoadNotes()
164 $loader = new XliffFileLoader();
165 $catalogue = $loader->load(__DIR__.'/../fixtures/withnote.xlf', 'en', 'domain1');
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'));
174 public function testLoadVersion2()
176 $loader = new XliffFileLoader();
177 $resource = __DIR__.'/../fixtures/resources-2.0.xlf';
178 $catalogue = $loader->load($resource, 'en', 'domain1');
180 $this->assertEquals('en', $catalogue->getLocale());
181 $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
182 $this->assertSame(array(), libxml_get_errors());
184 $domains = $catalogue->all();
185 $this->assertCount(3, $domains['domain1']);
186 $this->assertContainsOnly('string', $catalogue->all('domain1'));
189 $this->assertEquals(array('target-attributes' => array('order' => 1)), $catalogue->getMetadata('bar', 'domain1'));
192 public function testLoadVersion2WithNoteMeta()
194 $loader = new XliffFileLoader();
195 $resource = __DIR__.'/../fixtures/resources-notes-meta.xlf';
196 $catalogue = $loader->load($resource, 'en', 'domain1');
198 $this->assertEquals('en', $catalogue->getLocale());
199 $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
200 $this->assertSame(array(), libxml_get_errors());
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']);
208 $this->assertEquals('state', $metadata['notes'][0]['category']);
209 $this->assertEquals('new', $metadata['notes'][0]['content']);
211 $this->assertEquals('approved', $metadata['notes'][1]['category']);
212 $this->assertEquals('true', $metadata['notes'][1]['content']);
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']);
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']);
224 $this->assertEquals('x', $metadata['notes'][0]['id']);
225 $this->assertEquals('x_content', $metadata['notes'][0]['content']);
227 $this->assertEquals('target', $metadata['notes'][1]['appliesTo']);
228 $this->assertEquals('quality', $metadata['notes'][1]['category']);
229 $this->assertEquals('Fuzzy', $metadata['notes'][1]['content']);
232 public function testLoadVersion2WithMultiSegmentUnit()
234 $loader = new XliffFileLoader();
235 $resource = __DIR__.'/../fixtures/resources-2.0-multi-segment-unit.xlf';
236 $catalog = $loader->load($resource, 'en', 'domain1');
238 $this->assertSame('en', $catalog->getLocale());
239 $this->assertEquals(array(new FileResource($resource)), $catalog->getResources());
240 $this->assertFalse(libxml_get_last_error());
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']);
248 $this->assertSame('processed', $metadata['notes'][0]['category']);
249 $this->assertSame('true', $metadata['notes'][0]['content']);
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']);
257 $this->assertSame('processed', $metadata['notes'][0]['category']);
258 $this->assertSame('true', $metadata['notes'][0]['content']);