summaryrefslogtreecommitdiffstats
path: root/core/modules/system/src/Tests/Entity/EntityLanguageTestBase.php
blob: 6aa3c912e8ccf97012476bf2ab070e3d1b18ff50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
128
129
130
131
132
133
134
135
136
137
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Entity\EntityLanguageTestBase.
 */

namespace Drupal\system\Tests\Entity;

use Drupal\Component\Utility\Unicode;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\field\Entity\FieldConfig;

/**
 * Base class for language-aware entity tests.
 */
abstract class EntityLanguageTestBase extends EntityUnitTestBase {

  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

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

  /**
   * The test field name.
   *
   * @var string
   */
  protected $field_name;

  /**
   * The untranslatable test field name.
   *
   * @var string
   */
  protected $untranslatable_field_name;

  public static $modules = array('language', 'entity_test');

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

    $this->languageManager = $this->container->get('language_manager');

    $this->installEntitySchema('entity_test_rev');
    $this->installEntitySchema('entity_test_mul');
    $this->installEntitySchema('entity_test_mulrev');

    $this->installConfig(array('language'));

    // Create the test field.
    entity_test_install();

    // Enable translations for the test entity type.
    $this->state->set('entity_test.translation', TRUE);

    // Create a translatable test field.
    $this->field_name = Unicode::strtolower($this->randomMachineName() . '_field_name');

    // Create an untranslatable test field.
    $this->untranslatable_field_name = Unicode::strtolower($this->randomMachineName() . '_field_name');

    // Create field fields in all entity variations.
    foreach (entity_test_entity_types() as $entity_type) {
      entity_create('field_storage_config', array(
        'field_name' => $this->field_name,
        'entity_type' => $entity_type,
        'type' => 'text',
        'cardinality' => 4,
      ))->save();
      entity_create('field_config', array(
        'field_name' => $this->field_name,
        'entity_type' => $entity_type,
        'bundle' => $entity_type,
        'translatable' => TRUE,
      ))->save();
      $this->field[$entity_type] = FieldConfig::load($entity_type . '.' . $entity_type . '.' . $this->field_name);

      entity_create('field_storage_config', array(
        'field_name' => $this->untranslatable_field_name,
        'entity_type' => $entity_type,
        'type' => 'text',
        'cardinality' => 4,
      ))->save();
      entity_create('field_config', array(
        'field_name' => $this->untranslatable_field_name,
        'entity_type' => $entity_type,
        'bundle' => $entity_type,
        'translatable' => FALSE,
      ))->save();
    }

    // Create the default languages.
    $this->installConfig(array('language'));

    // Create test languages.
    $this->langcodes = array();
    for ($i = 0; $i < 3; ++$i) {
      $language = ConfigurableLanguage::create(array(
        'id' => 'l' . $i,
        'label' => $this->randomString(),
        'weight' => $i,
      ));
      $this->langcodes[$i] = $language->getId();
      $language->save();
    }
  }

  /**
   * Toggles field storage translatability.
   *
   * @param string $entity_type
   *   The type of the entity fields are attached to.
   */
  protected function toggleFieldTranslatability($entity_type, $bundle) {
    $fields = array($this->field_name, $this->untranslatable_field_name);
    foreach ($fields as $field_name) {
      $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
      $translatable = !$field->isTranslatable();
      $field->set('translatable', $translatable);
      $field->save();
      $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
      $this->assertEqual($field->isTranslatable(), $translatable, 'Field translatability changed.');
    }
    \Drupal::cache('entity')->deleteAll();
  }

}