View source
<?php
namespace Drupal\media\Tests;
use Drupal\simpletest\WebTestBase;
class ImageBundleTest extends WebTestBase {
protected $strictConfigSchema = FALSE;
public static $modules = [
'media',
'media_entity',
'media_entity_image',
'image',
'node',
'editor',
];
protected $testBundle;
protected function setUp() {
parent::setUp();
$this->testBundle = $this->container
->get('entity.manager')
->getStorage('media_bundle')
->load('image');
$adminUser = $this
->drupalCreateUser([
'view media',
'create media',
'update media',
'update any media',
'delete media',
'delete any media',
'access media overview',
]);
$this
->drupalLogin($adminUser);
}
public function testMediaBundleCreationFromModule() {
$type_configuration = [
'source_field' => 'field_image',
'gather_exif' => FALSE,
];
$this
->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
$this
->assertEqual($this->testBundle
->get('label'), 'Image', 'Correct label detected.');
$this
->assertEqual($this->testBundle
->get('description'), 'Use Image for uploading locally hosted images.', 'Correct description detected.');
$this
->assertEqual($this->testBundle
->get('type'), 'image', 'Correct plugin ID detected.');
$this
->assertEqual($this->testBundle
->get('type_configuration'), $type_configuration, 'Type configuration correct.');
$this
->assertEqual($this->testBundle
->get('field_map'), [], 'Correct field map detected.');
}
public function testMediaBundleItemCreation() {
$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,
];
$this
->drupalPostForm('media/add/' . $this->testBundle
->id(), $edit, t('Save and publish'));
$this
->drupalPostForm(NULL, [
'field_image[0][alt]' => $name,
], t('Save and publish'));
$media_id = $this->container
->get('entity.query')
->get('media')
->execute();
$media_id = reset($media_id);
$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.");
}
}