You are here

class MediaEntityHelperTest in Media: Acquia DAM 8

Testing of the Media Entity helper class.

@group media_acquiadam

Hierarchy

Expanded class hierarchy of MediaEntityHelperTest

File

tests/src/Unit/MediaEntityHelperTest.php, line 24

Namespace

Drupal\Tests\media_acquiadam\Unit
View source
class MediaEntityHelperTest extends UnitTestCase {
  use AcquiadamAssetDataTrait, AcquiadamMockedMediaEntityTrait;

  /**
   * Container builder helper.
   *
   * @var \Drupal\Core\DependencyInjection\ContainerBuilder
   */
  protected $container;

  /**
   * Validate we can get file from a media entity.
   */
  public function testGetFile() {
    $helper = $this
      ->getNewMediaEntityHelper();
    $this
      ->assertInstanceOf(FileInterface::class, $helper
      ->getFile());

    // Change the source field to a failing one to simulate a missing file.
    $media = $this
      ->getMockedMediaEntity($this
      ->getAssetData()->id, 'phpunit_test_fail');
    $this
      ->assertFalse($this
      ->getNewMediaEntityHelper($media)
      ->getFile());
  }

  /**
   * Validate we can properly load an existing file.
   */
  public function testGetExistingFile() {
    $this
      ->assertInstanceOf(FileInterface::class, $this
      ->getNewMediaEntityHelper()
      ->getExistingFile());
    $media = $this
      ->getMockBuilder(MediaInterface::class)
      ->disableOriginalConstructor()
      ->getMock();

    /** @var \Drupal\media\MediaInterface|\PHPUnit\Framework\MockObject\MockObject $media */
    $this
      ->assertFalse($this
      ->getNewMediaEntityHelper($media)
      ->getExistingFile());
  }

