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\Translation\Loader\XliffFileLoader;
16 use Symfony\Component\Config\Resource\FileResource;
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')))), $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'))), $catalogue->getMetadata('foo', 'domain1'));
168 // message without target
169 $this->assertEquals(array('notes' => array(array('content' => 'bar', 'from' => 'foo'))), $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'))), $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'));