View source
<?php
namespace Drupal\media_entity_slideshow\Tests;
use Drupal\Core\Language\Language;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\media_entity\Entity\Media;
use Drupal\media_entity\MediaInterface;
use Drupal\simpletest\WebTestBase;
class MediaEntitySlideshowTest extends WebTestBase {
public static $modules = [
'media_entity_slideshow_test',
'node',
];
protected $slideshowMediaBundle;
protected $imageMediaBundle;
protected $mediaImageCollection;
public function setUp() {
parent::setUp();
$bundle_storage = $this->container
->get('entity_type.manager')
->getStorage('media_bundle');
$this->slideshowMediaBundle = $bundle_storage
->load('slideshow_bundle');
$this->imageMediaBundle = $bundle_storage
->load('image_bundle');
$adminUser = $this
->drupalCreateUser([
'view media',
'create media',
'update media',
'update any media',
'delete media',
'delete any media',
'access media overview',
]);
$this
->drupalLogin($adminUser);
$this->mediaImageCollection = $this
->createMediaImageCollection();
}
public function testMediaEntitySlideshow() {
$this
->assertTrue((bool) $this->slideshowMediaBundle, 'The media bundle from default configuration has been created in the database.');
$this
->drupalGet('media/add/' . $this->slideshowMediaBundle
->id());
$edit = [
'name[0][value]' => 'My first slideshow',
'field_slides[0][target_id]' => $this->mediaImageCollection[0]
->label() . ' (' . $this->mediaImageCollection[0]
->id() . ')',
];
$this
->drupalPostForm(NULL, $edit, t('Save and publish'));
$this
->assertText('Slideshow bundle My first slideshow has been created', 'Slideshow media entity was correctly created.');
$slideshow_id = $this->container
->get('entity.query')
->get('media')
->condition('bundle', 'slideshow_bundle')
->sort('created', 'DESC')
->execute();
$slideshow = $this
->loadMedia(reset($slideshow_id));
$this
->drupalGet('media/' . $slideshow
->id() . '/edit');
$edit = [
'field_slides[0][target_id]' => $this->mediaImageCollection[0]
->label() . ' (' . $this->mediaImageCollection[0]
->id() . ')',
'field_slides[1][target_id]' => $this->mediaImageCollection[1]
->label() . ' (' . $this->mediaImageCollection[1]
->id() . ')',
];
$this
->drupalPostForm(NULL, $edit, t('Save and keep published'));
$this
->assertResponse(200, 'Form submitted correctly');
$slideshow = $this
->loadMedia($slideshow
->id());
$this
->assertEqual($slideshow->field_slides
->count(), 2, 'A new slide was correctly added to the slideshow.');
$this
->drupalGet('media/' . $slideshow
->id() . '/edit');
$edit = [
'field_slides[0][target_id]' => $this->mediaImageCollection[0]
->label() . ' (' . $this->mediaImageCollection[0]
->id() . ')',
'field_slides[1][target_id]' => '',
];
$this
->drupalPostForm(NULL, $edit, t('Save and keep published'));
$this
->assertResponse(200, 'Form submitted correctly');
$slideshow = $this
->loadMedia($slideshow
->id());
$this
->assertEqual($slideshow->field_slides
->count(), 1, 'The deletion of one slide worked properly.');
$this
->drupalGet('/media/' . $slideshow
->id() . '/delete');
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertResponse(200, 'Form submitted correctly');
$this
->assertText('The media My first slideshow has been deleted', 'The slideshow was correctly deleted.');
}
private function createMediaImageCollection($count = 3) {
$collection = [];
for ($i = 1; $i <= $count; $i++) {
$media = Media::create([
'bundle' => $this->imageMediaBundle
->id(),
'name' => 'Image media ' . $i,
'uid' => '1',
'langcode' => Language::LANGCODE_DEFAULT,
'status' => Media::PUBLISHED,
]);
$image = $this
->getTestFile('image');
$media->field_imagefield->target_id = $image
->id();
$media
->save();
$collection[] = $media;
}
return $collection;
}
protected function loadMedia($id) {
$storage = $this->container
->get('entity_type.manager')
->getStorage('media');
return $storage
->loadUnchanged($id);
}
protected function getTestFile($type_name, $size = NULL) {
$file = current($this
->drupalGetTestFiles($type_name, $size));
$file->filesize = filesize($file->uri);
$file = File::create((array) $file);
$file
->setPermanent();
$file
->save();
return $file;
}
}