View source
<?php
namespace Drupal\Tests\xbbcode\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\BrowserTestBase;
class XBBCodeAdminTest extends BrowserTestBase {
public static $modules = [
'block',
'filter',
'node',
'xbbcode',
'xbbcode_test_plugin',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected $webUser;
protected $customTag;
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$this->adminUser = $this
->drupalCreateUser([
'administer filters',
'administer custom BBCode tags',
'administer BBCode tag sets',
'access site reports',
]);
$this->webUser = $this
->drupalCreateUser([
'create page content',
'edit own page content',
]);
$this
->drupalLogin($this->adminUser);
$this
->drupalPlaceBlock('local_actions_block');
$this->customTag = $this
->createCustomTag(FALSE);
}
private function createCustomTag($save = TRUE) : array {
$name = mb_strtolower($this
->randomMachineName());
$option = $this
->randomString();
$tag = [
'id' => mb_strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'description' => $this
->randomString(),
'name' => $name,
'sample' => "[{$name}='{$option}']" . $this
->randomMachineName() . "[/{$name}]",
'template_code' => '[' . $this
->randomMachineName() . '|{{ tag.option }}|{{ tag.content }}]',
'attached[library]' => "xbbcode/tag-form\nxbbcode_test_plugin/test-library",
];
if ($save) {
$this
->drupalGet('admin/config/content/xbbcode/tags/add');
$this
->submitForm($tag, 'Save');
$this
->assertSession()
->responseContains((string) new FormattableMarkup('The BBCode tag %tag has been created.', [
'%tag' => $tag['label'],
]));
}
return $tag;
}
public function testCustomTags() : void {
$this
->drupalGet('admin/config/content/xbbcode/tags');
$this
->assertSession()
->pageTextContains('Test Tag Label');
$this
->assertSession()
->pageTextContains('Test Tag Description');
$this
->assertSession()
->pageTextContains('[test_tag]Content[/test_tag]');
$this
->assertSession()
->linkByHrefNotExists('admin/config/content/xbbcode/tags/manage/test_tag_id/edit');
$this
->assertSession()
->linkByHrefNotExists('admin/config/content/xbbcode/tags/manage/test_tag_id/delete');
$this
->drupalGet('admin/config/content/xbbcode/tags/manage/test_tag_id/edit');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('admin/config/content/xbbcode/tags/manage/test_tag_id/delete');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('admin/config/content/xbbcode/tags');
$this
->assertSession()
->linkByHrefExists('admin/config/content/xbbcode/tags/manage/test_tag_external/view');
$this
->drupalGet('admin/config/content/xbbcode/tags/manage/test_tag_external/view');
$template = <<<'EOD'
{#
/**
* @file
* Test template.
*/
#}
<em>{{ tag.content }}</em>
EOD;
$this
->assertSession()
->fieldValueEquals('template_code', rtrim($template));
$fields = $this
->xpath($this
->assertSession()
->buildXPathQuery('//input[@name=:name][@value=:value]', [
':name' => 'op',
':value' => 'Save',
]));
static::assertEmpty($fields);
$this
->clickLink('Copy');
$this
->assertSession()
->addressEquals('admin/config/content/xbbcode/tags/manage/test_tag_external/copy');
$this
->assertSession()
->fieldValueEquals('label', 'Test External Template 2');
static::assertNotEmpty($this
->xpath($this
->assertSession()
->buildXPathQuery('//input[@name=:name][@value=:value]', [
':name' => 'op',
':value' => 'Save',
])));
$this
->assertSession()
->linkNotExists('Copy');
$this
->drupalGet('admin/config/content/xbbcode/tags');
$this
->clickLink('Create custom tag');
$edit = $this
->createCustomTag();
$this
->assertSession()
->assertEscaped($edit['label']);
$this
->assertSession()
->assertEscaped($edit['description']);
$this
->assertSession()
->assertEscaped($edit['sample']);
$this
->assertSession()
->pageTextContains('[test_tag]Content[/test_tag]');
$this
->assertSession()
->linkByHrefExists('admin/config/content/xbbcode/tags/manage/' . $edit['id'] . '/edit');
$this
->assertSession()
->linkByHrefExists('admin/config/content/xbbcode/tags/manage/' . $edit['id'] . '/delete');
$this
->clickLink('Edit');
$this
->assertSession()
->responseContains('xbbcode_test_plugin/assets/test.css');
$this
->assertSession()
->fieldValueEquals('attached[library]', $edit['attached[library]']);
$this
->assertSession()
->linkByHrefExists('admin/config/content/xbbcode/tags/manage/' . $edit['id'] . '/delete');
$this
->assertSession()
->linkExists('Copy (discard unsaved changes)');
$name = mb_strtolower($this
->randomMachineName());
$new_edit = [
'label' => $this
->randomString(),
'description' => $this
->randomString(),
'name' => $name,
'sample' => str_replace($edit['name'], $name, $edit['sample']),
];
$this
->submitForm($new_edit, 'Save');
$this
->assertSession()
->responseContains((string) new FormattableMarkup('The BBCode tag %tag has been updated.', [
'%tag' => $new_edit['label'],
]));
$this
->assertSession()
->assertNoEscaped($edit['description']);
$this
->assertSession()
->assertEscaped($new_edit['description']);
$this
->assertSession()
->assertEscaped($new_edit['sample']);
$this
->clickLink('Delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->responseContains((string) new FormattableMarkup('The custom tag %tag has been deleted.', [
'%tag' => $new_edit['label'],
]));
$this
->assertSession()
->linkByHrefNotExists('admin/config/content/xbbcode/tags/manage/' . $edit['id'] . '/edit');
$this
->assertSession()
->assertNoEscaped($new_edit['description']);
$this
->clickLink('Create custom tag');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->assertEscaped($edit['description']);
$this
->assertSession()
->linkByHrefExists('admin/config/content/xbbcode/tags/manage/' . $edit['id'] . '/edit');
$invalid_edit['name'] = $this
->randomMachineName() . 'A';
$this
->clickLink('Edit');
$this
->submitForm($invalid_edit, 'Save');
$this
->assertSession()
->responseContains((string) new FormattableMarkup('%name field is not in the right format.', [
'%name' => 'Default name',
]));
$invalid_edit['name'] = mb_strtolower($this
->randomMachineName()) . '!';
$this
->submitForm($invalid_edit, 'Save');
$this
->assertSession()
->responseContains((string) new FormattableMarkup('%name field is not in the right format.', [
'%name' => 'Default name',
]));
}
public function testGlobalPlugins() : void {
$this
->drupalGet('filter/tips');
$this
->assertSession()
->pageTextContains('You may use the following BBCode tags:');
$this
->assertSession()
->pageTextContains('[test_plugin]');
$this
->assertSession()
->pageTextContains('[test_tag]');
$this
->assertSession()
->pageTextContains('[test_template]');
$tag = $this
->createCustomTag();
$this
->drupalGet('filter/tips');
$this
->assertSession()
->pageTextContains((string) $tag['name']);
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/add/page');
$this
->assertSession()
->pageTextContains('You may use the following BBCode tags:');
$this
->assertSession()
->pageTextContains('[test_plugin]');
}
public function testTagSet() : void {
$tag = $this
->createCustomTag();
$tags = [
'test_plugin_id' => 'test_plugin',
'xbbcode_tag:test_tag_id' => 'test_tag',
'xbbcode_tag:test_tag_external' => 'test_template',
"xbbcode_tag:{$tag['id']}" => $tag['name'],
];
$this
->drupalGet('admin/config/content/xbbcode/sets');
$this
->assertSession()
->pageTextContains('There are no tag sets yet.');
$this
->clickLink('Create tag set');
$this
->assertSession()
->checkboxNotChecked('formats[xbbcode]');
foreach ($tags as $id => $name) {
$this
->assertSession()
->checkboxNotChecked("_tags[available:{$id}]");
$this
->assertSession()
->fieldValueEquals("_settings[available:{$id}][name]", $name);
}
$tag_set = [
'label' => $this
->randomString(),
'id' => mb_strtolower($this
->randomMachineName()),
'formats[xbbcode]' => 1,
];
$this
->submitForm($tag_set, 'Save');
$this
->assertSession()
->responseContains((string) new FormattableMarkup('The BBCode tag set %set has been created.', [
'%set' => $tag_set['label'],
]));
$this
->assertSession()
->pageTextContains('None');
$this
->drupalGet('filter/tips');
$this
->assertSession()
->pageTextContains('BBCode is active, but no tags are available.');
$this
->drupalGet('admin/config/content/xbbcode/sets');
$this
->clickLink('Edit');
$this
->assertSession()
->checkboxChecked('formats[xbbcode]');
$invalid_edit = [
'_settings[available:test_plugin_id][name]' => mb_strtolower($this
->randomMachineName()) . 'A',
];
$this
->submitForm($invalid_edit, 'Save');
$this
->assertSession()
->responseContains((string) new FormattableMarkup('%name field is not in the right format.', [
'%name' => 'Tag name',
]));
$invalid_edit = [];
foreach (array_keys($tags) as $i => $id) {
$invalid_edit["_settings[available:{$id}][name]"] = $i >= 2 ? 'def' : 'abc';
$invalid_edit["_tags[available:{$id}]"] = $i <= 2;
}
$this
->submitForm($invalid_edit, 'Save');
$this
->assertSession()
->responseContains('The name [abc] is used by multiple tags.');
$this
->assertSession()
->responseNotContains('The name [def] is used by multiple tags.');
$this
->drupalGet('admin/config/content/xbbcode/sets');
$this
->clickLink('Edit');
$edit = [
"_tags[available:xbbcode_tag:{$tag['id']}]" => 1,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->responseContains((string) new FormattableMarkup('The BBCode tag set %set has been updated.', [
'%set' => $tag_set['label'],
]));
$this
->assertSession()
->pageTextContains("[{$tag['name']}]");
$this
->assertSession()
->pageTextNotContains('[test_tag]');
$this
->assertSession()
->pageTextNotContains('[test_template]');
$this
->assertSession()
->pageTextNotContains('[test_plugin]');
$this
->drupalGet('filter/tips');
$this
->assertSession()
->responseContains("<strong>[{$tag['name']}]</strong>");
$this
->assertSession()
->pageTextContains($tag['sample']);
$this
->assertSession()
->pageTextContains($tag['description']);
$this
->assertSession()
->pageTextNotContains('[test_tag]');
$this
->assertSession()
->pageTextNotContains('[test_template]');
$this
->assertSession()
->pageTextNotContains('[test_plugin]');
$this
->drupalLogin($this->webUser);
$this
->drupalGet('node/add/page');
$this
->assertSession()
->pageTextContains('You may use the following BBCode tags:');
$this
->assertSession()
->responseContains((string) new FormattableMarkup('<abbr title="@desc">[@name]</abbr>', [
'@desc' => $tag['description'],
'@name' => $tag['name'],
]));
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/xbbcode/sets');
$this
->clickLink('Delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->responseContains((string) new FormattableMarkup('The tag set %name has been deleted.', [
'%name' => $tag_set['label'],
]));
$this
->drupalGet('filter/tips');
$this
->assertSession()
->responseContains("<strong>[{$tag['name']}]</strong>");
$this
->assertSession()
->pageTextContains($tag['sample']);
$this
->assertSession()
->pageTextContains($tag['description']);
$this
->assertSession()
->pageTextContains('[test_tag]');
$this
->assertSession()
->pageTextContains('[test_template]');
$this
->assertSession()
->pageTextContains('[test_plugin]');
}
}