Further modules included.
[yaffs-website] / web / modules / contrib / media / src / Tests / GalleryBundleTest.php
1 <?php
2
3 namespace Drupal\media\Tests;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Ensures that media bundle for gallery can be created.
9  *
10  * @group media
11  */
12 class GalleryBundleTest extends WebTestBase {
13   /**
14    * Exempt from strict schema checking.
15    *
16    * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
17    *
18    * @var bool
19    */
20   protected $strictConfigSchema = FALSE;
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = [
28     'media',
29     'media_entity',
30     'media_entity_slideshow',
31     'node',
32     'editor',
33   ];
34
35   /**
36    * The test media bundle.
37    *
38    * @var \Drupal\media_entity\MediaBundleInterface
39    */
40   protected $testBundle;
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function setUp() {
46     parent::setUp();
47     $this->testBundle = $this->container->get('entity_type.manager')->getStorage('media_bundle')->load('gallery');
48
49     $adminUser = $this->drupalCreateUser([
50       'view media',
51       'create media',
52       'update media',
53       'update any media',
54       'delete media',
55       'delete any media',
56       'access media overview',
57       'access gallery_media_library entity browser pages',
58     ]);
59     $this->drupalLogin($adminUser);
60   }
61
62   /**
63    * Tests gallery media bundle creation from config files.
64    */
65   public function testMediaBundleCreationFromModule() {
66     $type_configuration = [
67       'source_field' => 'field_slide',
68     ];
69
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.');
76   }
77
78   /**
79    * Tests thumbnail of the gallery item.
80    */
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'));
87     $edit = [
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,
91     ];
92     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
93
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.");
104
105     $this->drupalGet('media/add/gallery');
106     $edit = [
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,
110     ];
111     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
112
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.");
118   }
119
120   /**
121    * Tests that gallery option isn't available in gallery create bundle filters.
122    */
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');
127   }
128
129   /**
130    * Adds image type item.
131    */
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);
137     $edit = [
138       'name[0][value]' => $name,
139       'files[field_image_0]' => $file_path,
140     ];
141     // Save the image.
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;
148
149     return $edit;
150   }
151
152   /**
153    * Adds video type item.
154    */
155   public function addVideoItem() {
156     $edit = [
157       'name[0][value]' => 'Drupal video!',
158       'field_video[0][value]' => 'https://www.youtube.com/watch?v=XgYu7-DQjDQ',
159     ];
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;
165
166     return $edit;
167   }
168
169   /**
170    * Loads the media entity item.
171    *
172    * @param int $id
173    *   The id of the item.
174    *
175    * @return \Drupal\media_entity\MediaInterface
176    *   The media entity item.
177    */
178   public function loadMediaItem($id) {
179     $item = $this->container->get('entity_type.manager')
180       ->getStorage('media')
181       ->loadUnchanged($id);
182     return $item;
183   }
184
185 }