You are here

no_nbsp.test in No Non-breaking Space Filter 7

Tests for the no_nbsp.module.

File

no_nbsp.test
View source
<?php

/**
 * @file
 * Tests for the no_nbsp.module.
 */

/**
 * Base class for the no non-breaking space filter tests.
 *
 * Some helper methods.
 */
abstract class NoNbspWebTestCase extends DrupalWebTestCase {

  /**
   * Create a new text format.
   *
   * Create a new text format with an enabled no non-breaking space filter
   * programmatically.
   *
   * @param string $name
   *   The machine name of the new text format.
   * @param bool $status
   *   If the filter is enabled or not.
   */
  protected function createTextFormatProgrammatically($name, $status) {
    $format = array(
      'format' => $name,
      'name' => $name,
      'filters' => array(
        'filter_no_nbsp' => array(
          'status' => $status,
        ),
      ),
    );
    $format = (object) $format;
    filter_format_save($format);
    return $format;
  }

  /**
   * Create a new text format.
   *
   * Create a new text format with an enabled no non-breaking space filter
   * using the web functions provided by simpletest.
   *
   * @param string $name
   *   The machine name of the new text format.
   * @param bool $status
   *   If the filter is enabled or not.
   */
  protected function createTextFormatWeb($name, $status) {
    $edit = array(
      'format' => $name,
      'name' => $name,
      'roles[1]' => 1,
      'roles[2]' => 1,
    );
    if ($status) {
      $edit['filters[filter_no_nbsp][status]'] = $status;
    }
    $this
      ->drupalPost('admin/config/content/formats/add', $edit, t('Save configuration'));
    filter_formats_reset();
    $formats = filter_formats();
    return $formats[$name];
  }

  /**
   * Create a new text format and a new node.
   *
   * @param string $text
   *   Body text of the node.
   * @param bool $status
   *   If the filter is enabled or not.
   */
  protected function createFormatAndNode($text, $status) {
    $format = $this
      ->createTextFormatWeb(strtolower($this
      ->randomName()), $status);
    filter_formats_reset();
    $edit = array();
    $edit['title'] = $this
      ->randomName();
    $edit['body[und][0][value]'] = $text;
    $edit['body[und][0][format]'] = $format->name;
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $node = $this
      ->drupalGetNodeByTitle($edit['title']);
    $this
      ->drupalGet('node/' . $node->nid);
    return $node;
  }

}

/**
 * Tests the administrative functionality of the Filter module.
 */
class NoNbspIntegrationTestCase extends NoNbspWebTestCase {

  /**
   * {@inheritdoc}
   */
  protected $profile = 'minimal';

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Integration tests',
      'description' => 'Add different text formats via the admin interface and create some nodes with or without the no non-breaking space filter.',
      'group' => 'No Non-breaking Space Filter',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp(array(
      'field_ui',
      'no_nbsp',
    ));
    $this
      ->drupalCreateContentType(array(
      'name' => 'page',
      'type' => 'page',
    ));
    $this->user = $this
      ->drupalCreateUser(array(
      'administer filters',
      'bypass node access',
      'administer content types',
      'administer fields',
    ));
    $this
      ->drupalLogin($this->user);
  }

  /**
   * Tests the format administration functionality.
   */
  public function testFormatAdmin() {

    // Check format add page.
    $this
      ->drupalGet('admin/config/content/formats');
    $this
      ->clickLink('Add text format');
    $this
      ->assertText(t('No Non-breaking Space Filter'), 'Title text is shown.');
    $this
      ->assertText(t('Delete all non-breaking space HTML entities.'), 'Description text is shown.');
    $this
      ->assertText(t('Preserve placeholders.'), 'Settings: Title.');
    $this
      ->assertText(t('A placeholder non-breaking space is surrounded by a HTML tag'), 'Settings: Description.');

    // Add new format.
    $format_id = 'no_nbsp_format';
    $name = 'No nbsp filter format';
    $edit = array(
      'format' => $format_id,
      'name' => $name,
      'roles[1]' => 1,
      'roles[2]' => 1,
      'filters[filter_no_nbsp][status]' => 1,
    );
    $this
      ->drupalPost(NULL, $edit, t('Save configuration'));

    // Text the filters tips.
    $this
      ->drupalGet('filter/tips');
    $this
      ->assertText(t('All non-breaking space HTML entities are replaced by blank space characters.'));
    $this
      ->assertText(t('Multiple contiguous space characters are replaced by a single blank space character.'));

    // Show submitted format edit page.
    $this
      ->drupalGet('admin/config/content/formats/' . $format_id);
    $input = $this
      ->xpath('//input[@id="edit-filters-filter-no-nbsp-status"]');
    $this
      ->assertEqual($input[0]
      ->attributes()->checked, 'checked');

    // Test the format object.
    filter_formats_reset();
    $formats = filter_formats($this->user);
    $this
      ->assertIdentical($formats[$format_id]->name, $name);

    // Check format overview page.
    $this
      ->drupalGet('admin/config/content/formats');
    $this
      ->assertText($name);

    // Generate a page without the enabled text filter.
    $node = $this
      ->createFormatAndNode('l&nbsp;&nbsp;&nbsp;o&nbsp;&nbsp;&nbsp;l', 0);
    $node_without_filter = $node;
    $this
      ->assertRaw('l&nbsp;&nbsp;&nbsp;o&nbsp;&nbsp;&nbsp;l');
    $this
      ->drupalGet('node/' . $node->nid . '/edit');

    // no_nbsp_format exists at this time.
    $this
      ->assertText(t('All non-breaking space HTML entities are replaced by blank space characters.'));
    $this
      ->assertNoText(t('Multiple contiguous space characters are replaced by a single blank space character.'));

    // Generate a page with the enabled text filter.
    $node = $this
      ->createFormatAndNode('l&nbsp;&nbsp;&nbsp;o&nbsp;&nbsp;&nbsp;l', 1);
    $this
      ->assertRaw('l o l');
    $this
      ->drupalGet('node/' . $node->nid . '/edit');
    $this
      ->assertText(t('All non-breaking space HTML entities are replaced by blank space characters.'));
    $this
      ->assertNoText(t('Multiple contiguous space characters are replaced by a single blank space character.'));

    // Field formatter.
    $this
      ->drupalGet('admin/structure/types/manage/page/display');
    $this
      ->assertText(t('No Non-breaking Space Filter'));
    $edit = array(
      'fields[body][type]' => 'no_nbsp',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->drupalGet('node/' . $node_without_filter->nid);
    $this
      ->assertRaw('l o l');
  }

}

