You are here

class GeshiFilterTest in GeSHi Filter for syntax highlighting 5.2

Same name and namespace in other branches
  1. 6 geshifilter.test \GeshiFilterTest
  2. 7 geshifilter.test \GeshiFilterTest

Unit tests for the GeSHi filter module.

Hierarchy

Expanded class hierarchy of GeshiFilterTest

File

tests/geshifilter.test, line 6

View source
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'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$_cleanupModules property
DrupalTestCase::$_cleanupRoles property
DrupalTestCase::$_cleanupUsers property
DrupalTestCase::$_cleanupVariables property
DrupalTestCase::$_content property
DrupalTestCase::assertCopy function Will trigger a pass if both parameters refer to different objects. Fail otherwise.
DrupalTestCase::assertEqual function Will trigger a pass if the two parameters have the same value only. Otherwise a fail.
DrupalTestCase::assertError function Confirms that an error has occurred and optionally that the error text matches exactly.
DrupalTestCase::assertErrorPattern function Confirms that an error has occurred and that the error text matches a Perl regular expression.
DrupalTestCase::assertIdentical function Will trigger a pass if the two parameters have the same value and same type. Otherwise a fail.
DrupalTestCase::assertIsA function Type and class test. Will pass if class matches the type name or is a subclass or if not an object, but the type is correct.
DrupalTestCase::assertNoErrors function Confirms that no errors have occurred so far in the test method.
DrupalTestCase::assertNotA function Type and class mismatch test. Will pass if class name or underling type does not match the one specified.
DrupalTestCase::assertNotEqual function Will trigger a pass if the two parameters have a different value. Otherwise a fail.
DrupalTestCase::assertNotIdentical function Will trigger a pass if the two parameters have the different value or different type.
DrupalTestCase::assertNotNull function Will be true if the value is set.
DrupalTestCase::assertNoUnwantedPattern function Will trigger a pass if the Perl regex pattern is not present in subject. Fail if found.
DrupalTestCase::assertNoUnwantedRaw function Will trigger a pass if the raw text is NOT found on the loaded page Fail otherwise.
DrupalTestCase::assertNull function Will be true if the value is null.
DrupalTestCase::assertReference function Will trigger a pass if both parameters refer to the same object. Fail otherwise.
DrupalTestCase::assertWantedPattern function Will trigger a pass if the Perl regex pattern is found in the subject. Fail otherwise.
DrupalTestCase::assertWantedRaw function Will trigger a pass if the raw text is found on the loaded page Fail otherwise.
DrupalTestCase::clickLink function Follows a link by name. Will click the first link found with this link text by default, or a later one if an index is given. Match is case insensitive with normalised space. Does make assertations if the click was sucessful or not and it does…
DrupalTestCase::drupalCheckAuth function @abstract Checks to see if we need to send a http-auth header to authenticate when browsing a site.
DrupalTestCase::drupalCreateRolePerm function Create a role / perm combination specified by permissions
DrupalTestCase::drupalCreateUserRolePerm function Creates a user / role / permissions combination specified by permissions
DrupalTestCase::drupalGet function @abstract Brokder for the get function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::drupalGetContent function @TODO: needs documentation
DrupalTestCase::drupalLoginUser function Logs in a user with the internal browser
DrupalTestCase::drupalModuleDisable function Disables a drupal module
DrupalTestCase::drupalModuleEnable function Enables a drupal module
DrupalTestCase::drupalPostRequest function Do a post request on a drupal page. It will be done as usual post request with SimpleBrowser
DrupalTestCase::drupalRawPost function @abstract Broker for the post function adds the authentication headers if necessary @author Earnest Berry III <earnest.berry@gmail.com>
DrupalTestCase::DrupalTestCase function
DrupalTestCase::drupalVariableSet function Set a druapl variable and keep track of the changes for tearDown()
DrupalTestCase::randomName function Generates a random string, to be used as name or whatever
DrupalTestCase::run function Just some info for the reporter
GeshiFilterTest::$filter_admin_user property A global filter adminstrator
GeshiFilterTest::$input_format_id property The id of the input format with only GeSHi filter in it
GeshiFilterTest::$normal_user property A global user for adding pages
GeshiFilterTest::assertGeshiFilterHighlighting function Assert function for testing if GeSHi highlighting works
GeshiFilterTest::get_info function Drupal SimpleTest method: return metadata about the test.
GeshiFilterTest::setUp function SimpleTest core method: code run before each and every test method.
GeshiFilterTest::tearDown function SimpleTest core method: code run after each and every test method. Overrides DrupalTestCase::tearDown
GeshiFilterTest::testPrefixTags function Test if tags like [cpp], [css], [csharp] aren't highjacked by [c]
GeshiFilterTest::testSpecialTags function Check if tags like [c++] and [c#] work Problem described in http://drupal.org/node/208720