  /**
   * Validate we can get an existing fild ID if one is present.
   */
  public function testGetExistingFileId() {
    $this
      ->assertEquals($this
      ->getMockedFileEntity()
      ->id(), $this
      ->getNewMediaEntityHelper()
      ->getExistingFileId());

    /** @var \Drupal\media\MediaInterface|\PHPUnit\Framework\MockObject\MockObject $media */
    $media = $this
      ->getMockBuilder(MediaInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this
      ->assertFalse($this
      ->getNewMediaEntityHelper($media)
      ->getExistingFileId());
  }

  /**
   * Validates we can get the file field assets data is stored in.
   */
  public function testGetAssetFileField() {
    $this
      ->assertEquals('phpunit_file_field', $this
      ->getNewMediaEntityHelper()
      ->getAssetFileField());

    /** @var \Drupal\media\MediaInterface|\PHPUnit\Framework\MockObject\MockObject $media */
    $media = $this
      ->getMockBuilder(MediaInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this
      ->assertFalse($this
      ->getNewMediaEntityHelper($media)
      ->getAssetFileField());
  }

  /**
   * Validates that we can get the asset.
   */
  public function testGetAsset() {
    $this
      ->assertInstanceOf(Asset::class, $this
      ->getNewMediaEntityHelper()
      ->getAsset());

    // Change our source field to simulate a missing asset.
    $media = $this
      ->getMockedMediaEntity($this
      ->getAssetData()->id, 'phpunit_test_fail');
    $this
      ->assertFalse($this
      ->getNewMediaEntityHelper($media)
      ->getAsset());
  }

  /**
   * Validates that we can get the asset ID.
   */
  public function testGetAssetId() {
    $this
      ->assertEquals($this
      ->getAssetData()->id, $this
      ->getNewMediaEntityHelper()
      ->getAssetId());

    // Change our source field to simulate a missing asset.
    $media = $this
      ->getMockedMediaEntity($this
      ->getAssetData()->id, 'phpunit_test_fail');
    $this
      ->assertFalse($this
      ->getNewMediaEntityHelper($media)
      ->getAssetId());
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->container = new ContainerBuilder();
    $this
      ->setMockedDrupalServices($this->container);
    $this
      ->setMockedAcquiaDamServices($this->container);
    \Drupal::setContainer($this->container);
  }

  /**
   * Gets an instance of the MediaEntityHelper class.
   *
   * @param \Drupal\media\MediaInterface|null $media
   *   The media entity to wrap.
   *
   * @return \Drupal\media_acquiadam\MediaEntityHelper
   *   An instance of the MediaEntityHelper class.
   */
  protected function getNewMediaEntityHelper(MediaInterface $media = NULL) {
    if (is_null($media)) {
      $media = $this
        ->getMockedMediaEntity($this
        ->getAssetData()->id);
    }
    return new MediaEntityHelper($media, $this->container
      ->get('entity_type.manager'), $this->container
      ->get('media_acquiadam.asset_data'), $this->container
      ->get('media_acquiadam.acquiadam'), $this->container
      ->get('media_acquiadam.asset_file.helper'));
  }

  /**
   * Sets Drupal mocked services into a container.
   *
   * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
   *   The container to set mocks into.
   */
  protected function setMockedDrupalServices(ContainerBuilder $container) {
    $media_bundle = $this
      ->getMockBuilder(\stdClass::class)
      ->setMethods([
      'getFieldMap',
    ])
      ->getMock();
    $media_bundle
      ->method('getFieldMap')
      ->willReturn([
      'file' => 'phpunit_file_field',
    ]);
    $entity_storage = $this
      ->getMockBuilder(EntityStorageInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $entity_storage
      ->method('load')
      ->willReturnMap([
      [
        $this
          ->getMockedFileEntity()
          ->id(),
        $this
          ->getMockedFileEntity(),
      ],
      [
        'acquiadam',
        $media_bundle,
      ],
    ]);
    $entity_storage
      ->method('loadByProperties')
      ->willReturnMap([
      [
        [
          'uri' => 'private://assets/replaced/' . $this
            ->getAssetData()->filename,
        ],
        [
          $this
            ->getMockedFileEntity(),
        ],
      ],
      [
        [
          'uri' => 'private://assets/replaced/Micro turbine 60.jpg',
        ],
        [
          $this
            ->getMockedFileEntity(),
        ],
      ],
    ]);
    $entity_type_manager = $this
      ->getMockBuilder(EntityTypeManagerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $entity_type_manager
      ->method('getStorage')
      ->willReturnMap([
      [
        'file',
        $entity_storage,
      ],
      [
        'media_type',
        $entity_storage,
      ],
    ]);
    $container
      ->set('entity_type.manager', $entity_type_manager);
  }

  /**
   * Sets Media: Acquia DAM mocked services into a container.
   *
   * @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
   *   The container to set mocks into.
   */
  protected function setMockedAcquiaDamServices(ContainerBuilder $container) {
    $asset_data = $this
      ->getMockBuilder(AssetData::class)
      ->disableOriginalConstructor()
      ->getMock();
    $asset_data
      ->method('isUpdatedAsset')
      ->willReturnOnConsecutiveCalls(FALSE, TRUE);
    $acquiadam = $this
      ->getMockBuilder(Acquiadam::class)
      ->disableOriginalConstructor()
      ->getMock();
    $acquiadam
      ->method('getAsset')
      ->willReturnMap([
      [
        $this
          ->getAssetData()->id,
        TRUE,
        $this
          ->getAssetData(),
      ],
    ]);
    $asset_file_helper = $this
      ->getMockBuilder(AssetFileEntityHelper::class)
      ->disableOriginalConstructor()
      ->getMock();
    $asset_file_helper
      ->method('getDestinationFromEntity')
      ->willReturn('private://assets/replaced');
    $asset_file_helper
      ->method('createNewFile')
      ->with($this
      ->anything(), 'private://assets/replaced')
      ->willReturn($this
      ->getMockedFileEntity());
    $container
      ->set('media_acquiadam.asset_data', $asset_data);
    $container
      ->set('media_acquiadam.acquiadam', $acquiadam);
    $container
      ->set('media_acquiadam.asset_file.helper', $asset_file_helper);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiadamAssetDataTrait::generateNewVersion protected function Create a new version of a given asset.
AcquiadamAssetDataTrait::getAssetData protected function Returns an Asset object for testing against.
MediaEntityHelperTest::$container protected property Container builder helper.
MediaEntityHelperTest::getNewMediaEntityHelper protected function Gets an instance of the MediaEntityHelper class.
MediaEntityHelperTest::setMockedAcquiaDamServices protected function Sets Media: Acquia DAM mocked services into a container.
MediaEntityHelperTest::setMockedDrupalServices protected function Sets Drupal mocked services into a container.
MediaEntityHelperTest::setUp protected function Overrides UnitTestCase::setUp
MediaEntityHelperTest::testGetAsset public function Validates that we can get the asset.
MediaEntityHelperTest::testGetAssetFileField public function Validates we can get the file field assets data is stored in.
MediaEntityHelperTest::testGetAssetId public function Validates that we can get the asset ID.
MediaEntityHelperTest::testGetExistingFile public function Validate we can properly load an existing file.
MediaEntityHelperTest::testGetExistingFileId public function Validate we can get an existing fild ID if one is present.
MediaEntityHelperTest::testGetFile public function Validate we can get file from a media entity.
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.