3 namespace Drupal\media\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Ensures that media bundle for gallery can be created.
12 class GalleryBundleTest extends WebTestBase {
14 * Exempt from strict schema checking.
16 * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
20 protected $strictConfigSchema = FALSE;
27 public static $modules = [
30 'media_entity_slideshow',
36 * The test media bundle.
38 * @var \Drupal\media_entity\MediaBundleInterface
40 protected $testBundle;
45 protected function setUp() {
47 $this->testBundle = $this->container->get('entity_type.manager')->getStorage('media_bundle')->load('gallery');
49 $adminUser = $this->drupalCreateUser([
56 'access media overview',
57 'access gallery_media_library entity browser pages',
59 $this->drupalLogin($adminUser);
63 * Tests gallery media bundle creation from config files.
65 public function testMediaBundleCreationFromModule() {
66 $type_configuration = [
67 'source_field' => 'field_slide',
70 $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
71 $this->assertEqual($this->testBundle->get('label'), 'Gallery', 'Correct label detected.');
72 $this->assertEqual($this->testBundle->get('description'), 'Use Gallery for creating a collection of different media items.', 'Correct description detected.');
73 $this->assertEqual($this->testBundle->get('type'), 'slideshow', 'Correct plugin ID detected.');
74 $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
75 $this->assertEqual($this->testBundle->get('field_map'), [], 'Correct field map detected.');
79 * Tests thumbnail of the gallery item.
81 public function testGalleryItemThumbnail() {
82 // Let's add one image and one video.
83 $imageItem = $this->addImageItem();
84 $videoItem = $this->addVideoItem();
85 $this->drupalGet('media/add/gallery');
86 $pathValue = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-slide-entity-browser-entity-browser-path"]/@value'));
88 'name[0][value]' => 'Gallery item',
89 'field_slide[target_id]' => 'media:' . $imageItem['id'] . ' media:' . $videoItem['id'],
90 'field_slide[entity_browser][entity_browser][path]' => $pathValue,
92 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
94 // Let's load all the media items.
95 $gallery_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'gallery')->sort('created', 'DESC')->execute();
96 $gallery = $this->loadMediaItem(reset($gallery_id));
97 $image = $this->loadMediaItem($imageItem['id']);
98 $video = $this->loadMediaItem($videoItem['id']);
99 // Let's check thumbnail now.
100 $gallery_thumbnail = $gallery->getType()->thumbnail($gallery);
101 $image_thumbnail = $image->getType()->thumbnail($image);
102 $video_thumbnail = $video->getType()->thumbnail($video);
103 $this->assertEqual($gallery_thumbnail, $image_thumbnail, "Correct thumbnail detected.");
105 $this->drupalGet('media/add/gallery');
107 'name[0][value]' => 'Gallery item 2',
108 'field_slide[target_id]' => 'media:' . $videoItem['id'] . ' media:' . $imageItem['id'],
109 'field_slide[entity_browser][entity_browser][path]' => $pathValue,
111 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
113 // Let's check the thumbnail again.
114 $gallery_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'gallery')->sort('created', 'DESC')->execute();
115 $gallery = $this->loadMediaItem(reset($gallery_id));
116 $gallery_thumbnail = $gallery->getType()->thumbnail($gallery);
117 $this->assertEqual($gallery_thumbnail, $video_thumbnail, "Correct thumbnail detected.");
121 * Tests that gallery option isn't available in gallery create bundle filters.
123 public function testGalleryOption() {
124 // Open the media library iframe used on add gallery page.
125 $this->drupalGet('entity-browser/modal/gallery_media_library');
126 $this->assertNoOption('edit-bundle-1', 'gallery');
130 * Adds image type item.
132 public function addImageItem() {
133 // Let's add image first.
134 $name = $this->randomMachineName();
135 $testImage = current($this->drupalGetTestFiles('image'));
136 $file_path = $this->container->get('file_system')->realpath($testImage->uri);
138 'name[0][value]' => $name,
139 'files[field_image_0]' => $file_path,
142 $this->drupalPostForm('media/add/image', $edit, t('Save and publish'));
143 $this->drupalPostForm(NULL, ['field_image[0][alt]' => $name], t('Save and publish'));
144 // Obtain the image id.
145 $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'image')->sort('created', 'DESC')->execute();
146 $media_id = reset($media_id);
147 $edit['id'] = $media_id;
153 * Adds video type item.
155 public function addVideoItem() {
157 'name[0][value]' => 'Drupal video!',
158 'field_video[0][value]' => 'https://www.youtube.com/watch?v=XgYu7-DQjDQ',
160 $this->drupalPostForm('media/add/video', $edit, t('Save and publish'));
161 // Obtain the video id.
162 $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'video')->sort('created', 'DESC')->execute();
163 $media_id = reset($media_id);
164 $edit['id'] = $media_id;
170 * Loads the media entity item.
173 * The id of the item.
175 * @return \Drupal\media_entity\MediaInterface
176 * The media entity item.
178 public function loadMediaItem($id) {
179 $item = $this->container->get('entity_type.manager')
180 ->getStorage('media')
181 ->loadUnchanged($id);