View source
<?php
namespace Drupal\Tests\media\FunctionalJavascript;
use Drupal\Component\Utility\Html;
class MediaTypeCreationTest extends MediaJavascriptTestBase {
protected $defaultTheme = 'stark';
public function testSourceChangeOnMediaTypeCreationForm() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
$label = 'Type with Default Field';
$mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
$this
->drupalGet('admin/structure/media/add');
$page
->fillField('label', $label);
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', '.machine-name-value'));
$assert_session
->selectExists('Media source')
->selectOption('test_different_displays');
$this
->assertNotEmpty($assert_session
->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]'));
$assert_session
->selectExists('Media source')
->selectOption('test');
$this
->assertNotEmpty($assert_session
->waitForElement('css', 'fieldset[data-drupal-selector="edit-source-configuration"] .fieldset-wrapper .placeholder:contains("Text (plain)")'));
$page
->pressButton('Save');
$this
->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");
$assert_session
->pageTextContains('The media source cannot be changed after the media type is created');
$assert_session
->fieldDisabled('Media source');
}
public function testMediaTypeCreationFormWithDefaultField() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
$label = 'Type with Default Field';
$mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
$this
->drupalGet('admin/structure/media/add');
$assert_session
->fieldExists('Media source');
$assert_session
->optionExists('Media source', 'test');
$page
->selectFieldOption('Media source', 'test');
$result = $assert_session
->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
$this
->assertNotEmpty($result);
$page
->fillField('label', $label);
$session
->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
$page
->pressButton('Save');
$this
->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
$assert_session
->elementContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[2]', 'field_media_test');
$assert_session
->elementTextContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[3]', 'Text (plain)');
$this
->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");
$assert_session
->pageTextContains('Test source field is used to store the essential information about the media item.');
$assert_session
->fieldDisabled('Media source');
$assert_session
->pageTextContains('The media source cannot be changed after the media type is created.');
$this
->drupalGet("/media/add/{$mediaTypeMachineName}");
$form_selector = '#media-' . Html::cleanCssIdentifier($mediaTypeMachineName) . '-add-form';
$form = $assert_session
->elementExists('css', $form_selector);
$form_html = $form
->getOuterHtml();
$name_field = $assert_session
->fieldExists('Name', $form)
->getOuterHtml();
$test_source_field = $assert_session
->fieldExists('Test source', $form)
->getOuterHtml();
$vertical_tabs = $assert_session
->elementExists('css', '.vertical-tabs', $form)
->getOuterHtml();
$date_field = $assert_session
->fieldExists('Date', $form)
->getOuterHtml();
$published_checkbox = $assert_session
->fieldExists('Published', $form)
->getOuterHtml();
$this
->assertTrue(strpos($form_html, $test_source_field) > strpos($form_html, $name_field));
$this
->assertTrue(strpos($form_html, $vertical_tabs) > strpos($form_html, $test_source_field));
$this
->assertTrue(strpos($form_html, $published_checkbox) > strpos($form_html, $date_field));
$this
->drupalGet('admin/structure/media/add');
$page
->fillField('label', $label);
$session
->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
$page
->selectFieldOption('Media source', 'test');
$assert_session
->assertWaitOnAjaxRequest();
$page
->pressButton('Save');
$assert_session
->pageTextContains('The machine-readable name is already in use. It must be unique.');
}
public function testMediaTypeCreationReuseSourceField() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('/admin/structure/media/add');
$page
->selectFieldOption('Media source', 'test');
$result = $assert_session
->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
$this
->assertNotEmpty($result);
$page
->fillField('label', 'Pastafazoul');
$session
->wait(5000, "jQuery('.machine-name-value').text() === 'pastafazoul'");
$page
->pressButton('Save');
$label = 'Type reusing Default Field';
$mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
$this
->drupalGet('admin/structure/media/add');
$assert_session
->fieldExists('Media source');
$assert_session
->optionExists('Media source', 'test');
$page
->selectFieldOption('Media source', 'test');
$result = $assert_session
->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
$this
->assertNotEmpty($result);
$page
->selectFieldOption('source_configuration[source_field]', 'field_media_test');
$page
->fillField('label', $label);
$session
->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
$page
->pressButton('Save');
$this
->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
$assert_session
->pageTextContains('field_media_test');
$assert_session
->pageTextNotContains('field_media_test_1');
}
}