From dc1bd59f2eb3ae1d86d77986d7eb110c2f0bedaf Mon Sep 17 00:00:00 2001 From: Samuell1 Date: Tue, 28 Apr 2020 09:12:03 +0200 Subject: [PATCH 1/2] Allow to export, import default locale --- models/MessageExport.php | 32 ++++++++++++++----------- models/MessageImport.php | 4 +--- tests/unit/models/ExportMessageTest.php | 17 ++++++++++++- tests/unit/models/ImportMessageTest.php | 23 +++++++++++++++++- 4 files changed, 57 insertions(+), 19 deletions(-) diff --git a/models/MessageExport.php b/models/MessageExport.php index 186d3099..d37aae05 100644 --- a/models/MessageExport.php +++ b/models/MessageExport.php @@ -5,14 +5,15 @@ class MessageExport extends ExportModel { const CODE_COLUMN_NAME = 'code'; + const DEFAULT_COLUMN_NAME = 'default'; /** * Exports the message data with each locale in a separate column. * - * code | en | de | fr - * ------------------------------- - * title | Title | Titel | Titre - * name | Name | Name | Prénom + * code | default | en | de | fr + * ---------------------------------------------- + * title | Title | Title | Titel | Titre + * item.name | Name | Name | Name | Prénom * ... * * @param $columns @@ -21,28 +22,31 @@ class MessageExport extends ExportModel */ public function exportData($columns, $sessionKey = null) { - return Message::all()->map(function($message) use($columns) { + return Message::all()->map(function ($message) use ($columns) { $data = $message->message_data; - // Add code to data to simplify algorithm $data[self::CODE_COLUMN_NAME] = $message->code; $result = []; - foreach ($columns as $column) { $result[$column] = isset($data[$column]) ? $data[$column] : ''; } - - return $result; + return $result; })->toArray(); } + /** + * getColumns + * + * code, default column + all existing locales + * + * @return array + */ public static function getColumns() { - // Code column + all existing locales - return array_merge( - [self::CODE_COLUMN_NAME => self::CODE_COLUMN_NAME], - Locale::lists(self::CODE_COLUMN_NAME, self::CODE_COLUMN_NAME) - ); + return array_merge([ + self::CODE_COLUMN_NAME => self::CODE_COLUMN_NAME, + Message::DEFAULT_LOCALE => self::DEFAULT_COLUMN_NAME, + ], Locale::lists(self::CODE_COLUMN_NAME, self::CODE_COLUMN_NAME)); } } diff --git a/models/MessageImport.php b/models/MessageImport.php index 57148475..e7909ad8 100644 --- a/models/MessageImport.php +++ b/models/MessageImport.php @@ -44,7 +44,7 @@ public function importData($results, $sessionKey = null) // Create empty array, if $message is new $message->message_data = $message->message_data ?: []; - if(!isset($message->message_data[Message::DEFAULT_LOCALE])) { + if (!isset($message->message_data[Message::DEFAULT_LOCALE])) { $result[Message::DEFAULT_LOCALE] = $code; } @@ -57,7 +57,6 @@ public function importData($results, $sessionKey = null) } $message->save(); - } else { $this->logSkipped($index, 'No code provided'); } @@ -66,5 +65,4 @@ public function importData($results, $sessionKey = null) } } } - } diff --git a/tests/unit/models/ExportMessageTest.php b/tests/unit/models/ExportMessageTest.php index 41c88d99..76262c9b 100644 --- a/tests/unit/models/ExportMessageTest.php +++ b/tests/unit/models/ExportMessageTest.php @@ -3,6 +3,7 @@ use PluginTestCase; use RainLab\Translate\Models\Message; use RainLab\Translate\Models\MessageExport; +use RainLab\Translate\Models\Locale; class ExportMessageTest extends PluginTestCase { @@ -80,4 +81,18 @@ private function createMessages() ]); } -} \ No newline at end of file + public function testGetColumns() + { + Locale::unguard(); + Locale::create(['code' => 'de', 'name' => 'German', 'is_enabled' => true]); + + $columns = MessageExport::getColumns(); + + $this->assertEquals([ + MessageExport::CODE_COLUMN_NAME => MessageExport::CODE_COLUMN_NAME, + Message::DEFAULT_LOCALE => MessageExport::DEFAULT_COLUMN_NAME, + 'en' => 'en', + 'de' => 'de', + ], $columns); + } +} diff --git a/tests/unit/models/ImportMessageTest.php b/tests/unit/models/ImportMessageTest.php index 491eb5d7..8b9c02ab 100644 --- a/tests/unit/models/ImportMessageTest.php +++ b/tests/unit/models/ImportMessageTest.php @@ -71,4 +71,25 @@ public function testMissingCodeIsSkipped() $this->assertEquals(true, $stats->hasMessages); $this->assertEquals(Message::count(), 0); } -} \ No newline at end of file + + public function testDefaultLocaleIsImported() + { + $messageImport = new MessageImport(); + $data = [ + ['code' => 'test.me', 'x' => 'foo bar', 'de' => 'Neu 2', 'en' => 'new 2'] + ]; + + $messageImport->importData($data); + + $stats = $messageImport->getResultStats(); + $this->assertEquals(1, $stats->created); + $this->assertEquals(0, $stats->updated); + $this->assertEquals(0, $stats->skippedCount); + $this->assertEquals(false, $stats->hasMessages); + $this->assertEquals(Message::count(), 1); + + $message = Message::where('code', 'test.me')->first(); + + $this->assertEquals('foo bar', $message->message_data['x']); + } +} From 3602248a009124514bc71f93c1d23ebf29990ae3 Mon Sep 17 00:00:00 2001 From: Samuell1 Date: Tue, 28 Apr 2020 09:24:12 +0200 Subject: [PATCH 2/2] Update MessageExport.php --- models/MessageExport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/MessageExport.php b/models/MessageExport.php index d37aae05..415e88d2 100644 --- a/models/MessageExport.php +++ b/models/MessageExport.php @@ -13,7 +13,7 @@ class MessageExport extends ExportModel * code | default | en | de | fr * ---------------------------------------------- * title | Title | Title | Titel | Titre - * item.name | Name | Name | Name | Prénom + * name | Name | Name | Name | Prénom * ... * * @param $columns