You are here

public function LingotekGetSourceDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable in Lingotek Translation 8

File

src/Tests/LingotekGetSourceDataTest.php, line 60

Class

LingotekGetSourceDataTest
Tests the Lingotek content service extract data from entities correctly.

Namespace

Drupal\lingotek\Tests

Code

public function testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable() {

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

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

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

  // Create a node.
  $node = $this
    ->createNode([
    'type' => 'article',
  ]);

  /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
  $translation_service = \Drupal::service('lingotek.content_translation');
  $serialized_node = $translation_service
    ->getSourceData($node);
  $this
    ->assertTrue(isset($serialized_node['body']), 'The body is included in the extracted data.');

  // Make the field as not translatable.
  $field
    ->setTranslatable(FALSE)
    ->save();
  $this
    ->assertTrue($field_storage
    ->isTranslatable(), 'Field storage is translatable.');
  $this
    ->assertFalse($field
    ->isTranslatable(), 'Field instance is not translatable.');

  // If the field is not translatable, the field is not there.
  $translation_service = \Drupal::service('lingotek.content_translation');
  $serialized_node = $translation_service
    ->getSourceData($node);
  $this
    ->assertFalse(isset($serialized_node['body']), 'The body is not included in the extracted data.');
}