You are here

public function LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable in Lingotek Translation 3.7.x

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/LingotekSaveTargetDataTest.php \Drupal\Tests\lingotek\Functional\LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable()
  2. 4.0.x tests/src/Functional/LingotekSaveTargetDataTest.php \Drupal\Tests\lingotek\Functional\LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable()
  3. 3.0.x tests/src/Functional/LingotekSaveTargetDataTest.php \Drupal\Tests\lingotek\Functional\LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable()
  4. 3.1.x tests/src/Functional/LingotekSaveTargetDataTest.php \Drupal\Tests\lingotek\Functional\LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable()
  5. 3.2.x tests/src/Functional/LingotekSaveTargetDataTest.php \Drupal\Tests\lingotek\Functional\LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable()
  6. 3.3.x tests/src/Functional/LingotekSaveTargetDataTest.php \Drupal\Tests\lingotek\Functional\LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable()
  7. 3.4.x tests/src/Functional/LingotekSaveTargetDataTest.php \Drupal\Tests\lingotek\Functional\LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable()
  8. 3.5.x tests/src/Functional/LingotekSaveTargetDataTest.php \Drupal\Tests\lingotek\Functional\LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable()
  9. 3.6.x tests/src/Functional/LingotekSaveTargetDataTest.php \Drupal\Tests\lingotek\Functional\LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable()
  10. 3.8.x tests/src/Functional/LingotekSaveTargetDataTest.php \Drupal\Tests\lingotek\Functional\LingotekSaveTargetDataTest::testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable()

File

tests/src/Functional/LingotekSaveTargetDataTest.php, line 135

Class

LingotekSaveTargetDataTest
Tests the Lingotek content service saves data to entities correctly.

Namespace

Drupal\Tests\lingotek\Functional

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.
  $this
    ->createNode([
    'type' => 'article',
  ]);
  $node = Node::load(1);
  $title = $node
    ->getTranslation('en')
    ->getTitle();
  $body = $node
    ->getTranslation('en')->body->value;
  $this
    ->verbose($body);

  /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
  $translation_service = \Drupal::service('lingotek.content_translation');
  $es_data = [
    'title' => [
      0 => [
        'value' => 'es title',
      ],
    ],
    'body' => [
      0 => [
        'value' => 'es body',
      ],
    ],
  ];
  $node = $translation_service
    ->saveTargetData($node, 'es', $es_data);
  $this
    ->assertEqual('es body', $node
    ->getTranslation('es')->body->value, 'The body is translated if the field is translatable.');
  $this
    ->assertEqual($body, $node
    ->getTranslation('en')->body->value, 'The body in the original language is not overridden.');
  $this
    ->assertEqual('es title', $node
    ->getTranslation('es')
    ->getTitle(), 'The title in the translation is the one given.');
  $this
    ->assertEqual($title, $node
    ->getTranslation('en')
    ->getTitle(), 'The title in the original language is not overridden.');

  // 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.');
  $de_data = [
    'title' => [
      0 => [
        'value' => 'de title',
      ],
    ],
    'body' => [
      0 => [
        'value' => 'de body',
      ],
    ],
  ];

  // If the field is not translatable, the field is not there.
  $node = $translation_service
    ->saveTargetData($node, 'de', $de_data);
  $this
    ->assertEqual($body, $node
    ->getTranslation('de')->body->value, 'The body is not written if the field is not translatable.');
  $this
    ->assertEqual($body, $node
    ->getTranslation('en')->body->value, 'The body is not overridden if the field is not translatable.');
  $this
    ->assertEqual('de title', $node
    ->getTranslation('de')
    ->getTitle(), 'The title in the translation is the one given.');
  $this
    ->assertEqual($title, $node
    ->getTranslation('en')
    ->getTitle(), 'The title in the original language is not overridden.');
}