3 namespace Drupal\media\Tests;
5 use Drupal\simpletest\WebTestBase;
8 * Ensures that media bundle for document can be created.
12 class DocumentBundleTest 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_document',
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('document');
49 $adminUser = $this->drupalCreateUser([
56 'access media overview',
58 $this->drupalLogin($adminUser);
62 * Tests document media bundle creation from config files.
64 public function testMediaBundleCreationFromModule() {
65 $type_configuration = [
66 'source_field' => 'field_document',
69 'mime' => 'field_mime_type',
70 'size' => 'field_document_size',
73 $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
74 $this->assertEqual($this->testBundle->get('label'), 'Document', 'Correct label detected.');
75 $this->assertEqual($this->testBundle->get('description'), 'Use Document for uploading document files such as PDF.', 'Correct description detected.');
76 $this->assertEqual($this->testBundle->get('type'), 'document', 'Correct plugin ID detected.');
77 $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
78 $this->assertEqual($this->testBundle->get('field_map'), $field_map, 'Correct field map detected.');
82 * Tests thumbnails of the document items.
84 public function testDocumentItemThumbnail() {
85 // Array of test files and corresponding file icons.
87 'Test.pdf' => 'public://media-icons/generic/application-pdf.png',
88 'Test.doc' => 'public://media-icons/generic/application-msword.png',
89 'Test.docx' => 'public://media-icons/generic/application-vnd.openxmlformats-officedocument.wordprocessingml.document.png',
90 'Test.ods' => 'public://media-icons/generic/application-vnd.oasis.opendocument.spreadsheet.png',
91 'Test.odt' => 'public://media-icons/generic/application-vnd.oasis.opendocument.text.png',
92 'Test.ott' => 'public://media-icons/generic/application-vnd.oasis.opendocument.text-template.png',
93 'Test.ppt' => 'public://media-icons/generic/application-vnd.ms-powerpoint.png',
94 'Test.pptx' => 'public://media-icons/generic/application-vnd.openxmlformats-officedocument.presentationml.presentation.png',
95 'Test.rtf' => 'public://media-icons/generic/application-rtf.png',
96 'Test.txt' => 'public://media-icons/generic/text-plain.png',
97 'Test.xls' => 'public://media-icons/generic/application-vnd.ms-excel.png',
98 'Test.xlsx' => 'public://media-icons/generic/application-vnd.openxmlformats-officedocument.spreadsheetml.sheet.png',
101 foreach ($files as $fileName => $thumbnail) {
102 $file = drupal_get_path('module', 'media') . '/files/' . $fileName;
103 $name = $this->randomMachineName();
104 $this->drupalGet('media/add/document');
106 'files[field_document_0]' => $file,
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'));
111 'name[0][value]' => $name,
112 'form_id' => 'media_document_form',
113 'field_document[0][fids]' => $fid,
114 'field_document[0][display]' => 1,
116 $this->drupalPostForm(NULL, $edit, t('Save and publish'));
117 $recentThumbnail = $this->getMostRecentThumbnail();
118 $this->assertEqual($thumbnail, $recentThumbnail, "Correct thumbnail detected for " . $fileName);
123 * Returns the thumbnail of the most recent document.
126 * Path of the thumbnail.
128 public function getMostRecentThumbnail() {
129 $document_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'document')->sort('created', 'DESC')->execute();
130 $item = $this->container->get('entity_type.manager')
131 ->getStorage('media')
132 ->loadUnchanged(reset($document_id));
133 return $item->getType()->thumbnail($item);