View source
<?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ContentLanguageSettings;
class LingotekGetSourceDataTest extends LingotekTestBase {
public static $modules = [
'node',
'image',
];
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this->rootUser);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
$this
->applyEntityUpdates();
$this
->saveLingotekContentTranslationSettingsForNodeTypes();
drupal_static_reset();
\Drupal::entityTypeManager()
->clearCachedDefinitions();
$this
->applyEntityUpdates();
$this
->rebuildContainer();
}
public function testFieldsAreNotExtractedIfNotTranslatableEvenIfStorageIsTranslatable() {
$field_storage = FieldStorageConfig::loadByName('node', 'body');
$field_storage
->setTranslatable(TRUE)
->save();
$field = FieldConfig::loadByName('node', 'article', 'body');
$field
->setTranslatable(TRUE)
->save();
$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.');
$node = $this
->createNode([
'type' => 'article',
]);
$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.');
$field
->setTranslatable(FALSE)
->save();
$this
->assertTrue($field_storage
->isTranslatable(), 'Field storage is translatable.');
$this
->assertFalse($field
->isTranslatable(), 'Field instance is not translatable.');
$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.');
}
public function testContentEntityMetadataIsIncluded() {
$node = $this
->createNode([
'type' => 'article',
]);
$translation_service = \Drupal::service('lingotek.content_translation');
$serialized_node = $translation_service
->getSourceData($node);
$this
->assertTrue(isset($serialized_node['_lingotek_metadata']), 'The Lingotek metadata is included in the extracted data.');
$this
->assertEqual('node', $serialized_node['_lingotek_metadata']['_entity_type_id'], 'Entity type id is included as metadata.');
$this
->assertEqual(1, $serialized_node['_lingotek_metadata']['_entity_id'], 'Entity id is included as metadata.');
$this
->assertEqual(1, $serialized_node['_lingotek_metadata']['_entity_revision'], 'Entity revision id is included as metadata.');
$node
->setNewRevision();
$node
->setTitle($this
->randomString(10));
$node
->save();
$serialized_node = $translation_service
->getSourceData($node);
$this
->assertTrue(isset($serialized_node['_lingotek_metadata']), 'The Lingotek metadata is included in the extracted data.');
$this
->assertEqual('node', $serialized_node['_lingotek_metadata']['_entity_type_id'], 'Entity type id is included as metadata.');
$this
->assertEqual(1, $serialized_node['_lingotek_metadata']['_entity_id'], 'Entity id is included as metadata.');
$this
->assertEqual(2, $serialized_node['_lingotek_metadata']['_entity_revision'], 'Entity revision id is included as metadata, and has changed.');
}
}