You are here

public function LingotekSettingsTabContentFormTest::testFieldsAreNotAvailableIfTranslatableEvenIfStorageIsTranslatable in Lingotek Translation 8

File

src/Tests/Form/LingotekSettingsTabContentFormTest.php, line 234

Class

LingotekSettingsTabContentFormTest
Tests the Lingotek content settings form.

Namespace

Drupal\lingotek\Tests\Form

Code

public function testFieldsAreNotAvailableIfTranslatableEvenIfStorageIsTranslatable() {

  // Enable translation for the current entity type and ensure the change is
  // picked up.
  ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
    ->setLanguageAlterable(TRUE)
    ->save();
  \Drupal::service('content_translation.manager')
    ->setEnabled('node', 'article', TRUE);
  drupal_static_reset();
  \Drupal::entityManager()
    ->clearCachedDefinitions();
  \Drupal::service('entity.definition_update_manager')
    ->applyUpdates();

  // Rebuild the container so that the new languages are picked up by services
  // that hold a list of languages.
  $this
    ->rebuildContainer();
  \Drupal::service('entity.definition_update_manager')
    ->applyUpdates();

  // Ensure field storage is translatable.
  $field_storage = FieldStorageConfig::loadByName('node', 'field_image');
  $field_storage
    ->setTranslatable(TRUE)
    ->save();

  // Ensure field instance is not translatable.
  $field = FieldConfig::loadByName('node', 'article', 'field_image');
  $field
    ->setTranslatable(FALSE)
    ->save();

  // Ensure changes were saved correctly.
  $field_storage = FieldStorageConfig::loadByName('node', 'field_image');
  $field = FieldConfig::loadByName('node', 'article', 'field_image');
  $this
    ->assertTrue($field_storage
    ->isTranslatable(), 'Field storage is translatable.');
  $this
    ->assertFalse($field
    ->isTranslatable(), 'Field instance is not translatable.');

  // Get the form and check the field is not available, even if the storage
  // is translatable.
  $this
    ->drupalGet('admin/lingotek/settings');
  $this
    ->assertNoFieldById('edit-node-article-fields-field-image', '', 'The image field is not present after marked as not translatable.');

  // Make the field translatable again.
  $field
    ->setTranslatable(TRUE)
    ->save();

  // If the field is translatable, the field is available again.
  $this
    ->drupalGet('admin/lingotek/settings');
  $this
    ->assertFieldById('edit-node-article-fields-field-image', '', 'The image field is present after marked as translatable.');
}