/**
 * The no non-breaking space filter acts as a field formatter.
 */
class FieldFormatterTest extends NoNbspWebTestCase {

  /**
   * {@inheritdoc}
   */
  protected $profile = 'minimal';

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Field formatter',
      'description' => 'The no non-breaking space filter acts as a field formatter.',
      'group' => 'No Non-breaking Space Filter',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp(array(
      'field_ui',
      'no_nbsp',
    ));
    $this
      ->drupalCreateContentType(array(
      'type' => 'page',
      'name' => 'Basic page',
    ));
    $this->user = $this
      ->drupalCreateUser(array(
      'administer filters',
      'create page content',
      'administer content types',
      'administer fields',
    ));
    $this
      ->drupalLogin($this->user);
  }

  /**
   * Field formatter.
   */
  public function testFieldFormatter() {
    $this
      ->createTextFormatWeb('with_no_nbsp', TRUE);
    $this
      ->createTextFormatWeb('without_no_nbsp', FALSE);

    // Check admin interface.
    $this
      ->drupalGet('admin/structure/types/manage/page/display');
    $this
      ->assertText(t('No Non-breaking Space Filter'));

    // Create node with full html support.
    $edit = array();
    $title = $this
      ->randomName();
    $edit['title'] = $title;
    $edit['body[und][0][value]'] = 'l&nbsp;&nbsp;&nbsp;o&nbsp;&nbsp;&nbsp;l';
    $edit['body[und][0][format]'] = 'without_no_nbsp';
    $this
      ->drupalPost('node/add/page', $edit, t('Save'));
    $node = $this
      ->drupalGetNodeByTitle($title);

    // The field formatter is not set.
    $this
      ->assertRaw('l&nbsp;&nbsp;&nbsp;o&nbsp;&nbsp;&nbsp;l');

    // Change display.
    $edit = array(
      'fields[body][type]' => 'no_nbsp',
    );
    $this
      ->drupalPost('admin/structure/types/manage/page/display', $edit, t('Save'));

    // Check if the field formatter works.
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertRaw('l o l');
  }

}

/**
 * Unit tests.
 */
class NoNbspUnitTestCase extends DrupalUnitTestCase {

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Unit tests',
      'description' => 'Run unit tests on some functions.',
      'group' => 'No Non-breaking Space Filter',
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    drupal_load('module', 'no_nbsp');
    parent::setUp();
  }

  /**
   * Test the function _no_nbsp_eraser.
   */
  public function testFunctionNoNbspEraser() {
    $this
      ->assertEqual(_no_nbsp_eraser('l&nbsp;o&nbsp;l'), 'l o l');
    $this
      ->assertEqual(_no_nbsp_eraser('l&nbsp;&nbsp;o&nbsp;&nbsp;l'), 'l o l');
    $this
      ->assertEqual(_no_nbsp_eraser('l&nbsp; o&nbsp; l'), 'l o l');
    $this
      ->assertEqual(_no_nbsp_eraser('l &nbsp; o &nbsp; l'), 'l o l');
    $this
      ->assertEqual(_no_nbsp_eraser('l &nbsp;o &nbsp;l'), 'l o l');
    $this
      ->assertEqual(_no_nbsp_eraser('l  o  l'), 'l o l');
    $this
      ->assertEqual(_no_nbsp_eraser('l o l'), 'l o l');
    $this
      ->assertEqual(_no_nbsp_eraser('l&nbspol'), 'l&nbspol');
    $this
      ->assertEqual(_no_nbsp_eraser(' '), ' ');
    $this
      ->assertEqual(_no_nbsp_eraser('&nbsp;'), ' ');
    $this
      ->assertEqual(_no_nbsp_eraser('&nbsp;&nbsp;&nbsp;'), ' ');
    $this
      ->assertEqual(_no_nbsp_eraser('&NBSP;'), ' ');
  }

}

Classes

Namesort descending Description
FieldFormatterTest The no non-breaking space filter acts as a field formatter.
NoNbspIntegrationTestCase Tests the administrative functionality of the Filter module.
NoNbspUnitTestCase Unit tests.
NoNbspWebTestCase Base class for the no non-breaking space filter tests.