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

Allows to export default locale #577

Merged
merged 2 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 18 additions & 14 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_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
* name | Name | Name | Name | Prénom
* ...
*
* @param $columns
Expand All @@ -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));
}
}
4 changes: 1 addition & 3 deletions models/MessageImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -57,7 +57,6 @@ public function importData($results, $sessionKey = null)
}

$message->save();

} else {
$this->logSkipped($index, 'No code provided');
}
Expand All @@ -66,5 +65,4 @@ public function importData($results, $sessionKey = null)
}
}
}

}
17 changes: 16 additions & 1 deletion tests/unit/models/ExportMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -80,4 +81,18 @@ private function createMessages()
]);
}

}
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);
}
}
23 changes: 22 additions & 1 deletion 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']);
}
}