You are here

public function ContentTranslationSyncUnitTest::testMultipleSyncedValues in Zircon Profile 8

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

Tests that items holding the same values are correctly synchronized.

File

core/modules/content_translation/src/Tests/ContentTranslationSyncUnitTest.php, line 181
Contains \Drupal\content_translation\Tests\ContentTranslationSyncUnitTest.

Class

ContentTranslationSyncUnitTest
Tests the field synchronization logic.

Namespace

Drupal\content_translation\Tests

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 = array(
    // 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) {
          $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);
    $result = TRUE;
    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.
          $value = $delta > 0 && $delta_callback($delta) ? $changed_items[0][$column] : $unchanged_items[$delta][$column];
          $result = $result && $field_values[$langcode][$delta][$column] == $value;
        }
      }
    }
    $this
      ->assertTrue($result, 'Multiple synced items have been correctly synchronized.');
  }
}