You are here

birthdays.test in Birthdays 6

Same filename and directory in other branches
  1. 7 birthdays.test

Tests for the Birthdays module.

File

birthdays.test
View source
<?php

/**
 * @file
 * Tests for the Birthdays module.
 */
class BirthdaysTestCase extends DrupalWebTestCase {
  protected $admin_user;
  protected $web_user;
  protected $field;

  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Birthdays module'),
      'description' => t('Test all major and minor functions of the birthdays module.'),
      'group' => t('Birthdays'),
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp('birthdays', 'profile');

    // Create users.
    $this->admin_user = $this
      ->drupalCreateUser(array(
      'administer site configuration',
      'access administration pages',
      'administer users',
      'administer permissions',
      'access birthdays',
    ));
    $this->web_user = $this
      ->drupalCreateUser(array(
      'access birthdays',
    ));
    $this
      ->drupalLogin($this->admin_user);

    // Add profile field.
    $field = array(
      'category' => $this
        ->randomName(8),
      'title' => $this
        ->randomName(8),
      'name' => 'profile_' . $this
        ->randomName(8),
      'required' => 1,
    );
    $this
      ->drupalPost('admin/user/profile/add/date', $field, t('Save field'));

    // Get profile field ID.
    $fid = db_result(db_query("SELECT fid FROM {profile_fields} WHERE name = '%s'", $field['name']));

    // Set profile field as birthdays field.
    $this
      ->drupalPost('admin/settings/birthdays', array(
      'birthdays_field_id' => $fid,
    ), t('Save configuration'));

