You are here

geshifilter.test in GeSHi Filter for syntax highlighting 5.2

File

tests/geshifilter.test
View source
<?php

/**
* Unit tests for the GeSHi filter module.
*/
class GeshiFilterTest extends DrupalTestCase {

  /**
   * A global filter adminstrator
   */
  var $filter_admin_user;

  /**
   * A global user for adding pages
   */
  var $normal_user;

  /**
   * The id of the input format with only GeSHi filter in it
   */
  var $input_format_id;

  /**
   * Drupal SimpleTest method: return metadata about the test.
   */
  function get_info() {
    return array(
      'name' => t('GeSHi input filter'),
      'desc' => t('Test the GeSHi input filter.'),
      'group' => t('GeSHi module'),
    );
  }

  /**
   * SimpleTest core method: code run before each and every test method.
   *
   * Optional. You only need this if you have setup tasks.
   */
  function setUp() {

    // Always call the setUp() function from the parent class.
    parent::setUp();

    // Make sure that Geshi filter module is enabled.
    $this
      ->drupalModuleEnable('geshifilter');

    // Disable CAPTCHA module so users can post without trouble (just in case)
    $this
      ->drupalModuleDisable('captcha');

    // Create a filter admin user
    $permissions = array(
      'administer filters',
      'access devel information',
    );
    $this->filter_admin_user = $this
      ->drupalCreateUserRolePerm($permissions);

    // Create a normal user for page creation
    $permissions = array(
      'access devel information',
      'edit own page content',
      'create page content',
    );
    $this->normal_user = $this
      ->drupalCreateUserRolePerm($permissions);

    // log in with filter admin user
    $this
      ->drupalLoginUser($this->filter_admin_user);

    // add an input format with only geshi filter
    $edit = array(
      'name' => $this
        ->randomName(10, 'inputformat_'),
      'filters[geshifilter/0]' => TRUE,
      'roles[2]' => TRUE,
    );
    $this
      ->drupalPostRequest('admin/settings/filters/add', $edit, t('Save configuration'));

    // store the format id of the created input format
    $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)'));

    // set some default GeSHi filter admin settings
    $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);

    // log out as filter admin
    $this
      ->drupalGet(url('logout', NULL, NULL, TRUE));

    // log in as the normal user for adding pages
    $this
      ->drupalLoginUser($this->normal_user);

    // include GeSHi filtering functions
    require_once drupal_get_path('module', 'geshifilter') . '/geshifilter.pages.inc';
  }

  /**
   * SimpleTest core method: code run after each and every test method.
   *
   * Optional. You only need this if you have setup tasks.
   */
  function tearDown() {

    // log in as filter admin
    $this
      ->drupalGet(url('logout', NULL, NULL, TRUE));
    $this
      ->drupalLoginUser($this->filter_admin_user);

    // remove input format
    $this
      ->drupalPostRequest('admin/settings/filters/delete/' . $this->input_format_id, array(), t('Delete'));

    // Always call the tearDown() function from the parent class.
    parent::tearDown();
  }

  /**
   * Assert function for testing if GeSHi highlighting works
   */
  function assertGeshiFilterHighlighting($open_tag, $source_code, $close_tag, $lang, $description) {

    // Create content.
    $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'));

    // check posted node
    $node = node_load(array(
      'title' => $edit['title'],
    ));
    $this
      ->assertTrue($node, 'Node found in database. %s');

    // check if highlighting succeeded
    $highlighted = geshifilter_geshi_process($source_code, $lang);
    $this
      ->assertWantedRaw($highlighted, $description);
  }

  /**
   * Check if tags like [c++] and [c#] work
   * Problem described in http://drupal.org/node/208720
   */
  function testSpecialTags() {

    // Enabled the tags
    $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#');

    // Test the tags
    $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'));
  }

  /**
   * Test if tags like [cpp], [css], [csharp] aren't highjacked by [c]
   */
  function testPrefixTags() {

    // enabled the tags
    $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');

    // Test the tags
    $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'));
  }

}

Classes

Namesort descending Description
GeshiFilterTest Unit tests for the GeSHi filter module.