View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_cron\Unit;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\entity_share_client\Entity\Remote;
use Drupal\entity_share_cron\EntityShareCronService;
use Drupal\entity_share_cron\EntityShareCronServiceInterface;
class EntityShareCronServiceTest extends UnitTestCase {
const REMOTE_ID = 'test_remote';
const CHANNEL_ID = 'test_channel';
const CHANNEL_URL = 'https://example.com';
protected $service;
protected $remotesConfig = [
self::REMOTE_ID => [
'enabled' => TRUE,
'channels' => [
self::CHANNEL_ID => [
'enabled' => TRUE,
'url' => self::CHANNEL_URL,
'url_uuid' => self::CHANNEL_URL,
'operations' => [
'create' => TRUE,
'update' => TRUE,
],
],
],
],
];
protected $nodes = [];
protected $responseData = [
[
[
'type' => 'node--article',
'id' => 'node1',
],
],
[
[
'type' => 'node--article',
'id' => 'node2',
],
],
[
[
'type' => 'node--article',
'id' => 'node3',
],
[
'type' => 'node--article',
'id' => 'node4',
],
],
];
protected $responsePages;
protected $queue;
protected $requestCount;
public function testSynchronizationAllPages() {
$this->responsePages = 3;
$this->queue
->expects($this
->never())
->method('createItem');
$info = [
'url' => self::CHANNEL_URL,
];
$imported = $this->service
->sync(self::REMOTE_ID, self::CHANNEL_ID, $info);
$this
->assertCount(4, $imported);
$this
->assertContains($this->responseData[0][0], $imported);
$this
->assertContains($this->responseData[1][0], $imported);
$this
->assertContains($this->responseData[2][0], $imported);
$this
->assertContains($this->responseData[2][1], $imported);
}
public function testSynchronizationWithPageLimit() {
$this->responsePages = 3;
$expected_item = [
'remote_id' => self::REMOTE_ID,
'channel_id' => self::CHANNEL_ID,
'channel_info' => [
'url' => 'page_2',
],
];
$this->queue
->expects($this
->once())
->method('createItem')
->with($this
->equalTo($expected_item));
$info = [
'url' => self::CHANNEL_URL,
];
$imported = $this->service
->sync(self::REMOTE_ID, self::CHANNEL_ID, $info, 2);
$this
->assertCount(2, $imported);
$this
->assertContains($this->responseData[0][0], $imported);
$this
->assertContains($this->responseData[1][0], $imported);
}
public function testSynchronizationCreateOnly() {
$this->remotesConfig[self::REMOTE_ID]['channels'][self::CHANNEL_ID]['operations'] = [
'create' => TRUE,
'update' => FALSE,
];
$this->nodes[] = $this
->createEntity('node1');
$this->nodes[] = $this
->createEntity('node3');
$info = [
'url' => self::CHANNEL_URL,
];
$imported = $this->service
->sync(self::REMOTE_ID, self::CHANNEL_ID, $info);
$this
->assertCount(2, $imported);
$this
->assertContains($this->responseData[1][0], $imported);
$this
->assertContains($this->responseData[2][1], $imported);
}
public function testSynchronizationUpdateOnly() {
$this->remotesConfig[self::REMOTE_ID]['channels'][self::CHANNEL_ID]['operations'] = [
'create' => FALSE,
'update' => TRUE,
];
$this->nodes[] = $this
->createEntity('node1');
$this->nodes[] = $this
->createEntity('node3');
$info = [
'url' => self::CHANNEL_URL,
];
$imported = $this->service
->sync(self::REMOTE_ID, self::CHANNEL_ID, $info);
$this
->assertCount(2, $imported);
$this
->assertContains($this->responseData[0][0], $imported);
$this
->assertContains($this->responseData[2][0], $imported);
}
public function getResponseBody() {
$this->responsePages -= 1;
$json = [
'data' => $this->responseData[$this->requestCount++],
'links' => [
'next' => [
'href' => $this->responsePages > 0 ? 'page_' . $this->requestCount : FALSE,
],
],
];
$contents = json_encode($json);
$stream = $this
->createMock('Psr\\Http\\Message\\StreamInterface');
$stream
->expects($this
->any())
->method('getContents')
->will($this
->returnValue($contents));
return $stream;
}
public function loadNodeByProperties(array $properties) {
$uuid = $properties['uuid'];
$filtered_nodes = [];
foreach ($this->nodes as $node) {
if (is_array($uuid) && in_array($node
->uuid(), $uuid)) {
$filtered_nodes[] = $node;
}
elseif ($node
->uuid() == $uuid) {
$filtered_nodes[] = $node;
}
}
return $filtered_nodes;
}
public function getRemotesConfig() {
return $this->remotesConfig;
}
protected function createEntity($uuid) {
$entity = $this
->createMock('Drupal\\Core\\Entity\\EntityInterface');
$entity
->expects($this
->any())
->method('uuid')
->will($this
->returnValue($uuid));
return $entity;
}
protected function setUp() {
$this->responsePages = count($this->responseData);
$this->requestCount = 0;
$settings = $this
->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
->disableOriginalConstructor()
->getMock();
$settings
->expects($this
->any())
->method('get')
->with('remotes')
->will($this
->returnCallback([
$this,
'getRemotesConfig',
]));
$config_factory = $this
->createMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
$config_factory
->expects($this
->any())
->method('get')
->with('entity_share_cron.settings')
->will($this
->returnValue($settings));
$remote = new Remote([
'id' => self::REMOTE_ID,
], 'remote');
$remote_storage = $this
->createMock('Drupal\\Core\\Config\\Entity\\ConfigEntityStorageInterface');
$remote_storage
->expects($this
->any())
->method('load')
->with($this
->equalTo(self::REMOTE_ID))
->will($this
->returnValue($remote));
$node_storage = $this
->createMock('Drupal\\Core\\Entity\\ContentEntityStorageInterface');
$node_storage
->expects($this
->any())
->method('loadByProperties')
->will($this
->returnCallback([
$this,
'loadNodeByProperties',
]));
$node_definition = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
$node_definition
->expects($this
->any())
->method('getKey')
->with($this
->equalTo('uuid'))
->will($this
->returnValue('uuid'));
$entity_type_manager = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeManagerInterface');
$entity_type_manager
->expects($this
->any())
->method('getStorage')
->will($this
->returnValueMap([
[
'node',
$node_storage,
],
[
'remote',
$remote_storage,
],
]));
$entity_type_manager
->expects($this
->any())
->method('getDefinition')
->with($this
->equalTo('node'))
->will($this
->returnValue($node_definition));
$entity_type_repository = $this
->createMock('Drupal\\Core\\Entity\\EntityTypeRepositoryInterface');
$entity_type_repository
->expects($this
->any())
->method('getEntityTypeFromClass')
->will($this
->returnValue('remote'));
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $entity_type_manager);
$container
->set('entity_type.repository', $entity_type_repository);
\Drupal::setContainer($container);
$response = $this
->createMock('Psr\\Http\\Message\\ResponseInterface');
$response
->expects($this
->any())
->method('getBody')
->will($this
->returnCallback([
$this,
'getResponseBody',
]));
$http_client = $this
->createMock('GuzzleHttp\\Client');
$http_client
->expects($this
->atLeastOnce())
->method('__call')
->with($this
->equalTo('get'))
->will($this
->returnValue($response));
$remote_manager = $this
->createMock('Drupal\\entity_share_client\\Service\\RemoteManagerInterface');
$remote_manager
->expects($this
->any())
->method('prepareJsonApiClient')
->will($this
->returnValue($http_client));
$jsonapi_helper = $this
->createMock('Drupal\\entity_share_client\\Service\\JsonapiHelperInterface');
$jsonapi_helper
->expects($this
->once())
->method('importEntityListData')
->will($this
->returnArgument(0));
$this->queue = $this
->createMock('Drupal\\Core\\Queue\\QueueInterface');
$queue_factory = $this
->getMockBuilder('Drupal\\Core\\Queue\\QueueFactory')
->disableOriginalConstructor()
->setMethods([
'get',
])
->getMock();
$queue_factory
->expects($this
->any())
->method('get')
->with($this
->equalTo(EntityShareCronServiceInterface::PENDING_QUEUE_NAME))
->will($this
->returnValue($this->queue));
$this->service = new EntityShareCronService($config_factory, $remote_manager, $jsonapi_helper, $queue_factory, $entity_type_manager);
}
}