You are here

function EntityTranslationTest::testFieldDefinitions in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Tests/Entity/EntityTranslationTest.php \Drupal\system\Tests\Entity\EntityTranslationTest::testFieldDefinitions()

Check that field translatability is handled properly.

File

core/modules/system/src/Tests/Entity/EntityTranslationTest.php, line 656
Contains \Drupal\system\Tests\Entity\EntityTranslationTest.

Class

EntityTranslationTest
Tests entity translation functionality.

Namespace

Drupal\system\Tests\Entity

Code

function testFieldDefinitions() {

  // Check that field translatability can be altered to be enabled or disabled
  // in field definitions.
  $entity_type = 'entity_test_mulrev';
  $this->state
    ->set('entity_test.field_definitions.translatable', array(
    'name' => FALSE,
  ));
  $this->entityManager
    ->clearCachedFieldDefinitions();
  $definitions = $this->entityManager
    ->getBaseFieldDefinitions($entity_type);
  $this
    ->assertFalse($definitions['name']
    ->isTranslatable(), 'Field translatability can be disabled programmatically.');
  $this->state
    ->set('entity_test.field_definitions.translatable', array(
    'name' => TRUE,
  ));
  $this->entityManager
    ->clearCachedFieldDefinitions();
  $definitions = $this->entityManager
    ->getBaseFieldDefinitions($entity_type);
  $this
    ->assertTrue($definitions['name']
    ->isTranslatable(), 'Field translatability can be enabled programmatically.');

  // Check that field translatability is disabled by default.
  $base_field_definitions = EntityTestMulRev::baseFieldDefinitions($this->entityManager
    ->getDefinition($entity_type));
  $this
    ->assertTrue(!isset($base_field_definitions['id']->translatable), 'Translatability for the <em>id</em> field is not defined.');
  $this
    ->assertFalse($definitions['id']
    ->isTranslatable(), 'Field translatability is disabled by default.');

  // Check that entity id keys have the expect translatability.
  $translatable_fields = array(
    'id' => TRUE,
    'uuid' => TRUE,
    'revision_id' => TRUE,
    'type' => TRUE,
    'langcode' => FALSE,
  );
  foreach ($translatable_fields as $name => $translatable) {
    $this->state
      ->set('entity_test.field_definitions.translatable', array(
      $name => $translatable,
    ));
    $this->entityManager
      ->clearCachedFieldDefinitions();
    $message = format_string('Field %field cannot be translatable.', array(
      '%field' => $name,
    ));
    try {
      $this->entityManager
        ->getBaseFieldDefinitions($entity_type);
      $this
        ->fail($message);
    } catch (\LogicException $e) {
      $this
        ->pass($message);
    }
  }
}