You are here

EntityReferenceTestBase.php in Feeds 8.3

File

tests/src/Unit/Feeds/Target/EntityReferenceTestBase.php
View source
<?php

namespace Drupal\Tests\feeds\Unit\Feeds\Target;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\feeds\EntityFinderInterface;
use Drupal\feeds\Exception\EmptyFeedException;
use Drupal\feeds\FieldTargetDefinition;

/**
 * Base class for entity reference target tests.
 */
abstract class EntityReferenceTestBase extends FieldTargetTestBase {

  /**
   * The entity type manager prophecy used in the test.
   *
   * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity storage prophecy used in the test.
   *
   * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityStorageInterface
   */
  protected $entityStorage;

  /**
   * The Feeds entity finder service.
   *
   * @var \Drupal\feeds\EntityFinderInterface
   */
  protected $entityFinder;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $referencable_entity_type_id = $this
      ->getReferencableEntityTypeId();

    // Entity type manager.
    $this->entityTypeManager = $this
      ->prophesize(EntityTypeManagerInterface::class);

    // Entity storage (needed for entity queries).
    $this->entityStorage = $this
      ->prophesize($this
      ->getEntityStorageClass());
    $this->entityTypeManager
      ->getStorage($referencable_entity_type_id)
      ->willReturn($this->entityStorage);

    // Made-up entity type that we are referencing to.
    $this->entityTypeManager
      ->getDefinition($referencable_entity_type_id)
      ->willReturn($this
      ->createReferencableEntityType())
      ->shouldBeCalled();

    // Entity finder.
    $this->entityFinder = $this
      ->prophesize(EntityFinderInterface::class);
  }

  /**
   * Creates a new target plugin instance.
   *
   * @param array $configuration
   *   (optional) The configuration for the target plugin.
   *
   * @return \Drupal\feeds\Plugin\Type\Target\TargetInterface
   *   A FeedsTarget plugin instance.
   */
  protected abstract function createTargetPluginInstance(array $configuration = []);

  /**
   * Returns the entity storage class name to use in this test.
   *
   * @return string
   *   The full name of the entity storage class.
   */
  protected function getEntityStorageClass() {
    return EntityStorageInterface::class;
  }

  /**
   * Returns the entity type machine name to use in this test.
   *
   * @return string
   *   The entity type ID.
   */
  protected function getReferencableEntityTypeId() {
    return 'referenceable_entity_type';
  }

  /**
   * Builds the Drupal service container.
   */
  protected function buildContainer() {

    // EntityReference::prepareTarget() accesses the entity type manager from
    // the global container.
    // @see \Drupal\feeds\Feeds\Target\EntityReference::prepareTarget()
    $container = new ContainerBuilder();
    $container
      ->set('entity_type.manager', $this->entityTypeManager
      ->reveal());
    $container
      ->set('string_translation', $this
      ->getStringTranslationStub());
    \Drupal::setContainer($container);
  }

  /**
   * Creates a Feeds target definition mock.
   *
   * @return \Drupal\feeds\TargetDefinitionInterface
   *   A mocked target definition.
   */
  protected function createTargetDefinitionMock() {
    $referencable_entity_type_id = $this
      ->getReferencableEntityTypeId();
    $method = $this
      ->getMethod($this
      ->getTargetClass(), 'prepareTarget')
      ->getClosure();
    $field_definition_mock = $this
      ->getMockFieldDefinition([
      'target_type' => $referencable_entity_type_id,
      'handler_settings' => [
        'target_bundles' => [],
      ],
    ]);
    $field_definition_mock
      ->expects($this
      ->once())
      ->method('getSetting')
      ->willReturn($referencable_entity_type_id);
    return $method($field_definition_mock);
  }

  /**
   * Creates a referencable entity type instance.
   *
   * @return \Drupal\Core\Entity\EntityTypeInterface
   *   The entity type to use in tests.
   */
  protected abstract function createReferencableEntityType();

  /**
   * @covers ::prepareTarget
   */
  public function testPrepareTarget() {
    $field_definition_mock = $this
      ->getMockFieldDefinition();
    $field_definition_mock
      ->expects($this
      ->once())
      ->method('getSetting')
      ->will($this
      ->returnValue($this
      ->getReferencableEntityTypeId()));
    $method = $this
      ->getMethod($this
      ->getTargetClass(), 'prepareTarget')
      ->getClosure();
    $this
      ->assertInstanceof(FieldTargetDefinition::class, $method($field_definition_mock));
  }

  /**
   * Tests prepareValue() without passing values.
   *
   * @covers ::prepareValue
   */
  public function testPrepareValueEmptyFeed() {
    $method = $this
      ->getProtectedClosure($this
      ->createTargetPluginInstance(), 'prepareValue');
    $values = [
      'target_id' => '',
    ];
    $this
      ->expectException(EmptyFeedException::class);
    $method(0, $values);
  }

}

Classes

Namesort descending Description
EntityReferenceTestBase Base class for entity reference target tests.