You are here

public function ImportEntityManagerTest::testEntityPresaveCompareIncludeFieldReference in Acquia Content Hub 8

Tests the entityPresave() method.

Include content entities from comparison.

@covers ::entityPresave

File

tests/src/Unit/ImportEntityManagerTest.php, line 604

Class

ImportEntityManagerTest
PHPUnit test for the ImportEntityManager class.

Namespace

Drupal\Tests\acquia_contenthub\Unit

Code

public function testEntityPresaveCompareIncludeFieldReference() {
  $node = $this
    ->createMock('\\Drupal\\node\\NodeInterface');
  $original_node = $this
    ->createMock('\\Drupal\\node\\NodeInterface');
  $node->original = $original_node;
  $node
    ->expects($this
    ->any())
    ->method('id')
    ->willReturn(12);
  $node
    ->expects($this
    ->any())
    ->method('getEntityTypeId')
    ->willReturn('node');
  $this->contentHubEntitiesTracking
    ->expects($this
    ->once())
    ->method('loadImportedByDrupalEntity')
    ->with('node', 12)
    ->willReturn($this->contentHubEntitiesTracking);
  $node
    ->expects($this
    ->any())
    ->method('referencedEntities')
    ->willReturn([]);
  $node->original
    ->expects($this
    ->any())
    ->method('referencedEntities')
    ->willReturn([]);
  $this->contentHubEntitiesTracking
    ->expects($this
    ->once())
    ->method('isPendingSync')
    ->willReturn(FALSE);
  $this->contentHubEntitiesTracking
    ->expects($this
    ->once())
    ->method('hasLocalChange')
    ->willReturn(FALSE);
  $fieldReferenceInterface = $this
    ->createMock('Drupal\\Core\\Field\\EntityReferenceFieldItemListInterface');

  // Get reference entity.
  $referenced_entities = [];
  $entity1 = $this
    ->createMock('\\Drupal\\Core\\Entity\\EntityInterface');
  $entity_type = $this
    ->createMock('\\Drupal\\Core\\Entity\\EntityTypeInterface');
  $entity1
    ->expects($this
    ->any())
    ->method('getEntityType')
    ->will($this
    ->returnValue($entity_type));
  $entity_type
    ->expects($this
    ->once())
    ->method('entityClassImplements')
    ->willReturn(FALSE);
  $referenced_entities[] = $entity1;
  $fieldReferenceInterface
    ->expects($this
    ->any())
    ->method('referencedEntities')
    ->willReturn($referenced_entities);
  $fieldDefinition = $this
    ->createMock('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
  $fieldDefinition
    ->expects($this
    ->once())
    ->method('getType')
    ->willReturn('entity_reference');
  $node
    ->expects($this
    ->once())
    ->method('getFieldDefinition')
    ->with('same_field_1')
    ->willReturn($fieldDefinition);
  $node
    ->expects($this
    ->once())
    ->method('get')
    ->with('same_field_1')
    ->willReturn($fieldReferenceInterface);
  $field_comparisons = [
    '12:node.same_field_1' => [
      '#data' => [
        '#left' => 'same_value_1',
        '#right' => 'same_value_2',
      ],
    ],
  ];
  $this->diffEntityComparison
    ->expects($this
    ->once())
    ->method('compareRevisions')
    ->with($original_node, $node)
    ->willReturn($field_comparisons);
  $this->contentHubEntitiesTracking
    ->expects($this
    ->once())
    ->method('setLocalChange');
  $this->contentHubEntitiesTracking
    ->expects($this
    ->once())
    ->method('save');
  $this->importEntityManager
    ->entityPresave($node);
}