Completed
Push — 8.x-1.x ( a381b9...4faedf )
by Tadej
10s
created

GalleryBundleTest::testGalleryItemThumbnail()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 33
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 44
rs 8.8571
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
  ];
33
34
  /**
35
   * The test media bundle.
36
   *
37
   * @var \Drupal\media_entity\MediaBundleInterface
38
   */
39
  protected $testBundle;
40
41
  /**
42
   * {@inheritdoc}
43
   */
44 View Code Duplication
  protected function setUp() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    parent::setUp();
46
    $this->testBundle = $this->container->get('entity_type.manager')->getStorage('media_bundle')->load('gallery');
47
48
    $adminUser = $this->drupalCreateUser([
49
      'view media',
50
      'create media',
51
      'update media',
52
      'update any media',
53
      'delete media',
54
      'delete any media',
55
      'access media overview',
56
    ]);
57
    $this->drupalLogin($adminUser);
58
  }
59
60
  /**
61
   * Tests gallery media bundle creation from config files.
62
   */
63 View Code Duplication
  public function testMediaBundleCreationFromModule() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    $type_configuration = [
65
      'source_field' => 'field_slide',
66
    ];
67
68
    $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
69
    $this->assertEqual($this->testBundle->get('label'), 'Gallery', 'Correct label detected.');
70
    $this->assertEqual($this->testBundle->get('description'), 'Use Gallery for creating a collection of different media items.', 'Correct description detected.');
71
    $this->assertEqual($this->testBundle->get('type'), 'slideshow', 'Correct plugin ID detected.');
72
    $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
73
    $this->assertEqual($this->testBundle->get('field_map'), [], 'Correct field map detected.');
74
  }
75
76
  /**
77
   * Tests thumbnail of the gallery item.
78
   */
79
  public function testGalleryItemThumbnail() {
80
    // Let's add one image and one video.
81
    $imageItem = $this->addImageItem();
82
    $videoItem = $this->addVideoItem();
83
84
    $this->drupalGet('media/add/gallery');
85
    $edit = [
86
      'name[0][value]' => 'Gallery item',
87
      'field_slide[0][target_id]' => $imageItem['name[0][value]'] . ' (' . $imageItem['id'] . ')',
88
      'field_slide[0][_weight]' => 0,
89
    ];
90
    $this->drupalPostAjaxForm(NULL, $edit, 'field_slide_add_more');
91
    $edit['field_slide[1][target_id]'] = $videoItem['name[0][value]'] . ' (' . $videoItem['id'] . ')';
92
    $edit['field_slide[1][_weight]'] = 1;
93
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
94
95
    // Let's load all the media items.
96
    $gallery_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'gallery')->sort('created', 'DESC')->execute();
97
    $gallery = $this->loadMediaItem(reset($gallery_id));
98
    $image = $this->loadMediaItem($imageItem['id']);
99
    $video = $this->loadMediaItem($videoItem['id']);
100
    // Let's check thumbnail now.
101
    $gallery_thumbnail = $gallery->getType()->thumbnail($gallery);
102
    $image_thumbnail = $image->getType()->thumbnail($image);
103
    $video_thumbnail = $video->getType()->thumbnail($video);
104
    $this->assertEqual($gallery_thumbnail, $image_thumbnail, "Correct thumbnail detected.");
105
106
    $this->drupalGet('media/add/gallery');
107
    $edit = [
108
      'name[0][value]' => 'Gallery item 2',
109
      'field_slide[0][target_id]' => $videoItem['name[0][value]'] . ' (' . $videoItem['id'] . ')',
110
      'field_slide[0][_weight]' => 0,
111
    ];
112
    $this->drupalPostAjaxForm(NULL, $edit, 'field_slide_add_more');
113
    $edit['field_slide[1][target_id]'] = $imageItem['name[0][value]'] . ' (' . $imageItem['id'] . ')';
114
    $edit['field_slide[1][_weight]'] = 1;
115
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
116
117
    // Let's check the thumbnail again.
118
    $gallery_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'gallery')->sort('created', 'DESC')->execute();
119
    $gallery = $this->loadMediaItem(reset($gallery_id));
120
    $gallery_thumbnail = $gallery->getType()->thumbnail($gallery);
121
    $this->assertEqual($gallery_thumbnail, $video_thumbnail, "Correct thumbnail detected.");
122
  }
123
124
  /**
125
   * Adds image type item.
126
   */
127
  public function addImageItem() {
128
    // Let's add image first.
129
    $name = $this->randomMachineName();
130
    $testImage = current($this->drupalGetTestFiles('image'));
131
    $file_path = $this->container->get('file_system')->realpath($testImage->uri);
132
    $edit = [
133
      'name[0][value]' => $name,
134
      'files[field_image_0]' => $file_path,
135
    ];
136
    // Save the image.
137
    $this->drupalPostForm('media/add/image', $edit, t('Save and publish'));
138
    $this->drupalPostForm(NULL, ['field_image[0][alt]' => $name], t('Save and publish'));
139
    // Obtain the image id.
140
    $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'image')->sort('created', 'DESC')->execute();
141
    $media_id = reset($media_id);
142
    $edit['id'] = $media_id;
143
144
    return $edit;
145
  }
146
147
  /**
148
   * Adds video type item.
149
   */
150
  public function addVideoItem() {
151
    $edit = [
152
      'name[0][value]' => 'Drupal video!',
153
      'field_video[0][value]' => 'https://www.youtube.com/watch?v=XgYu7-DQjDQ',
154
    ];
155
    $this->drupalPostForm('media/add/video', $edit, t('Save and publish'));
156
    // Obtain the video id.
157
    $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'video')->sort('created', 'DESC')->execute();
158
    $media_id = reset($media_id);
159
    $edit['id'] = $media_id;
160
161
    return $edit;
162
  }
163
164
  /**
165
   * Loads the media entity item.
166
   *
167
   * @param int $id
168
   *   The id of the item.
169
   *
170
   * @return \Drupal\media_entity\MediaInterface
171
   *   The media entity item.
172
   */
173
  public function loadMediaItem($id) {
174
    $item = $this->container->get('entity_type.manager')
175
      ->getStorage('media')
176
      ->loadUnchanged($id);
177
    return $item;
178
  }
179
180
}
181