You are here

public function ImageBundleTest::testMediaBundleItemCreation in D7 Media 8

Tests item creation and thumbnail.

File

src/Tests/ImageBundleTest.php, line 82

Class

ImageBundleTest
Ensures that media bundle for images can be created.

Namespace

Drupal\media\Tests

Code

public function testMediaBundleItemCreation() {

  // Define the media item name.
  $name = $this
    ->randomMachineName();
  $image_files = $this
    ->drupalGetTestFiles('image');
  $testImage = current($image_files);
  $file_path = $this->container
    ->get('file_system')
    ->realpath($testImage->uri);
  $edit = [
    'name[0][value]' => $name,
    'files[field_image_0]' => $file_path,
  ];

  // Save the image.
  $this
    ->drupalPostForm('media/add/' . $this->testBundle
    ->id(), $edit, t('Save and publish'));
  $this
    ->drupalPostForm(NULL, [
    'field_image[0][alt]' => $name,
  ], t('Save and publish'));

  // Let's retrieve the media id and corresponding media entity object.
  $media_id = $this->container
    ->get('entity.query')
    ->get('media')
    ->execute();
  $media_id = reset($media_id);

  /** @var \Drupal\media_entity\MediaInterface $media */
  $media = $this->container
    ->get('entity_type.manager')
    ->getStorage('media')
    ->loadUnchanged($media_id);
  $this
    ->assertEqual($media
    ->get('name')[0]
    ->getValue()['value'], $name, "Correct name stored.");
  $image = $media
    ->getType();
  $thumbnail = $image
    ->thumbnail($media);
  $default_thumbnail = $image
    ->getDefaultThumbnail();
  $this
    ->assertNotEqual($thumbnail, $default_thumbnail, "The thumbnail generated is different from the default thumbnail.");
}