View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\Component\Utility\UrlHelper;
use Drupal\node\NodeInterface;
class CliTest extends EntityShareClientFunctionalTestBase {
protected $stateInformation;
protected $cliService;
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->cliService = $this->container
->get('entity_share_client.cli');
$this
->postSetupFixture();
}
protected function getEntitiesDataArray() {
return [
'node' => [
'en' => [
'es_test_1' => $this
->getCompleteNodeInfos([
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
'es_test_2' => $this
->getCompleteNodeInfos([
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
],
],
];
}
public function testPull() {
$channel_infos = $this->remoteManager
->getChannelsInfos($this->remote);
$channel_info = array_shift($channel_infos);
$channel_url = $channel_info['url'];
$this->cliService
->pull($this->remote, $channel_url);
$this
->checkCreatedEntities();
}
public function testPullUpdate() {
$channel_infos = $this->remoteManager
->getChannelsInfos($this->remote);
$channel_info = array_shift($channel_infos);
$channel_url = $channel_info['url'];
$channel_url_uuid = $channel_info['url_uuid'];
$update_count = $this->cliService
->pullUpdates($this->remote, $channel_url, $channel_url_uuid, 'node');
$this->jsonapiHelper
->clearImportedEntities();
$this
->assertEquals(2, $update_count, 'On the first run, the two contents had been pulled.');
$this
->checkCreatedEntities();
$update_count = $this->cliService
->pullUpdates($this->remote, $channel_url, $channel_url_uuid, 'node');
$this->jsonapiHelper
->clearImportedEntities();
$this
->assertEquals(0, $update_count, 'On the second run, no content had been pulled.');
$node = $this
->loadEntity('node', 'es_test_1');
$node
->set('title', $this
->randomString());
$node
->setChangedTime($this->faker
->unixTime());
$node
->save();
$update_count = $this->cliService
->pullUpdates($this->remote, $channel_url, $channel_url_uuid, 'node');
$this->jsonapiHelper
->clearImportedEntities();
$this
->assertEquals(1, $update_count, 'On the third run, only the changed content had been pulled.');
$this
->checkCreatedEntities();
$update_count = $this->cliService
->pullUpdates($this->remote, $channel_url, $channel_url_uuid, 'node');
$this->jsonapiHelper
->clearImportedEntities();
$this
->assertEquals(0, $update_count, 'On the fourth run, no content had been pulled.');
}
protected function populateRequestService() {
parent::populateRequestService();
$selected_entities = [
'es_test_1',
];
$prepared_url = $this
->prepareUrlFilteredOnUuids($selected_entities, 'node_es_test_en');
$this->jsonapiHelper
->setRemote($this->remote);
$http_client = $this->remoteManager
->prepareJsonApiClient($this->remote);
$this
->discoverJsonApiEndpoints($http_client, $prepared_url);
$selected_entities = [
'es_test_1',
'es_test_2',
];
$prepared_url = $this
->prepareUrlFilteredOnUuids($selected_entities, 'node_es_test_en');
$this->jsonapiHelper
->setRemote($this->remote);
$http_client = $this->remoteManager
->prepareJsonApiClient($this->remote);
$this
->discoverJsonApiEndpoints($http_client, $prepared_url);
$channel_infos = $this->remoteManager
->getChannelsInfos($this->remote);
$channel_url_uuid = $channel_infos['node_es_test_en']['url_uuid'];
$offset = 0;
$parsed_url = UrlHelper::parse($channel_url_uuid);
$parsed_url['query']['page']['offset'] = $offset;
$query = UrlHelper::buildQuery($parsed_url['query']);
$revisions_url = $parsed_url['path'] . '?' . $query;
$this->jsonapiHelper
->setRemote($this->remote);
$http_client = $this->remoteManager
->prepareJsonApiClient($this->remote);
$this
->discoverJsonApiEndpoints($http_client, $revisions_url);
}
}