You are here

public function ReferenceTest::testProcessCreate in YAML Content 8

Reference processing should not create new entity if no matches are found.

@covers ::process

File

tests/src/Unit/Plugin/yaml_content/process/ReferenceTest.php, line 94

Class

ReferenceTest
Test entity reference processing.

Namespace

Drupal\Tests\yaml_content\Unit\Plugin\yaml_content\process

Code

public function testProcessCreate() {
  $data = [
    '#process' => [
      'callback' => 'reference',
    ],
  ];

  // Mock a query so we can control and assert the mapping of arguments to entity.
  $query = $this
    ->prophesize(QueryInterface::class);
  $query
    ->condition('title', 'My First Blog Post')
    ->shouldBeCalled();
  $query
    ->execute()
    ->shouldBeCalled()
    ->willReturn();
  $this->entityStorageHandler
    ->getQuery('AND')
    ->shouldBeCalled()
    ->willReturn($query
    ->reveal());

  // Stub an existing entity since entities are complex and db bound.
  $entity = $this
    ->prophesize(EntityInterface::class);
  $entity
    ->id()
    ->willReturn(2);

  // Create should not be called.
  $this->entityStorageHandler
    ->create(Argument::any())
    ->shouldBeCalled()
    ->willReturn($entity);
  $this->reference
    ->process(new ProcessingContext(), $data);
  $this
    ->assertArrayEquals([
    'target_id' => 2,
  ], $data);
}