|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace Gettext\Tests; |
| 5 | + |
| 6 | +use Gettext\Loader\Loader; |
| 7 | +use Gettext\Translation; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +abstract class BasePoLoaderTestCase extends TestCase |
| 11 | +{ |
| 12 | + abstract protected function createPoLoader(): Loader; |
| 13 | + |
| 14 | + public function testPoLoader() |
| 15 | + { |
| 16 | + $loader = $this->createPoLoader(); |
| 17 | + $translations = $loader->loadFile(__DIR__.'/assets/translations.po'); |
| 18 | + |
| 19 | + $description = $translations->getDescription(); |
| 20 | + $this->assertSame( |
| 21 | + <<<'EOT' |
| 22 | +SOME DESCRIPTIVE TITLE |
| 23 | +Copyright (C) YEAR Free Software Foundation, Inc. |
| 24 | +This file is distributed under the same license as the PACKAGE package. |
| 25 | +FIRST AUTHOR <EMAIL@ADDRESS>, YEAR. |
| 26 | +EOT |
| 27 | + , |
| 28 | + $description |
| 29 | + ); |
| 30 | + |
| 31 | + $this->assertSame(['fuzzy'], $translations->getFlags()->toArray()); |
| 32 | + |
| 33 | + $this->assertCount(14, $translations); |
| 34 | + |
| 35 | + $array = $translations->getTranslations(); |
| 36 | + |
| 37 | + $this->translation1(array_shift($array)); |
| 38 | + $this->translation2(array_shift($array)); |
| 39 | + $this->translation3(array_shift($array)); |
| 40 | + $this->translation4(array_shift($array)); |
| 41 | + $this->translation5(array_shift($array)); |
| 42 | + $this->translation6(array_shift($array)); |
| 43 | + $this->translation7(array_shift($array)); |
| 44 | + $this->translation8(array_shift($array)); |
| 45 | + $this->translation9(array_shift($array)); |
| 46 | + $this->translation10(array_shift($array)); |
| 47 | + $this->translation11(array_shift($array)); |
| 48 | + $this->translation12(array_shift($array)); |
| 49 | + $this->translation13(array_shift($array)); |
| 50 | + $this->translation14(array_shift($array)); |
| 51 | + |
| 52 | + $headers = $translations->getHeaders()->toArray(); |
| 53 | + |
| 54 | + $this->assertCount(12, $headers); |
| 55 | + |
| 56 | + $this->assertSame('text/plain; charset=UTF-8', $headers['Content-Type']); |
| 57 | + $this->assertSame('8bit', $headers['Content-Transfer-Encoding']); |
| 58 | + $this->assertSame('', $headers['POT-Creation-Date']); |
| 59 | + $this->assertSame('', $headers['PO-Revision-Date']); |
| 60 | + $this->assertSame('', $headers['Last-Translator']); |
| 61 | + $this->assertSame('', $headers['Language-Team']); |
| 62 | + $this->assertSame('1.0', $headers['MIME-Version']); |
| 63 | + $this->assertSame('bs', $headers['Language']); |
| 64 | + $this->assertSame( |
| 65 | + 'nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);', |
| 66 | + $headers['Plural-Forms'] |
| 67 | + ); |
| 68 | + $this->assertSame('Poedit 1.6.5', $headers['X-Generator']); |
| 69 | + $this->assertSame('gettext generator test', $headers['Project-Id-Version']); |
| 70 | + $this->assertSame('testingdomain', $headers['X-Domain']); |
| 71 | + |
| 72 | + $this->assertSame('testingdomain', $translations->getDomain()); |
| 73 | + $this->assertSame('bs', $translations->getLanguage()); |
| 74 | + } |
| 75 | + |
| 76 | + private function translation1(Translation $translation) |
| 77 | + { |
| 78 | + $this->assertSame( |
| 79 | + 'Ensure this value has at least %(limit_value)d character (it has %sd).', |
| 80 | + $translation->getOriginal() |
| 81 | + ); |
| 82 | + $this->assertSame( |
| 83 | + 'Ensure this value has at least %(limit_value)d characters (it has %sd).', |
| 84 | + $translation->getPlural() |
| 85 | + ); |
| 86 | + $this->assertSame('', $translation->getTranslation()); |
| 87 | + $this->assertSame(['', ''], $translation->getPluralTranslations()); |
| 88 | + } |
| 89 | + |
| 90 | + private function translation2(Translation $translation) |
| 91 | + { |
| 92 | + $this->assertSame( |
| 93 | + 'Ensure this value has at most %(limit_value)d character (it has %sd).', |
| 94 | + $translation->getOriginal() |
| 95 | + ); |
| 96 | + $this->assertSame( |
| 97 | + 'Ensure this value has at most %(limit_value)d characters (it has %sd).', |
| 98 | + $translation->getPlural() |
| 99 | + ); |
| 100 | + $this->assertSame('', $translation->getTranslation()); |
| 101 | + $this->assertSame(['', ''], $translation->getPluralTranslations()); |
| 102 | + } |
| 103 | + |
| 104 | + private function translation3(Translation $translation) |
| 105 | + { |
| 106 | + $this->assertSame('%ss must be unique for %ss %ss.', $translation->getOriginal()); |
| 107 | + $this->assertNull($translation->getPlural()); |
| 108 | + $this->assertSame('%ss mora da bude jedinstven za %ss %ss.', $translation->getTranslation()); |
| 109 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 110 | + } |
| 111 | + |
| 112 | + private function translation4(Translation $translation) |
| 113 | + { |
| 114 | + $this->assertSame('and', $translation->getOriginal()); |
| 115 | + $this->assertNull($translation->getPlural()); |
| 116 | + $this->assertSame('i', $translation->getTranslation()); |
| 117 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 118 | + $this->assertSame(['c-format'], $translation->getFlags()->toArray()); |
| 119 | + } |
| 120 | + |
| 121 | + private function translation5(Translation $translation) |
| 122 | + { |
| 123 | + $this->assertSame('Value %sr is not a valid choice.', $translation->getOriginal()); |
| 124 | + $this->assertNull($translation->getPlural()); |
| 125 | + $this->assertSame('', $translation->getTranslation()); |
| 126 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 127 | + $this->assertSame(['This is a extracted comment'], $translation->getExtractedComments()->toArray()); |
| 128 | + } |
| 129 | + |
| 130 | + private function translation6(Translation $translation) |
| 131 | + { |
| 132 | + $this->assertSame('This field cannot be null.', $translation->getOriginal()); |
| 133 | + $this->assertNull($translation->getPlural()); |
| 134 | + $this->assertSame('Ovo polje ne može ostati prazno.', $translation->getTranslation()); |
| 135 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 136 | + $this->assertCount(1, $translation->getReferences()); |
| 137 | + $this->assertSame(['C:/Users/Me/Documents/foo2.php' => [1]], $translation->getReferences()->toArray()); |
| 138 | + } |
| 139 | + |
| 140 | + private function translation7(Translation $translation) |
| 141 | + { |
| 142 | + $this->assertSame('This field cannot be blank.', $translation->getOriginal()); |
| 143 | + $this->assertNull($translation->getPlural()); |
| 144 | + $this->assertSame('Ovo polje ne može biti prazno.', $translation->getTranslation()); |
| 145 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 146 | + $this->assertCount(1, $translation->getReferences()); |
| 147 | + $this->assertSame(['C:/Users/Me/Documents/foo1.php' => []], $translation->getReferences()->toArray()); |
| 148 | + } |
| 149 | + |
| 150 | + private function translation8(Translation $translation) |
| 151 | + { |
| 152 | + $this->assertSame('Field of type: %ss', $translation->getOriginal()); |
| 153 | + $this->assertNull($translation->getPlural()); |
| 154 | + $this->assertSame('Polje tipa: %ss', $translation->getTranslation()); |
| 155 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 156 | + $this->assertCount(2, $translation->getReferences()); |
| 157 | + $this->assertSame( |
| 158 | + [ |
| 159 | + 'attributes/address/composer.php' => [8], |
| 160 | + 'attributes/address/form.php' => [7], |
| 161 | + ], |
| 162 | + $translation->getReferences()->toArray() |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + private function translation9(Translation $translation) |
| 167 | + { |
| 168 | + $this->assertSame('Integer', $translation->getOriginal()); |
| 169 | + $this->assertNull($translation->getPlural()); |
| 170 | + $this->assertSame('Cijeo broj', $translation->getTranslation()); |
| 171 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 172 | + $this->assertCount(0, $translation->getReferences()); |
| 173 | + $this->assertCount(1, $translation->getComments()); |
| 174 | + $this->assertSame(['a simple line comment is above'], $translation->getComments()->toArray()); |
| 175 | + } |
| 176 | + |
| 177 | + private function translation10(Translation $translation) |
| 178 | + { |
| 179 | + $this->assertSame('{test1}', $translation->getOriginal()); |
| 180 | + $this->assertNull($translation->getPlural()); |
| 181 | + $this->assertSame("test1\n<div>\n test2\n</div>\ntest3", $translation->getTranslation()); |
| 182 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 183 | + $this->assertCount(0, $translation->getComments()); |
| 184 | + $this->assertCount(3, $translation->getReferences()); |
| 185 | + $this->assertSame( |
| 186 | + [ |
| 187 | + '/var/www/test/test.php' => [96, 97], |
| 188 | + '/var/www/test/test2.php' => [98], |
| 189 | + ], |
| 190 | + $translation->getReferences()->toArray() |
| 191 | + ); |
| 192 | + } |
| 193 | + |
| 194 | + private function translation11(Translation $translation) |
| 195 | + { |
| 196 | + $this->assertSame('{test2}', $translation->getOriginal()); |
| 197 | + $this->assertNull($translation->getPlural()); |
| 198 | + $this->assertSame("test1\n<div>\n test2\n</div>\ntest3", $translation->getTranslation()); |
| 199 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 200 | + $this->assertCount(0, $translation->getComments()); |
| 201 | + $this->assertCount(1, $translation->getReferences()); |
| 202 | + $this->assertSame( |
| 203 | + ['/var/www/test/test.php' => [96]], |
| 204 | + $translation->getReferences()->toArray() |
| 205 | + ); |
| 206 | + } |
| 207 | + |
| 208 | + private function translation12(Translation $translation) |
| 209 | + { |
| 210 | + $this->assertSame('Multibyte test', $translation->getOriginal()); |
| 211 | + $this->assertNull($translation->getPlural()); |
| 212 | + $this->assertSame('日本人は日本で話される言語です!', $translation->getTranslation()); |
| 213 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 214 | + $this->assertCount(0, $translation->getComments()); |
| 215 | + $this->assertCount(0, $translation->getReferences()); |
| 216 | + } |
| 217 | + |
| 218 | + private function translation13(Translation $translation) |
| 219 | + { |
| 220 | + $this->assertSame('Tabulation test', $translation->getOriginal()); |
| 221 | + $this->assertNull($translation->getPlural()); |
| 222 | + $this->assertSame("FIELD\tFIELD", $translation->getTranslation()); |
| 223 | + $this->assertCount(0, $translation->getPluralTranslations()); |
| 224 | + $this->assertCount(0, $translation->getComments()); |
| 225 | + $this->assertCount(0, $translation->getReferences()); |
| 226 | + } |
| 227 | + |
| 228 | + private function translation14(Translation $translation) |
| 229 | + { |
| 230 | + $this->assertSame('%s has been added to your cart.', $translation->getOriginal()); |
| 231 | + $this->assertSame('%s have been added to your cart.', $translation->getPlural()); |
| 232 | + $this->assertSame('%s has been added to your cart.', $translation->getTranslation()); |
| 233 | + $this->assertSame(['%s have been added to your cart.'], $translation->getPluralTranslations()); |
| 234 | + $this->assertCount(1, $translation->getComments()); |
| 235 | + $this->assertCount(0, $translation->getReferences()); |
| 236 | + } |
| 237 | + |
| 238 | + public function stringDecodeProvider() |
| 239 | + { |
| 240 | + return [ |
| 241 | + ['"test"', 'test'], |
| 242 | + ['"\'test\'"', "'test'"], |
| 243 | + ['"Special chars: \\n \\t \\\\ "', "Special chars: \n \t \\ "], |
| 244 | + ['"Newline\nSlash and n\\\\nend"', "Newline\nSlash and n\\nend"], |
| 245 | + ['"Quoted \\"string\\" with %s"', 'Quoted "string" with %s'], |
| 246 | + ['"\\\\x07 - aka \\\\a: \\a"', "\\x07 - aka \\a: \x07"], |
| 247 | + ['"\\\\x08 - aka \\\\b: \\b"', "\\x08 - aka \\b: \x08"], |
| 248 | + ['"\\\\x09 - aka \\\\t: \\t"', "\\x09 - aka \\t: \t"], |
| 249 | + ['"\\\\x0a - aka \\\\n: \\n "', "\\x0a - aka \\n: \n "], |
| 250 | + ['"\\\\x0b - aka \\\\v: \\v"', "\\x0b - aka \\v: \x0b"], |
| 251 | + ['"\\\\x0c - aka \\\\f: \\f"', "\\x0c - aka \\f: \x0c"], |
| 252 | + ['"\\\\x0d - aka \\\\r: \\r "', "\\x0d - aka \\r: \r "], |
| 253 | + ['"\\\\x22 - aka \\": \\""', '\x22 - aka ": "'], |
| 254 | + ['"\\\\x5c - aka \\\\: \\\\"', '\\x5c - aka \\: \\'], |
| 255 | + ]; |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * @dataProvider stringDecodeProvider |
| 260 | + * @param mixed $source |
| 261 | + * @param mixed $decoded |
| 262 | + */ |
| 263 | + public function testStringDecode($source, $decoded) |
| 264 | + { |
| 265 | + $po = <<<EOT |
| 266 | +msgid "source" |
| 267 | +msgstr {$source} |
| 268 | +EOT; |
| 269 | + $translations = $this->createPoLoader()->loadString($po); |
| 270 | + $this->assertSame($decoded, $translations->find(null, 'source')->getTranslation()); |
| 271 | + } |
| 272 | + |
| 273 | + public function testMultilineDisabledTranslations() |
| 274 | + { |
| 275 | + $po = <<<'EOT' |
| 276 | +#~ msgid "Last agent hours-description" |
| 277 | +#~ msgstr "" |
| 278 | +#~ "How many hours in the past can system look at finding the last agent? " |
| 279 | +#~ "This parameter is only used if 'Call Last Agent' is set to 'YES'." |
| 280 | +EOT; |
| 281 | + $loader = $this->createPoLoader(); |
| 282 | + $translations = $loader->loadString($po); |
| 283 | + $translation = $translations->find(null, 'Last agent hours-description'); |
| 284 | + |
| 285 | + $this->assertTrue($translation->isDisabled()); |
| 286 | + $this->assertEquals( |
| 287 | + "How many hours in the past can system look at finding the last agent? This parameter is only used if 'Call Last Agent' is set to 'YES'.", |
| 288 | + $translation->getTranslation() |
| 289 | + ); |
| 290 | + } |
| 291 | +} |
0 commit comments