You are here

public function MultilingualTest::testMultilingualImport in Entity Share 8.3

Test several scenarios of importing the multilingual entities.

File

modules/entity_share_client/tests/src/Functional/MultilingualTest.php, line 90

Class

MultilingualTest
General functional test class for multilingual scenarios.

Namespace

Drupal\Tests\entity_share_client\Functional

Code

public function testMultilingualImport() {

  // Test that it is possible to pull the same entity in several languages
  // during the same process.
  $this
    ->pullEveryChannels();
  $this
    ->checkCreatedEntities();
  $this
    ->deleteAllEntities();

  // Test pulling content in its default translation first.
  $this
    ->pullChannel('node_es_test_en');
  $this
    ->pullChannel('node_es_test_fr');
  $this
    ->checkCreatedEntities();

  /** @var \Drupal\node\NodeInterface $node */
  $node = $this
    ->loadEntity('node', 'es_test');
  $node_translation = $node
    ->getTranslation('en');
  $this
    ->assertTrue($node_translation
    ->isDefaultTranslation(), 'The node default translation is the same as the initial one as it had been pulled in its default language first.');
  $this
    ->deleteAllEntities();

  // Test pulling content NOT in its default translation first.
  $this
    ->pullChannel('node_es_test_fr');
  $this
    ->pullChannel('node_es_test_en');
  $this
    ->checkCreatedEntities();

  /** @var \Drupal\node\NodeInterface $node */
  $node = $this
    ->loadEntity('node', 'es_test');
  $node_translation = $node
    ->getTranslation('fr');
  $this
    ->assertTrue($node_translation
    ->isDefaultTranslation(), 'The node default translation has changed as it had been pulled in another language first.');
  $this
    ->deleteAllEntities();

  // Test state information.
  // 1: No import: en and fr channels data should indicate a new entity.
  $this
    ->expectedState(StateInformationInterface::INFO_ID_NEW, StateInformationInterface::INFO_ID_NEW);

  // 2: Import entity in en: en should indicate synchronized and fr should
  // indicate a new translation.
  $this
    ->pullChannel('node_es_test_en');
  $this->importService
    ->getRuntimeImportContext()
    ->clearImportedEntities();
  $this
    ->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_NEW_TRANSLATION);

  // 3: Import entity in fr: en and fr should indicate synchronized.
  $this
    ->pullChannel('node_es_test_fr');
  $this->importService
    ->getRuntimeImportContext()
    ->clearImportedEntities();
  $this
    ->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_SYNCHRONIZED);

  // 4: Rig 'changed' JSON data attribute of en translation (this emulates a
  // change on the client website): en should indicate changed and fr should
  // indicate synchronized.
  $this
    ->expectedState(StateInformationInterface::INFO_ID_CHANGED, StateInformationInterface::INFO_ID_SYNCHRONIZED, [
    'en' => [
      'fast_forward_changed_time',
    ],
  ]);

  // 5: Import entity in en: en and fr should indicate synchronized.
  $this
    ->pullChannel('node_es_test_en');
  $this->importService
    ->getRuntimeImportContext()
    ->clearImportedEntities();
  $this
    ->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_SYNCHRONIZED);

  // 6: Rig 'changed' JSON data attribute of fr translation (this emulates a
  // change on the client website): en should indicate synchronized and fr
  // should indicate changed.
  $this
    ->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_CHANGED, [
    'fr' => [
      'fast_forward_changed_time',
    ],
  ]);

  // 7: Import entity in fr: en and fr should indicate synchronized.
  $this
    ->pullChannel('node_es_test_fr');
  $this->importService
    ->getRuntimeImportContext()
    ->clearImportedEntities();
  $this
    ->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_SYNCHRONIZED);
}