You are here

node_teaser.test in SimpleTest 6

File

tests/node_teaser.test
View source
<?php

class NodeTeaserTest extends DrupalTestCase {

  /**
   * Implementation of get_info() for information
   */
  function get_info() {
    return array(
      'name' => t('Node teaser tests'),
      'desc' => t('Calls node_teaser() with different strings and lengths.'),
      'group' => 'Node Tests',
    );
  }
  function setUp() {
    parent::setUp();
  }
  function tearDown() {
    parent::tearDown();
  }

  /**
   * Simpletest test. Tests an edge case where if the first sentence is a
   * question and subsequent sentences are not.
   * This failed in drupal 5.
   * Test and patch for drupal 6 (committed) from
   * http://drupal.org/node/180425
   */
  function testFirstSentenceQuestion() {
    $body = 'A question? A sentence. Another sentence.';
    $expectedTeaser = 'A question? A sentence.';
    $this
      ->callNodeTeaser($body, $expectedTeaser, NULL, 30);
  }

  /**
   * Simpletest test. A real-life example of the above edge case.
   */
  function testFirstSentenceQuestion2() {
    $body = 'Are you an UberBabe? (Or an appreciator of UberBabes?) I am most definitely an UberBabe, and I\'m proud of it. Now, before anyone screams "sexism" or "bias" or "cheap" or anything more profane, let me clarify. An UberBabe is not someone who\'s playfully pierced navel protrudes from a belly bearing top. Not necessarily anyway. An UberBabe is a woman who likes being totally feminine, but is also smart as hell, brave, a rule breaker, speaks her mind, finds her own way, goes up against "the system" in a way that allows the system to evolve, and so on. UberBabes, frankly, kick booty - and they just may save the world.';
    $expectedTeaser = 'Are you an UberBabe? (Or an appreciator of UberBabes?) I am most definitely an UberBabe, and I\'m proud of it. Now, before anyone screams "sexism" or "bias" or "cheap" or anything more profane, let me clarify.';
    $this
      ->callNodeTeaser($body, $expectedTeaser, NULL, 300);
  }

  /**
   * Simpletest test. Runs a test adapted from
   * http://drupal.org/node/180425#comment-634230
   */
  function testLength() {

    // This body string tests a number of edge cases.
    $body = "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>";

    // The teasers we expect node_teaser() to return when $size is the index
    // of each array item.
    // Using an input format with no line-break filter:
    $teasers = array(
      "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
      "<",
      "<p",
      "<p>",
      "<p>\n",
      "<p>\nH",
      "<p>\nHi",
      "<p>\nHi\n",
      "<p>\nHi\n<",
      "<p>\nHi\n</",
      "<p>\nHi\n</p",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
      "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
      "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
    );

    // And Using an input format WITH the line-break filter.
    $teasers_lb = array(
      "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
      "<",
      "<p",
      "<p>",
      "<p>",
      "<p>",
      "<p>",
      "<p>\nHi",
      "<p>\nHi",
      "<p>\nHi",
      "<p>\nHi",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>",
      "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
      "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
      "<p>\nHi\n</p>\n<p>\nfolks\n<br />\n!\n</p>",
    );

    // Test node_teaser() for different sizes.
    for ($i = 0; $i <= 37; $i++) {
      $this
        ->callNodeTeaser($body, $teasers[$i], NULL, $i);
      $this
        ->callNodeTeaser($body, $teasers_lb[$i], 1, $i);
      $this
        ->callNodeTeaser($body, $teasers_lb[$i], 2, $i);
    }
  }

  /**
   * Calls node_teaser() and asserts that the expected teaser is returned.
   */
  function callNodeTeaser($body, $expectedTeaser, $format = NULL, $size = NULL) {
    $teaser = node_teaser($body, $format, $size);
    $this
      ->assertIdentical($teaser, $expectedTeaser);
  }

}

Classes

Namesort descending Description
NodeTeaserTest