ContentTranslationTestBase.php 6.47 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\entity\Tests\ContentTranslationTestBase.
6 7
 */

8
namespace Drupal\content_translation\Tests;
9

10
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
11
use Drupal\language\Entity\ConfigurableLanguage;
12 13 14
use Drupal\simpletest\WebTestBase;

/**
15
 * Base class for content translation tests.
16
 */
17
abstract class ContentTranslationTestBase extends WebTestBase {
18

19 20 21 22 23 24 25
  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('text');

26 27 28 29 30
  /**
   * The entity type being tested.
   *
   * @var string
   */
31
  protected $entityTypeId = 'entity_test_mul';
32 33 34 35 36 37 38 39 40

  /**
   * The bundle being tested.
   *
   * @var string
   */
  protected $bundle;

  /**
41
   * The added languages.
42 43 44 45 46 47 48 49
   *
   * @var array
   */
  protected $langcodes;

  /**
   * The account to be used to test translation operations.
   *
50
   * @var \Drupal\user\UserInterface
51 52 53 54 55 56
   */
  protected $translator;

  /**
   * The account to be used to test multilingual entity editing.
   *
57
   * @var \Drupal\user\UserInterface
58 59 60 61 62 63
   */
  protected $editor;

  /**
   * The account to be used to test access to both workflows.
   *
64
   * @var \Drupal\user\UserInterface
65 66 67 68 69 70 71 72 73 74 75 76 77
   */
  protected $administrator;

  /**
   * The name of the field used to test translation.
   *
   * @var string
   */
  protected $fieldName;

  /**
   * The translation controller for the current entity type.
   *
78
   * @var \Drupal\content_translation\ContentTranslationHandlerInterface
79 80 81
   */
  protected $controller;

82
  protected function setUp() {
83 84 85 86 87 88 89 90
    parent::setUp();

    $this->setupLanguages();
    $this->setupBundle();
    $this->enableTranslation();
    $this->setupUsers();
    $this->setupTestFields();

91
    $this->controller = \Drupal::entityManager()->getHandler($this->entityTypeId, 'translation');
92 93 94 95

    // Rebuild the container so that the new languages are picked up by services
    // that hold a list of languages.
    $this->rebuildContainer();
96 97 98
  }

  /**
99
   * Adds additional languages.
100 101 102 103
   */
  protected function setupLanguages() {
    $this->langcodes = array('it', 'fr');
    foreach ($this->langcodes as $langcode) {
104
      ConfigurableLanguage::createFromLangcode($langcode)->save();
105
    }
106
    array_unshift($this->langcodes, \Drupal::languageManager()->getDefaultLanguage()->getId());
107 108 109 110 111 112
  }

  /**
   * Returns an array of permissions needed for the translator.
   */
  protected function getTranslatorPermissions() {
113
    return array_filter(array($this->getTranslatePermission(), 'create content translations', 'update content translations', 'delete content translations'));
114 115 116 117 118 119
  }

  /**
   * Returns the translate permissions for the current entity and bundle.
   */
  protected function getTranslatePermission() {
120 121 122
    $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
    if ($permission_granularity = $entity_type->getPermissionGranularity()) {
      return $permission_granularity == 'bundle' ? "translate {$this->bundle} {$this->entityTypeId}" : "translate {$this->entityTypeId}";
123 124 125 126 127 128 129 130 131 132 133
    }
  }

  /**
   * Returns an array of permissions needed for the editor.
   */
  protected function getEditorPermissions() {
    // Every entity-type-specific test needs to define these.
    return array();
  }

134 135 136 137 138 139 140
  /**
   * Returns an array of permissions needed for the administrator.
   */
  protected function getAdministratorPermissions() {
    return array_merge($this->getEditorPermissions(), $this->getTranslatorPermissions(), array('administer content translation'));
  }

141 142 143 144 145 146
  /**
   * Creates and activates translator, editor and admin users.
   */
  protected function setupUsers() {
    $this->translator = $this->drupalCreateUser($this->getTranslatorPermissions(), 'translator');
    $this->editor = $this->drupalCreateUser($this->getEditorPermissions(), 'editor');
147
    $this->administrator = $this->drupalCreateUser($this->getAdministratorPermissions(), 'administrator');
148 149 150 151 152 153 154 155
    $this->drupalLogin($this->translator);
  }

  /**
   * Creates or initializes the bundle date if needed.
   */
  protected function setupBundle() {
    if (empty($this->bundle)) {
156
      $this->bundle = $this->entityTypeId;
157 158 159 160 161 162 163 164 165
    }
  }

  /**
   * Enables translation for the current entity type and bundle.
   */
  protected function enableTranslation() {
    // Enable translation for the current entity type and ensure the change is
    // picked up.
166
    \Drupal::service('content_translation.manager')->setEnabled($this->entityTypeId, $this->bundle, TRUE);
167
    drupal_static_reset();
168
    \Drupal::entityManager()->clearCachedDefinitions();
169
    \Drupal::service('router.builder')->rebuild();
170 171 172 173 174 175
  }

  /**
   * Creates the test fields.
   */
  protected function setupTestFields() {
176 177
    if (empty($this->fieldName)) {
      $this->fieldName = 'field_test_et_ui_test';
178
    }
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    entity_create('field_storage_config', array(
      'field_name' => $this->fieldName,
      'type' => 'string',
      'entity_type' => $this->entityTypeId,
      'cardinality' => 1,
    ))->save();
    entity_create('field_config', array(
      'entity_type' => $this->entityTypeId,
      'field_name' => $this->fieldName,
      'bundle' => $this->bundle,
      'label' => 'Test translatable text-field',
    ))->save();
    entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
      ->setComponent($this->fieldName, array(
        'type' => 'string_textfield',
        'weight' => 0,
      ))
      ->save();
197 198 199 200 201 202 203 204 205 206 207 208 209
  }

  /**
   * Creates the entity to be translated.
   *
   * @param array $values
   *   An array of initial values for the entity.
   * @param string $langcode
   *   The initial language code of the entity.
   * @param string $bundle_name
   *   (optional) The entity bundle, if the entity uses bundles. Defaults to
   *   NULL. If left NULL, $this->bundle will be used.
   *
210
   * @return string
211 212 213 214 215
   *   The entity id.
   */
  protected function createEntity($values, $langcode, $bundle_name = NULL) {
    $entity_values = $values;
    $entity_values['langcode'] = $langcode;
216 217
    $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
    if ($bundle_key = $entity_type->getKey('bundle')) {
218
      $entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
219
    }
220
    $controller = $this->container->get('entity.manager')->getStorage($this->entityTypeId);
221
    if (!($controller instanceof SqlContentEntityStorage)) {
222 223 224 225 226 227
      foreach ($values as $property => $value) {
        if (is_array($value)) {
          $entity_values[$property] = array($langcode => $value);
        }
      }
    }
228
    $entity = entity_create($this->entityTypeId, $entity_values);
229 230 231 232 233
    $entity->save();
    return $entity->id();
  }

}