View source
<?php
namespace Drupal\Tests\media_entity_slideshow\Functional;
use Drupal\Core\Language\Language;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\TestFileCreationTrait;
class MediaEntitySlideshowTest extends BrowserTestBase {
use TestFileCreationTrait;
protected static $modules = [
'media_entity_slideshow_test',
'node',
];
protected $slideshowMediaBundle;
protected $imageMediaBundle;
protected $mediaImageCollection;
protected $defaultTheme = 'stark';
public function setUp() {
parent::setUp();
$bundle_storage = $this->container
->get('entity_type.manager')
->getStorage('media_type');
$this->slideshowMediaBundle = $bundle_storage
->load('slideshow_bundle');
$this->imageMediaBundle = $bundle_storage
->load('image_bundle');
$adminUser = $this
->drupalCreateUser([
'administer media',
'view media',
'create ' . $this->slideshowMediaBundle
->id() . ' media',
'edit any ' . $this->slideshowMediaBundle
->id() . ' media',
'delete any ' . $this->slideshowMediaBundle
->id() . ' media',
]);
$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'));
$this
->assertSession()
->pageTextContains('Slideshow bundle My first slideshow has been created');
$media_storage = $this->container
->get('entity_type.manager')
->getStorage('media');
$slideshow_id = $media_storage
->getQuery()
->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'));
$this
->assertSession()
->statusCodeEquals(200);
$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'));
$this
->assertSession()
->statusCodeEquals(200);
$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
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('The media item My first slideshow has been 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' => TRUE,
]);
$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
->getTestFiles($type_name, $size));
$file->filesize = filesize($file->uri);
$file = File::create((array) $file);
$file
->setPermanent();
$file
->save();
return $file;
}
}