You are here

site_banner.test in Site Banner 7

File

site_banner.test
View source
<?php

/**
 * @file
 * Automated tests using the core "Testing" Drupal module for
 * the site banner module.
 */
module_load_include('inc', 'site_banner', 'site_banner_options');
module_load_include('module', 'site_banner', 'site_banner');

/**
 * Implements tests for checking that preset colors are valid.
 */
class SiteBannerColorUnitTestCase extends DrupalUnitTestCase {

  /**
   * This test suite ensures the site banner colors are active and valid.
   */
  public static function getInfo() {
    return array(
      'name' => 'Site banner color unit tests',
      'description' => 'Ensure that the site banner colors are active and valid.',
      'group' => 'Site Banner',
    );
  }

  /**
   * Tests whether there is at least one preset background color.
   */
  public function testBackgroundColorsExist() {
    $background_colors = site_banner_get_background_colors();
    $result = count($background_colors) > 0;
    $message = 'Background colors should exist.';
    $this
      ->assertTrue($result, $message);
  }

  /**
   * Tests whether there is at least one preset text color.
   */
  public function testTextColorsExist() {
    $colors = site_banner_get_text_colors();
    $result = count($colors) > 0;
    $message = 'Text colors should exist.';
    $this
      ->assertTrue($result, $message);
  }

  /**
   * Tests whether all text colors pass validation.
   */
  public function testBackgroundColorsAreValid() {
    $background_colors = site_banner_get_background_colors();
    $result = TRUE;
    foreach (array_keys($background_colors) as $color_code) {
      $regex_result = preg_match(site_banner_get_html_color_regex_pattern(), $color_code);
      $result = !($regex_result === FALSE || $regex_result === 0);
    }
    $message = 'Background colors should be valid HTML colors.';
    $this
      ->assertTrue($result, $message);
  }

  /**
   * Tests whether all preset text colors pass validation.
   */
  public function testTextColorsAreValid() {
    $text_colors = site_banner_get_text_colors();
    $result = TRUE;
    foreach (array_keys($text_colors) as $color_code) {
      $regex_result = preg_match(site_banner_get_html_color_regex_pattern(), $color_code);
      $result = !($regex_result === FALSE || $regex_result === 0);
    }
    $message = 'Background colors should be valid HTML colors.';
    $this
      ->assertTrue($result, $message);
  }

}

/**
 * Implements tests for checking that banner rendering functions are correct.
 */
class SiteBannerRenderingUnitTestCase extends DrupalWebTestCase {

  /**
   * This test suite tests the site banner outputs.
   */
  public static function getInfo() {
    return array(
      'name' => 'Site banner rendering unit tests',
      'description' => 'Ensure that the site banner renders valid results.',
      'group' => 'Site Banner',
    );
  }

  /**
   * Test setup function.
   */
  public function setUp() {
    parent::setUp('site_banner');
    variable_set('site_banner_status', TRUE);
    variable_set('site_banner_debug_status', FALSE);
    variable_set('site_banner_text', 'TEST BANNER TEXT');
    variable_set('site_banner_text_color', '#FFFFFF');
    variable_set('site_banner_background_color', '#000000');
  }

  /**
   * Tests if header banner is in right CSS tag.
   */
  public function testBannerHeaderTagExists() {
    $page = array();
    $page = site_banner_build_banner($page);
    $result = $page['page_top'][0]['#attributes']['id'] == 'siteBannerHeaderBanner';
    $message = 'Banner header tag in page array.';
    $this
      ->assertTrue($result, $message);
  }

  /**
   * Tests if footer banner is in right CSS tag.
   */
  public function testBannerFooterTagExists() {
    $page = array();
    site_banner_page_build($page);
    $result = $page['page_top'][1]['#attributes']['id'] == 'siteBannerFooterBanner';
    $message = 'Banner footer tag in page array.';
    $this
      ->assertTrue($result, $message);
  }

  /**
   * Tests if header banner text built by hook implementation.
   */
  public function testBannerHeaderTextExists() {
    $banner = variable_get('site_banner_text', '');
    $page = array();
    site_banner_page_build($page);
    $result = $page['page_top'][0]['#value'] == $banner;
    $message = 'Banner header displays correct text: "' . $banner . '".';
    $this
      ->assertTrue($result, $message);
  }

