1<?php 2 3namespace Drupal\Tests\media\FunctionalJavascript; 4 5use Drupal\field\FieldConfigInterface; 6use Drupal\media\Entity\Media; 7use Drupal\media\Entity\MediaType; 8use Drupal\media\MediaSourceInterface; 9 10/** 11 * Ensures that media UI works correctly. 12 * 13 * @group media 14 */ 15class MediaUiJavascriptTest extends MediaJavascriptTestBase { 16 17 /** 18 * Modules to enable. 19 * 20 * @var array 21 */ 22 public static $modules = [ 23 'block', 24 'media_test_source', 25 ]; 26 27 /** 28 * {@inheritdoc} 29 */ 30 protected $defaultTheme = 'stark'; 31 32 /** 33 * The test media type. 34 * 35 * @var \Drupal\media\MediaTypeInterface 36 */ 37 protected $testMediaType; 38 39 /** 40 * {@inheritdoc} 41 */ 42 protected function setUp() { 43 parent::setUp(); 44 $this->drupalPlaceBlock('local_actions_block'); 45 $this->drupalPlaceBlock('local_tasks_block'); 46 } 47 48 /** 49 * Tests a media type administration. 50 */ 51 public function testMediaTypes() { 52 $session = $this->getSession(); 53 $page = $session->getPage(); 54 $assert_session = $this->assertSession(); 55 56 $this->drupalGet('admin/structure/media'); 57 $assert_session->pageTextContains('No media types available. Add media type.'); 58 $assert_session->linkExists('Add media type'); 59 60 // Test the creation of a media type using the UI. 61 $name = $this->randomMachineName(); 62 $description = $this->randomMachineName(); 63 $this->drupalGet('admin/structure/media/add'); 64 $page->fillField('label', $name); 65 $machine_name = strtolower($name); 66 $this->assertJsCondition("jQuery('.machine-name-value').html() == '$machine_name'"); 67 $page->selectFieldOption('source', 'test'); 68 $this->assertJsCondition("jQuery('.form-item-source-configuration-test-config-value').length > 0;"); 69 $page->fillField('description', $description); 70 $page->pressButton('Save'); 71 // The wait prevents intermittent test failures. 72 $result = $assert_session->waitForLink('Add media type'); 73 $this->assertNotEmpty($result); 74 $assert_session->addressEquals('admin/structure/media'); 75 $assert_session->pageTextContains('The media type ' . $name . ' has been added.'); 76 $this->drupalGet('admin/structure/media'); 77 $assert_session->pageTextContains($name); 78 $assert_session->pageTextContains($description); 79 80 // We need to clear the statically cached field definitions to account for 81 // fields that have been created by API calls in this test, since they exist 82 // in a separate memory space from the web server. 83 $this->container->get('entity_field.manager')->clearCachedFieldDefinitions(); 84 // Assert that the field and field storage were created. 85 $media_type = MediaType::load($machine_name); 86 $source = $media_type->getSource(); 87 /** @var \Drupal\field\FieldConfigInterface $source_field */ 88 $source_field = $source->getSourceFieldDefinition($media_type); 89 $this->assertInstanceOf(FieldConfigInterface::class, $source_field); 90 $this->assertFalse($source_field->isNew(), 'Source field was saved.'); 91 /** @var \Drupal\field\FieldStorageConfigInterface $storage */ 92 $storage = $source_field->getFieldStorageDefinition(); 93 $this->assertFalse($storage->isNew(), 'Source field storage definition was saved.'); 94 $this->assertFalse($storage->isLocked(), 'Source field storage definition was not locked.'); 95 96 /** @var \Drupal\media\MediaTypeInterface $media_type_storage */ 97 $media_type_storage = $this->container->get('entity_type.manager')->getStorage('media_type'); 98 $this->testMediaType = $media_type_storage->load(strtolower($name)); 99 100 // Check if all action links exist. 101 $assert_session->linkByHrefExists('admin/structure/media/add'); 102 $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id()); 103 $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/fields'); 104 $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/form-display'); 105 $assert_session->linkByHrefExists('admin/structure/media/manage/' . $this->testMediaType->id() . '/display'); 106 107 // Assert that fields have expected values before editing. 108 $page->clickLink('Edit'); 109 $assert_session->fieldValueEquals('label', $name); 110 $assert_session->fieldValueEquals('description', $description); 111 $assert_session->fieldValueEquals('source', 'test'); 112 $assert_session->fieldValueEquals('label', $name); 113 $assert_session->checkboxNotChecked('edit-options-new-revision'); 114 $assert_session->checkboxChecked('edit-options-status'); 115 $assert_session->checkboxNotChecked('edit-options-queue-thumbnail-downloads'); 116 $assert_session->pageTextContains('Create new revision'); 117 $assert_session->pageTextContains('Automatically create new revisions. Users with the "Administer media" permission will be able to override this option.'); 118 $assert_session->pageTextContains('Download thumbnails via a queue.'); 119 $assert_session->pageTextContains('Media will be automatically published when created.'); 120 $assert_session->pageTextContains('Media sources can provide metadata fields such as title, caption, size information, credits, etc. Media can automatically save this metadata information to entity fields, which can be configured below. Information will only be mapped if the entity field is empty.'); 121 122 // Try to change media type and check if new configuration sub-form appears. 123 $page->selectFieldOption('source', 'test'); 124 $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]'); 125 $this->assertNotEmpty($result); 126 $assert_session->fieldExists('Test config value'); 127 $assert_session->fieldValueEquals('Test config value', 'This is default value.'); 128 $assert_session->fieldExists('Attribute 1'); 129 $assert_session->fieldExists('Attribute 2'); 130 131 // Test if the edit machine name is not editable. 132 $assert_session->fieldDisabled('Machine-readable name'); 133 134 // Edit and save media type form fields with new values. 135 $new_name = $this->randomMachineName(); 136 $new_description = $this->randomMachineName(); 137 $page->fillField('label', $new_name); 138 $page->fillField('description', $new_description); 139 $page->selectFieldOption('source', 'test'); 140 $page->fillField('Test config value', 'This is new config value.'); 141 $page->selectFieldOption('field_map[attribute_1]', 'name'); 142 $page->checkField('options[new_revision]'); 143 $page->uncheckField('options[status]'); 144 $page->checkField('options[queue_thumbnail_downloads]'); 145 $page->pressButton('Save'); 146 // The wait prevents intermittent test failures. 147 $result = $assert_session->waitForLink('Add media type'); 148 $this->assertNotEmpty($result); 149 $assert_session->addressEquals('admin/structure/media'); 150 $assert_session->pageTextContains("The media type $new_name has been updated."); 151 152 // Test if edit worked and if new field values have been saved as expected. 153 $this->drupalGet('admin/structure/media/manage/' . $this->testMediaType->id()); 154 $assert_session->fieldValueEquals('label', $new_name); 155 $assert_session->fieldValueEquals('description', $new_description); 156 $assert_session->fieldValueEquals('source', 'test'); 157 $assert_session->checkboxChecked('options[new_revision]'); 158 $assert_session->checkboxNotChecked('options[status]'); 159 $assert_session->checkboxChecked('options[queue_thumbnail_downloads]'); 160 $assert_session->fieldValueEquals('Test config value', 'This is new config value.'); 161 $assert_session->fieldValueEquals('Attribute 1', 'name'); 162 $assert_session->fieldValueEquals('Attribute 2', MediaSourceInterface::METADATA_FIELD_EMPTY); 163 164 /** @var \Drupal\media\MediaTypeInterface $loaded_media_type */ 165 $loaded_media_type = $this->container->get('entity_type.manager') 166 ->getStorage('media_type') 167 ->load($this->testMediaType->id()); 168 $this->assertSame($loaded_media_type->id(), $this->testMediaType->id()); 169 $this->assertSame($loaded_media_type->label(), $new_name); 170 $this->assertSame($loaded_media_type->getDescription(), $new_description); 171 $this->assertSame($loaded_media_type->getSource()->getPluginId(), 'test'); 172 $this->assertSame($loaded_media_type->getSource()->getConfiguration()['test_config_value'], 'This is new config value.'); 173 $this->assertTrue($loaded_media_type->shouldCreateNewRevision()); 174 $this->assertTrue($loaded_media_type->thumbnailDownloadsAreQueued()); 175 $this->assertFalse($loaded_media_type->getStatus()); 176 $this->assertSame($loaded_media_type->getFieldMap(), ['attribute_1' => 'name']); 177 178 // We need to clear the statically cached field definitions to account for 179 // fields that have been created by API calls in this test, since they exist 180 // in a separate memory space from the web server. 181 $this->container->get('entity_field.manager')->clearCachedFieldDefinitions(); 182 183 // Test that a media item being created with default status to "FALSE", 184 // will be created unpublished. 185 /** @var \Drupal\media\MediaInterface $unpublished_media */ 186 $unpublished_media = Media::create(['name' => 'unpublished test media', 'bundle' => $loaded_media_type->id()]); 187 $this->assertFalse($unpublished_media->isPublished()); 188 $unpublished_media->delete(); 189 190 // Tests media type delete form. 191 $page->clickLink('Delete'); 192 $assert_session->addressEquals('admin/structure/media/manage/' . $this->testMediaType->id() . '/delete'); 193 $page->pressButton('Delete'); 194 $assert_session->addressEquals('admin/structure/media'); 195 $assert_session->pageTextContains('The media type ' . $new_name . ' has been deleted.'); 196 197 // Test that the system for preventing the deletion of media types works 198 // (they cannot be deleted if there is media content of that type/bundle). 199 $media_type2 = $this->createMediaType('test'); 200 $label2 = $media_type2->label(); 201 $media = Media::create(['name' => 'lorem ipsum', 'bundle' => $media_type2->id()]); 202 $media->save(); 203 $this->drupalGet('admin/structure/media/manage/' . $media_type2->id()); 204 $page->clickLink('Delete'); 205 $assert_session->addressEquals('admin/structure/media/manage/' . $media_type2->id() . '/delete'); 206 $assert_session->buttonNotExists('edit-submit'); 207 $assert_session->pageTextContains("$label2 is used by 1 media item on your site. You can not remove this media type until you have removed all of the $label2 media items."); 208 } 209 210} 211