View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\entity_share_client\Service\StateInformationInterface;
use Drupal\node\NodeInterface;
use Drupal\user\UserInterface;
class MultilingualTest extends EntityShareClientFunctionalTestBase {
protected $stateInformation;
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected function setUp() {
parent::setUp();
$this->stateInformation = $this->container
->get('entity_share_client.state_information');
$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 testBasicPull() {
$this
->pullEveryChannels();
$this
->checkCreatedEntities();
}
public function testDefaultTranslationFirstPull() {
$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.');
}
public function testNonDefaultTranslationFirstPull() {
$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.');
}
public function testComparison() {
$this
->expectedState(StateInformationInterface::INFO_ID_NEW, StateInformationInterface::INFO_ID_NEW);
$this
->pullChannel('node_es_test_en');
$this->jsonapiHelper
->clearImportedEntities();
$this
->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_NEW_TRANSLATION);
$this
->pullChannel('node_es_test_fr');
$this->jsonapiHelper
->clearImportedEntities();
$this
->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_SYNCHRONIZED);
$node = $this
->loadEntity('node', 'es_test');
$node_translation = $node
->getTranslation('en');
$node_translation
->setChangedTime($this->faker
->unixTime());
$node_translation
->save();
$this
->expectedState(StateInformationInterface::INFO_ID_CHANGED, StateInformationInterface::INFO_ID_SYNCHRONIZED);
$this
->pullChannel('node_es_test_en');
$this->jsonapiHelper
->clearImportedEntities();
$this
->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_SYNCHRONIZED);
$node = $this
->loadEntity('node', 'es_test');
$node_translation = $node
->getTranslation('fr');
$node_translation
->setChangedTime($this->faker
->unixTime());
$node_translation
->save();
$this
->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_CHANGED);
$this
->pullChannel('node_es_test_fr');
$this->jsonapiHelper
->clearImportedEntities();
$this
->expectedState(StateInformationInterface::INFO_ID_SYNCHRONIZED, StateInformationInterface::INFO_ID_SYNCHRONIZED);
}
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) {
$json_data = $this
->getEntityJsonData('node_es_test_en', 'es_test');
$status = $this->stateInformation
->getStatusInfo($json_data);
$this
->assertEqual($status['info_id'], $en_expected_state);
$json_data = $this
->getEntityJsonData('node_es_test_fr', 'es_test');
$status = $this->stateInformation
->getStatusInfo($json_data);
$this
->assertEqual($status['info_id'], $fr_expected_state);
}
}