TranslationWebTest.php 4.12 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\field\Tests\TranslationWebTest.
 */

namespace Drupal\field\Tests;

10
use Drupal\language\Entity\ConfigurableLanguage;
11 12

/**
13 14 15
 * Tests multilanguage fields logic that require a full environment.
 *
 * @group field
16 17 18 19 20 21 22 23
 */
class TranslationWebTest extends FieldTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
24
  public static $modules = array('language', 'field_test', 'entity_test');
25

26 27 28 29 30 31 32 33 34 35 36 37
  /**
   * The name of the field to use in this test.
   *
   * @var string
   */
  protected $field_name;

  /**
   * The name of the entity type to use in this test.
   *
   * @var string
   */
38
  protected $entity_type = 'entity_test_mulrev';
39 40

  /**
41
   * The field storage to use in this test.
42
   *
43
   * @var \Drupal\field\Entity\FieldStorageConfig
44
   */
45
  protected $fieldStorage;
46 47 48 49

  /**
   * The field instance to use in this test.
   *
50
   * @var \Drupal\field\Entity\FieldInstanceConfig
51 52 53
   */
  protected $instance;

54
  protected function setUp() {
55 56
    parent::setUp();

57
    $this->field_name = drupal_strtolower($this->randomMachineName() . '_field_name');
58 59

    $field = array(
60 61
      'name' => $this->field_name,
      'entity_type' => $this->entity_type,
62 63 64 65
      'type' => 'test_field',
      'cardinality' => 4,
      'translatable' => TRUE,
    );
66 67
    entity_create('field_storage_config', $field)->save();
    $this->fieldStorage = entity_load('field_storage_config', $this->entity_type . '.' . $this->field_name);
68 69

    $instance = array(
70
      'field_storage' => $this->fieldStorage,
71
      'bundle' => $this->entity_type,
72
    );
73
    entity_create('field_instance_config', $instance)->save();
74
    $this->instance = entity_load('field_instance_config', $this->entity_type . '.' . $instance['bundle'] . '.' . $this->field_name);
75

76
    entity_get_form_display($this->entity_type, $this->entity_type, 'default')
77 78 79
      ->setComponent($this->field_name)
      ->save();

80
    for ($i = 0; $i < 3; ++$i) {
81
      ConfigurableLanguage::create(array(
82
        'id' => 'l' . $i,
83 84
        'label' => $this->randomString(),
      ))->save();
85 86 87 88 89 90 91
    }
  }

  /**
   * Tests field translations when creating a new revision.
   */
  function testFieldFormTranslationRevisions() {
92
    $web_user = $this->drupalCreateUser(array('view test entity', 'administer entity_test content'));
93 94 95 96
    $this->drupalLogin($web_user);

    // Prepare the field translations.
    field_test_entity_info_translatable($this->entity_type, TRUE);
97
    $entity = entity_create($this->entity_type);
98
    $available_langcodes = array_flip(array_keys($this->container->get('language_manager')->getLanguages()));
99
    $field_name = $this->fieldStorage->getName();
100 101

    // Store the field translations.
102
    ksort($available_langcodes);
103
    $entity->langcode->value = key($available_langcodes);
104
    foreach ($available_langcodes as $langcode => $value) {
105
      $entity->getTranslation($langcode)->{$field_name}->value = $value + 1;
106
    }
107
    $entity->save();
108 109

    // Create a new revision.
110
    $edit = array(
111 112
      'user_id' => 1,
      'name' => $this->randomMachineName(),
113
      "{$field_name}[0][value]" => $entity->{$field_name}->value,
114 115
      'revision' => TRUE,
    );
116
    $this->drupalPostForm($this->entity_type . '/manage/' . $entity->id(), $edit, t('Save'));
117 118

    // Check translation revisions.
119 120
    $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId(), $available_langcodes);
    $this->checkTranslationRevisions($entity->id(), $entity->getRevisionId() + 1, $available_langcodes);
121 122 123 124 125 126
  }

  /**
   * Check if the field translation attached to the entity revision identified
   * by the passed arguments were correctly stored.
   */
127
  private function checkTranslationRevisions($id, $revision_id, $available_langcodes) {
128
    $field_name = $this->fieldStorage->getName();
129
    $entity = entity_revision_load($this->entity_type, $revision_id);
130
    foreach ($available_langcodes as $langcode => $value) {
131 132
      $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())));
133 134 135
    }
  }
}