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

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

namespace Drupal\system\Tests\Entity;

10
use Drupal\Component\Utility\Unicode;
11
use Drupal\language\Entity\ConfigurableLanguage;
12
use Drupal\field\Entity\FieldConfig;
13 14 15 16 17 18

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

19 20 21 22 23 24 25 26 27 28 29 30
  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The available language codes.
   *
   * @var array
   */
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  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');

49
  protected function setUp() {
50 51
    parent::setUp();

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

54 55 56 57 58 59
    foreach (entity_test_entity_types() as $entity_type_id) {
      // The entity_test schema is installed by the parent.
      if ($entity_type_id != 'entity_test') {
        $this->installEntitySchema($entity_type_id);
      }
    }
60

61 62 63 64 65 66 67 68 69
    $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.
70
    $this->field_name = Unicode::strtolower($this->randomMachineName() . '_field_name');
71 72

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

75
    // Create field fields in all entity variations.
76
    foreach (entity_test_entity_types() as $entity_type) {
77
      entity_create('field_storage_config', array(
78
        'field_name' => $this->field_name,
79 80 81 82
        'entity_type' => $entity_type,
        'type' => 'text',
        'cardinality' => 4,
      ))->save();
83
      entity_create('field_config', array(
84 85 86
        'field_name' => $this->field_name,
        'entity_type' => $entity_type,
        'bundle' => $entity_type,
87
        'translatable' => TRUE,
88
      ))->save();
89
      $this->field[$entity_type] = FieldConfig::load($entity_type . '.' . $entity_type . '.' . $this->field_name);
90

91
      entity_create('field_storage_config', array(
92
        'field_name' => $this->untranslatable_field_name,
93 94 95 96
        'entity_type' => $entity_type,
        'type' => 'text',
        'cardinality' => 4,
      ))->save();
97
      entity_create('field_config', array(
98 99 100
        'field_name' => $this->untranslatable_field_name,
        'entity_type' => $entity_type,
        'bundle' => $entity_type,
101
        'translatable' => FALSE,
102 103 104 105
      ))->save();
    }

    // Create the default languages.
106
    $this->installConfig(array('language'));
107 108 109 110

    // Create test languages.
    $this->langcodes = array();
    for ($i = 0; $i < 3; ++$i) {
111
      $language = ConfigurableLanguage::create(array(
112
        'id' => 'l' . $i,
113
        'label' => $this->randomString(),
114 115
        'weight' => $i,
      ));
116
      $this->langcodes[$i] = $language->getId();
117
      $language->save();
118 119 120 121
    }
  }

  /**
122
   * Toggles field storage translatability.
123 124 125 126
   *
   * @param string $entity_type
   *   The type of the entity fields are attached to.
   */
127
  protected function toggleFieldTranslatability($entity_type, $bundle) {
128 129
    $fields = array($this->field_name, $this->untranslatable_field_name);
    foreach ($fields as $field_name) {
130 131 132 133 134 135
      $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.');
136
    }
137
    \Drupal::cache('entity')->deleteAll();
138 139 140
  }

}