View source
<?php
namespace Drupal\Tests\customfilter\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\customfilter\Entity\CustomFilter;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\RoleInterface;
class CustomFilterTest extends BrowserTestBase {
protected $adminUser;
protected $defaultTheme = 'stable';
protected static $modules = [
'filter',
'node',
'customfilter',
];
public function setUp() : void {
parent::setUp();
$settings = [
'type' => 'test',
'name' => 'Test',
];
$this
->drupalCreateContentType($settings);
$this->adminUser = $this
->drupalCreateUser([
'administer permissions',
'administer customfilter',
'administer filters',
'administer nodes',
'access administration pages',
'create test content',
'edit any test content',
]);
$this
->drupalLogin($this->adminUser);
}
protected function createFilter() {
$edit = [];
$edit['id'] = 'test_filter';
$edit['name'] = 'filter_name';
$edit['disable_cache'] = FALSE;
$edit['description'] = 'filter description';
$edit['shorttip'] = 'short tip';
$edit['longtip'] = 'long tip';
$this
->drupalGet('admin/config/content/customfilter/add');
$this
->submitForm($edit, 'Save');
$this
->drupalGet('admin/config/content/customfilter');
$this
->assertSession()
->pageTextContains('filter_name');
$this
->assertTrue(CustomFilter::load('test_filter')
->getCache());
}
protected function createRule() {
$edit = [];
$edit['rid'] = 'test_rule';
$edit['name'] = 'Rule #1 Name';
$edit['description'] = 'rule description';
$edit['enabled'] = TRUE;
$edit['pattern'] = '/\\[test\\](.+)\\[\\/test\\]/iUs';
$edit['code'] = FALSE;
$edit['replacement'] = '<b>$1</b>';
$this
->drupalGet('admin/config/content/customfilter/filter/test_filter/add');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Rule #1 Name');
}
protected function createTimeRule() {
$edit = [];
$edit['rid'] = 'time_rule';
$edit['name'] = 'Rule #2 Name';
$edit['description'] = 'rule description';
$edit['enabled'] = TRUE;
$edit['pattern'] = '/\\[time\\]/iUs';
$edit['code'] = TRUE;
$edit['replacement'] = '$result = "<time>" . time () . "</time>";';
$this
->drupalGet('admin/config/content/customfilter/filter/test_filter/add');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Rule #2 Name');
}
protected function createSubRule() {
$edit = [];
$edit['rid'] = 'test_subrule';
$edit['name'] = 'Subrule Name';
$edit['description'] = 'subrule description';
$edit['enabled'] = TRUE;
$edit['matches'] = '1';
$edit['pattern'] = '/drupal/i';
$edit['code'] = FALSE;
$edit['replacement'] = '<font color="red">Drupal</font>';
$this
->drupalGet('admin/config/content/customfilter/filter/test_filter/rule/test_rule/add');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("Subrule Name");
}
protected function createTextFormat() {
$edit = [];
$edit['format'] = 'customfilter';
$edit['name'] = $this
->randomMachineName();
$edit['roles[' . RoleInterface::AUTHENTICATED_ID . ']'] = 1;
$edit['filters[customfilter_test_filter][status]'] = TRUE;
$this
->drupalGet('admin/config/content/formats/add');
$this
->assertSession()
->pageTextContains('filter_name');
$this
->submitForm($edit, 'Save configuration');
$message = new FormattableMarkup('Added text format %filter.', [
'%filter' => $edit['name'],
]);
$this
->assertSession()
->responseContains($message);
$this
->drupalGet('admin/config/content/formats');
}
protected function deleteFilter() {
$edit = [];
$this
->drupalGet('admin/config/content/customfilter/filter/test_filter/delete');
$this
->submitForm($edit, 'Delete');
$this
->drupalGet('admin/config/content/formats/manage/customfilter');
$this
->assertSession()
->pageTextNotContains('filter_name');
}
protected function deleteSubRule() {
$edit = [];
$this
->drupalGet('admin/config/content/customfilter/filter/test_filter/rule/test_rule/delete');
$this
->submitForm($edit, 'Delete');
}
protected function editRule() {
$edit = [];
$edit['name'] = 'New rule label';
$edit['description'] = 'rule description';
$edit['enabled'] = TRUE;
$edit['pattern'] = '/Goodbye Drupal 7/i';
$edit['code'] = FALSE;
$edit['replacement'] = 'Come back Drupal 7';
$this
->drupalGet('admin/config/content/customfilter/filter/test_filter/rule/test_rule/edit');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->addressEquals('admin/config/content/customfilter/filter/test_filter');
$this
->drupalGet('admin/config/content/customfilter/filter/test_filter');
$this
->assertSession()
->responseContains('New rule label');
$this
->assertSession()
->responseNotContains('Rule #1 Name');
}
protected function editSubRule() {
$edit = [];
$edit['name'] = 'Renamed Subrule';
$edit['description'] = 'New subrule description';
$edit['matches'] = 1;
$edit['pattern'] = '/Drupal/i';
$edit['code'] = FALSE;
$edit['replacement'] = '<font color="blue">Drupal</font>';
$this
->drupalGet('admin/config/content/customfilter/filter/test_filter/rule/test_subrule/edit');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Renamed Subrule');
$this
->assertSession()
->addressEquals('admin/config/content/customfilter/filter/test_filter');
$this
->assertSession()
->responseContains('Renamed Subrule');
$this
->assertSession()
->responseNotContains('Subrule Name');
}
protected function rulesFilteringTest() {
$this
->drupalGet('node/1');
$this
->assertSession()
->responseContains('Drupal <b>Goodbye <font color="red">Drupal</font> 7</b>');
$this
->assertSession()
->responseContains('<time>');
$time = $this
->xpath('//time');
$this
->assertCount(1, $time);
$time = $time[0]
->getText();
$this
->assertNotNull($time);
sleep(1);
$this
->drupalGet('node/1');
$newtime = $this
->xpath('//time')[0]
->getText();
$this
->assertSame($time, $newtime);
CustomFilter::load('test_filter')
->set('cache', FALSE)
->updateRule([
'rid' => 'test_subrule',
'replacement' => '<font color="orange">Drupal</font>',
])
->save();
sleep(1);
$this
->drupalGet('node/1');
$this
->assertSession()
->responseContains('Drupal <b>Goodbye <font color="orange">Drupal</font> 7</b>');
$this
->assertSession()
->responseContains('<time>');
$time = $this
->xpath('//time')[0]
->getText();
$this
->assertNotNull($time);
sleep(1);
$this
->drupalGet('node/1');
$newtime = $this
->xpath('//time')[0]
->getText();
$this
->assertNotSame($time, $newtime);
}
public function testModule() {
$this
->createFilter();
$this
->createRule();
$this
->createTimeRule();
$this
->createSubRule();
$this
->createTextFormat();
$node = [
'title' => 'Test for Custom Filter',
'body' => [
[
'value' => 'Drupal [test]Goodbye Drupal 7[/test] at [time]',
'format' => 'customfilter',
],
],
'type' => 'test',
];
$this
->drupalCreateNode($node);
$this
->rulesFilteringTest();
$this
->editRule();
$this
->editSubRule();
$this
->deleteSubRule();
$this
->deleteFilter();
}
public function testAdminPage() {
$this
->drupalGet('admin/config');
$this
->assertSession()
->responseContains('Custom Filter');
$this
->assertSession()
->responseContains('Create and manage your own custom filters.');
$this
->assertSession()
->linkByHrefExists('admin/config/content/customfilter', 0, 'A link to custom filter is on the page.');
}
}