3 namespace Drupal\media\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Ensures that media bundle for images can be created.
12 class ImageBundleTest extends WebTestBase {
14 * Exempt from strict schema checking.
16 * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
20 protected $strictConfigSchema = FALSE;
27 public static $modules = [
37 * The test media bundle.
39 * @var \Drupal\media_entity\MediaBundleInterface
41 protected $testBundle;
46 protected function setUp() {
48 $this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('image');
50 $adminUser = $this->drupalCreateUser([
57 'access media overview',
59 $this->drupalLogin($adminUser);
63 * Tests image media bundle creation from config files.
65 public function testMediaBundleCreationFromModule() {
66 $type_configuration = [
67 'source_field' => 'field_image',
68 'gather_exif' => FALSE,
71 $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
72 $this->assertEqual($this->testBundle->get('label'), 'Image', 'Correct label detected.');
73 $this->assertEqual($this->testBundle->get('description'), 'Use Image for uploading locally hosted images.', 'Correct description detected.');
74 $this->assertEqual($this->testBundle->get('type'), 'image', 'Correct plugin ID detected.');
75 $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
76 $this->assertEqual($this->testBundle->get('field_map'), [], 'Correct field map detected.');
80 * Tests item creation and thumbnail.
82 public function testMediaBundleItemCreation() {
83 // Define the media item name.
84 $name = $this->randomMachineName();
85 $image_files = $this->drupalGetTestFiles('image');
86 $testImage = current($image_files);
87 $file_path = $this->container->get('file_system')->realpath($testImage->uri);
89 'name[0][value]' => $name,
90 'files[field_image_0]' => $file_path,
94 $this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish'));
95 $this->drupalPostForm(NULL, ['field_image[0][alt]' => $name], t('Save and publish'));
97 // Let's retrieve the media id and corresponding media entity object.
98 $media_id = $this->container->get('entity.query')->get('media')->execute();
99 $media_id = reset($media_id);
100 /** @var \Drupal\media_entity\MediaInterface $media */
101 $media = $this->container->get('entity_type.manager')
102 ->getStorage('media')
103 ->loadUnchanged($media_id);
104 $this->assertEqual($media->get('name')[0]->getValue()['value'], $name, "Correct name stored.");
105 $image = $media->getType();
106 $thumbnail = $image->thumbnail($media);
107 $default_thumbnail = $image->getDefaultThumbnail();
108 $this->assertNotEqual($thumbnail, $default_thumbnail, "The thumbnail generated is different from the default thumbnail.");