You are here

public function ContentTranslationSyncUnitTest::testFieldSync 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::testFieldSync()

Tests the field synchronization algorithm.

File

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

Class

ContentTranslationSyncUnitTest
Tests the field synchronization logic.

Namespace

Drupal\content_translation\Tests

Code

public function testFieldSync() {

  // Add a new item to the source items and check that its added to all the
  // translations.
  $sync_langcode = $this->langcodes[2];
  $unchanged_items = $this->unchangedFieldValues[$sync_langcode];
  $field_values = $this->unchangedFieldValues;
  $item = array();
  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) {

    // Check that the old values are still in place.
    for ($delta = 0; $delta < $this->cardinality; $delta++) {
      foreach ($this->columns as $column) {
        $result = $result && $this->unchangedFieldValues[$langcode][$delta][$column] == $field_values[$langcode][$delta][$column];
      }
    }

    // Check that the new item is available in all languages.
    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.');

  // Remove an item from the source items and check that its removed from all
  // the translations.
  $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]);

  // Renumber deltas to start from 0.
  $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;

    // Check that the old values are still in place.
    for ($delta = 0; $delta < $this->cardinality; $delta++) {

      // Skip the removed item.
      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.');

  // Move the items around in the source items and check that they are moved
  // in all the translations.
  $sync_langcode = $this->langcodes[3];
  $unchanged_items = $this->unchangedFieldValues[$sync_langcode];
  $field_values = $this->unchangedFieldValues;
  $field_values[$sync_langcode] = array();

  // Scramble the items.
  foreach ($unchanged_items as $delta => $item) {
    $new_delta = ($delta + 1) % $this->cardinality;
    $field_values[$sync_langcode][$new_delta] = $item;
  }

  // Renumber deltas to start from 0.
  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)) {

          // If we are dealing with a synchronize column the current value is
          // supposed to be the same of the source items.
          $result = $result && $field_values[$sync_langcode][$delta][$column] == $value;
        }
        else {

          // Otherwise the values should be unchanged.
          $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.');
}