View source
<?php
namespace Drupal\content_translation\Tests;
use Drupal\Core\Entity\EntityInterface;
class ContentTranslationSyncImageTest extends ContentTranslationTestBase {
protected $cardinality;
protected $files;
public static $modules = array(
'language',
'content_translation',
'entity_test',
'image',
'field_ui',
);
protected function setUp() {
parent::setUp();
$this->files = $this
->drupalGetTestFiles('image');
}
protected function setupTestFields() {
$this->fieldName = 'field_test_et_ui_image';
$this->cardinality = 3;
entity_create('field_storage_config', array(
'field_name' => $this->fieldName,
'entity_type' => $this->entityTypeId,
'type' => 'image',
'cardinality' => $this->cardinality,
))
->save();
entity_create('field_config', array(
'entity_type' => $this->entityTypeId,
'field_name' => $this->fieldName,
'bundle' => $this->entityTypeId,
'label' => 'Test translatable image field',
'third_party_settings' => array(
'content_translation' => array(
'translation_sync' => array(
'file' => FALSE,
'alt' => 'alt',
'title' => 'title',
),
),
),
))
->save();
}
protected function getEditorPermissions() {
return array(
'administer entity_test_mul fields',
'administer languages',
'administer content translation',
);
}
function testImageFieldSync() {
$this
->drupalLogin($this->editor);
$this
->drupalGet('entity_test_mul/structure/' . $this->entityTypeId . '/fields/' . $this->entityTypeId . '.' . $this->entityTypeId . '.' . $this->fieldName);
$this
->assertFieldChecked('edit-third-party-settings-content-translation-translation-sync-alt');
$this
->assertFieldChecked('edit-third-party-settings-content-translation-translation-sync-title');
$edit = array(
'third_party_settings[content_translation][translation_sync][alt]' => FALSE,
'third_party_settings[content_translation][translation_sync][title]' => FALSE,
);
$this
->drupalPostForm(NULL, $edit, t('Save settings'));
$this
->drupalGet('admin/config/regional/content-language');
$this
->assertNoFieldChecked('edit-settings-entity-test-mul-entity-test-mul-columns-field-test-et-ui-image-alt');
$this
->assertNoFieldChecked('edit-settings-entity-test-mul-entity-test-mul-columns-field-test-et-ui-image-title');
$edit = array(
'settings[entity_test_mul][entity_test_mul][fields][field_test_et_ui_image]' => TRUE,
'settings[entity_test_mul][entity_test_mul][columns][field_test_et_ui_image][alt]' => TRUE,
'settings[entity_test_mul][entity_test_mul][columns][field_test_et_ui_image][title]' => TRUE,
);
$this
->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
$errors = $this
->xpath('//div[contains(@class, "messages--error")]');
$this
->assertFalse($errors, 'Settings correctly stored.');
$this
->assertFieldChecked('edit-settings-entity-test-mul-entity-test-mul-columns-field-test-et-ui-image-alt');
$this
->assertFieldChecked('edit-settings-entity-test-mul-entity-test-mul-columns-field-test-et-ui-image-title');
$this
->drupalLogin($this->translator);
$default_langcode = $this->langcodes[0];
$langcode = $this->langcodes[1];
$values = array(
'name' => $this
->randomMachineName(),
'user_id' => mt_rand(1, 128),
'langcode' => $default_langcode,
);
$entity = entity_create($this->entityTypeId, $values);
$values = array();
for ($delta = 0; $delta < $this->cardinality; $delta++) {
$index = $delta;
$field_values = array(
'uri' => $this->files[$index]->uri,
'uid' => \Drupal::currentUser()
->id(),
'status' => FILE_STATUS_PERMANENT,
);
$file = entity_create('file', $field_values);
$file
->save();
$fid = $file
->id();
$this->files[$index]->fid = $fid;
$item = array(
'target_id' => $fid,
'alt' => $default_langcode . '_' . $fid . '_' . $this
->randomMachineName(),
'title' => $default_langcode . '_' . $fid . '_' . $this
->randomMachineName(),
);
$entity->{$this->fieldName}[] = $item;
$values[$default_langcode][$fid] = $item;
}
$entity = $this
->saveEntity($entity);
$translation = $entity
->addTranslation($langcode);
for ($delta = 0; $delta < $this->cardinality - 1; $delta++) {
$index = ($delta + 1) % $this->cardinality;
$fid = $this->files[$index]->fid;
$item = array(
'target_id' => $fid,
'alt' => $langcode . '_' . $fid . '_' . $this
->randomMachineName(),
'title' => $langcode . '_' . $fid . '_' . $this
->randomMachineName(),
);
$translation->{$this->fieldName}[] = $item;
$values[$langcode][$fid] = $item;
}
$this->manager
->getTranslationMetadata($translation)
->setSource($default_langcode);
$entity = $this
->saveEntity($translation);
$translation = $entity
->getTranslation($langcode);
$assert = count($entity->{$this->fieldName}) == 2;
$this
->assertTrue($assert, 'One item correctly removed from the synchronized field values.');
$fids = array();
foreach ($entity->{$this->fieldName} as $delta => $item) {
$value = $values[$default_langcode][$item->target_id];
$source_item = $translation->{$this->fieldName}
->get($delta);
$assert = $item->target_id == $source_item->target_id && $item->alt == $value['alt'] && $item->title == $value['title'];
$this
->assertTrue($assert, format_string('Field item @fid has been successfully synchronized.', array(
'@fid' => $item->target_id,
)));
$fids[$item->target_id] = TRUE;
}
$removed_fid = $this->files[0]->fid;
$this
->assertTrue(!isset($fids[$removed_fid]), format_string('Field item @fid has been correctly removed.', array(
'@fid' => $removed_fid,
)));
$values[$langcode][$removed_fid] = array(
'target_id' => $removed_fid,
'alt' => $langcode . '_' . $removed_fid . '_' . $this
->randomMachineName(),
'title' => $langcode . '_' . $removed_fid . '_' . $this
->randomMachineName(),
);
$translation->{$this->fieldName}
->setValue(array_values($values[$langcode]));
$entity = $this
->saveEntity($translation);
$translation = $entity
->getTranslation($langcode);
$assert = count($entity->{$this->fieldName}
->getValue()) == 3;
$this
->assertTrue($assert, 'One item correctly added to the synchronized field values.');
foreach ($entity->{$this->fieldName} as $delta => $item) {
$fid_langcode = $item->target_id != $removed_fid ? $default_langcode : $langcode;
$value = $values[$fid_langcode][$item->target_id];
$source_item = $translation->{$this->fieldName}
->get($delta);
$assert = $item->target_id == $source_item->target_id && $item->alt == $value['alt'] && $item->title == $value['title'];
$this
->assertTrue($assert, format_string('Field item @fid has been successfully synchronized.', array(
'@fid' => $item->target_id,
)));
}
}
protected function saveEntity(EntityInterface $entity) {
$entity
->save();
$entity = entity_test_mul_load($entity
->id(), TRUE);
return $entity;
}
}