You are here

blocked_ips_expire.bulk_assign.test in Blocked IPs Expire 7

File

tests/blocked_ips_expire.bulk_assign.test
View source
<?php

/**
 * @file
 * Contains \BlockedIpsExpireBulkAssign.
 */

/**
 * Tests that IPs without expiry dates can be fixed.
 */
class BlockedIpsExpireBulkAssign extends BlockedIpsExpireTestCase {

  /**
   * An IP address without an expiry date.
   *
   * @var string
   */
  protected $ipWithoutExpiry;

  /**
   * An IP address with an expiry date.
   *
   * @var string
   */
  protected $ipWithExpiry;

  /**
   * An expiry date for an IP address with an expiry date.
   *
   * @var int
   */
  protected $ipWithExpiryExpiryDate;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => 'Blocked IPs Expire: Bulk-assign tests',
      'description' => 'Tests that IPs without expiry dates can be fixed.',
      'group' => 'Blocked IPs Expire',
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {

    // SimpleTest Turbo makes things really fast. Let's use it if we can.
    if (module_exists('simpletest_turbo')) {
      $id = simpletest_turbo_id('simpletest_turboOptimized');
      if (db_table_exists('fasttest' . $id . 'node')) {
        $this
          ->prepareDatabasePrefix();
        $this
          ->prepareEnvironment();
        $this
          ->changeDatabasePrefix();
        simpletest_turbo_copy_tables('fasttest' . $id, $this->databasePrefix);
        variable_set('file_public_path', $this->public_files_directory);
        variable_set('file_private_path', $this->private_files_directory);
        variable_set('file_temporary_path', $this->temp_files_directory);
        $this->setup = TRUE;
      }
      else {
        parent::setUp();
        simpletest_turbo_copy_tables($this->databasePrefix, 'fasttest' . $id);
      }
    }
    else {
      parent::setUp();
    }

    // Add an IP address.
    $this->ipWithoutExpiry = BlockedIpsExpireBulkAssign::generateIpAddress();
    $this->ipWithExpiry = BlockedIpsExpireBulkAssign::generateIpAddress();
    $this->ipWithExpiryExpiryDate = BlockedIpsExpireBulkAssign::randomDatetime();
  }

  /**
   * Tests that the status report shows when IPs don't have expiry dates.
   */
  public function testBulkAssignMessageOnStatusReport() {

    // Create an admin and log in.
    $admin_user = $this
      ->drupalCreateUser(array(
      'administer site configuration',
      'access site reports',
    ));
    $this
      ->drupalLogin($admin_user);

    // Get the status report and test there is no status report row.
    $this
      ->drupalGet('admin/reports/status');
    $this
      ->assertNoText(t("to assign expiry dates to blocked IP addresses that don't have them."), 'Status report does not show a message when all IP addresses have expiry dates.');

    // Add an IP address without an expiry date.
    BlockedIpsExpireBulkAssign::addIpWithoutExpiryTime($this->ipWithoutExpiry);

    // Get the status report and test there is now a status report row.
    $this
      ->drupalGet('admin/reports/status');
    $this
      ->assertText(t("to assign expiry dates to blocked IP addresses that don't have them."), 'Status report correctly reports if there are IP addresses without expiry dates.');
  }

  /**
   * Tests that the bulk assign page works.
   */
  public function testBulkAssignPage() {

    // Create an admin and log in.
    $admin_user = $this
      ->drupalCreateUser(array(
      'block IP addresses',
    ));
    $this
      ->drupalLogin($admin_user);

    // Test the bulk-assignment page correctly reports when no IP addresses need
    // expiry dates.
    $this
      ->drupalGet('admin/config/people/blocked_ips_expire/bulk_assign');
    $this
      ->assertText(t('No IP addresses need expiry dates.'), 'Bulk-assignment page correctly reports when no IP addresses need expiry dates.');

    // Add two IP addresses, one with an expiry date, and one without.
    BlockedIpsExpireBulkAssign::addIpWithoutExpiryTime($this->ipWithoutExpiry);
    _blocked_ips_expire_add_ip($this->ipWithExpiry, $this->ipWithExpiryExpiryDate);

    // Test the form correctly reports there are IPs needing expiry dates.
    $this
      ->drupalGet('admin/config/people/blocked_ips_expire/bulk_assign');
    $this
      ->assertText(t('The following IP addresses have not been assigned expiry dates'), 'Bulk-assignment page correctly reports that there are IP addresses without expiry dates.');
    $this
      ->assertText($this->ipWithoutExpiry, 'Bulk-assignment page correctly lists the IP that is missing an expiry date.');
    $this
      ->assertNoText($this->ipWithExpiry, 'Bulk-assignment page does not list the IP that does have an expiry date.');
  }

  /**
   * Tests that the bulk assign form sets expiry dates as-specified by the user.
   */
  public function testBulkAssignSetsUserSpecifiedExpiryDates() {
    $specific_date = '2014-09-27 13:41:13 +0000';

    // Create an admin and log in.
    $admin_user = $this
      ->drupalCreateUser(array(
      'block IP addresses',
    ));
    $this
      ->drupalLogin($admin_user);

    // Enter a specific date on the bulk-assignment page and try to run it.
    $iid = BlockedIpsExpireBulkAssign::addIpWithoutExpiryTime($this->ipWithoutExpiry);
    $this
      ->drupalPost('admin/config/people/blocked_ips_expire/bulk_assign', array(
      'bulk_assign_new_date' => $specific_date,
    ), t('Assign new expiry date to all IP addresses without one already'));
    $this
      ->assertText(t('Assigned the date @date to', array(
      '@date' => format_date(strtotime($specific_date)),
    )));

    // Test the IP we added has the new date.
    $ip_info = _blocked_ips_expire_get_one($iid);
    $this
      ->assertEqual($ip_info->expiry_date, strtotime($specific_date), 'Specific date assigned correctly.');
  }

}

Classes

Namesort descending Description
BlockedIpsExpireBulkAssign Tests that IPs without expiry dates can be fixed.