You are here

public function ContentTranslationSyncUnitTest::testMultipleSyncedValues in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/content_translation/tests/src/Kernel/ContentTranslationSyncUnitTest.php \Drupal\Tests\content_translation\Kernel\ContentTranslationSyncUnitTest::testMultipleSyncedValues()

Tests that items holding the same values are correctly synchronized.

File

core/modules/content_translation/tests/src/Kernel/ContentTranslationSyncUnitTest.php, line 176

Class

ContentTranslationSyncUnitTest
Tests the field synchronization logic.

Namespace

Drupal\Tests\content_translation\Kernel

Code

public function testMultipleSyncedValues() {
  $sync_langcode = $this->langcodes[1];
  $unchanged_items = $this->unchangedFieldValues[$sync_langcode];

  // Determine whether the unchanged values should be altered depending on
  // their delta.
  $delta_callbacks = [
    // Continuous field values: all values are equal.
    function ($delta) {
      return TRUE;
    },
    // Alternated field values: only the even ones are equal.
    function ($delta) {
      return $delta % 2 !== 0;
    },
    // Sparse field values: only the "middle" ones are equal.
    function ($delta) {
      return $delta === 1 || $delta === 2;
    },
    // Sparse field values: only the "extreme" ones are equal.
    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) {

          // The first item is always unchanged hence it is retained by the
          // synchronization process. The other ones are retained or synced
          // depending on the logic implemented by the delta callback and
          // whether it is a sync column or not.
          $value = $delta > 0 && $delta_callback($delta) && in_array($column, $this->synchronized) ? $changed_items[0][$column] : $unchanged_items[$delta][$column];
          $this
            ->assertEqual($field_values[$langcode][$delta][$column], $value, "Item {$delta} column {$column} for langcode {$langcode} synced correctly");
        }
      }
    }
  }
}