|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of CodeIgniter 4 framework. |
| 5 | + * |
| 6 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace CodeIgniter\Commands\Translation; |
| 13 | + |
| 14 | +use CodeIgniter\Test\CIUnitTestCase; |
| 15 | +use CodeIgniter\Test\StreamFilterTrait; |
| 16 | +use Config\App; |
| 17 | +use Config\Services; |
| 18 | +use Locale; |
| 19 | + |
| 20 | +/** |
| 21 | + * @group Others |
| 22 | + * |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +final class LocalizationFinderTest extends CIUnitTestCase |
| 26 | +{ |
| 27 | + use StreamFilterTrait; |
| 28 | + |
| 29 | + private static string $locale; |
| 30 | + private static string $languageTestPath; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + parent::setUp(); |
| 35 | + self::$locale = Locale::getDefault(); |
| 36 | + self::$languageTestPath = SUPPORTPATH . 'Language/'; |
| 37 | + $this->clearGeneratedFiles(); |
| 38 | + } |
| 39 | + |
| 40 | + public function testUpdateDefaultLocale(): void |
| 41 | + { |
| 42 | + @mkdir(self::$languageTestPath . self::$locale, 0777, true); |
| 43 | + Services::commands()->run('lang:find', [ |
| 44 | + 'dir' => 'Services/Translation', |
| 45 | + ]); |
| 46 | + $this->realizeAssertion(); |
| 47 | + $this->clearGeneratedFiles(); |
| 48 | + } |
| 49 | + |
| 50 | + public function testUpdateWithLocaleOption(): void |
| 51 | + { |
| 52 | + self::$locale = config(App::class)->supportedLocales[0]; |
| 53 | + @mkdir(self::$languageTestPath . self::$locale, 0777, true); |
| 54 | + Services::commands()->run('lang:find', [ |
| 55 | + 'dir' => 'Services/Translation', |
| 56 | + 'locale' => self::$locale, |
| 57 | + ]); |
| 58 | + $this->realizeAssertion(); |
| 59 | + $this->clearGeneratedFiles(); |
| 60 | + } |
| 61 | + |
| 62 | + public function testUpdateWithIncorrectLocaleOption(): void |
| 63 | + { |
| 64 | + self::$locale = 'test_locale_incorrect'; |
| 65 | + @mkdir(self::$languageTestPath . self::$locale, 0777, true); |
| 66 | + $status = Services::commands()->run('lang:find', [ |
| 67 | + 'dir' => 'Services/Translation', |
| 68 | + 'locale' => self::$locale, |
| 69 | + ]); |
| 70 | + $this->assertSame($status, -1); |
| 71 | + $this->clearGeneratedFiles(); |
| 72 | + } |
| 73 | + |
| 74 | + public function testUpdateWithEmptyDirOption(): void |
| 75 | + { |
| 76 | + @mkdir(self::$languageTestPath . self::$locale, 0777, true); |
| 77 | + Services::commands()->run('lang:find', []); |
| 78 | + $this->realizeAssertion(); |
| 79 | + $this->clearGeneratedFiles(); |
| 80 | + } |
| 81 | + |
| 82 | + public function testUpdateWithIncorrectDirOption(): void |
| 83 | + { |
| 84 | + @mkdir(self::$languageTestPath . self::$locale, 0777, true); |
| 85 | + $status = Services::commands()->run('lang:find', [ |
| 86 | + 'dir' => 'Services/Translation/NotExistFolder', |
| 87 | + ]); |
| 88 | + $this->assertSame($status, -1); |
| 89 | + $this->clearGeneratedFiles(); |
| 90 | + } |
| 91 | + |
| 92 | + public function testShowNewTranslation(): void |
| 93 | + { |
| 94 | + @mkdir(self::$languageTestPath . self::$locale, 0777, true); |
| 95 | + Services::commands()->run('lang:find', [ |
| 96 | + 'dir' => 'Services/Translation', |
| 97 | + 'show-new' => null, |
| 98 | + ]); |
| 99 | + $this->assertStringContainsString($this->getActualTableWithNewKeys(), $this->getStreamFilterBuffer()); |
| 100 | + $this->clearGeneratedFiles(); |
| 101 | + } |
| 102 | + |
| 103 | + private function getActualTranslationOneKeys(): array |
| 104 | + { |
| 105 | + return [ |
| 106 | + 'title' => 'TranslationOne.title', |
| 107 | + 'DESCRIPTION' => 'TranslationOne.DESCRIPTION', |
| 108 | + 'subTitle' => 'TranslationOne.subTitle', |
| 109 | + 'overflow_style' => 'TranslationOne.overflow_style', |
| 110 | + 'metaTags' => 'TranslationOne.metaTags', |
| 111 | + 'Copyright' => 'TranslationOne.Copyright', |
| 112 | + 'last_operation_success' => 'TranslationOne.last_operation_success', |
| 113 | + ]; |
| 114 | + } |
| 115 | + |
| 116 | + private function getActualTranslationThreeKeys(): array |
| 117 | + { |
| 118 | + return [ |
| 119 | + 'alerts' => [ |
| 120 | + 'created' => 'TranslationThree.alerts.created', |
| 121 | + 'failed_insert' => 'TranslationThree.alerts.failed_insert', |
| 122 | + 'CANCELED' => 'TranslationThree.alerts.CANCELED', |
| 123 | + 'missing_keys' => 'TranslationThree.alerts.missing_keys', |
| 124 | + 'Updated' => 'TranslationThree.alerts.Updated', |
| 125 | + 'DELETED' => 'TranslationThree.alerts.DELETED', |
| 126 | + ], |
| 127 | + 'formFields' => [ |
| 128 | + 'new' => [ |
| 129 | + 'name' => 'TranslationThree.formFields.new.name', |
| 130 | + 'TEXT' => 'TranslationThree.formFields.new.TEXT', |
| 131 | + 'short_tag' => 'TranslationThree.formFields.new.short_tag', |
| 132 | + ], |
| 133 | + 'edit' => [ |
| 134 | + 'name' => 'TranslationThree.formFields.edit.name', |
| 135 | + 'TEXT' => 'TranslationThree.formFields.edit.TEXT', |
| 136 | + 'short_tag' => 'TranslationThree.formFields.edit.short_tag', |
| 137 | + ], |
| 138 | + ], |
| 139 | + 'formErrors' => [ |
| 140 | + 'edit' => [ |
| 141 | + 'empty_name' => 'TranslationThree.formErrors.edit.empty_name', |
| 142 | + 'INVALID_TEXT' => 'TranslationThree.formErrors.edit.INVALID_TEXT', |
| 143 | + 'missing_short_tag' => 'TranslationThree.formErrors.edit.missing_short_tag', |
| 144 | + ], |
| 145 | + ], |
| 146 | + ]; |
| 147 | + } |
| 148 | + |
| 149 | + private function getActualTranslationFourKeys(): array |
| 150 | + { |
| 151 | + return [ |
| 152 | + 'dashed' => [ |
| 153 | + 'key-with-dash' => 'Translation-Four.dashed.key-with-dash', |
| 154 | + 'key-with-dash-two' => 'Translation-Four.dashed.key-with-dash-two', |
| 155 | + ], |
| 156 | + ]; |
| 157 | + } |
| 158 | + |
| 159 | + private function getActualTableWithNewKeys(): string |
| 160 | + { |
| 161 | + return <<<'TEXT_WRAP' |
| 162 | + +------------------+----------------------------------------------------+ |
| 163 | + | File | Key | |
| 164 | + +------------------+----------------------------------------------------+ |
| 165 | + | Translation-Four | Translation-Four.dashed.key-with-dash | |
| 166 | + | Translation-Four | Translation-Four.dashed.key-with-dash-two | |
| 167 | + | TranslationOne | TranslationOne.Copyright | |
| 168 | + | TranslationOne | TranslationOne.DESCRIPTION | |
| 169 | + | TranslationOne | TranslationOne.last_operation_success | |
| 170 | + | TranslationOne | TranslationOne.metaTags | |
| 171 | + | TranslationOne | TranslationOne.overflow_style | |
| 172 | + | TranslationOne | TranslationOne.subTitle | |
| 173 | + | TranslationOne | TranslationOne.title | |
| 174 | + | TranslationThree | TranslationThree.alerts.CANCELED | |
| 175 | + | TranslationThree | TranslationThree.alerts.DELETED | |
| 176 | + | TranslationThree | TranslationThree.alerts.Updated | |
| 177 | + | TranslationThree | TranslationThree.alerts.created | |
| 178 | + | TranslationThree | TranslationThree.alerts.failed_insert | |
| 179 | + | TranslationThree | TranslationThree.alerts.missing_keys | |
| 180 | + | TranslationThree | TranslationThree.formErrors.edit.INVALID_TEXT | |
| 181 | + | TranslationThree | TranslationThree.formErrors.edit.empty_name | |
| 182 | + | TranslationThree | TranslationThree.formErrors.edit.missing_short_tag | |
| 183 | + | TranslationThree | TranslationThree.formFields.edit.TEXT | |
| 184 | + | TranslationThree | TranslationThree.formFields.edit.name | |
| 185 | + | TranslationThree | TranslationThree.formFields.edit.short_tag | |
| 186 | + | TranslationThree | TranslationThree.formFields.new.TEXT | |
| 187 | + | TranslationThree | TranslationThree.formFields.new.name | |
| 188 | + | TranslationThree | TranslationThree.formFields.new.short_tag | |
| 189 | + +------------------+----------------------------------------------------+ |
| 190 | + TEXT_WRAP; |
| 191 | + } |
| 192 | + |
| 193 | + private function realizeAssertion(): void |
| 194 | + { |
| 195 | + $this->assertFileExists(self::$languageTestPath . self::$locale . '/TranslationOne.php'); |
| 196 | + $this->assertFileExists(self::$languageTestPath . self::$locale . '/TranslationThree.php'); |
| 197 | + |
| 198 | + $translationOneKeys = require self::$languageTestPath . self::$locale . '/TranslationOne.php'; |
| 199 | + $translationThreeKeys = require self::$languageTestPath . self::$locale . '/TranslationThree.php'; |
| 200 | + $translationFourKeys = require self::$languageTestPath . self::$locale . '/Translation-Four.php'; |
| 201 | + |
| 202 | + $this->assertSame($translationOneKeys, $this->getActualTranslationOneKeys()); |
| 203 | + $this->assertSame($translationThreeKeys, $this->getActualTranslationThreeKeys()); |
| 204 | + $this->assertSame($translationFourKeys, $this->getActualTranslationFourKeys()); |
| 205 | + } |
| 206 | + |
| 207 | + private function clearGeneratedFiles(): void |
| 208 | + { |
| 209 | + if (is_file(self::$languageTestPath . self::$locale . '/TranslationOne.php')) { |
| 210 | + unlink(self::$languageTestPath . self::$locale . '/TranslationOne.php'); |
| 211 | + } |
| 212 | + |
| 213 | + if (is_file(self::$languageTestPath . self::$locale . '/TranslationThree.php')) { |
| 214 | + unlink(self::$languageTestPath . self::$locale . '/TranslationThree.php'); |
| 215 | + } |
| 216 | + |
| 217 | + if (is_file(self::$languageTestPath . self::$locale . '/Translation-Four.php')) { |
| 218 | + unlink(self::$languageTestPath . self::$locale . '/Translation-Four.php'); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
0 commit comments