Completed
Pull Request — 8.x-1.x (#10)
by Vijay
02:12
created

testMediaBundleCreationFromModule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 16
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
c 1
b 0
f 0
nc 1
nop 0
dl 16
loc 16
rs 9.4285
1
<?php
2
3
namespace Drupal\media\Tests;
4
5
use Drupal\simpletest\WebTestBase;
6
7
/**
8
 * Ensures that media bundle for document can be created.
9
 *
10
 * @group media
11
 */
12
class DocumentBundleTest 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_document',
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
  protected function setUp() {
45
    parent::setUp();
46
    $this->testBundle = $this->container->get('entity_type.manager')->getStorage('media_bundle')->load('document');
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 document 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_document',
66
    ];
67
    $field_map = [
68
      'mime' => 'field_mime_type',
69
      'size' => 'field_document_size',
70
    ];
71
72
    $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
73
    $this->assertEqual($this->testBundle->get('label'), 'Document', 'Correct label detected.');
74
    $this->assertEqual($this->testBundle->get('description'), 'Use Document for uploading document files such as PDF.', 'Correct description detected.');
75
    $this->assertEqual($this->testBundle->get('type'), 'document', 'Correct plugin ID detected.');
76
    $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
77
    $this->assertEqual($this->testBundle->get('field_map'), $field_map, 'Correct field map detected.');
78
  }
79
80
  /**
81
   * Tests thumbnails of the document items.
82
   */
