You are here

public function EntityManagerTest::testIsEligibleDependency in Acquia Content Hub 8

Test for isEligibleDependency(), isEligibleEntity() methods.

@covers ::isEligibleDependency

File

tests/src/Unit/EntityManagerTest.php, line 433

Class

EntityManagerTest
PHPUnit for the EntityManager class.

Namespace

Drupal\Tests\acquia_contenthub\Unit

Code

public function testIsEligibleDependency() {

  // Initializing Container.
  $container = $this
    ->createMock('Drupal\\Core\\DependencyInjection\\Container');
  \Drupal::setContainer($container);
  $container
    ->method('get')
    ->willReturnCallback(function ($argument) {
    $module_handler = $this
      ->createMock('\\Drupal\\Core\\Extension\\ModuleHandlerInterface');
    $module_handler
      ->expects($this
      ->any())
      ->method('invokeAll')
      ->willReturn([]);
    $config1 = $this
      ->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
      ->disableOriginalConstructor()
      ->setMethods([
      'get',
    ])
      ->getMock();
    $config2 = $this
      ->getMockBuilder('Drupal\\Core\\Config\\ImmutableConfig')
      ->disableOriginalConstructor()
      ->setMethods([
      'get',
    ])
      ->getMock();
    $config2
      ->method('get')
      ->with('user_role')
      ->willReturn(AccountInterface::ANONYMOUS_ROLE);
    $config1
      ->method('get')
      ->with('acquia_contenthub.entity_config')
      ->willReturn($config2);
    switch ($argument) {
      case 'config.factory':
        return $config1;
      case 'module_handler':
        return $module_handler;
    }
  });

  // Defining configuration entities.
  $node_config_entity = $this
    ->getContentHubEntityTypeConfigEntityId('node');
  $file_config_entity = $this
    ->getContentHubEntityTypeConfigEntityId('file');
  $config_entities = [
    'node' => $node_config_entity,
    'file' => $file_config_entity,
  ];
  $config_storage = $this
    ->createMock('Drupal\\Core\\Entity\\EntityStorageInterface');
  $config_storage
    ->method('loadMultiple')
    ->willReturn($config_entities);
  $this->entityTypeManager
    ->method('getStorage')
    ->with('acquia_contenthub_entity_config')
    ->willReturn($config_storage);
  $entity_manager = new EntityManager($this->loggerFactory, $this->configFactory, $this->clientManager, $this->contentHubEntitiesTracking, $this->entityTypeManager, $this->entityTypeBundleInfoManager, $this->kernel);

  // Testing a Node that has been already synchronized.
  $access = $this
    ->createMock('\\Drupal\\Core\\Access\\AccessResultInterface');
  $access
    ->expects($this
    ->any())
    ->method('isAllowed')
    ->willReturn(TRUE);
  $node = $this
    ->createMock('\\Drupal\\node\\NodeInterface');
  $node
    ->method('id')
    ->willReturn(1);
  $node
    ->expects($this
    ->any())
    ->method('getEntityTypeId')
    ->willReturn('node');
  $node
    ->expects($this
    ->any())
    ->method('bundle')
    ->willReturn('article');
  $node
    ->expects($this
    ->any())
    ->method('access')
    ->withAnyParameters()
    ->willReturn($access);
  $node->__contenthub_entity_syncing = TRUE;
  $result = $entity_manager
    ->isEligibleDependency($node);
  $this
    ->assertFalse($result);

  // Testing a bundle that is not configured to be exportable.
  unset($node->__contenthub_entity_syncing);
  $node
    ->expects($this
    ->at(0))
    ->method('bundle')
    ->willReturn('page');
  $result = $entity_manager
    ->isEligibleDependency($node);
  $this
    ->assertFalse($result);

  // Testing an qualified entity that was not previously imported or exported.
  $type = $this
    ->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
  $type
    ->expects($this
    ->any())
    ->method('hasKey')
    ->willReturnMap([
    [
      'status',
      TRUE,
    ],
    [
      'revision',
      TRUE,
    ],
  ]);
  $type
    ->expects($this
    ->any())
    ->method('getKey')
    ->willReturnMap([
    [
      'status',
      'status',
    ],
    [
      'revision',
      'vid',
    ],
  ]);
  $field_storage = $this
    ->createMock('\\Drupal\\Core\\Field\\FieldStorageDefinitionInterface');
  $field_storage
    ->expects($this
    ->any())
    ->method('getMainPropertyName')
    ->willReturn('value');
  $field_definition = $this
    ->createMock('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
  $field_definition
    ->expects($this
    ->any())
    ->method('getFieldStorageDefinition')
    ->willReturn($field_storage);
  $node
    ->expects($this
    ->any())
    ->method('getFieldDefinition')
    ->with('status')
    ->willReturn($field_definition);
  $status_field = new \stdClass();
  $status_field->value = TRUE;
  $node
    ->expects($this
    ->any())
    ->method('get')
    ->with('status')
    ->willReturn($status_field);
  $node
    ->expects($this
    ->any())
    ->method('getEntityType')
    ->willReturn($type);
  $node
    ->expects($this
    ->at(1))
    ->method('bundle')
    ->willReturn('article');
  $this->contentHubEntitiesTracking
    ->expects($this
    ->any())
    ->method('loadImportedByDrupalEntity')
    ->willReturn(FALSE);
  $this->contentHubEntitiesTracking
    ->expects($this
    ->any())
    ->method('loadExportedByUuid')
    ->willReturn(FALSE);
  $result = $entity_manager
    ->isEligibleDependency($node);
  $this
    ->assertTrue($result);

  // Testing a file entity with status = TEMPORARY.
  $entity_type = $this
    ->createMock('Drupal\\Core\\Entity\\EntityTypeInterface');
  $entity_type
    ->method('hasKey')
    ->willReturn(FALSE);
  $file = $this
    ->createMock('\\Drupal\\file\\FileInterface');
  $file
    ->method('id')
    ->willReturn(1);
  $file
    ->expects($this
    ->any())
    ->method('getEntityTypeId')
    ->willReturn('file');
  $file
    ->expects($this
    ->any())
    ->method('bundle')
    ->willReturn('image');
  $file
    ->expects($this
    ->any())
    ->method('access')
    ->withAnyParameters()
    ->willReturn($access);
  $file
    ->expects($this
    ->any())
    ->method('getEntityType')
    ->willReturn($entity_type);
  $file
    ->expects($this
    ->at(3))
    ->method('isTemporary')
    ->willReturn(TRUE);
  $file
    ->expects($this
    ->at(4))
    ->method('isTemporary')
    ->willReturn(FALSE);
  $result = $entity_manager
    ->isEligibleDependency($file);
  $this
    ->assertFalse($result);

  // Testing a file entity with status = PERMANENT.
  // Testing an qualified entity that was not previously imported or exported.
  $type = $this
    ->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
  $type
    ->expects($this
    ->any())
    ->method('hasKey')
    ->willReturnMap([
    [
      'status',
      FALSE,
    ],
    [
      'revision',
      FALSE,
    ],
  ]);
  $file
    ->expects($this
    ->any())
    ->method('getEntityType')
    ->willReturn($type);
  $result = $entity_manager
    ->isEligibleDependency($file);
  $this
    ->assertTrue($result);
}