Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\field\Tests\TranslationWebTest.
Angie Byron
committed
*/
namespace Drupal\field\Tests;
use Drupal\Component\Utility\Unicode;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ConfigurableLanguage;
Angie Byron
committed
/**
* Tests multilanguage fields logic that require a full environment.
*
* @group field
Angie Byron
committed
*/
class TranslationWebTest extends FieldTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('language', 'field_test', 'entity_test');
Angie Byron
committed
Dries Buytaert
committed
/**
* The name of the field to use in this test.
*
* @var string
*/
Alex Pott
committed
protected $fieldName;
Dries Buytaert
committed
/**
* The name of the entity type to use in this test.
*
* @var string
*/
Alex Pott
committed
protected $entityTypeId = 'entity_test_mulrev';
Dries Buytaert
committed
/**
* The field storage to use in this test.
Dries Buytaert
committed
*
* @var \Drupal\field\Entity\FieldStorageConfig
Dries Buytaert
committed
*/
protected $fieldStorage;
Dries Buytaert
committed
/**
* The field to use in this test.
Dries Buytaert
committed
*
* @var \Drupal\field\Entity\FieldConfig
Dries Buytaert
committed
*/
protected $field;
Dries Buytaert
committed
Alex Pott
committed
protected function setUp() {
Angie Byron
committed
parent::setUp();
Alex Pott
committed
$this->fieldName = Unicode::strtolower($this->randomMachineName() . '_field_name');
Angie Byron
committed
$field_storage = array(
Alex Pott
committed
'field_name' => $this->fieldName,
'entity_type' => $this->entityTypeId,
Angie Byron
committed
'type' => 'test_field',
'cardinality' => 4,
);
entity_create('field_storage_config', $field_storage)->save();
Alex Pott
committed
$this->fieldStorage = FieldStorageConfig::load($this->entityTypeId . '.' . $this->fieldName);
Angie Byron
committed
$field = array(
'field_storage' => $this->fieldStorage,
Alex Pott
committed
'bundle' => $this->entityTypeId,
Angie Byron
committed
);
entity_create('field_config', $field)->save();
Alex Pott
committed
$this->field = FieldConfig::load($this->entityTypeId . '.' . $field['bundle'] . '.' . $this->fieldName);
Angie Byron
committed
Alex Pott
committed
entity_get_form_display($this->entityTypeId, $this->entityTypeId, 'default')
->setComponent($this->fieldName)
Alex Pott
committed
->save();
Angie Byron
committed
for ($i = 0; $i < 3; ++$i) {
ConfigurableLanguage::create(array(
'id' => 'l' . $i,
'label' => $this->randomString(),
))->save();
Angie Byron
committed
}
}
/**
* Tests field translations when creating a new revision.
*/
function testFieldFormTranslationRevisions() {
$web_user = $this->drupalCreateUser(array('view test entity', 'administer entity_test content'));
Angie Byron
committed
$this->drupalLogin($web_user);
// Prepare the field translations.
Alex Pott
committed
field_test_entity_info_translatable($this->entityTypeId, TRUE);
$entity = entity_create($this->entityTypeId);
$available_langcodes = array_flip(array_keys($this->container->get('language_manager')->getLanguages()));
$field_name = $this->fieldStorage->getName();
Angie Byron
committed
// Store the field translations.
ksort($available_langcodes);
$entity->langcode->value = key($available_langcodes);
Angie Byron
committed
foreach ($available_langcodes as $langcode => $value) {
$translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
$translation->{$field_name}->value = $value + 1;
Angie Byron
committed
}
$entity->save();
Angie Byron
committed
// Create a new revision.
Angie Byron
committed
"{$field_name}[0][value]" => $entity->{$field_name}->value,
'revision' => TRUE,
);
$this->drupalPostForm($this->entityTypeId . '/manage/' . $entity->id() . '/edit', $edit, t('Save'));
Angie Byron
committed
// Check translation revisions.
$this->checkTranslationRevisions($entity->id(), $entity->getRevisionId(), $available_langcodes);
$this->checkTranslationRevisions($entity->id(), $entity->getRevisionId() + 1, $available_langcodes);
Angie Byron
committed
}
/**
* Check if the field translation attached to the entity revision identified
* by the passed arguments were correctly stored.
*/
private function checkTranslationRevisions($id, $revision_id, $available_langcodes) {
$field_name = $this->fieldStorage->getName();
Alex Pott
committed
$entity = entity_revision_load($this->entityTypeId, $revision_id);
Angie Byron
committed
foreach ($available_langcodes as $langcode => $value) {
$passed = $entity->getTranslation($langcode)->{$field_name}->value == $value + 1;
$this->assertTrue($passed, format_string('The @language translation for revision @revision was correctly stored', array('@language' => $langcode, '@revision' => $entity->getRevisionId())));