EntityReferenceFieldTranslatedReferenceViewTest.php 9.08 KB
Newer Older
1 2
<?php

3
namespace Drupal\field\Tests\EntityReference;
4

5
use Drupal\field\Entity\FieldConfig;
6 7 8
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\simpletest\WebTestBase;
9
use Drupal\field\Entity\FieldStorageConfig;
10 11 12 13 14 15 16

/**
 * Tests the translation of entity reference field display on nodes.
 *
 * @group entity_reference
 */
class EntityReferenceFieldTranslatedReferenceViewTest extends WebTestBase {
17 18 19 20 21 22 23 24

  /**
   * Flag indicating whether the field is translatable.
   *
   * @var bool
   */
  protected $translatable = TRUE;

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 62 63 64 65 66 67 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 98 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
  /**
   * The langcode of the source language.
   *
   * @var string
   */
  protected $baseLangcode = 'en';

  /**
   * Target langcode for translation.
   *
   * @var string
   */
  protected $translateToLangcode = 'hu';

  /**
   * The test entity type name.
   *
   * @var string
   */
  protected $testEntityTypeName = 'node';

  /**
   * Entity type which have the entity reference field.
   *
   * @var \Drupal\node\Entity\NodeType
   */
  protected $referrerType;

  /**
   * Entity type which can be referenced.
   *
   * @var \Drupal\node\Entity\NodeType
   */
  protected $referencedType;

  /**
   * The referrer entity.
   *
   * @var \Drupal\node\Entity\Node
   */
  protected $referrerEntity;

  /**
   * The entity to refer.
   *
   * @var \Drupal\node\Entity\Node
   */
  protected $referencedEntityWithoutTranslation;

  /**
   * The entity to refer.
   *
   * @var \Drupal\node\Entity\Node
   */
  protected $referencedEntityWithTranslation;

  /**
   * The machine name of the entity reference field.
   *
   * @var string
   */
  protected $referenceFieldName = 'test_reference_field';

  /**
   * The label of the untranslated referenced entity, used in assertions.
   *
   * @var string
   */
  protected $labelOfNotTranslatedReference;

  /**
   * The original label of the referenced entity, used in assertions.
   *
   * @var string
   */
  protected $originalLabel;

  /**
   * The translated label of the referenced entity, used in assertions.
   *
   * @var string
   */
  protected $translatedLabel;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'language',
    'content_translation',
    'node',
  );

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

    $this->labelOfNotTranslatedReference = $this->randomMachineName();
    $this->originalLabel = $this->randomMachineName();
    $this->translatedLabel = $this->randomMachineName();

    $this->setUpLanguages();
128 129 130 131 132

    // We setup languages, so we need to ensure that the language manager
    // and language path processor is updated.
    $this->rebuildContainer();