  /**
   * Tests if footer banner text built by hook implementation.
   */
  public function testBannerFooterTextExists() {
    $banner = variable_get('site_banner_text', '');
    $page = array();
    site_banner_page_build($page);
    $result = $page['page_top'][1]['#value'] == $banner;
    $message = 'Banner footer displays correct text: "' . $banner . '".';
    $this
      ->assertTrue($result, $message);
  }

  /**
   * Tests whether correct colors are rendered in the CSS tags.
   */
  public function testBannerColorsRendered() {
    $background_color = variable_get('site_banner_background_color', '');
    $text_color = variable_get('site_banner_text_color', '');
    $page = array();
    site_banner_page_build($page);
    $css_string = "#siteBannerFooterBanner, #siteBannerHeaderBanner {background-color:{$background_color}; color:{$text_color};}";
    $result = FALSE;
    foreach (drupal_add_css() as $css_tags) {
      $result |= $css_string === $css_tags['data'];
    }
    $message = 'Banner colors appear in HEAD tag in correct format. Current CSS string: ' . $css_string;
    $this
      ->assertTrue($result, $message);
  }

  /**
   * Tests whether multiple contexts changing a banner text is properly handled.
   */
  public function testBannerContextTextChange() {
    $result = FALSE;
    $baseline_banner_text = 'Example Text PrependText01 Text1/Text2 AppendText01';
    $banner_text = variable_get('site_banner_text', '');
    $existing_banner_text = $banner_text;
    $contexts = array(
      'context_1' => (object) array(
        'name' => 'context_1',
        'description' => 'context_1',
        'tag' => 'tag1',
        'conditions' => array(
          'node_taxonomy' => array(
            'values' => array(
              1 => 1,
            ),
            'options' => array(
              'node_form' => '1',
            ),
          ),
        ),
        'reactions' => array(
          'change_banner_background_color' => '#ffffcc',
          'change_banner_text' => array(
            'site_banner_text' => 'Example Text',
            'site_banner_tag_prepend_text' => '',
            'site_banner_tag_delimiter_text' => '',
            'site_banner_tag_append_text' => '',
          ),
          'change_banner_text_color' => '#000000',
        ),
        'condition_mode' => '0',
        'table' => 'context',
        'type' => 'Normal',
        'export_type' => 1,
      ),
      'context_2' => (object) array(
        'name' => 'context_2',
        'description' => 'context_2',
        'tag' => 'tag2',
        'conditions' => array(
          'node_taxonomy' => array(
            'values' => array(
              12 => 12,
            ),
            'options' => array(
              'node_form' => '1',
            ),
          ),
        ),
        'reactions' => array(
          'change_banner_text' => array(
            'site_banner_text' => 'Text1',
            'site_banner_tag_prepend_text' => 'PrependText01 ',
            'site_banner_tag_delimiter_text' => '/',
            'site_banner_tag_append_text' => ' AppendText01',
          ),
        ),
        'condition_mode' => '0',
        'table' => 'context',
        'type' => 'Normal',
        'export_type' => 1,
      ),
      'context_3' => (object) array(
        'name' => 'context_3',
        'description' => 'context_3',
        'tag' => 'tag2',
        'conditions' => array(
          'node_taxonomy' => array(
            'values' => array(
              16 => 16,
            ),
            'options' => array(
              'node_form' => '1',
            ),
          ),
        ),
        'reactions' => array(
          'change_banner_text' => array(
            'site_banner_text' => 'Text2',
            'site_banner_tag_prepend_text' => 'PrependText01 ',
            'site_banner_tag_delimiter_text' => '/',
            'site_banner_tag_append_text' => ' AppendText01',
          ),
        ),
        'condition_mode' => '0',
        'table' => 'context',
        'type' => 'Normal',
        'export_type' => 1,
      ),
    );
    site_banner_generate_context_banner_text_from_contexts($contexts, $banner_text);
    $result = $banner_text == $baseline_banner_text;
    $message = 'Test context can render an altered banner text properly: ';
    $message .= "Existing banner text = \"{$existing_banner_text}\", Updated banner text = \"{$banner_text}\"";
    $message .= ", Baseline banner text = \"{$baseline_banner_text}\".";
    $this
      ->assertTrue($result, $message);
  }

}

Classes

Namesort descending Description
SiteBannerColorUnitTestCase Implements tests for checking that preset colors are valid.
SiteBannerRenderingUnitTestCase Implements tests for checking that banner rendering functions are correct.