View source
<?php
class GeshiFilterTest extends DrupalTestCase {
var $filter_admin_user;
var $normal_user;
var $input_format_id;
function get_info() {
return array(
'name' => t('GeSHi input filter'),
'desc' => t('Test the GeSHi input filter.'),
'group' => t('GeSHi module'),
);
}
function setUp() {
parent::setUp();
$this
->drupalModuleEnable('geshifilter');
$this
->drupalModuleDisable('captcha');
$permissions = array(
'administer filters',
'access devel information',
);
$this->filter_admin_user = $this
->drupalCreateUserRolePerm($permissions);
$permissions = array(
'access devel information',
'edit own page content',
'create page content',
);
$this->normal_user = $this
->drupalCreateUserRolePerm($permissions);
$this
->drupalLoginUser($this->filter_admin_user);
$edit = array(
'name' => $this
->randomName(10, 'inputformat_'),
'filters[geshifilter/0]' => TRUE,
'roles[2]' => TRUE,
);
$this
->drupalPostRequest('admin/settings/filters/add', $edit, t('Save configuration'));
$this->input_format_id = db_result(db_query("SELECT format FROM {filter_formats} WHERE name = '%s'", $edit['name']));
$this
->assertTrue($this->input_format_id, t('Input format id (%s)'));
$this
->drupalVariableSet('geshifilter_format_specific_options', FALSE);
$this
->drupalVariableSet('geshifilter_brackets', GESHIFILTER_BRACKETS_BOTH);
$this
->drupalVariableSet('geshifilter_default_line_numbering', GESHIFILTER_LINE_NUMBERS_DEFAULT_NONE);
$this
->drupalGet(url('logout', NULL, NULL, TRUE));
$this
->drupalLoginUser($this->normal_user);
require_once drupal_get_path('module', 'geshifilter') . '/geshifilter.pages.inc';
}
function tearDown() {
$this
->drupalGet(url('logout', NULL, NULL, TRUE));
$this
->drupalLoginUser($this->filter_admin_user);
$this
->drupalPostRequest('admin/settings/filters/delete/' . $this->input_format_id, array(), t('Delete'));
parent::tearDown();
}
function assertGeshiFilterHighlighting($open_tag, $source_code, $close_tag, $lang, $description) {
$edit = array(
'title' => $this
->randomName(32, 'pagetitle_'),
'body' => $open_tag . $source_code . $close_tag,
'format' => $this->input_format_id,
);
$this
->drupalPostRequest('node/add/page', $edit, t('Submit'));
$node = node_load(array(
'title' => $edit['title'],
));
$this
->assertTrue($node, 'Node found in database. %s');
$highlighted = geshifilter_geshi_process($source_code, $lang);
$this
->assertWantedRaw($highlighted, $description);
}
function testSpecialTags() {
$this
->drupalVariableSet('geshifilter_language_enabled_cpp', TRUE);
$this
->drupalVariableSet('geshifilter_language_tags_cpp', 'c++');
$this
->drupalVariableSet('geshifilter_language_enabled_csharp', TRUE);
$this
->drupalVariableSet('geshifilter_language_tags_csharp', 'c#');
$this
->assertGeshiFilterHighlighting('<c++>', "//C++ source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n}", '</c++>', 'cpp', t('Source code in <c++>...</c++> should work'));
$this
->assertGeshiFilterHighlighting('[c++]', "//C++ source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n}", '[/c++]', 'cpp', t('Source code in [c++]...[/c++] should work'));
$this
->assertGeshiFilterHighlighting('<c#>', "//C# source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n}", '</c#>', 'csharp', t('Source code in <c#>...</c#> should work'));
$this
->assertGeshiFilterHighlighting('[c#]', "//C# source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n}", '[/c#]', 'csharp', t('Source code in [c#]...[/c#] should work'));
}
function testPrefixTags() {
$this
->drupalVariableSet('geshifilter_language_enabled_c', TRUE);
$this
->drupalVariableSet('geshifilter_language_tags_c', 'c');
$this
->drupalVariableSet('geshifilter_language_enabled_cpp', TRUE);
$this
->drupalVariableSet('geshifilter_language_tags_cpp', 'cpp');
$this
->drupalVariableSet('geshifilter_language_enabled_csharp', TRUE);
$this
->drupalVariableSet('geshifilter_language_tags_csharp', 'csharp');
$this
->drupalVariableSet('geshifilter_language_enabled_css', TRUE);
$this
->drupalVariableSet('geshifilter_language_tags_css', 'css');
$this
->assertGeshiFilterHighlighting('<cpp>', "//C++ source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n}", '</cpp>', 'cpp', t('Source code in <cpp>...</cpp> should work when <c>...</c> is also enabled'));
$this
->assertGeshiFilterHighlighting('<csharp>', "//C# source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n}", '</csharp>', 'csharp', t('Source code in <csharp>...</csharp> should work when <c>...</c> is also enabled'));
$this
->assertGeshiFilterHighlighting('<css>', "//CSS source code\nbody {\n color: black; text-align: right; \n}", '</css>', 'css', t('Source code in <css>...</css> should work when <c>...</c> is also enabled'));
}
}