View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\entity_share\EntityShareUtility;
use Drupal\entity_share_client\Service\StateInformationInterface;
use Drupal\node\NodeInterface;
use Drupal\user\UserInterface;
class MultilingualTest extends EntityShareClientFunctionalTestBase {
protected $stateInformation;
protected $resourceTypeRepository;
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected function setUp() : void {
parent::setUp();
$this->stateInformation = $this->container
->get('entity_share_client.state_information');
$this->resourceTypeRepository = $this->container
->get('jsonapi.resource_type.repository');
$this
->postSetupFixture();
}
protected function getEntitiesDataArray() {
return [
'node' => [
'en' => [
'es_test' => $this
->getCompleteNodeInfos([
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
],
'fr' => [
'es_test' => $this
->getCompleteNodeInfos([
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
],
],
];
}
public function testMultilingualImport() {
$this
->pullEveryChannels();
$this
->checkCreatedEntities();
$this
->deleteAllEntities();
$this
->pullChannel('node_es_test_en');
$this
->pullChannel('node_es_test_fr');
$this
->checkCreatedEntities();
$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();
$this
->pullChannel('node_es_test_fr');
$this
->pullChannel('node_es_test_en');
$this
->checkCreatedEntities();
$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();
$this
->expectedState(StateInformationInterface::INFO_ID_NEW, StateInformationInterface::INFO_ID_NEW);
$this
->pullChannel('node_es_test_en');
$this->importService
->getRuntimeImportContext()
->clearImportedEntities();
$this
->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_NEW_TRANSLATION);
$this
->pullChannel('node_es_test_fr');
$this->importService
->getRuntimeImportContext()
->clearImportedEntities();
$this
->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_SYNCHRONIZED);
$this
->expectedState(StateInformationInterface::INFO_ID_CHANGED, StateInformationInterface::INFO_ID_SYNCHRONIZED, [
'en' => [
'fast_forward_changed_time',
],
]);
$this
->pullChannel('node_es_test_en');
$this->importService
->getRuntimeImportContext()
->clearImportedEntities();
$this
->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_SYNCHRONIZED);
$this
->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_CHANGED, [
'fr' => [
'fast_forward_changed_time',
],
]);
$this
->pullChannel('node_es_test_fr');
$this->importService
->getRuntimeImportContext()
->clearImportedEntities();
$this
->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_SYNCHRONIZED);
}
protected function deleteAllEntities() {
$entity_storage = $this->entityTypeManager
->getStorage('node');
$entities = $entity_storage
->loadByProperties();
if ($entities) {
foreach ($entities as $entity) {
$entity
->delete();
}
}
$this->entities = [];
}
protected function createChannel(UserInterface $user) {
parent::createChannel($user);
$channel_storage = $this->entityTypeManager
->getStorage('channel');
$channel = $channel_storage
->create([
'id' => 'node_es_test_fr',
'label' => $this
->randomString(),
'channel_entity_type' => 'node',
'channel_bundle' => 'es_test',
'channel_langcode' => 'fr',
'authorized_users' => [
$user
->uuid(),
],
]);
$channel
->save();
$this->channels[$channel
->id()] = $channel;
}
protected function expectedState($en_expected_state, $fr_expected_state, array $overrides = []) {
$json_data = $this
->getEntityJsonData('node_es_test_en', 'es_test');
if (!empty($overrides['en'])) {
$json_data['attributes'] = $this
->overrideJsonDataAttributes($overrides['en'], $json_data['attributes']);
}
$status = $this->stateInformation
->getStatusInfo($json_data);
$this
->assertEquals($status['info_id'], $en_expected_state);
$json_data = $this
->getEntityJsonData('node_es_test_fr', 'es_test');
if (!empty($overrides['fr'])) {
$json_data['attributes'] = $this
->overrideJsonDataAttributes($overrides['fr'], $json_data['attributes']);
}
$status = $this->stateInformation
->getStatusInfo($json_data);
$this
->assertEquals($status['info_id'], $fr_expected_state);
}
protected function overrideJsonDataAttributes(array $overrides, array $attributes) {
foreach ($overrides as $override_type) {
switch ($override_type) {
case 'fast_forward_changed_time':
$resource_type = $this->resourceTypeRepository
->get(static::$entityTypeId, static::$entityBundleId);
$changed_public_name = FALSE;
if ($resource_type
->hasField('changed')) {
$changed_public_name = $resource_type
->getPublicName('changed');
}
$changed = $attributes[$changed_public_name] ?? FALSE;
if ($changed !== FALSE) {
$changed_timestamp = EntityShareUtility::convertChangedTime($changed);
$attributes['changed'] = (string) ($changed_timestamp + 3600);
}
break;
}
}
return $attributes;
}
}