Completed
Push — 8.x-1.x ( bea2b9...d5cdc8 )
by Tadej
9s
created

DocumentBundleTest::getMostRecentThumbnail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
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
    // Array of test files and corresponding file icons.
85
    $files = [
86
      'Test.pdf' => 'public://media-icons/generic/application-pdf.png',
87
      'Test.doc' => 'public://media-icons/generic/application-msword.png',
88
      'Test.docx' => 'public://media-icons/generic/application-vnd.openxmlformats-officedocument.wordprocessingml.document.png',
89
      'Test.ods' => 'public://media-icons/generic/application-vnd.oasis.opendocument.spreadsheet.png',
90
      'Test.odt' => 'public://media-icons/generic/application-vnd.oasis.opendocument.text.png',
91
      'Test.ott' => 'public://media-icons/generic/application-vnd.oasis.opendocument.text-template.png',
92
      'Test.ppt' => 'public://media-icons/generic/application-vnd.ms-powerpoint.png',
93
      'Test.pptx' => 'public://media-icons/generic/application-vnd.openxmlformats-officedocument.presentationml.presentation.png',
94
      'Test.rtf' => 'public://media-icons/generic/application-rtf.png',
95
      'Test.txt' => 'public://media-icons/generic/text-plain.png',
96
      'Test.xls' => 'public://media-icons/generic/application-vnd.ms-excel.png',
97
      'Test.xlsx' => 'public://media-icons/generic/application-vnd.openxmlformats-officedocument.spreadsheetml.sheet.png',
98
    ];
99
100
    foreach ($files as $fileName => $thumbnail) {
101
      $file = drupal_get_path('module', 'media') . '/files/' . $fileName;
102
      $name = $this->randomMachineName();
103
      $this->drupalGet('media/add/document');
104
      $edit = [
105
        'files[field_document_0]' => $file,
106
      ];
107
      $this->drupalPostAjaxForm(NULL, $edit, "field_document_0_upload_button");
108
      $fid = (string) current($this->xpath('//input[@data-drupal-selector="edit-field-document-0-fids"]/@value'));
109
      $edit = [
110
        'name[0][value]' => $name,
111
        'form_id' => 'media_document_form',
112
        'field_document[0][fids]' => $fid,
113
        'field_document[0][display]' => 1,
114
      ];
115
      $this->drupalPostForm(NULL, $edit, t('Save and publish'));
116
      $recentThumbnail = $this->getMostRecentThumbnail();
117
      $this->assertEqual($thumbnail, $recentThumbnail, "Correct thumbnail detected for " . $fileName);
118
    }
119
  }
120
121
  /**
122
   * Returns the thumbnail of the most recent document.
123
   *
124
   * @return string
125
   *   Path of the thumbnail.
126
   */
127
  public function getMostRecentThumbnail() {
128
    $document_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'document')->sort('created', 'DESC')->execute();
129
    $item = $this->container->get('entity_type.manager')
130
      ->getStorage('media')
131
      ->loadUnchanged(reset($document_id));
132
    return $item->getType()->thumbnail($item);
133
  }
134
135
}
136