You are here

class EntityShareCronServiceTest in Entity Share Cron 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/EntityShareCronServiceTest.php \Drupal\Tests\entity_share_cron\Unit\EntityShareCronServiceTest

@coversDefaultClass \Drupal\entity_share_cron\EntityShareCronService @group entity_share_cron

Hierarchy

Expanded class hierarchy of EntityShareCronServiceTest

File

tests/src/Unit/EntityShareCronServiceTest.php, line 15

Namespace

Drupal\Tests\entity_share_cron\Unit
View source
class EntityShareCronServiceTest extends UnitTestCase {
  const REMOTE_ID = 'test_remote';
  const CHANNEL_ID = 'test_channel';
  const CHANNEL_URL = 'https://example.com';

  /**
   * The service under test.
   *
   * @var \Drupal\entity_share_cron\EntityShareCronServiceInterface
   */
  protected $service;

  /**
   * Remote settings.
   *
   * @var array
   */
  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,
          ],
        ],
      ],
    ],
  ];

  /**
   * Existing nodes.
   *
   * @var array
   */
  protected $nodes = [];

  /**
   * The entities data received by the HTTP client on each request.
   *
   * @var array
   */
  protected $responseData = [
    [
      [
        'type' => 'node--article',
        'id' => 'node1',
      ],
    ],
    [
      [
        'type' => 'node--article',
        'id' => 'node2',
      ],
    ],
    [
      [
        'type' => 'node--article',
        'id' => 'node3',
      ],
      [
        'type' => 'node--article',
        'id' => 'node4',
      ],
    ],
  ];

  /**
   * The number of pages that the response should have.
   *
   * @var int
   */
  protected $responsePages;

  /**
   * Mocked queue.
   *
   * @var \Drupal\Core\Queue\QueueInterface
   */
  protected $queue;

  /**
   * The number of requests made during the test to customize returned bodies.
   *
   * @var int
   */
  protected $requestCount;

  /**
   * Tests the sync() method running through all pages.
   */
  public function testSynchronizationAllPages() {
    $this->responsePages = 3;

    // No page can be enqueued.
    $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);
  }

  /**
   * Tests the sync() method with page limit.
   */
  public function testSynchronizationWithPageLimit() {
    $this->responsePages = 3;

    // Sets expectation to get third page enqueued.
    $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);
  }

  /**
   * Tests synchronization when only entity creation is allowed.
   */
  public function testSynchronizationCreateOnly() {

    // Enables only entity creation.
    $this->remotesConfig[self::REMOTE_ID]['channels'][self::CHANNEL_ID]['operations'] = [
      'create' => TRUE,
      'update' => FALSE,
    ];

    // Creates some nodes.
    $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);
  }

  /**
   * Tests synchronization when only entity update is allowed.
   */
  public function testSynchronizationUpdateOnly() {

    // Enables only entity update.
    $this->remotesConfig[self::REMOTE_ID]['channels'][self::CHANNEL_ID]['operations'] = [
      'create' => FALSE,
      'update' => TRUE,
    ];

    // Creates some nodes.
    $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);
  }

  /**
   * Returns the body of a request for a page.
   *
   * @return \Psr\Http\Message\StreamInterface
   *   The mocked body.
   */
  public function getResponseBody() {
    $this->responsePages -= 1;
    $json = [
      'data' => $this->responseData[$this->requestCount++],
      'links' => [
        'next' => $this->responsePages > 0 ? 'page_' . $this->requestCount : FALSE,
      ],
    ];
    $contents = json_encode($json);
    $stream = $this
      ->getMock('Psr\\Http\\Message\\StreamInterface');
    $stream
      ->expects($this
      ->any())
      ->method('getContents')
      ->will($this
      ->returnValue($contents));
    return $stream;
  }

  /**
   * Method used to mock node storage. Loads a node by its UUID.
   *
   * @param array $properties
   *   Array of properties to filter the nodes.
   */
  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;
  }

  /**
   * Returns the remotes configuration.
   *
   * @return array
   *   Remotes configuration.
   */
  public function getRemotesConfig() {
    return $this->remotesConfig;
  }

  /**
   * Creates a mocked entity with the specified UUID.
   */
  protected function createEntity($uuid) {
    $entity = $this
      ->getMock('Drupal\\Core\\Entity\\EntityInterface');
    $entity
      ->expects($this
      ->any())
      ->method('uuid')
      ->will($this
      ->returnValue($uuid));
    return $entity;
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->responsePages = count($this->responseData);
    $this->requestCount = 0;

    // Mocks a configuration factory for the module settings.
    $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
      ->getMock('Drupal\\Core\\Config\\ConfigFactoryInterface');
    $config_factory
      ->expects($this
      ->any())
      ->method('get')
      ->with('entity_share_cron.settings')
      ->will($this
      ->returnValue($settings));

    // Mocks storages to load remotes and nodes.
    $remote = new Remote([
      'id' => self::REMOTE_ID,
    ], 'remote');
    $remote_storage = $this
      ->getMock('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
      ->getMock('Drupal\\Core\\Entity\\ContentEntityStorageInterface');
    $node_storage
      ->expects($this
      ->any())
      ->method('loadByProperties')
      ->will($this
      ->returnCallback([
      $this,
      'loadNodeByProperties',
    ]));

    // Mocks node entity type definition.
    $node_definition = $this
      ->getMock('Drupal\\Core\\Entity\\EntityTypeInterface');
    $node_definition
      ->expects($this
      ->any())
      ->method('getKey')
      ->with($this
      ->equalTo('uuid'))
      ->will($this
      ->returnValue('uuid'));

    // Mocks entity type manager and repository and add them to the container.
    $entity_type_manager = $this
      ->getMock('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
      ->getMock('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);

    // Mocks a remote manager.
    $response = $this
      ->getMock('Psr\\Http\\Message\\ResponseInterface');
    $response
      ->expects($this
      ->any())
      ->method('getBody')
      ->will($this
      ->returnCallback([
      $this,
      'getResponseBody',
    ]));
    $http_client = $this
      ->getMock('GuzzleHttp\\Client');
    $http_client
      ->expects($this
      ->atLeastOnce())
      ->method('__call')
      ->with($this
      ->equalTo('get'))
      ->will($this
      ->returnValue($response));
    $remote_manager = $this
      ->getMock('Drupal\\entity_share_client\\Service\\RemoteManagerInterface');
    $remote_manager
      ->expects($this
      ->any())
      ->method('prepareJsonApiClient')
      ->will($this
      ->returnValue($http_client));

    // Mocks a JSON API Helper.
    $jsonapi_helper = $this
      ->getMock('Drupal\\entity_share_client\\Service\\JsonapiHelperInterface');
    $jsonapi_helper
      ->expects($this
      ->any())
      ->method('prepareData')
      ->will($this
      ->returnArgument(0));
    $jsonapi_helper
      ->expects($this
      ->once())
      ->method('importEntityListData')
      ->will($this
      ->returnArgument(0));

    // Mocks a queue factory.
    $this->queue = $this
      ->getMock('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));

    // Instantiates a service to test.
    $this->service = new EntityShareCronService($config_factory, $remote_manager, $jsonapi_helper, $queue_factory, $entity_type_manager);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityShareCronServiceTest::$nodes protected property Existing nodes.
EntityShareCronServiceTest::$queue protected property Mocked queue.
EntityShareCronServiceTest::$remotesConfig protected property Remote settings.
EntityShareCronServiceTest::$requestCount protected property The number of requests made during the test to customize returned bodies.
EntityShareCronServiceTest::$responseData protected property The entities data received by the HTTP client on each request.
EntityShareCronServiceTest::$responsePages protected property The number of pages that the response should have.
EntityShareCronServiceTest::$service protected property The service under test.
EntityShareCronServiceTest::CHANNEL_ID constant
EntityShareCronServiceTest::CHANNEL_URL constant
EntityShareCronServiceTest::createEntity protected function Creates a mocked entity with the specified UUID.
EntityShareCronServiceTest::getRemotesConfig public function Returns the remotes configuration.
EntityShareCronServiceTest::getResponseBody public function Returns the body of a request for a page.
EntityShareCronServiceTest::loadNodeByProperties public function Method used to mock node storage. Loads a node by its UUID.
EntityShareCronServiceTest::REMOTE_ID constant
EntityShareCronServiceTest::setUp protected function Overrides UnitTestCase::setUp
EntityShareCronServiceTest::testSynchronizationAllPages public function Tests the sync() method running through all pages.
EntityShareCronServiceTest::testSynchronizationCreateOnly public function Tests synchronization when only entity creation is allowed.
EntityShareCronServiceTest::testSynchronizationUpdateOnly public function Tests synchronization when only entity update is allowed.
EntityShareCronServiceTest::testSynchronizationWithPageLimit public function Tests the sync() method with page limit.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.