You are here

class ContentHubEntityEmbedHandlerTest in Acquia Content Hub 8

PHPUnit for the ContentHubEntityEmbedHandler class.

@coversDefaultClass \Drupal\acquia_contenthub\ContentHubEntityEmbedHandler

@group acquia_contenthub

Hierarchy

Expanded class hierarchy of ContentHubEntityEmbedHandlerTest

File

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

Namespace

Drupal\Tests\acquia_contenthub\Unit
View source
class ContentHubEntityEmbedHandlerTest extends UnitTestCase {

  /**
   * Drupal field object.
   *
   * @var \Drupal\Core\Field\FieldItemListInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $field;

  /**
   * Entity Type Manager Interface.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $entityTypeManager;

  /**
   * The dependency injection container.
   *
   * @var \Symfony\Component\DependencyInjection\ContainerBuilder|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $container;

  /**
   * The Entity Embed Handler.
   *
   * @var \Drupal\acquia_contenthub\ContentHubEntityEmbedHandler
   */
  protected $entityEmbedHandler;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Setting up the Module Handler.
    $this->field = $this
      ->createMock('\\Drupal\\Core\\Field\\FieldItemListInterface');
    $this->entityTypeManager = $this
      ->createMock('\\Drupal\\Core\\Entity\\EntityTypeManagerInterface');

    // Setting up the Container.
    $this->container = $this
      ->createMock('Drupal\\Core\\DependencyInjection\\Container');
    $this->container
      ->method('get')
      ->willReturnCallback(function ($name) {
      switch ($name) {
        case 'entity_type.manager':
          return $this->entityTypeManager;
      }
      return NULL;
    });
    \Drupal::setContainer($this->container);

    // Initializing Entity Embed Handler.
    $this->entityEmbedHandler = new ContentHubEntityEmbedHandler($this->field);
  }

  /**
   * Test for getReferencedUuids method.
   *
   * @covers ::getReferencedUuids
   */
  public function testGetReferencedUuids() {
    $text = '<drupal-entity data-caption="some_image" data-embed-button="media_browser" data-entity-embed-display="view_mode:media.embedded" data-entity-type="media" data-entity-uuid="00000000-1111-0000-0000-000000000000"></drupal-entity>';
    $uuids = $this->entityEmbedHandler
      ->getReferencedUuids($text);
    $expected = [
      "00000000-1111-0000-0000-000000000000",
    ];
    $this
      ->assertEquals($expected, $uuids);
  }

  /**
   * Test for getReferencedEntities method.
   *
   * @covers ::getReferencedEntities
   */
  public function testGetReferencedEntities() {
    $uuid = "00000000-1111-0000-0000-000000000000";
    $type = "media";
    $entity = $this
      ->createMock('\\Drupal\\Core\\Entity\\EntityInterface');
    $entity
      ->method('uuid')
      ->willReturn($uuid);
    $entity_storage = $this
      ->createMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
    $expected = [
      $uuid => $entity,
    ];
    $entity_storage
      ->expects($this
      ->once())
      ->method('loadByProperties')
      ->with([
      'uuid' => $uuid,
    ])
      ->willReturn($expected);
    $this->entityTypeManager
      ->expects($this
      ->once())
      ->method('getStorage')
      ->with($type)
      ->willReturn($entity_storage);
    $text = '<drupal-entity data-caption="some_image" data-embed-button="media_browser" data-entity-embed-display="view_mode:media.embedded" data-entity-type="' . $type . '" data-entity-uuid="' . $uuid . '"></drupal-entity>';
    $this->field
      ->expects($this
      ->once())
      ->method('getString')
      ->willReturn($text);
    $result = $this->entityEmbedHandler
      ->getReferencedEntities();
    $this
      ->assertEquals($expected, $result);
  }

  /**
   * Test for isProcessable method.
   *
   * @covers ::isProcessable
   */
  public function testIsProcessable() {
    $field_definition = $this
      ->createMock('\\Drupal\\Core\\Field\\FieldDefinitionInterface');
    $field_definition
      ->expects($this
      ->at(0))
      ->method('getType')
      ->willReturn('text_with_summary');
    $field_definition
      ->expects($this
      ->at(1))
      ->method('getType')
      ->willReturn('string_long');
    $field_definition
      ->expects($this
      ->at(2))
      ->method('getType')
      ->willReturn('text_long');
    $field_definition
      ->expects($this
      ->at(3))
      ->method('getType')
      ->willReturn('text');
    $this->field
      ->expects($this
      ->any())
      ->method('getFieldDefinition')
      ->willReturn($field_definition);
    $this
      ->assertTrue($this->entityEmbedHandler
      ->isProcessable());
    $this
      ->assertTrue($this->entityEmbedHandler
      ->isProcessable());
    $this
      ->assertTrue($this->entityEmbedHandler
      ->isProcessable());
    $this
      ->assertFalse($this->entityEmbedHandler
      ->isProcessable());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentHubEntityEmbedHandlerTest::$container protected property The dependency injection container.
ContentHubEntityEmbedHandlerTest::$entityEmbedHandler protected property The Entity Embed Handler.
ContentHubEntityEmbedHandlerTest::$entityTypeManager protected property Entity Type Manager Interface.
ContentHubEntityEmbedHandlerTest::$field protected property Drupal field object.
ContentHubEntityEmbedHandlerTest::setUp protected function Overrides UnitTestCase::setUp
ContentHubEntityEmbedHandlerTest::testGetReferencedEntities public function Test for getReferencedEntities method.
ContentHubEntityEmbedHandlerTest::testGetReferencedUuids public function Test for getReferencedUuids method.
ContentHubEntityEmbedHandlerTest::testIsProcessable public function Test for isProcessable method.
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.