XliffFileLoaderTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. }