XliffFileLoaderTest.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\Translation\Loader\XliffFileLoader;
  13. use Symfony\Component\Config\Resource\FileResource;
  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(array(new FileResource($resource)), $catalogue->getResources());
  23. $this->assertSame(array(), 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(array(), 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(array(new FileResource($resource)), $catalogue->getResources());
  35. $this->assertSame(array(), 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(array(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(array('foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'), $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(array('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(array('notes' => array(array('content' => utf8_decode('bäz'))), 'id' => '1'), $catalogue->getMetadata('foo', 'domain1'));
  68. }
  69. public function testTargetAttributesAreStoredCorrectly()
  70. {
  71. $loader = new XliffFileLoader();
  72. $catalogue = $loader->load(__DIR__.'/../fixtures/with-attributes.xlf', 'en', 'domain1');
  73. $metadata = $catalogue->getMetadata('foo', 'domain1');
  74. $this->assertEquals('translated', $metadata['target-attributes']['state']);
  75. }
  76. /**
  77. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  78. */
  79. public function testLoadInvalidResource()
  80. {
  81. $loader = new XliffFileLoader();
  82. $loader->load(__DIR__.'/../fixtures/resources.php', 'en', 'domain1');
  83. }
  84. /**
  85. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  86. */
  87. public function testLoadResourceDoesNotValidate()
  88. {
  89. $loader = new XliffFileLoader();
  90. $loader->load(__DIR__.'/../fixtures/non-valid.xlf', 'en', 'domain1');
  91. }
  92. /**
  93. * @expectedException \Symfony\Component\Translation\Exception\NotFoundResourceException
  94. */
  95. public function testLoadNonExistingResource()
  96. {
  97. $loader = new XliffFileLoader();
  98. $resource = __DIR__.'/../fixtures/non-existing.xlf';
  99. $loader->load($resource, 'en', 'domain1');
  100. }
  101. /**
  102. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  103. */
  104. public function testLoadThrowsAnExceptionIfFileNotLocal()
  105. {
  106. $loader = new XliffFileLoader();
  107. $resource = 'http://example.com/resources.xlf';
  108. $loader->load($resource, 'en', 'domain1');
  109. }
  110. /**
  111. * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException
  112. * @expectedExceptionMessage Document types are not allowed.
  113. */
  114. public function testDocTypeIsNotAllowed()
  115. {
  116. $loader = new XliffFileLoader();
  117. $loader->load(__DIR__.'/../fixtures/withdoctype.xlf', 'en', 'domain1');
  118. }
  119. public function testParseEmptyFile()
  120. {
  121. $loader = new XliffFileLoader();
  122. $resource = __DIR__.'/../fixtures/empty.xlf';
  123. if (method_exists($this, 'expectException')) {
  124. $this->expectException('Symfony\Component\Translation\Exception\InvalidResourceException');
  125. $this->expectExceptionMessage(sprintf('Unable to load "%s":', $resource));
  126. } else {
  127. $this->setExpectedException('Symfony\Component\Translation\Exception\InvalidResourceException', sprintf('Unable to load "%s":', $resource));
  128. }
  129. $loader->load($resource, 'en', 'domain1');
  130. }
  131. public function testLoadNotes()
  132. {
  133. $loader = new XliffFileLoader();
  134. $catalogue = $loader->load(__DIR__.'/../fixtures/withnote.xlf', 'en', 'domain1');
  135. $this->assertEquals(array('notes' => array(array('priority' => 1, 'content' => 'foo')), 'id' => '1'), $catalogue->getMetadata('foo', 'domain1'));
  136. // message without target
  137. $this->assertEquals(array('notes' => array(array('content' => 'bar', 'from' => 'foo')), 'id' => '2'), $catalogue->getMetadata('extra', 'domain1'));
  138. // message with empty target
  139. $this->assertEquals(array('notes' => array(array('content' => 'baz'), array('priority' => 2, 'from' => 'bar', 'content' => 'qux')), 'id' => '123'), $catalogue->getMetadata('key', 'domain1'));
  140. }
  141. public function testLoadVersion2()
  142. {
  143. $loader = new XliffFileLoader();
  144. $resource = __DIR__.'/../fixtures/resources-2.0.xlf';
  145. $catalogue = $loader->load($resource, 'en', 'domain1');
  146. $this->assertEquals('en', $catalogue->getLocale());
  147. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
  148. $this->assertSame(array(), libxml_get_errors());
  149. $domains = $catalogue->all();
  150. $this->assertCount(3, $domains['domain1']);
  151. $this->assertContainsOnly('string', $catalogue->all('domain1'));
  152. // target attributes
  153. $this->assertEquals(array('target-attributes' => array('order' => 1)), $catalogue->getMetadata('bar', 'domain1'));
  154. }
  155. public function testLoadVersion2WithNoteMeta()
  156. {
  157. $loader = new XliffFileLoader();
  158. $resource = __DIR__.'/../fixtures/resources-notes-meta.xlf';
  159. $catalogue = $loader->load($resource, 'en', 'domain1');
  160. $this->assertEquals('en', $catalogue->getLocale());
  161. $this->assertEquals(array(new FileResource($resource)), $catalogue->getResources());
  162. $this->assertSame(array(), libxml_get_errors());
  163. // test for "foo" metadata
  164. $this->assertTrue($catalogue->defines('foo', 'domain1'));
  165. $metadata = $catalogue->getMetadata('foo', 'domain1');
  166. $this->assertNotEmpty($metadata);
  167. $this->assertCount(3, $metadata['notes']);
  168. $this->assertEquals('state', $metadata['notes'][0]['category']);
  169. $this->assertEquals('new', $metadata['notes'][0]['content']);
  170. $this->assertEquals('approved', $metadata['notes'][1]['category']);
  171. $this->assertEquals('true', $metadata['notes'][1]['content']);
  172. $this->assertEquals('section', $metadata['notes'][2]['category']);
  173. $this->assertEquals('1', $metadata['notes'][2]['priority']);
  174. $this->assertEquals('user login', $metadata['notes'][2]['content']);
  175. // test for "baz" metadata
  176. $this->assertTrue($catalogue->defines('baz', 'domain1'));
  177. $metadata = $catalogue->getMetadata('baz', 'domain1');
  178. $this->assertNotEmpty($metadata);
  179. $this->assertCount(2, $metadata['notes']);
  180. $this->assertEquals('x', $metadata['notes'][0]['id']);
  181. $this->assertEquals('x_content', $metadata['notes'][0]['content']);
  182. $this->assertEquals('target', $metadata['notes'][1]['appliesTo']);
  183. $this->assertEquals('quality', $metadata['notes'][1]['category']);
  184. $this->assertEquals('Fuzzy', $metadata['notes'][1]['content']);
  185. }
  186. public function testLoadVersion2WithMultiSegmentUnit()
  187. {
  188. $loader = new XliffFileLoader();
  189. $resource = __DIR__.'/../fixtures/resources-2.0-multi-segment-unit.xlf';
  190. $catalog = $loader->load($resource, 'en', 'domain1');
  191. $this->assertSame('en', $catalog->getLocale());
  192. $this->assertEquals(array(new FileResource($resource)), $catalog->getResources());
  193. $this->assertFalse(libxml_get_last_error());
  194. // test for "foo" metadata
  195. $this->assertTrue($catalog->defines('foo', 'domain1'));
  196. $metadata = $catalog->getMetadata('foo', 'domain1');
  197. $this->assertNotEmpty($metadata);
  198. $this->assertCount(1, $metadata['notes']);
  199. $this->assertSame('processed', $metadata['notes'][0]['category']);
  200. $this->assertSame('true', $metadata['notes'][0]['content']);
  201. // test for "bar" metadata
  202. $this->assertTrue($catalog->defines('bar', 'domain1'));
  203. $metadata = $catalog->getMetadata('bar', 'domain1');
  204. $this->assertNotEmpty($metadata);
  205. $this->assertCount(1, $metadata['notes']);
  206. $this->assertSame('processed', $metadata['notes'][0]['category']);
  207. $this->assertSame('true', $metadata['notes'][0]['content']);
  208. }
  209. }