diff --git a/models/MessageExport.php b/models/MessageExport.php index cf2b8cb6..65bd22c0 100644 --- a/models/MessageExport.php +++ b/models/MessageExport.php @@ -9,10 +9,10 @@ class MessageExport extends ExportModel /** * 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 | x | en | de | fr + * ---------------------------------------------- + * title | title | Title | Titel | Titre + * item.name | Item Name | Name | Name | Prénom * ... * * @param $columns @@ -21,6 +21,7 @@ class MessageExport extends ExportModel */ public function exportData($columns, $sessionKey = null) { + return Message::all()->map(function($message) use($columns) { $data = $message->message_data; // add code to data to simplify algorithm @@ -36,8 +37,11 @@ public function exportData($columns, $sessionKey = null) 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)); + // code, default column + all existing locales + return array_merge([ + self::CODE_COLUMN_NAME => self::CODE_COLUMN_NAME, + Message::DEFAULT_LOCALE => Message::DEFAULT_LOCALE, + ], Locale::lists(self::CODE_COLUMN_NAME, self::CODE_COLUMN_NAME)); } + } diff --git a/models/MessageImport.php b/models/MessageImport.php index 9895b8a6..6176007e 100644 --- a/models/MessageImport.php +++ b/models/MessageImport.php @@ -38,14 +38,11 @@ public function importData($results, $sessionKey = null) $message = Message::firstOrNew(['code' => $code]); - // create empty array, if $message is new - $message->message_data = $message->message_data ?: []; - - if(!isset($message->message_data[Message::DEFAULT_LOCALE])) { + // write default if not existing + if(!isset($result[Message::DEFAULT_LOCALE])) { $result[Message::DEFAULT_LOCALE] = $code; } - - $message->message_data = array_merge($message->message_data, $result); + $message->message_data = array_merge($message->message_data ?: [], $result); if ($message->exists) { $this->logUpdated(); @@ -57,6 +54,7 @@ public function importData($results, $sessionKey = null) $this->logSkipped($index, 'no code provided'); } } catch (\Exception $exception) { + echo $exception->getMessage(); $this->logError($index, $exception->getMessage()); } } diff --git a/tests/unit/models/ExportMessageTest.php b/tests/unit/models/ExportMessageTest.php index 41c88d99..6330063d 100644 --- a/tests/unit/models/ExportMessageTest.php +++ b/tests/unit/models/ExportMessageTest.php @@ -1,6 +1,7 @@ assertEquals($expected, $result); } + 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 => Message::DEFAULT_LOCALE, + 'en' => 'en', + 'de' => 'de', + ], $columns); + } + private function createMessages() { Message::create([ diff --git a/tests/unit/models/ImportMessageTest.php b/tests/unit/models/ImportMessageTest.php index 491eb5d7..a6932c4a 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); } + + 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']); + } } \ No newline at end of file diff --git a/updates/version.yaml b/updates/version.yaml index 66813e8f..887b9f52 100755 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -51,3 +51,4 @@ 1.4.0: Add importer and exporter for messages 1.4.1: Updated Hungarian translation. Added Arabic translation. Fixed issue where default texts are overwritten by import. Fixed issue where the language switcher for repeater fields would overlap with the first repeater row. 1.4.2: Add multilingual MediaFinder +1.4.3: Export and import messages default locale.