ContentTranslationSyncUnitTest.php 9.5 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\entity\Tests\ContentTranslationSyncUnitTest.
6 7
 */

8
namespace Drupal\content_translation\Tests;
9

10
use Drupal\simpletest\KernelTestBase;
11
use Drupal\content_translation\FieldTranslationSynchronizer;
12 13

/**
14 15 16
 * Tests the field synchronization logic.
 *
 * @group content_translation
17
 */
18
class ContentTranslationSyncUnitTest extends KernelTestBase {
19 20 21 22

  /**
   * The synchronizer class to be tested.
   *
23
   * @var \Drupal\content_translation\FieldTranslationSynchronizer
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
   */
  protected $synchronizer;

  /**
   * The colums to be synchronized.
   *
   * @var array
   */
  protected $synchronized;

  /**
   * All the field colums.
   *
   * @var array
   */
  protected $columns;

  /**
   * The available language codes.
   *
   * @var array
   */
  protected $langcodes;

  /**
   * The field cardinality.
   *
   * @var integer
   */
  protected $cardinality;

  /**
   * The unchanged field values.
   *
   * @var array
   */
  protected $unchangedFieldValues;

62
  public static $modules = array('language', 'content_translation');
63 64 65 66

  protected function setUp() {
    parent::setUp();

67
    $this->synchronizer = new FieldTranslationSynchronizer($this->container->get('entity.manager'));
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    $this->synchronized = array('sync1', 'sync2');
    $this->columns = array_merge($this->synchronized, array('var1', 'var2'));
    $this->langcodes = array('en', 'it', 'fr', 'de', 'es');
    $this->cardinality = 4;
    $this->unchangedFieldValues = array();

    // Set up an initial set of values in the correct state, that is with
    // "synchronized" values being equal.
    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;
        }
      }
    }
  }

  /**
   * Tests the field synchronization algorithm.
   */
  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) {
98
      $item[$column] = $this->randomMachineName();
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    }
    $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.');
  }

  /**
   * Tests that items holding the same values are correctly synchronized.
   */
  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.');
    }
  }

  /**
   * Tests that one change in a synchronized column triggers a change in all columns.
   */
  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);

    $result = TRUE;
    foreach ($this->unchangedFieldValues as $langcode => $unchanged_items) {
      for ($delta = 0; $delta < $this->cardinality; $delta++) {
        foreach ($this->columns as $column) {
          $result = $result && ($field_values[$langcode][$delta][$column] == $changed_items[$delta][$column]);
        }
      }
    }
    $this->assertTrue($result, 'Differing synced columns have been correctly synchronized.');
  }

}