View source
<?php
namespace Drupal\customfilter\Tests;
use Drupal\simpletest\WebTestBase;
class CustomFilterTest extends WebTestBase {
protected $adminUser;
public static $modules = array(
'filter',
'customfilter',
'node',
);
public function setUp() {
parent::setUp();
$settings = array(
'type' => 'test',
'name' => 'Test',
);
$this
->drupalCreateContentType($settings);
$this->adminUser = $this
->drupalCreateUser(array(
'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 = array();
$edit['id'] = 'test_filter';
$edit['name'] = 'filter_name';
$edit['cache'] = FALSE;
$edit['description'] = 'filter description';
$edit['shorttip'] = 'short tip';
$edit['longtip'] = 'long tip';
$this
->drupalPostForm('admin/config/content/customfilter/add', $edit, t('Save'));
$this
->drupalGet('admin/config/content/customfilter');
$this
->assertText("filter_name", 'Filter created with sucess');
}
protected function createRule() {
$edit = array();
$edit['rid'] = 'test_rule';
$edit['name'] = 'Rule Name';
$edit['description'] = 'rule description';
$edit['enabled'] = TRUE;
$edit['pattern'] = '/\\[test\\](.+)\\[\\/test\\]/iUs';
$edit['code'] = FALSE;
$edit['replacement'] = '<b>$1</b>';
$this
->drupalPostForm('admin/config/content/customfilter/test_filter/add', $edit, t('Save'));
$this
->assertText("Rule Name", 'Rule created with sucess');
}
protected function createSubRule() {
$edit = array();
$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
->drupalPostForm('admin/config/content/customfilter/test_filter/test_rule/add', $edit, t('Save'));
$this
->assertText("Subrule Name", 'Subrule created with sucess');
}
protected function createTextFormat($format_name, array $filters) {
$edit = array();
$edit['format'] = $format_name;
$edit['name'] = $this
->randomMachineName();
$edit['roles[' . DRUPAL_AUTHENTICATED_RID . ']'] = 1;
foreach ($filters as $filter) {
$edit['filters[' . $filter . '][status]'] = TRUE;
}
$this
->drupalPostForm('admin/config/content/formats/add', $edit, t('Save configuration'));
$this
->assertRaw(t('Added text format %format.', array(
'%format' => $edit['name'],
)), 'New filter created.');
$this
->drupalGet('admin/config/content/formats');
}
protected function deleteFilter() {
$edit = array();
$this
->drupalPostForm('admin/config/content/customfilter/test_filter/delete', $edit, t('Delete'));
}
protected function deleteSubRule() {
$edit = array();
$this
->drupalPostForm('admin/config/content/customfilter/test_filter/test_rule/delete', $edit, t('Delete'));
}
protected function editRule() {
$edit = array();
$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
->drupalPostForm('admin/config/content/customfilter/test_filter/test_rule/edit', $edit, t('Save'));
$this
->assertUrl('admin/config/content/customfilter/test_filter');
$this
->drupalGet('admin/config/content/customfilter/test_filter');
$this
->assertRaw('New rule label', 'Updated rule with sucess.');
$this
->assertNoRaw('Rule Name', 'Previous rule do not exist.');
}
protected function editSubRule() {
$edit = array();
$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
->drupalPostForm('admin/config/content/customfilter/test_filter/test_subrule/edit', $edit, t('Save'));
$this
->assertText("Renamed Subrule", 'Subrule edited with sucess');
$this
->assertUrl('admin/config/content/customfilter/test_filter');
$this
->assertRaw('Renamed Subrule', 'Updated rule with sucess.');
$this
->assertNoRaw('Subrule Name', 'Previous rule do not exist.');
}
protected function ruleContent() {
$this
->drupalGet('node/1');
$this
->assertRaw('Drupal <b>Goodbye <font color="red">Drupal</font> 7</b>', 'Node content is replaced');
}
public function testModule() {
$this
->createFilter();
$this
->createRule();
$this
->createSubRule();
$this
->createTextFormat('customfilter', array(
'customfilter_test_filter',
));
$node = array(
'title' => 'Test for Custom Filter',
'body' => array(
array(
'value' => 'Drupal [test]Goodbye Drupal 7[/test]',
'format' => 'customfilter',
),
),
'type' => 'test',
);
$this
->drupalCreateNode($node);
$this
->ruleContent();
$this
->editRule();
$this
->editSubRule();
$this
->deleteSubRule();
$this
->deleteFilter();
}
public function testAdminPage() {
$this
->drupalGet('admin/config');
$this
->assertRaw('Custom Filter', 'Custom Filter is in admin/config');
$this
->assertRaw('Create and manage your own custom filters.', 'Description is in admin/config');
$this
->assertLinkByHref('admin/config/content/customfilter', 0, 'A link to custom filter is on the page.');
}
}