You are here

public function XBBCodeAdminTest::testCustomTags in Extensible BBCode 4.0.x

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/XBBCodeAdminTest.php \Drupal\Tests\xbbcode\Functional\XBBCodeAdminTest::testCustomTags()

Test the custom tag page.

Throws

\Behat\Mink\Exception\ExpectationException

File

tests/src/Functional/XBBCodeAdminTest.php, line 118

Class

XBBCodeAdminTest
Test the administrative interface.

Namespace

Drupal\Tests\xbbcode\Functional

Code

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]');

  // Check that the tag can't be edited or deleted.
  $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);

  // Check for the View operation.
  $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));

  // The read-only form has no save button.
  $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',
  ])));

  // No copy button on a new tag.
  $this
    ->assertSession()
    ->linkNotExists('Copy');
  $this
    ->drupalGet('admin/config/content/xbbcode/tags');
  $this
    ->clickLink('Create custom tag');
  $edit = $this
    ->createCustomTag();

  // We should have been redirected to the tag list.
  // Our new custom tag is there.
  $this
    ->assertSession()
    ->assertEscaped($edit['label']);
  $this
    ->assertSession()
    ->assertEscaped($edit['description']);
  $this
    ->assertSession()
    ->assertEscaped($edit['sample']);

  // And so is the old one.
  $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');

  // Check that the stylesheet is included when rendering the preview.
  $this
    ->assertSession()
    ->responseContains('xbbcode_test_plugin/assets/test.css');

  // Check the contents of the library field.
  $this
    ->assertSession()
    ->fieldValueEquals('attached[library]', $edit['attached[library]']);

  // Check for the delete link on the editing form.
  $this
    ->assertSession()
    ->linkByHrefExists('admin/config/content/xbbcode/tags/manage/' . $edit['id'] . '/delete');
  $this
    ->assertSession()
    ->linkExists('Copy (discard unsaved changes)');
  $name = mb_strtolower($this
    ->randomMachineName());

  // Edit the description and the name.
  $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']);

  // Delete the tag.
  $this
    ->clickLink('Delete');
  $this
    ->submitForm([], 'Delete');
  $this
    ->assertSession()
    ->responseContains((string) new FormattableMarkup('The custom tag %tag has been deleted.', [
    '%tag' => $new_edit['label'],
  ]));

  // It's gone.
  $this
    ->assertSession()
    ->linkByHrefNotExists('admin/config/content/xbbcode/tags/manage/' . $edit['id'] . '/edit');
  $this
    ->assertSession()
    ->assertNoEscaped($new_edit['description']);

  // And the ID is available for re-use.
  $this
    ->clickLink('Create custom tag');
  $this
    ->submitForm($edit, 'Save');

  // And it's back.
  $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',
  ]));
}