View source
<?php
namespace Drupal\Tests\embed\FunctionalJavascript;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class EmbedButtonAdminTest extends WebDriverTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'block',
'embed',
'embed_test',
'editor',
'ckeditor',
];
protected $adminUser;
protected $webUser;
protected function setUp() {
parent::setUp();
$format = FilterFormat::create([
'format' => 'embed_test',
'name' => 'Embed format',
'filters' => [],
]);
$format
->save();
$editor_group = [
'name' => 'Embed',
'items' => [
'embed_test_default',
],
];
$editor = Editor::create([
'format' => 'embed_test',
'editor' => 'ckeditor',
'settings' => [
'toolbar' => [
'rows' => [
[
$editor_group,
],
],
],
],
]);
$editor
->save();
$this->adminUser = $this
->drupalCreateUser([
'administer embed buttons',
'use text format embed_test',
]);
$this->webUser = $this
->drupalCreateUser([
'use text format embed_test',
]);
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('local_actions_block');
}
public function testEmbedButtonAdmin() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('admin/config/content/embed');
$assert_session
->pageTextContains('You are not authorized to access this page.');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/embed');
$this
->clickLink('Add embed button');
$button_label = $this
->randomMachineName();
$button_id = strtolower($button_label);
$page
->fillField('label', $button_label);
$this
->assertNotEmpty($assert_session
->waitForText("Machine name: {$button_id}"));
$edit = [
'type_id' => 'embed_test_default',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->drupalGet('admin/config/content/embed');
$assert_session
->pageTextContains($button_label);
$this
->drupalGet('admin/config/content/embed/button/manage/' . $button_id);
$new_button_label = $this
->randomMachineName();
$edit = [
'label' => $new_button_label,
];
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->drupalGet('admin/config/content/embed');
$assert_session
->pageTextContains($new_button_label);
$assert_session
->pageTextNotContains($button_label);
$this
->drupalGet('admin/config/content/embed/button/manage/' . $button_id . '/delete');
$this
->drupalPostForm(NULL, [], 'Delete');
$this
->drupalGet('admin/config/content/embed/button/manage/' . $button_id);
$assert_session
->pageTextContains('The requested page could not be found.');
$this
->drupalGet('admin/config/content/embed');
$assert_session
->pageTextNotContains($button_label);
}
public function testButtonValidation() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/embed/button/add');
$button_label = $this
->randomMachineName();
$button_id = strtolower($button_label);
$page
->fillField('label', $button_label);
$this
->assertNotEmpty($assert_session
->waitForText("Machine name: {$button_id}"));
$page
->selectFieldOption('type_id', 'embed_test_aircraft');
$aircraft_type = $assert_session
->waitForField('type_settings[aircraft_type]');
$this
->assertNotEmpty($aircraft_type);
$this
->assertSame('fixed-wing', $aircraft_type
->getValue());
$edit['type_settings[aircraft_type]'] = 'invalid';
$this
->drupalPostForm(NULL, $edit, 'Save');
$assert_session
->pageTextContains('Cannot select invalid aircraft type.');
$edit['type_settings[aircraft_type]'] = 'helicopters';
$this
->drupalPostForm(NULL, $edit, 'Save');
$assert_session
->pageTextContains('Helicopters are just rotorcraft.');
$this
->drupalGet('admin/config/content/embed/button/manage/' . $button_id);
$this
->assertFieldByName('type_settings[aircraft_type]', 'rotorcraft');
}
public function testCKEditorButtonConflict() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/embed/button/add');
$button_label = $this
->randomMachineName();
$button_id = strtolower($button_label);
$page
->fillField('label', $button_label);
$this
->assertNotEmpty($assert_session
->waitForText("Machine name: {$button_id}"));
$assert_session
->elementExists('css', '#edit-label-machine-name-suffix')
->pressButton('Edit');
$id = $assert_session
->waitForField('id');
$this
->assertNotEmpty($id);
$id
->setValue('DrupalImage');
$edit = [
'type_id' => 'embed_test_default',
];
$this
->drupalPostForm(NULL, $edit, 'Save');
}
}