/[drupal]/contributions/modules/geshifilter/tests/geshifilter.pages.test
ViewVC logotype

Contents of /contributions/modules/geshifilter/tests/geshifilter.pages.test

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Sat Jul 4 11:05:56 2009 UTC (4 months, 3 weeks ago) by soxofaan
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +3 -1 lines
File MIME type: text/x-php
merged PHP style code block option with tag style option
1 <?php
2 // $Id: geshifilter.pages.test,v 1.4 2009/07/04 09:49:00 soxofaan Exp $
3
4 /**
5 * Unit tests for the GeSHi filter module.
6 */
7 class GeshiFilterTest extends DrupalTestCase {
8
9 /**
10 * A global filter adminstrator
11 */
12 var $filter_admin_user;
13
14 /**
15 * A global user for adding pages
16 */
17 var $normal_user;
18
19 /**
20 * The id of the input format with only GeSHi filter in it
21 */
22 var $input_format_id;
23
24 /**
25 * Drupal SimpleTest method: return metadata about the test.
26 */
27 function get_info() {
28 return array(
29 'name' => t('GeSHi input filter'),
30 'desc' => t('Test the input filter capabilities of the GeSHi filter.'),
31 'group' => t('GeSHi module'),
32 );
33 }
34
35 /**
36 * SimpleTest core method: code run before each and every test method.
37 *
38 * Optional. You only need this if you have setup tasks.
39 */
40 function setUp() {
41 // Always call the setUp() function from the parent class.
42 parent::setUp();
43
44 // Make sure that Geshi filter module is enabled.
45 $this->drupalModuleEnable('geshifilter');
46
47 // Create a filter admin user
48 $permissions = array(
49 'administer filters',
50 );
51 $this->filter_admin_user = $this->drupalCreateUserRolePerm($permissions);
52 // Create a normal user for page creation
53 $permissions = array(
54 'edit own page content',
55 'create page content'
56 );
57 $this->normal_user = $this->drupalCreateUserRolePerm($permissions);
58
59 // log in with filter admin user
60 $this->drupalLoginUser($this->filter_admin_user);
61
62 // add an input format with only geshi filter
63 $edit = array(
64 'name' => $this->randomName(10, 'inputformat_'),
65 'filters[geshifilter/0]' => TRUE,
66 'roles[2]' => TRUE,
67 );
68 $this->drupalPost('admin/settings/filters/add', $edit, t('Save configuration'));
69 // store the format id of the created input format
70 $this->input_format_id = db_result(db_query("SELECT format FROM {filter_formats} WHERE name = '%s'", $edit['name']));
71 $this->assertTrue($this->input_format_id, t('Input format id (%s)'));
72
73 // set some default GeSHi filter admin settings
74 // Set default highlighting mode to "do nothing".
75 $this->drupalVariableSet('geshifilter_default_highlighting', GESHIFILTER_DEFAULT_PLAINTEXT);
76 $this->drupalVariableSet('geshifilter_format_specific_options', FALSE);
77 $this->drupalVariableSet('geshifilter_tag_styles', array(
78 GESHIFILTER_BRACKETS_ANGLE => GESHIFILTER_BRACKETS_ANGLE,
79 GESHIFILTER_BRACKETS_SQUARE => GESHIFILTER_BRACKETS_SQUARE,
80 ));
81 $this->drupalVariableSet('geshifilter_default_line_numbering', GESHIFILTER_LINE_NUMBERS_DEFAULT_NONE);
82
83 // log out as filter admin
84 $this->drupalGet('logout');
85
86 // log in as the normal user for adding pages
87 $this->drupalLoginUser($this->normal_user);
88
89 // include GeSHi filtering functions
90 require_once(drupal_get_path('module', 'geshifilter') .'/geshifilter.pages.inc');
91
92 }
93
94 /**
95 * SimpleTest core method: code run after each and every test method.
96 *
97 * Optional. You only need this if you have setup tasks.
98 */
99 function tearDown() {
100 // log in as filter admin
101 $this->drupalGet('logout');
102 $this->drupalLoginUser($this->filter_admin_user);
103
104 // remove input format
105 $this->drupalPost('admin/settings/filters/delete/'. $this->input_format_id, array(), t('Delete'));
106
107 // Always call the tearDown() function from the parent class.
108 parent::tearDown();
109 }
110
111 /**
112 * Assert function for testing if GeSHi highlighting works
113 * @param $body the body text of the node
114 * @param $check_list list of items that should be in rendered output (assertWantedRaw)
115 * an item is something like array($source_code, $lang, $line_numbering, $linenumbers_start, $inline_mode)
116 * if $lang is set, GeSHifilter syntax highlighting is applied to $sourcecode
117 * if $lang is false, $sourcecode is directly looked for
118 * @param $description description of the assertion
119 */
120 function assertGeshiFilterHighlighting($body, $check_list, $description) {
121 // Create content.
122 $edit = array(
123 'title' => $this->randomName(32, 'simpletest_pagetitle_'),
124 'body' => $body ."\n". $this->randomName(100),
125 'format' => $this->input_format_id,
126 );
127 // Post node
128 $this->drupalPost('node/add/page', $edit, t('Save'));
129 // check posted node
130 $node = node_load(array('title' => $edit['title']));
131 $this->assertTrue($node, 'Node found in database. %s');
132 // check if highlighting succeeded
133 foreach ($check_list as $fragment) {
134 list($source_code, $lang, $line_numbering, $linenumbers_start, $inline_mode) = $fragment;
135 if ($lang) {
136 // apply syntax highlighting
137 $source_code = geshifilter_geshi_process($source_code, $lang, $line_numbering, $linenumbers_start, $inline_mode);
138 }
139 $this->assertWantedRaw($source_code, $description);
140 }
141 }
142
143 /**
144 * Test the standard functionality of the generic tags
145 */
146 function testGenericTags() {
147 $this->drupalVariableSet('geshifilter_tags', 'code');
148 $this->drupalVariableSet('geshifilter_language_enabled_c', TRUE);
149 $this->drupalVariableSet('geshifilter_language_enabled_cpp', TRUE);
150 $this->drupalVariableSet('geshifilter_language_enabled_csharp', TRUE);
151 $this->drupalVariableSet('geshifilter_language_enabled_java', TRUE);
152
153 // body material
154 $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}";
155
156 // check language argument
157 $this->assertGeshiFilterHighlighting('<code type="cpp">'. $source_code .'</code>',
158 array(array($source_code, 'cpp', 0, 1, FALSE)),
159 t('Checking type="..." argument'));
160 $this->assertGeshiFilterHighlighting('<code lang="cpp">'. $source_code .'</code>',
161 array(array($source_code, 'cpp', 0, 1, FALSE)),
162 t('Checking lang="..." argument'));
163 $this->assertGeshiFilterHighlighting('<code language="cpp">'. $source_code .'</code>',
164 array(array($source_code, 'cpp', 0, 1, FALSE)),
165 t('Checking language="..." argument'));
166
167 // check some languages
168 $languages = array('c', 'cpp', 'java');
169 foreach ($languages as $lang) {
170 $this->assertGeshiFilterHighlighting('<code language="'. $lang .'">'. $source_code .'</code>',
171 array(array($source_code, $lang, 0, 1, FALSE)),
172 t('Checking language="@lang"', array('@lang' => $lang)));
173 }
174
175 // check line_numbering argument
176 $this->assertGeshiFilterHighlighting('<code type="cpp" linenumbers="off">'. $source_code .'</code>',
177 array(array($source_code, 'cpp', 0, 1, FALSE)),
178 t('Checking linenumbers="off" argument'));
179 $this->assertGeshiFilterHighlighting('<code type="cpp" linenumbers="normal">'. $source_code .'</code>',
180 array(array($source_code, 'cpp', 1, 1, FALSE)),
181 t('Checking linenumbers="normal" argument'));
182 $this->assertGeshiFilterHighlighting('<code type="cpp" start="27">'. $source_code .'</code>',
183 array(array($source_code, 'cpp', 1, 27, FALSE)),
184 t('Checking start="27" argument'));
185 $this->assertGeshiFilterHighlighting('<code type="cpp" linenumbers="fancy">'. $source_code .'</code>',
186 array(array($source_code, 'cpp', 5, 1, FALSE)),
187 t('Checking linenumbers="fancy" argument'));
188 $this->assertGeshiFilterHighlighting('<code type="cpp" fancy="3">'. $source_code .'</code>',
189 array(array($source_code, 'cpp', 3, 1, FALSE)),
190 t('Checking fancy="3" argument'));
191 }
192
193 /**
194 * Check if brackets work
195 */
196 function testBrackets() {
197 $this->drupalVariableSet('geshifilter_tags', 'code');
198 $this->drupalVariableSet('geshifilter_language_enabled_cpp', TRUE);
199
200 // body material
201 $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}";
202
203 $this->drupalVariableSet('geshifilter_tag_styles', array(
204 GESHIFILTER_BRACKETS_ANGLE => GESHIFILTER_BRACKETS_ANGLE,
205 GESHIFILTER_BRACKETS_SQUARE => GESHIFILTER_BRACKETS_SQUARE,
206 ));
207 $this->assertGeshiFilterHighlighting('<code language="cpp">'. $source_code .'</code>',
208 array(array($source_code, 'cpp', 0, 1, FALSE)),
209 t('Checking <foo> brackets style in angle+square tag style mode'));
210 $this->assertGeshiFilterHighlighting('[code language="cpp"]'. $source_code .'[/code]',
211 array(array($source_code, 'cpp', 0, 1, FALSE)),
212 t('Checking [foo] brackets style in angle+square tag style mode mode'));
213
214 $this->drupalVariableSet('geshifilter_tag_styles', array(
215 GESHIFILTER_BRACKETS_ANGLE => GESHIFILTER_BRACKETS_ANGLE,
216 ));
217 $this->assertGeshiFilterHighlighting('<code language="cpp">'. $source_code .'</code>',
218 array(array($source_code, 'cpp', 0, 1, FALSE)),
219 t('Checking <foo> brackets style in GESHIFILTER_BRACKETS_ANGLE mode'));
220 $this->assertGeshiFilterHighlighting('[code language="cpp"]'. $source_code .'[/code]',
221 array(array($source_code, NULL, 0, 1, FALSE)),
222 t('Checking [foo] brackets style in GESHIFILTER_BRACKETS_ANGLE mode'));
223
224 $this->drupalVariableSet('geshifilter_tag_styles', array(
225 GESHIFILTER_BRACKETS_SQUARE => GESHIFILTER_BRACKETS_SQUARE,
226 ));
227 $this->assertGeshiFilterHighlighting('<code language="cpp">'. $source_code .'</code>',
228 array(array($source_code, NULL, 0, 1, FALSE)),
229 t('Checking <foo> brackets style in GESHIFILTER_BRACKETS_SQUARE mode'));
230 $this->assertGeshiFilterHighlighting('[code language="cpp"]'. $source_code .'[/code]',
231 array(array($source_code, 'cpp', 0, 1, FALSE)),
232 t('Checking [foo] brackets style in GESHIFILTER_BRACKETS_SQUARE mode'));
233
234 // @todo: test php style code blocks.
235 }
236
237 /**
238 * Check if tags like [c++] and [c#] work
239 * Problem described in http://drupal.org/node/208720
240 */
241 function testSpecialTags() {
242 // Enabled the tags
243 $this->drupalVariableSet('geshifilter_language_enabled_cpp', TRUE);
244 $this->drupalVariableSet('geshifilter_language_tags_cpp', 'c++');
245 $this->drupalVariableSet('geshifilter_language_enabled_csharp', TRUE);
246 $this->drupalVariableSet('geshifilter_language_tags_csharp', 'c#');
247 // body material
248 $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}";
249 // Test the tags
250 $this->assertGeshiFilterHighlighting('<c++>'. $source_code .'</c++>',
251 array(array($source_code, 'cpp', 0, 1, FALSE)),
252 t('Checking <c++>..</c++>'));
253 $this->assertGeshiFilterHighlighting('<c#>'. $source_code .'</c#>',
254 array(array($source_code, 'csharp', 0, 1, FALSE)),
255 t('Checking <c#>..</c#>'));
256 }
257
258 /**
259 * Test if tags like [cpp], [css], [csharp] aren't highjacked by [c]
260 */
261 function testPrefixTags() {
262 // enabled the tags
263 $this->drupalVariableSet('geshifilter_language_enabled_c', TRUE);
264 $this->drupalVariableSet('geshifilter_language_tags_c', 'c');
265 $this->drupalVariableSet('geshifilter_language_enabled_cpp', TRUE);
266 $this->drupalVariableSet('geshifilter_language_tags_cpp', 'cpp');
267 $this->drupalVariableSet('geshifilter_language_enabled_csharp', TRUE);
268 $this->drupalVariableSet('geshifilter_language_tags_csharp', 'csharp');
269 // body material
270 $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}";
271 // Test the tags
272 $this->assertGeshiFilterHighlighting('<cpp>'. $source_code .'</cpp>',
273 array(array($source_code, 'cpp', 0, 1, FALSE)),
274 t('Source code in <cpp>...</cpp> should work when <c>...</c> is also enabled'));
275 $this->assertGeshiFilterHighlighting('<csharp>'. $source_code .'</csharp>',
276 array(array($source_code, 'csharp', 0, 1, FALSE)),
277 t('Source code in <csharp>...</csharp> should work when <c>...</c> is also enabled'));
278 }
279
280 function testDoNothingMode() {
281 // Enable C++.
282 $this->drupalVariableSet('geshifilter_language_enabled_cpp', TRUE);
283 $this->drupalVariableSet('geshifilter_language_tags_cpp', 'cpp');
284 // Set default highlighting mode to "do nothing".
285 $this->drupalVariableSet('geshifilter_default_highlighting', GESHIFILTER_DEFAULT_DONOTHING);
286 // Body material with some characters ('<' and '&') that would be escaped
287 // except in "do nothing" mode
288 $source_code = "//C++-ish source code\nfor (int i=0; i!=10; ++i) {\n fun(i);\n bar.foo(x, y);\n}";
289 // Tests
290 $this->assertGeshiFilterHighlighting('<code>'. $source_code .'</code>',
291 array(array('<code>'. $source_code .'</code>', FALSE, 0, 1, FALSE)),
292 t('Do nothing mode should not touch given source code')
293 );
294 $this->assertGeshiFilterHighlighting('<code language="cpp">'. $source_code .'</code>',
295 array(array($source_code, 'cpp', 0, 1, FALSE)),
296 t('Highlighting with language="cpp" should work when default is "do nothing"')
297 );
298 $this->assertGeshiFilterHighlighting('<cpp>'. $source_code .'</cpp>',
299 array(array($source_code, 'cpp', 0, 1, FALSE)),
300 t('Highlighting with <cpp>...</cpp> should work when default is "do nothing"')
301 );
302 }
303
304 }

  ViewVC Help
Powered by ViewVC 1.1.2