View source
<?php
namespace Drupal\Tests\geshifilter\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\geshifilter\GeshiFilter;
use Drupal\geshifilter\GeshiFilterProcess;
class GeshiFilterTest extends BrowserTestBase {
protected $filterAdminUser;
protected $normalUser;
protected $config;
public static $modules = [
'node',
'geshifilter',
'filter',
];
protected $node;
public function setUp() {
parent::setUp();
$this->node = 1;
$this->config = \Drupal::configFactory()
->getEditable('geshifilter.settings');
$this->config
->set('geshi_dir', '/libraries/geshi');
$settings = [
'type' => 'geshifilter_content_type',
'name' => 'Geshifilter Content',
];
$this
->drupalCreateContentType($settings);
$permissions = [
'administer filters',
'administer nodes',
'access administration pages',
'create geshifilter_content_type content',
'edit any geshifilter_content_type content',
'administer site configuration',
];
$this->filterAdminUser = $this
->drupalCreateUser($permissions);
$this
->drupalLogin($this->filterAdminUser);
$this
->createTextFormat('geshifilter_text_format', [
'filter_geshifilter',
]);
$this->config
->set('default_highlighting', GeshiFilter::DEFAULT_PLAINTEXT);
$this->config
->set('use_format_specific_options', FALSE);
$this->config
->set('tag_styles', [
GeshiFilter::BRACKETS_ANGLE => GeshiFilter::BRACKETS_ANGLE,
GeshiFilter::BRACKETS_SQUARE => GeshiFilter::BRACKETS_SQUARE,
]);
$this->config
->set('default_line_numbering', GeshiFilter::LINE_NUMBERS_DEFAULT_NONE);
$this->config
->save();
}
protected function createTextFormat($format_name, array $filters) {
$edit = [];
$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.', [
'%format' => $edit['name'],
]), 'New filter created.');
$this
->drupalGet('admin/config/content/formats');
}
protected function assertGeshiFilterHighlighting($body, array $check_list, $description, $invert = FALSE) {
$node = [
'title' => 'Test for GeShi Filter',
'body' => [
[
'value' => $body . "\n" . $this
->randomMachineName(100),
'format' => 'geshifilter_text_format',
],
],
'type' => 'geshifilter_content_type',
];
$this
->drupalCreateNode($node);
$this
->drupalGet('node/' . $this->node);
$this->node++;
foreach ($check_list as $fragment) {
list($source_code, $lang, $line_numbering, $linenumbers_start, $inline_mode) = $fragment;
if ($lang) {
$source_code = GeshiFilterProcess::geshiProcess($source_code, $lang, $line_numbering, $linenumbers_start, $inline_mode);
}
if ($invert) {
$this
->assertNoRaw($source_code, $description);
}
else {
$this
->assertRaw($source_code, $description);
}
}
}
public function testGenericTags() {
$this->config
->set('tags', 'code');
$this->config
->set('language.c.enabled', TRUE);
$this->config
->set('language.cpp.enabled', TRUE);
$this->config
->set('language.csharp.enabled', TRUE);
$this->config
->set('language.java.enabled', TRUE);
$this->config
->save();
$source_code = "//C++-ish source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n bar.foo(x, y);\n server->start(&pool); \n}";
$this
->assertGeshiFilterHighlighting('<code type="cpp">' . $source_code . '</code>', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Checking type="..." argument'));
$this
->assertGeshiFilterHighlighting('<code lang="cpp">' . $source_code . '</code>', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Checking lang="..." argument'));
$this
->assertGeshiFilterHighlighting('<code language="cpp">' . $source_code . '</code>', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Checking language="..." argument'));
$languages = [
'c',
'cpp',
'java',
];
foreach ($languages as $lang) {
$this
->assertGeshiFilterHighlighting('<code language="' . $lang . '">' . $source_code . '</code>', [
[
$source_code,
$lang,
0,
1,
FALSE,
],
], t('Checking language="@lang"', [
'@lang' => $lang,
]));
}
$this
->assertGeshiFilterHighlighting('<code type="cpp" linenumbers="off">' . $source_code . '</code>', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Checking linenumbers="off" argument'));
$this
->assertGeshiFilterHighlighting('<code type="cpp" linenumbers="normal">' . $source_code . '</code>', [
[
$source_code,
'cpp',
1,
1,
FALSE,
],
], t('Checking linenumbers="normal" argument'));
$this
->assertGeshiFilterHighlighting('<code type="cpp" start="27">' . $source_code . '</code>', [
[
$source_code,
'cpp',
1,
27,
FALSE,
],
], t('Checking start="27" argument'));
$this
->assertGeshiFilterHighlighting('<code type="cpp" linenumbers="fancy">' . $source_code . '</code>', [
[
$source_code,
'cpp',
5,
1,
FALSE,
],
], t('Checking linenumbers="fancy" argument'));
$this
->assertGeshiFilterHighlighting('<code type="cpp" fancy="3">' . $source_code . '</code>', [
[
$source_code,
'cpp',
3,
1,
FALSE,
],
], t('Checking fancy="3" argument'));
}
public function testBracketsOnlyAngle() {
$this->config
->set('tags', 'code');
$this->config
->set('language.cpp.enabled', TRUE);
$this->config
->set('tag_styles', [
GeshiFilter::BRACKETS_ANGLE => GeshiFilter::BRACKETS_ANGLE,
]);
$this->config
->save();
$source_code = "//C++ source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n bar.foo(x, y);\n server->start(&pool); \n}";
$this
->assertGeshiFilterHighlighting('<code language="cpp">' . $source_code . '</code>', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Checking angle brackets style in GeshiFilter::BRACKETS_ANGLE mode'));
$this
->assertGeshiFilterHighlighting('[code language="cpp"]' . $source_code . '[/code]', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking [foo] brackets style in GeshiFilter::BRACKETS_ANGLE mode'));
$this
->assertGeshiFilterHighlighting('[[code language="cpp"]]' . $source_code . '[[/code]]', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking [[foo]] brackets style in GeshiFilter::BRACKETS_ANGLE mode'));
$this
->assertGeshiFilterHighlighting('<?php' . $source_code . '?>', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking php code blocks in GeshiFilter::BRACKETS_ANGLE mode'));
}
public function testBracketsOnlySquare() {
$this->config
->set('tags', 'code');
$this->config
->set('language.cpp.enabled', TRUE);
$source_code = "//C++ source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n bar.foo(x, y);\n server->start(&pool); \n}";
$this->config
->set('tag_styles', [
GeshiFilter::BRACKETS_SQUARE => GeshiFilter::BRACKETS_SQUARE,
]);
$this->config
->save();
$this
->assertGeshiFilterHighlighting('[code language="cpp"]' . $source_code . '[/code]', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Checking [foo] brackets style in GeshiFilter::BRACKETS_SQUARE mode'));
$this
->assertGeshiFilterHighlighting('<code language="cpp">' . $source_code . '</code>', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking angle brackets style in GeshiFilter::BRACKETS_SQUARE mode'));
$this
->assertGeshiFilterHighlighting('[[code language="cpp"]]' . $source_code . '[[/code]]', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking [[foo]] brackets style in GeshiFilter::BRACKETS_SQUARE mode'));
$this
->assertGeshiFilterHighlighting('<?php' . $source_code . '?>', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking php code blocks in GeshiFilter::BRACKETS_SQUARE mode'));
}
public function testBracketsOnlyDoubleSquare() {
$this->config
->set('tags', 'code');
$this->config
->set('language.cpp.enabled', TRUE);
$source_code = "//C++ source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n bar.foo(x, y);\n server->start(&pool); \n}";
$this->config
->set('tag_styles', [
GeshiFilter::BRACKETS_DOUBLESQUARE => GeshiFilter::BRACKETS_DOUBLESQUARE,
]);
$this->config
->save();
$this
->assertGeshiFilterHighlighting('[[code language="cpp"]]' . $source_code . '[[/code]]', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Checking [[foo]] brackets style in GeshiFilter::BRACKETS_DOUBLESQUARE mode'));
$this
->assertGeshiFilterHighlighting('<code language="cpp">' . $source_code . '</code>', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking angle brackets style in GeshiFilter::BRACKETS_DOUBLESQUARE mode'));
$this
->assertGeshiFilterHighlighting('[code language="cpp"]' . $source_code . '[/code]', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking [foo] brackets style in GeshiFilter::BRACKETS_DOUBLESQUARE mode'));
$this
->assertGeshiFilterHighlighting('<?php' . $source_code . '?>', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking php code blocks in GeshiFilter::BRACKETS_DOUBLESQUARE mode'));
}
public function testMarkdown() {
$this->config
->set('tags', 'code');
$this->config
->set('language.cpp.enabled', TRUE);
$source_code = "//C++ source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n bar.foo(x, y);\n server->start(&pool); \n}";
$this->config
->set('tag_styles', [
GeshiFilter::BRACKETS_MARKDOWNBLOCK => GeshiFilter::BRACKETS_MARKDOWNBLOCK,
]);
$this->config
->save();
$this
->assertGeshiFilterHighlighting("```cpp\n" . $source_code . "\n```", [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Checking [[foo]] brackets style in GeshiFilter::BRACKETS_DOUBLESQUARE mode'));
$this
->assertGeshiFilterHighlighting('<code language="cpp">' . $source_code . '</code>', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking angle brackets style in GeshiFilter::BRACKETS_DOUBLESQUARE mode'));
$this
->assertGeshiFilterHighlighting('[code language="cpp"]' . $source_code . '[/code]', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking [foo] brackets style in GeshiFilter::BRACKETS_DOUBLESQUARE mode'));
$this
->assertGeshiFilterHighlighting('<?php' . $source_code . '?>', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking php code blocks in GeshiFilter::BRACKETS_DOUBLESQUARE mode'));
}
public function testBracketsOnlyPhpCodeBlock() {
$this->config
->set('tags', 'code');
$this->config
->set('language.cpp.enabled', TRUE);
$source_code = "//C++ source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n bar.foo(x, y);\n server->start(&pool); \n}";
$this->config
->set('tag_styles', [
GeshiFilter::BRACKETS_PHPBLOCK => GeshiFilter::BRACKETS_PHPBLOCK,
]);
$this->config
->save();
$this
->assertGeshiFilterHighlighting('<?php' . $source_code . '?>', [
[
$source_code,
'php',
0,
1,
FALSE,
],
], t('Checking php code blocks in GeshiFilter::BRACKETS_PHPBLOCK mode'));
$this
->assertGeshiFilterHighlighting('<code language="cpp">' . $source_code . '</code>', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking angle brackets style in GeshiFilter::BRACKETS_PHPBLOCK mode'));
$this
->assertGeshiFilterHighlighting('[code language="cpp"]' . $source_code . '[/code]', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking [foo] brackets style in GeshiFilter::BRACKETS_PHPBLOCK mode'));
$this
->assertGeshiFilterHighlighting('[[code language="cpp"]]' . $source_code . '[[/code]]', [
[
$source_code,
NULL,
0,
1,
FALSE,
],
], t('Checking [[foo]] brackets style in GeshiFilter::BRACKETS_PHPBLOCK mode'));
}
public function testSpecialTags() {
$this->config
->set('language.cpp.enabled', TRUE);
$this->config
->set('language.cpp.tags', 'c++');
$this->config
->set('language.csharp.enabled', TRUE);
$this->config
->set('language.csharp.tags', 'c#');
$this->config
->save();
$source_code = "//C++-ish source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n bar.foo(x, y);\n server->start(&pool); \n}";
$this
->assertGeshiFilterHighlighting('<c++>' . $source_code . '</c++>', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Checking <c++>..</c++>'));
$this
->assertGeshiFilterHighlighting('<c#>' . $source_code . '</c#>', [
[
$source_code,
'csharp',
0,
1,
FALSE,
],
], t('Checking <c#>..</c#>'));
}
public function testPrefixTags() {
$this->config
->set('language.c.enabled', TRUE);
$this->config
->set('language.c.tags', 'c');
$this->config
->set('language.cpp.enabled', TRUE);
$this->config
->set('language.cpp.tags', 'cpp');
$this->config
->set('language.csharp.enabled', TRUE);
$this->config
->set('language.csharp.tags', 'csharp');
$this->config
->save();
$source_code = "//C++-ish source code\nfor (int i=0; i<10; ++i) {\n fun(i);\n bar.foo(x, y);\n server->start(&pool); \n}";
$this
->assertGeshiFilterHighlighting('<cpp>' . $source_code . '</cpp>', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Source code in <cpp>...</cpp> should work when <c>...</c> is also enabled'));
$this
->assertGeshiFilterHighlighting('<csharp>' . $source_code . '</csharp>', [
[
$source_code,
'csharp',
0,
1,
FALSE,
],
], t('Source code in <csharp>...</csharp> should work when <c>...</c> is also enabled'));
}
public function testDoNothingMode() {
$this->config
->set('language.cpp.enabled', TRUE);
$this->config
->set('language.cpp.tags', 'cpp');
$this->config
->set('default_highlighting', GeshiFilter::DEFAULT_DONOTHING);
$this->config
->save();
$source_code = "//C++-ish source code\nfor (int i=0; i!=10; ++i) {\n fun(i);\n bar.foo(x, y);\n}";
$this
->assertGeshiFilterHighlighting('<code>' . $source_code . '</code>', [
[
'<code>' . $source_code . '</code>',
FALSE,
0,
1,
FALSE,
],
], t('Do nothing mode should not touch given source code'));
$this
->assertGeshiFilterHighlighting('<code language="cpp">' . $source_code . '</code>', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Highlighting with language="cpp" should work when default is "do nothing"'));
$this
->assertGeshiFilterHighlighting('<cpp>' . $source_code . '</cpp>', [
[
$source_code,
'cpp',
0,
1,
FALSE,
],
], t('Highlighting with <cpp>...</cpp> should work when default is "do nothing"'));
}
public function testTitleAttributeOnCodeBlock() {
$source_code = "for (int i=0; i!=10; ++i) {\n fun(i);\n bar.foo(x, y);\n}";
$this
->assertGeshiFilterHighlighting('<code language="cpp">' . $source_code . '</code>', [
[
'geshifilter-title',
FALSE,
0,
0,
0,
],
], t('Setting the title attritbute on code block.'), TRUE);
$this
->assertGeshiFilterHighlighting('<code language="cpp" title="Foo the bar!">' . $source_code . '</code>', [
[
'<div class="geshifilter-title">Foo the bar!</div>',
FALSE,
0,
0,
0,
],
], t('Setting the title attritbute on code block.'));
}
public function testTitleAttributeOnInlineCode() {
$source_code = "for (int i=0; i!=10; ++i) { fun(i); }";
$this
->assertGeshiFilterHighlighting('<code language="cpp">' . $source_code . '</code>', [
[
'<span class="geshifilter">',
FALSE,
0,
0,
0,
],
], t('Setting the title attritbute on inline code.'));
$this
->assertGeshiFilterHighlighting('<code language="cpp" title="Foo the bar!">' . $source_code . '</code>', [
[
'<span class="geshifilter" title="Foo the bar!">',
FALSE,
0,
0,
0,
],
], t('Setting the title attritbute on inline code.'));
}
public function testSquareBracketConfusion() {
$this->config
->set('tags', 'code');
$this->config
->set('language.ini.enabled', TRUE);
$source_code = "[section]\nserver=192.0.2.62 ; IP address\nport=12345";
$this->config
->set('tag_styles', [
GeshiFilter::BRACKETS_SQUARE => GeshiFilter::BRACKETS_SQUARE,
]);
$this->config
->save();
$this
->assertGeshiFilterHighlighting('[code]' . $source_code . '[/code]', [
[
$source_code,
'text',
0,
1,
FALSE,
],
], t('Checking if [code][section]...[/code] works'));
$this
->assertGeshiFilterHighlighting('[code language="ini"]' . $source_code . '[/code]', [
[
$source_code,
'ini',
0,
1,
FALSE,
],
], t('Checking if [code language="ini"][section]...[/code] works'));
}
public function testSpecialChars() {
$this->config
->set('tags', 'code');
$this->config
->set('language.php.enabled', TRUE);
$this->config
->set('decode_entities', TRUE);
$this->config
->save();
$source = '<code language="php"><?php echo("<b>Hi</b>"); ?></code>';
$node = [
'title' => 'Test for Custom Filter',
'body' => [
[
'value' => $source,
'format' => 'geshifilter_text_format',
],
],
'type' => 'geshifilter_content_type',
];
$this
->drupalCreateNode($node);
$this
->drupalGet('node/1');
$this
->assertRaw('"<b>Hi</b>"', 'The code is not double encoded.');
}
}