3 namespace Drupal\Tests\media\FunctionalJavascript;
5 use Drupal\Component\Utility\Html;
8 * Tests the media type creation.
12 class MediaTypeCreationTest extends MediaJavascriptTestBase {
15 * Tests the media type creation form.
17 public function testMediaTypeCreationFormWithDefaultField() {
18 $session = $this->getSession();
19 $page = $session->getPage();
20 $assert_session = $this->assertSession();
22 $label = 'Type with Default Field';
23 $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
25 $this->drupalGet('admin/structure/media/add');
27 // Fill in a label to the media type.
28 $page->fillField('label', $label);
29 // Wait for machine name generation. Default: waitUntilVisible(), does not
31 $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
33 // Select the media source used by our media type.
34 $assert_session->fieldExists('Media source');
35 $assert_session->optionExists('Media source', 'test');
36 $page->selectFieldOption('Media source', 'test');
37 $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
38 $this->assertNotEmpty($result);
40 $page->pressButton('Save');
42 // Check whether the source field was correctly created.
43 $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
45 // Check 2nd column of first data row, to be machine name for field name.
46 $assert_session->elementContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[2]', 'field_media_test');
47 // Check 3rd column of first data row, to be correct field type.
48 $assert_session->elementTextContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[3]', 'Text (plain)');
50 // Check that the source field is correctly assigned to media type.
51 $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");
53 $assert_session->pageTextContains('Test source field is used to store the essential information about the media item.');
55 // Check that the plugin cannot be changed after it is set on type creation.
56 $assert_session->fieldDisabled('Media source');
57 $assert_session->pageTextContains('The media source cannot be changed after the media type is created.');
59 // Open up the media add form and verify that the source field is right
60 // after the name, and before the vertical tabs.
61 $this->drupalGet("/media/add/$mediaTypeMachineName");
63 // Get the form element, and its HTML representation.
64 $form_selector = '#media-' . Html::cleanCssIdentifier($mediaTypeMachineName) . '-add-form';
65 $form = $assert_session->elementExists('css', $form_selector);
66 $form_html = $form->getOuterHtml();
68 // The name field should come before the source field, which should itself
69 // come before the vertical tabs.
70 $name_field = $assert_session->fieldExists('Name', $form)->getOuterHtml();
71 $test_source_field = $assert_session->fieldExists('Test source', $form)->getOuterHtml();
72 $vertical_tabs = $assert_session->elementExists('css', '.vertical-tabs', $form)->getOuterHtml();
73 $date_field = $assert_session->fieldExists('Date', $form)->getOuterHtml();
74 $published_checkbox = $assert_session->fieldExists('Published', $form)->getOuterHtml();
75 $this->assertTrue(strpos($form_html, $test_source_field) > strpos($form_html, $name_field));
76 $this->assertTrue(strpos($form_html, $vertical_tabs) > strpos($form_html, $test_source_field));
77 // The "Published" checkbox should be the last element.
78 $this->assertTrue(strpos($form_html, $published_checkbox) > strpos($form_html, $date_field));
80 // Check that a new type with the same machine name cannot be created.
81 $this->drupalGet('admin/structure/media/add');
82 $page->fillField('label', $label);
83 $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
84 $page->selectFieldOption('Media source', 'test');
85 $assert_session->assertWaitOnAjaxRequest();
86 $page->pressButton('Save');
87 $assert_session->pageTextContains('The machine-readable name is already in use. It must be unique.');
91 * Test creation of media type, reusing an existing source field.
93 public function testMediaTypeCreationReuseSourceField() {
94 $session = $this->getSession();
95 $page = $session->getPage();
96 $assert_session = $this->assertSession();
98 // Create a new media type, which should create a new field we can reuse.
99 $this->drupalGet('/admin/structure/media/add');
100 $page->fillField('label', 'Pastafazoul');
101 $session->wait(5000, "jQuery('.machine-name-value').text() === 'pastafazoul'");
102 $page->selectFieldOption('Media source', 'test');
103 $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
104 $this->assertNotEmpty($result);
105 $page->pressButton('Save');
107 $label = 'Type reusing Default Field';
108 $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
110 $this->drupalGet('admin/structure/media/add');
112 // Fill in a label to the media type.
113 $page->fillField('label', $label);
115 // Wait for machine name generation. Default: waitUntilVisible(), does not
117 $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
119 // Select the media source used by our media type.
120 $assert_session->fieldExists('Media source');
121 $assert_session->optionExists('Media source', 'test');
122 $page->selectFieldOption('Media source', 'test');
123 $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
124 $this->assertNotEmpty($result);
125 // Select the existing field for re-use.
126 $page->selectFieldOption('source_configuration[source_field]', 'field_media_test');
127 $page->pressButton('Save');
129 // Check that no new fields were created.
130 $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
131 // The reused field should be present...
132 $assert_session->pageTextContains('field_media_test');
133 // ...not a new, unique one.
134 $assert_session->pageTextNotContains('field_media_test_1');