You are here

codefilter_prism.test in Code Filter 7

Unit tests for codefilter_prism.module.

File

modules/codefilter_prism/codefilter_prism.test
View source
<?php

/**
 * @file
 * Unit tests for codefilter_prism.module.
 */

/**
 * Contains unit tests for codefilter_prism.module.
 */
class CodeFilterPrismUnitTestCase extends DrupalUnitTestCase {

  // The filter object as returned from codefilter_prism_filter_info().
  private $filter;

  // Path to this file.
  private $path;

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'codefilter_prism module text filters',
      'description' => 'Tests raw filtering functions.',
      'group' => 'Code Filter Prism',
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->path = drupal_get_path('module', 'codefilter_prism') . '/tests';
    $this->filter = $this
      ->getFilter();
  }

  /**
   * Returns a codefilter filter defintion, with prism enabled.
   *
   * @return array
   *   The filter definition as defined in codefilter_filter_info().
   */
  protected function getFilter() {
    include_once dirname(__FILE__) . '/codefilter_prism.module';
    include_once dirname(__FILE__) . '/../../codefilter.module';
    $filter = codefilter_filter_info();

    // Enable prism.
    $filter['codefilter']['default settings']['codefilter_prism'] = TRUE;
    return $filter;
  }

  /**
   * Filters text through codefilter_prisms prepare and process callbacks.
   *
   * @param string $text
   *   The text to filter.
   * @param array $settings
   *   Additional settings.
   *
   * @return string
   *   The processed text.
   */
  protected function filterText($text, array $settings = array()) {
    $filter =& $this->filter['codefilter'];

    // Set up a dummy format using defaults.
    $format = new stdClass();
    $format->settings = array_merge($filter['default settings'], $settings);
    $text = call_user_func($filter['prepare callback'], $text, $format);
    $text = call_user_func($filter['process callback'], $text, $format);
    return $text;
  }

  /**
   * Checks that <?php tags are escaped and highlighted correctly.
   */
  public function testPhpFilter() {
    $input = file_get_contents($this->path . '/php-input.txt');
    $expected = file_get_contents($this->path . '/php-output.txt');
    $result = $this
      ->filterText($input);
    $this
      ->assertIdentical($expected, $result);
  }

  /**
   * Checks that <?php tags are escaped and highlighted correctly with markup.
   */
  public function testPhpFilterWithMarkup() {
    $input = <<<EOD
<?php
return '<p>' . \$result . '</p>';
?>
EOD;
    $expected = <<<EOD
<pre class="codeblock"><code class="language-php">return &#039;&lt;p&gt;&#039; . \$result . &#039;&lt;/p&gt;&#039;;
</code></pre>
EOD;
    $result = $this
      ->filterText($input);
    $this
      ->assertIdentical($expected, $result);
  }

  /**
   * Checks that <code> tags are escaped and highlighted correctly.
   */
  public function testCodeFilterPrism() {
    $input = file_get_contents($this->path . '/code-input.txt');
    $expected = file_get_contents($this->path . '/code-output.txt');
    $result = $this
      ->filterText($input);
    $this
      ->assertIdentical($expected, decode_entities($result));
  }

  /**
   * Checks that <code> and <?php inline escaped and highlighted correctly.
   */
  public function testInlineFilter() {
    $input = file_get_contents($this->path . '/inline-input.txt');
    $expected = file_get_contents($this->path . '/inline-output.txt');
    $result = $this
      ->filterText($input);
    $this
      ->assertIdentical($expected, $result);
  }

  /**
   * Tests <code class="..."> tags (with attributes).
   */
  public function testCodeFilterPrismAttributes() {
    $input = <<<EOD
<code class="diff">
line1
line2
</code>
EOD;
    $expected = <<<EOD
<pre class="codeblock"><code class="language-php diff">line1&#10;line2&#10;</code></pre>
EOD;
    $result = $this
      ->filterText($input);
    $this
      ->assertIdentical($expected, $result);

    // Existing language.
    $input = <<<EOD
<pre class="codeblock"><code class="language-css">
line1
line2
</code></pre>
EOD;
    $expected = <<<EOD
<pre class="codeblock"><code class="language-css">line1
line2
</code></pre>
EOD;
    $result = $this
      ->filterText($input);
    $this
      ->assertIdentical($expected, $result);
  }

  /**
   * Checks that CSS classes are added which JS uses for hover events.
   */
  public function testContainerExpand() {
    $input = file_get_contents($this->path . '/php-input.txt');
    $settings = array(
      'nowrap_expand' => TRUE,
    );
    $result = $this
      ->filterText($input, $settings);
    $this
      ->assertTrue(strpos($result, '<pre class="codeblock nowrap-expand">') !== FALSE, 'Expand class is added to codefilter_prism blocks that are too long when that option is specified.');
  }

  /**
   * Overrides DrupalTestCase::assertIdentical().
   *
   * Ignores $message and dump-exports $first and $second into the test result
   * output instead.
   */
  protected function assertIdentical($first, $second, $message = '', $group = '') {
    $message = format_string('<pre>@first</pre> is identical to <pre>@second</pre>', array(
      '@first' => var_export($first, TRUE),
      '@second' => var_export($second, TRUE),
    ));
    parent::assertIdentical($first, $second, $message);
  }

}

Classes

Namesort descending Description
CodeFilterPrismUnitTestCase Contains unit tests for codefilter_prism.module.