Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export and Import Message::DEFAULT_LOCALE #431

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions models/MessageExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
class MessageExport extends ExportModel
{
const CODE_COLUMN_NAME = 'code';
const DEFAULT_LOCALE_COLUMN_NAME = 'default';
munxar marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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 | Item Name | Name | Name | Prénom
* ...
*
* @param $columns
Expand All @@ -21,6 +22,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
Expand All @@ -36,8 +38,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));
}

}
10 changes: 4 additions & 6 deletions models/MessageImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/models/ExportMessageTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace RainLab\Translate\Tests\Unit\Models;

use PluginTestCase;
use RainLab\Translate\Models\Locale;
use RainLab\Translate\Models\Message;
use RainLab\Translate\Models\MessageExport;

Expand Down Expand Up @@ -70,6 +71,21 @@ public function testCanHandleNonExistingColumns()
$this->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([
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/models/ImportMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}