XliffFileLoaderTest.php 12 KB

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