83
  public function testDocumentItemThumbnail() {
84
    // Test thumbnail of PDF file.
85
    $file = drupal_get_path('module', 'media') . '/files/Test.pdf';
86
    $this->drupalGet('media/add/document');
87
    $edit = [
88
      'files[field_document_0]' => $file,
89
    ];
90
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
91
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
92
    $edit = [
93
      'name[0][value]' => 'PDF file',
94
      'form_id' => 'media_document_form',
95
      'field_document[0][fids]' => $fid,
96
      'field_document[0][display]' => 1,
97
    ];
98
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
99
    $thumbnail = $this->getMostRecentThumbnail();
100
    $this->assertEqual('public://media-icons/generic/application-pdf.png', $thumbnail, "Correct thumbnail detected for PDF File.");
101
102
    // Test thumbnail of DOC file.
103
    $file = drupal_get_path('module', 'media') . '/files/Test.doc';
104
    $this->drupalGet('media/add/document');
105
    $edit = [
106
      'files[field_document_0]' => $file,
107
    ];
108
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
109
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
110
    $edit = [
111
      'name[0][value]' => 'DOC file',
112
      'form_id' => 'media_document_form',
113
      'field_document[0][fids]' => $fid,
114
      'field_document[0][display]' => 1,
115
    ];
116
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
117
    $thumbnail = $this->getMostRecentThumbnail();
118
    $this->assertEqual('public://media-icons/generic/application-msword.png', $thumbnail, "Correct thumbnail detected for DOC File.");
119
120
    // Test thumbnail of DOCX file.
121
    $file = drupal_get_path('module', 'media') . '/files/Test.docx';
122
    $this->drupalGet('media/add/document');
123
    $edit = [
124
      'files[field_document_0]' => $file,
125
    ];
126
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
127
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
128
    $edit = [
129
      'name[0][value]' => 'DOCX file',
130
      'form_id' => 'media_document_form',
131
      'field_document[0][fids]' => $fid,
132
      'field_document[0][display]' => 1,
133
    ];
134
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
135
    $thumbnail = $this->getMostRecentThumbnail();
136
    $this->assertEqual('public://media-icons/generic/application-vnd.openxmlformats-officedocument.wordprocessingml.document.png', $thumbnail, "Correct thumbnail detected for DOCX File.");
137
138
    // Test thumbnail of ODS file.
139
    $file = drupal_get_path('module', 'media') . '/files/Test.ods';
140
    $this->drupalGet('media/add/document');
141
    $edit = [
142
      'files[field_document_0]' => $file,
143
    ];
144
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
145
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
146
    $edit = [
147
      'name[0][value]' => 'ODS file',
148
      'form_id' => 'media_document_form',
149
      'field_document[0][fids]' => $fid,
150
      'field_document[0][display]' => 1,
151
    ];
152
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
153
    $thumbnail = $this->getMostRecentThumbnail();
154
    $this->assertEqual('public://media-icons/generic/application-vnd.oasis.opendocument.spreadsheet.png', $thumbnail, "Correct thumbnail detected for ODS File.");
155
156
    // Test thumbnail of ODT file.
157
    $file = drupal_get_path('module', 'media') . '/files/Test.odt';
158
    $this->drupalGet('media/add/document');
159
    $edit = [
160
      'files[field_document_0]' => $file,
161
    ];
162
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
163
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
164
    $edit = [
165
      'name[0][value]' => 'ODT file',
166
      'form_id' => 'media_document_form',
167
      'field_document[0][fids]' => $fid,
168
      'field_document[0][display]' => 1,
169
    ];
170
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
171
    $thumbnail = $this->getMostRecentThumbnail();
172
    $this->assertEqual('public://media-icons/generic/application-vnd.oasis.opendocument.text.png', $thumbnail, "Correct thumbnail detected for ODT File.");
173
174
    // Test thumbnail of OTT file.
175
    $file = drupal_get_path('module', 'media') . '/files/Test.ott';
176
    $this->drupalGet('media/add/document');
177
    $edit = [
178
      'files[field_document_0]' => $file,
179
    ];
180
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
181
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
182
    $edit = [
183
      'name[0][value]' => 'OTT file',
184
      'form_id' => 'media_document_form',
185
      'field_document[0][fids]' => $fid,
186
      'field_document[0][display]' => 1,
187
    ];
188
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
189
    $thumbnail = $this->getMostRecentThumbnail();
190
    $this->assertEqual('public://media-icons/generic/application-vnd.oasis.opendocument.text-template.png', $thumbnail, "Correct thumbnail detected for OTT File.");
191
192
    // Test thumbnail of PPT file.
193
    $file = drupal_get_path('module', 'media') . '/files/Test.ppt';
194
    $this->drupalGet('media/add/document');
195
    $edit = [
196
      'files[field_document_0]' => $file,
197
    ];
198
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
199
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
200
    $edit = [
201
      'name[0][value]' => 'PPT file',
202
      'form_id' => 'media_document_form',
203
      'field_document[0][fids]' => $fid,
204
      'field_document[0][display]' => 1,
205
    ];
206
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
207
    $thumbnail = $this->getMostRecentThumbnail();
208
    $this->assertEqual('public://media-icons/generic/application-vnd.ms-powerpoint.png', $thumbnail, "Correct thumbnail detected for PPT File.");
209
210
    // Test thumbnail of PPTX file.
211
    $file = drupal_get_path('module', 'media') . '/files/Test.pptx';
212
    $this->drupalGet('media/add/document');
213
    $edit = [
214
      'files[field_document_0]' => $file,
215
    ];
216
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
217
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
218
    $edit = [
219
      'name[0][value]' => 'PPTX file',
220
      'form_id' => 'media_document_form',
221
      'field_document[0][fids]' => $fid,
222
      'field_document[0][display]' => 1,
223
    ];
224
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
225
    $thumbnail = $this->getMostRecentThumbnail();
226
    $this->assertEqual('public://media-icons/generic/application-vnd.openxmlformats-officedocument.presentationml.presentation.png', $thumbnail, "Correct thumbnail detected for PPTX File.");
227
228
    // Test thumbnail of RTF file.
229
    $file = drupal_get_path('module', 'media') . '/files/Test.rtf';
230
    $this->drupalGet('media/add/document');
231
    $edit = [
232
      'files[field_document_0]' => $file,
233
    ];
234
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
235
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
236
    $edit = [
237
      'name[0][value]' => 'RTF file',
238
      'form_id' => 'media_document_form',
239
      'field_document[0][fids]' => $fid,
240
      'field_document[0][display]' => 1,
241
    ];
242
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
243
    $thumbnail = $this->getMostRecentThumbnail();
244
    $this->assertEqual('public://media-icons/generic/application-rtf.png', $thumbnail, "Correct thumbnail detected for RTF File.");
245
246
    // Test thumbnail of TXT file.
247
    $file = drupal_get_path('module', 'media') . '/files/Test.txt';
248
    $this->drupalGet('media/add/document');
249
    $edit = [
250
      'files[field_document_0]' => $file,
251
    ];
252
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
253
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
254
    $edit = [
255
      'name[0][value]' => 'TXT file',
256
      'form_id' => 'media_document_form',
257
      'field_document[0][fids]' => $fid,
258
      'field_document[0][display]' => 1,
259
    ];
260
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
261
    $thumbnail = $this->getMostRecentThumbnail();
262
    $this->assertEqual('public://media-icons/generic/text-plain.png', $thumbnail, "Correct thumbnail detected for TXT File.");
263
264
    // Test thumbnail of XLS file.
265
    $file = drupal_get_path('module', 'media') . '/files/Test.xls';
266
    $this->drupalGet('media/add/document');
267
    $edit = [
268
      'files[field_document_0]' => $file,
269
    ];
270
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
271
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
272
    $edit = [
273
      'name[0][value]' => 'XLS file',
274
      'form_id' => 'media_document_form',
275
      'field_document[0][fids]' => $fid,
276
      'field_document[0][display]' => 1,
277
    ];
278
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
279
    $thumbnail = $this->getMostRecentThumbnail();
280
    $this->assertEqual('public://media-icons/generic/application-vnd.ms-excel.png', $thumbnail, "Correct thumbnail detected for XLS File.");
281
282
    // Test thumbnail of XLSX file.
283
    $file = drupal_get_path('module', 'media') . '/files/Test.xlsx';
284
    $this->drupalGet('media/add/document');
285
    $edit = [
286
      'files[field_document_0]' => $file,
287
    ];
288
    $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
289
    $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
290
    $edit = [
291
      'name[0][value]' => 'XLSX file',
292
      'form_id' => 'media_document_form',
293
      'field_document[0][fids]' => $fid,
294
      'field_document[0][display]' => 1,
295
    ];
296
    $this->drupalPostForm(NULL, $edit, t('Save and publish'));
297
    $thumbnail = $this->getMostRecentThumbnail();
298
    $this->assertEqual('public://media-icons/generic/application-vnd.openxmlformats-officedocument.spreadsheetml.sheet.png', $thumbnail, "Correct thumbnail detected for XLSX File.");
299
  }
300
301
  /**
302
   * Returns the thumbnail of the most recent document.
303
   *
304
   * @return string
305
   *   Path of the thumbnail.
306
   */
307
  public function getMostRecentThumbnail() {
308
    $document_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'document')->sort('created', 'DESC')->execute();
309
    $item = $this->container->get('entity_type.manager')
310
      ->getStorage('media')
311
      ->loadUnchanged(reset($document_id));
312
    return $item->getType()->thumbnail($item);
313
  }
314
315
}
316