Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / media / tests / src / FunctionalJavascript / MediaSourceAudioVideoTest.php
1 <?php
2
3 namespace Drupal\Tests\media\FunctionalJavascript;
4
5 use Drupal\Core\Entity\Entity\EntityViewDisplay;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\file\Entity\File;
9
10 /**
11  * Tests the Audio and Video media sources.
12  *
13  * @group media
14  */
15 class MediaSourceAudioVideoTest extends MediaSourceTestBase {
16
17   /**
18    * Check the Audio source functionality.
19    */
20   public function testAudioTypeCreation() {
21     $assert_session = $this->assertSession();
22     $page = $this->getSession()->getPage();
23
24     $source_id = 'audio_file';
25     $type_name = 'audio_type';
26     $field_name = 'field_media_' . $source_id;
27     $this->doTestCreateMediaType($type_name, $source_id);
28
29     // Check that the source field was created with the correct settings.
30     $storage = FieldStorageConfig::load("media.$field_name");
31     $this->assertInstanceOf(FieldStorageConfig::class, $storage);
32     $field = FieldConfig::load("media.$type_name.$field_name");
33     $this->assertInstanceOf(FieldConfig::class, $field);
34     $this->assertSame('mp3 wav aac', FieldConfig::load("media.$type_name.$field_name")->get('settings')['file_extensions']);
35
36     // Check that the display holds the correct formatter configuration.
37     $display = EntityViewDisplay::load("media.$type_name.default");
38     $this->assertInstanceOf(EntityViewDisplay::class, $display);
39     $formatter = $display->getComponent($field_name)['type'];
40     $this->assertSame('file_audio', $formatter);
41
42     // Create a media asset.
43     file_put_contents('public://file.mp3', str_repeat('t', 10));
44     $file = File::create([
45       'uri' => 'public://file.mp3',
46       'filename' => 'file.mp3',
47     ]);
48     $file->save();
49
50     $this->drupalGet("media/add/$type_name");
51     $page->fillField('Name', 'Audio media asset');
52     $page->attachFileToField("files[{$field_name}_0]", \Drupal::service('file_system')->realpath('public://file.mp3'));
53     $result = $assert_session->waitForButton('Remove');
54     $this->assertNotEmpty($result);
55     $page->pressButton('Save');
56
57     // Verify that there is a creation message and that it contains a link to
58     // the media entity.
59     $assert_session->pageTextContains("$type_name Audio media asset has been created.");
60     $this->drupalGet($this->assertLinkToCreatedMedia());
61
62     // Verify that the <audio> tag is present on the media entity view.
63     $assert_session->elementExists('css', "audio > source[type='audio/mpeg']");
64   }
65
66   /**
67    * Check the Video source functionality.
68    */
69   public function testVideoTypeCreation() {
70     $assert_session = $this->assertSession();
71     $page = $this->getSession()->getPage();
72
73     $source_id = 'video_file';
74     $type_name = 'video_type';
75     $field_name = 'field_media_' . $source_id;
76     $this->doTestCreateMediaType($type_name, $source_id);
77
78     // Check that the source field was created with the correct settings.
79     $storage = FieldStorageConfig::load("media.$field_name");
80     $this->assertInstanceOf(FieldStorageConfig::class, $storage);
81     $field = FieldConfig::load("media.$type_name.$field_name");
82     $this->assertInstanceOf(FieldConfig::class, $field);
83     $this->assertSame('mp4', FieldConfig::load("media.$type_name.$field_name")->getSetting('file_extensions'));
84
85     // Check that the display holds the correct formatter configuration.
86     $display = EntityViewDisplay::load("media.$type_name.default");
87     $this->assertInstanceOf(EntityViewDisplay::class, $display);
88     $formatter = $display->getComponent($field_name)['type'];
89     $this->assertSame('file_video', $formatter);
90
91     // Create a media asset.
92     file_put_contents('public://file.mp4', str_repeat('t', 10));
93     $file = File::create([
94       'uri' => 'public://file.mp4',
95       'filename' => 'file.mp4',
96     ]);
97     $file->save();
98
99     $this->drupalGet("media/add/$type_name");
100     $page->fillField('Name', 'Video media asset');
101     $page->attachFileToField("files[{$field_name}_0]", \Drupal::service('file_system')->realpath('public://file.mp4'));
102     $result = $assert_session->waitForButton('Remove');
103     $this->assertNotEmpty($result);
104     $page->pressButton('Save');
105
106     // Verify that there is a creation message and that it contains a link to
107     // the media entity.
108     $assert_session->pageTextContains("$type_name Video media asset has been created.");
109
110     $this->drupalGet($this->assertLinkToCreatedMedia());
111     // Verify that the <video> tag is present on the media entity view.
112     $assert_session->elementExists('css', "video > source[type='video/mp4']");
113   }
114
115 }