View source
<?php
namespace Drupal\Tests\content_translation\Kernel;
use Drupal\content_translation\FieldTranslationSynchronizer;
use Drupal\KernelTests\KernelTestBase;
class ContentTranslationSyncUnitTest extends KernelTestBase {
protected $synchronizer;
protected $synchronized;
protected $columns;
protected $langcodes;
protected $cardinality;
protected $unchangedFieldValues;
protected static $modules = [
'language',
'content_translation',
];
protected function setUp() : void {
parent::setUp();
$this->synchronizer = new FieldTranslationSynchronizer($this->container
->get('entity_type.manager'), $this->container
->get('plugin.manager.field.field_type'));
$this->synchronized = [
'sync1',
'sync2',
];
$this->columns = array_merge($this->synchronized, [
'var1',
'var2',
]);
$this->langcodes = [
'en',
'it',
'fr',
'de',
'es',
];
$this->cardinality = 4;
$this->unchangedFieldValues = [];
foreach ($this->langcodes as $langcode) {
for ($delta = 0; $delta < $this->cardinality; $delta++) {
foreach ($this->columns as $column) {
$sync = in_array($column, $this->synchronized) && $langcode != $this->langcodes[0];
$value = $sync ? $this->unchangedFieldValues[$this->langcodes[0]][$delta][$column] : $langcode . '-' . $delta . '-' . $column;
$this->unchangedFieldValues[$langcode][$delta][$column] = $value;
}
}
}
}
public function testFieldSync() {
$sync_langcode = $this->langcodes[2];
$unchanged_items = $this->unchangedFieldValues[$sync_langcode];
$field_values = $this->unchangedFieldValues;
$item = [];
foreach ($this->columns as $column) {
$item[$column] = $this
->randomMachineName();
}
$field_values[$sync_langcode][] = $item;
$this->synchronizer
->synchronizeItems($field_values, $unchanged_items, $sync_langcode, $this->langcodes, $this->synchronized);
$result = TRUE;
foreach ($this->unchangedFieldValues as $langcode => $items) {
for ($delta = 0; $delta < $this->cardinality; $delta++) {
foreach ($this->columns as $column) {
$result = $result && $this->unchangedFieldValues[$langcode][$delta][$column] == $field_values[$langcode][$delta][$column];
}
}
foreach ($this->columns as $column) {
$result = $result && $field_values[$langcode][$delta][$column] == $field_values[$sync_langcode][$delta][$column];
}
}
$this
->assertTrue($result, 'A new item has been correctly synchronized.');
$sync_langcode = $this->langcodes[1];
$unchanged_items = $this->unchangedFieldValues[$sync_langcode];
$field_values = $this->unchangedFieldValues;
$sync_delta = mt_rand(0, count($field_values[$sync_langcode]) - 1);
unset($field_values[$sync_langcode][$sync_delta]);
$field_values[$sync_langcode] = array_values($field_values[$sync_langcode]);
$this->synchronizer
->synchronizeItems($field_values, $unchanged_items, $sync_langcode, $this->langcodes, $this->synchronized);
$result = TRUE;
foreach ($this->unchangedFieldValues as $langcode => $items) {
$new_delta = 0;
for ($delta = 0; $delta < $this->cardinality; $delta++) {
if ($delta != $sync_delta) {
foreach ($this->columns as $column) {
$result = $result && $this->unchangedFieldValues[$langcode][$delta][$column] == $field_values[$langcode][$new_delta][$column];
}
$new_delta++;
}
}
}
$this
->assertTrue($result, 'A removed item has been correctly synchronized.');
$sync_langcode = $this->langcodes[3];
$unchanged_items = $this->unchangedFieldValues[$sync_langcode];
$field_values = $this->unchangedFieldValues;
$field_values[$sync_langcode] = [];
foreach ($unchanged_items as $delta => $item) {
$new_delta = ($delta + 1) % $this->cardinality;
$field_values[$sync_langcode][$new_delta] = $item;
}
ksort($field_values[$sync_langcode]);
$this->synchronizer
->synchronizeItems($field_values, $unchanged_items, $sync_langcode, $this->langcodes, $this->synchronized);
$result = TRUE;
foreach ($field_values as $langcode => $items) {
for ($delta = 0; $delta < $this->cardinality; $delta++) {
foreach ($this->columns as $column) {
$value = $field_values[$langcode][$delta][$column];
if (in_array($column, $this->synchronized)) {
$result = $result && $field_values[$sync_langcode][$delta][$column] == $value;
}
else {
$old_delta = ($delta > 0 ? $delta : $this->cardinality) - 1;
$result = $result && $this->unchangedFieldValues[$langcode][$old_delta][$column] == $value;
}
}
}
}
$this
->assertTrue($result, 'Scrambled items have been correctly synchronized.');
}
public function testMultipleSyncedValues() {
$sync_langcode = $this->langcodes[1];
$unchanged_items = $this->unchangedFieldValues[$sync_langcode];
$delta_callbacks = [
function ($delta) {
return TRUE;
},
function ($delta) {
return $delta % 2 !== 0;
},
function ($delta) {
return $delta === 1 || $delta === 2;
},
function ($delta) {
return $delta === 0 || $delta === 3;
},
];
foreach ($delta_callbacks as $delta_callback) {
$field_values = $this->unchangedFieldValues;
for ($delta = 0; $delta < $this->cardinality; $delta++) {
if ($delta_callback($delta)) {
foreach ($this->columns as $column) {
if (in_array($column, $this->synchronized)) {
$field_values[$sync_langcode][$delta][$column] = $field_values[$sync_langcode][0][$column];
}
}
}
}
$changed_items = $field_values[$sync_langcode];
$this->synchronizer
->synchronizeItems($field_values, $unchanged_items, $sync_langcode, $this->langcodes, $this->synchronized);
foreach ($this->unchangedFieldValues as $langcode => $unchanged_items) {
for ($delta = 0; $delta < $this->cardinality; $delta++) {
foreach ($this->columns as $column) {
$value = $delta > 0 && $delta_callback($delta) && in_array($column, $this->synchronized) ? $changed_items[0][$column] : $unchanged_items[$delta][$column];
$this
->assertEquals($field_values[$langcode][$delta][$column], $value, "Item {$delta} column {$column} for langcode {$langcode} synced correctly");
}
}
}
}
}
public function testDifferingSyncedColumns() {
$sync_langcode = $this->langcodes[2];
$unchanged_items = $this->unchangedFieldValues[$sync_langcode];
$field_values = $this->unchangedFieldValues;
for ($delta = 0; $delta < $this->cardinality; $delta++) {
$index = $delta % 2 + 1;
$field_values[$sync_langcode][$delta]['sync' . $index] .= '-updated';
}
$changed_items = $field_values[$sync_langcode];
$this->synchronizer
->synchronizeItems($field_values, $unchanged_items, $sync_langcode, $this->langcodes, $this->synchronized);
foreach ($this->unchangedFieldValues as $langcode => $unchanged_items) {
for ($delta = 0; $delta < $this->cardinality; $delta++) {
foreach ($this->columns as $column) {
$expected_value = in_array($column, $this->synchronized) ? $changed_items[$delta][$column] : $this->unchangedFieldValues[$langcode][$delta][$column];
$this
->assertEquals($expected_value, $field_values[$langcode][$delta][$column], "Differing Item {$delta} column {$column} for langcode {$langcode} synced correctly");
}
}
}
}
}