133 134 135 136 137 138 139
    $this->setUpContentTypes();
    $this->enableTranslation();
    $this->setUpEntityReferenceField();
    $this->createContent();
  }

  /**
140
   * Tests if the entity is displayed in an entity reference field.
141
   */
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  public function testEntityReferenceDisplay() {
    // Create a translated referrer entity.
    $this->referrerEntity = $this->createReferrerEntity();
    $this->assertEntityReferenceDisplay();

    // Disable translation for referrer content type.
    $this->drupalLogin($this->rootUser);
    $this->drupalPostForm('admin/config/regional/content-language', ['settings[node][referrer][translatable]' => FALSE], t('Save configuration'));

    // Create a referrer entity without translation.
    $this->referrerEntity = $this->createReferrerEntity(FALSE);
    $this->assertEntityReferenceDisplay();
  }

  /**
   * Assert entity reference display.
   */
  protected function assertEntityReferenceDisplay() {
160 161
    $url = $this->referrerEntity->urlInfo();
    $translation_url = $this->referrerEntity->urlInfo('canonical', ['language' => ConfigurableLanguage::load($this->translateToLangcode)]);
162

163
    $this->drupalGet($url);
164 165 166
    $this->assertText($this->labelOfNotTranslatedReference, 'The label of not translated reference is displayed.');
    $this->assertText($this->originalLabel, 'The default label of translated reference is displayed.');
    $this->assertNoText($this->translatedLabel, 'The translated label of translated reference is not displayed.');
167
    $this->drupalGet($translation_url);
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
    $this->assertText($this->labelOfNotTranslatedReference, 'The label of not translated reference is displayed.');
    $this->assertNoText($this->originalLabel, 'The default label of translated reference is not displayed.');
    $this->assertText($this->translatedLabel, 'The translated label of translated reference is displayed.');
  }

  /**
   * Adds additional languages.
   */
  protected function setUpLanguages() {
    ConfigurableLanguage::createFromLangcode($this->translateToLangcode)->save();
  }

  /**
   * Creates a test subject contents, with translation.
   */
  protected function createContent() {
    $this->referencedEntityWithTranslation = $this->createReferencedEntityWithTranslation();
    $this->referencedEntityWithoutTranslation = $this->createNotTranslatedReferencedEntity();
  }

  /**
   * Enables translations where it needed.
   */
  protected function enableTranslation() {
    // Enable translation for the entity types and ensure the change is picked
    // up.
194 195
    \Drupal::service('content_translation.manager')->setEnabled($this->testEntityTypeName, $this->referrerType->id(), TRUE);
    \Drupal::service('content_translation.manager')->setEnabled($this->testEntityTypeName, $this->referencedType->id(), TRUE);
196 197 198
    drupal_static_reset();
    \Drupal::entityManager()->clearCachedDefinitions();
    \Drupal::service('router.builder')->rebuild();
199
    \Drupal::service('entity.definition_update_manager')->applyUpdates();
200 201 202 203 204 205
  }

  /**
   * Adds term reference field for the article content type.
   */
  protected function setUpEntityReferenceField() {
206
    FieldStorageConfig::create(array(
207 208 209 210
      'field_name' => $this->referenceFieldName,
      'entity_type' => $this->testEntityTypeName,
      'type' => 'entity_reference',
      'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
211
      'translatable' => $this->translatable,
212 213 214 215 216 217 218 219 220
      'settings' => array(
        'allowed_values' => array(
          array(
            'target_type' => $this->testEntityTypeName,
          ),
        ),
      ),
    ))->save();

221
    FieldConfig::create([
222
      'field_name' => $this->referenceFieldName,
223
      'bundle' => $this->referrerType->id(),
224
      'entity_type' => $this->testEntityTypeName,
225
    ])
226
      ->save();
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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    entity_get_form_display($this->testEntityTypeName, $this->referrerType->id(), 'default')
      ->setComponent($this->referenceFieldName, array(
        'type' => 'entity_reference_autocomplete',
      ))
      ->save();
    entity_get_display($this->testEntityTypeName, $this->referrerType->id(), 'default')
      ->setComponent($this->referenceFieldName, array(
        'type' => 'entity_reference_label',
      ))
      ->save();
  }

  /**
   * Create content types.
   */
  protected function setUpContentTypes() {
    $this->referrerType = $this->drupalCreateContentType(array(
        'type' => 'referrer',
        'name' => 'Referrer',
      ));
    $this->referencedType = $this->drupalCreateContentType(array(
        'type' => 'referenced_page',
        'name' => 'Referenced Page',
      ));
  }

  /**
   * Create a referenced entity with a translation.
   */
  protected function createReferencedEntityWithTranslation() {
    /** @var \Drupal\node\Entity\Node $node */
    $node = entity_create($this->testEntityTypeName, array(
      'title' => $this->originalLabel,
      'type' => $this->referencedType->id(),
      'description' => array(
        'value' => $this->randomMachineName(),
        'format' => 'basic_html',
      ),
      'langcode' => $this->baseLangcode,
    ));
    $node->save();
    $node->addTranslation($this->translateToLangcode, array(
      'title' => $this->translatedLabel,
    ));
    $node->save();

    return $node;
  }


  /**
   * Create the referenced entity.
   */
  protected function createNotTranslatedReferencedEntity() {
    /** @var \Drupal\node\Entity\Node $node */
    $node = entity_create($this->testEntityTypeName, array(
      'title' => $this->labelOfNotTranslatedReference,
      'type' => $this->referencedType->id(),
      'description' => array(
        'value' => $this->randomMachineName(),
        'format' => 'basic_html',
      ),
      'langcode' => $this->baseLangcode,
    ));
    $node->save();

    return $node;
  }

  /**
   * Create the referrer entity.
   */
299
  protected function createReferrerEntity($translatable = TRUE) {
300 301 302 303 304 305 306 307 308 309 310 311 312 313
    /** @var \Drupal\node\Entity\Node $node */
    $node = entity_create($this->testEntityTypeName, array(
      'title' => $this->randomMachineName(),
      'type' => $this->referrerType->id(),
      'description' => array(
        'value' => $this->randomMachineName(),
        'format' => 'basic_html',
      ),
      $this->referenceFieldName => array(
        array('target_id' => $this->referencedEntityWithTranslation->id()),
        array('target_id' => $this->referencedEntityWithoutTranslation->id()),
      ),
      'langcode' => $this->baseLangcode,
    ));
314 315 316
    if ($translatable) {
      $node->addTranslation($this->translateToLangcode, $node->toArray());
    }
317 318 319 320 321 322
    $node->save();

    return $node;
  }

}