Further modules included.
[yaffs-website] / web / modules / contrib / media / src / Tests / DocumentBundleTest.php
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     '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('document');
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     ]);
58     $this->drupalLogin($adminUser);
59   }
60
61   /**
62    * Tests document media bundle creation from config files.
63    */
64   public function testMediaBundleCreationFromModule() {
65     $type_configuration = [
66       'source_field' => 'field_document',
67     ];
68     $field_map = [
69       'mime' => 'field_mime_type',
70       'size' => 'field_document_size',
71     ];
72
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.');
79   }
80
81   /**
82    * Tests thumbnails of the document items.
83    */
84   public function testDocumentItemThumbnail() {
85     // Array of test files and corresponding file icons.
86     $files = [
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',
99     ];
100
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');
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]' => $name,
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       $recentThumbnail = $this->getMostRecentThumbnail();
118       $this->assertEqual($thumbnail, $recentThumbnail, "Correct thumbnail detected for " . $fileName);
119     }
120   }
121
122   /**
123    * Returns the thumbnail of the most recent document.
124    *
125    * @return string
126    *   Path of the thumbnail.
127    */
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);
134   }
135
136 }