    // Confirm that the field_id has been set
    $this
      ->assertEqual(variable_get('birthdays_field_id', NULL), $fid, t('Birthdays field has been set successfully'));
    $this->field = _birthdays_get_field(variable_get('birthdays_field_id', NULL));
    $this
      ->drupalLogout();
  }

  /**
   * Test the delete permission and its effects in the profile.
   */
  function testDeleteOption() {
    $this
      ->drupalLogin($this->web_user);

    // Try to insert empty birthday, while required.
    $this
      ->drupalPost('user/' . $this->web_user->uid . '/edit/' . $this->field->category, array(), t('Save'));
    $this
      ->assertText($this->field->title . ' field is required.', t('Birthday field is required.'));

    // Set birthday of user.
    $birthday = array(
      $this->field->name . '[month]' => 3,
      $this->field->name . '[day]' => 23,
      $this->field->name . '[year]' => 1980,
    );
    $this
      ->drupalPost('user/' . $this->web_user->uid . '/edit/' . $this->field->category, $birthday, t('Save'));

    // Confirm that birthday is set correctly.
    $this
      ->drupalGet('user/' . $this->web_user->uid);
    $this
      ->assertText('03/23/1980', t('Birthday has been set.'));

    // Try to delete birthday.
    $birthday = array(
      $this->field->name . '[month]' => '',
      $this->field->name . '[day]' => '',
      $this->field->name . '[year]' => '',
    );
    $this
      ->drupalPost('user/' . $this->web_user->uid . '/edit/' . $this->field->category, $birthday, t('Save'));

    // Confirm deletion is not possible.
    $this
      ->assertText($this->field->title . ' field is required.', t('Birthday deletion prevented.'));

    // Set birthdays field optional, thus making deletion possible.
    db_query('UPDATE {profile_fields} SET required = 0 WHERE fid = %d', $this->field->fid);

    // Confirm deletion is possible.
    $this
      ->drupalPost('user/' . $this->web_user->uid . '/edit/' . $this->field->category, $birthday, t('Save'));
    $this
      ->assertNoText($this->field->title . ' field is required.', t('Birthday deletion allowed.'));

    // Confirm birthday is deleted (not displayed on profile page).
    $this
      ->drupalGet('user/' . $this->web_user->uid);
    $this
      ->assertNoText($this->field->title, t('Birthday has been set.'));
  }

  /**
   * Test the year of birth visibility options.
   */
  function testHideYearOption() {
    $this
      ->drupalLogin($this->web_user);

    // Set birthday of user.
    $birthday = array(
      $this->field->name . '[month]' => 3,
      $this->field->name . '[day]' => 23,
      $this->field->name . '[year]' => 1980,
    );
    $age = _birthdays_calculate_age(23, 3, 1980);
    $this
      ->drupalPost('user/' . $this->web_user->uid . '/edit/' . $this->field->category, $birthday, t('Save'));

    // Confirm there is no checkbox "Hide age and birth year", it's the default
    $this
      ->drupalGet('user/' . $this->web_user->uid . '/edit/' . $this->field->category);
    $this
      ->assertNoField('birthdays_user_hide_year', '"Hide age and birth year" option not present.');

    // Globally hide year of birth and age.
    variable_set('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_YES);

    // Confirm age and year are hidden, but the date of birth isn't.
    $this
      ->drupalGet('user');
    $this
      ->assertText('03/23', 'Date of birth visible.');
    $this
      ->assertNoText('1980', 'Year hidden.');
    $this
      ->assertNoText('(' . $age . ')', 'Age hidden.');

    // Set hiding year of birth and age as user option.
    variable_set('birthdays_hide_year', BIRTHDAYS_HIDE_YEAR_USER);

    // Confirm age and year are not hidden.
    $this
      ->drupalGet('user');
    $this
      ->assertText('1980', 'Year not hidden.');
    $this
      ->assertText('(' . $age . ')', 'Age not hidden.');

    // Hide year of birth and age for this user.
    $this
      ->drupalPost('user/' . $this->web_user->uid . '/edit/' . $this->field->category, array(
      'birthdays_user_hide_year' => BIRTHDAYS_HIDE_YEAR_USER_YES,
    ), t('Save'));

    // Confirm age and year are hidden, but the date of birth isn't.
    $this
      ->drupalGet('user');
    $this
      ->assertText('03/23', 'Date of birth visible.');
    $this
      ->assertNoText('1980', 'Year of birth hidden.');
    $this
      ->assertNoText('(' . $age . ')', 'Age hidden.');
  }

  /**
   * Test the starsigns visibility options.
   */
  function testStarsigns() {
    $this
      ->drupalLogin($this->web_user);

    // Set birthday of user.
    $birthday = array(
      $this->field->name . '[month]' => 3,
      $this->field->name . '[day]' => 23,
      $this->field->name . '[year]' => 1980,
    );
    $this
      ->drupalPost('user/' . $this->web_user->uid . '/edit/' . $this->field->category, $birthday, t('Save'));

    // Confirm absense of starsign.
    $this
      ->drupalGet('user');
    $this
      ->assertNoText('aries', 'Starsign is not displayed.');

    // Set starsigns to visible, without link.
    variable_set('birthdays_show_starsign', BIRTHDAYS_STARSIGN_NOLINK);

    // Confirm presence of starsign, but absense of link.
    $this
      ->drupalGet('user');
    $this
      ->assertRaw('<span class="birthdays-starsign"><img src="' . base_path() . drupal_get_path('module', 'birthdays') . '/starsigns/aries.gif" alt="aries" /></span>', 'Starsign is displayed without link.');

    // Set starsigns to visible, with link.
    variable_set('birthdays_show_starsign', BIRTHDAYS_STARSIGN_LINK);

    // Confirm presence of starsign, with link.
    $this
      ->drupalGet('user');
    $this
      ->assertRaw('<span class="birthdays-starsign"><a href="http://astrology.yahoo.com/astrology/general/dailyoverview/aries" target="_blank" title="aries"><img src="' . base_path() . drupal_get_path('module', 'birthdays') . '/starsigns/aries.gif" alt="aries" /></a></span>', 'Starsign is displayed with link.');
  }

}

Classes

Namesort descending Description
BirthdaysTestCase @file Tests for the Birthdays module.