You are here

ip_ban.test in IP Ban 8

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

Tests for ip_ban.module.

File

ip_ban.test
View source
<?php

/**
 * @file
 * Tests for ip_ban.module.
 */
define('IP_BAN_ADMIN_FORM_PATH', 'admin/config/ip_ban');

/**
 * Base class for common test configuration.
 */
class IPBanTestBase extends DrupalWebTestCase {
  protected $adminSitesUser;
  protected $ipBanType;

  /**
   * Implement setUp().
   */
  public function setUp() {

    // ip2country is required by ip_ban, and therefore needed for some of the
    // unit and functional testing.
    parent::setUp(array(
      'ip_ban',
      'ip2country',
    ));

    // Add a content type to create dummy page for testing valid paths and
    // proper redirects.
    $this->ipBanType = $this
      ->drupalCreateContentType(array(
      'name' => 'IP Ban Node',
      'type' => 'ipban_node',
    ));

    // Create and log in our privileged user.
    $this->adminSitesUser = $this
      ->drupalCreateUser(array(
      'administer site configuration',
      'create ipban_node content',
      'edit any ipban_node content',
      'access content',
      'bypass node access',
      'administer blocks',
      // The admin must have this permission to avoid immediately losing access
      // when using a spoofed IP address.
      'ignore ip_ban',
    ));
  }

  /**
   * Add a node of type ipban-node for path and other testing.
   */
  public function addBanNode($title = '') {
    if (empty($title)) {
      $title = $this
        ->randomName();
    }
    $edit = array(
      'title' => $title,
    );
    $this
      ->drupalPost('node/add/ipban-node', $edit, t('Save'));
    $this
      ->assertText('IP Ban Node ' . $edit['title'] . ' has been created', 'Found node creation message');
  }

}

/**
 * Tests the IP Ban admin page form.
 */
class IPBanFormTest extends IPBanTestBase {

  /**
   * Implement setUp().
   */
  public function setUp() {

    // Enable any modules required for the test.
    parent::setUp(array());
    $this
      ->drupalLogin($this->adminSitesUser);
  }

  /**
   * Implement getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'IP Ban admin form test',
      'description' => 'Basic testing for the admin form settings.',
      'group' => 'IP Ban',
    );
  }

  /**
   * Various tests for paths entered in the the admin form.
   */
  public function testBanPaths() {

    // Add node for testing path and making sure we land on the proper page
    // for read-only and complete ban paths.
    $this
      ->addBanNode("Test page");

    // Test the read only path saves correctly for valid path.
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();
    $edit['ip_ban_readonly_path'] = 'node/1';
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));

    // @FIXME
    // Could not extract the default value because it is either indeterminate, or
    // not scalar. You'll need to provide a default value in
    // config/install/ip_ban.settings.yml and config/schema/ip_ban.schema.yml.
    $readOnlyPath = \Drupal::config('ip_ban.settings')
      ->get('ip_ban_readonly_path');
    $this
      ->assertIdentical($readOnlyPath, 'node/1');

    // Test the read only path returns an error on invalid path.
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();
    $edit['ip_ban_readonly_path'] = 'readonly_path_' . $this
      ->randomName(16);
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertText(t('The path entered does not exist or you do not have permission to access it.'));

    // Test the complete ban path returns an error on invalid path.
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();
    $edit['ip_ban_completeban_path'] = 'completeban_path_' . $this
      ->randomName(16);
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertText(t('The path entered does not exist or you do not have permission to access it.'));
  }

  /**
   * Test setting setting the US within the table.
   */
  public function testSetCountry() {

    // Set the United States to Complete Ban.
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();

    // 0 = no action / 1 = Read Only / 2 = Complete Ban.
    $edit['ip_ban_US'] = 2;
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertOptionSelected('edit-ip-ban-us', 2, 'Complete ban set correctly in country list table.');
  }

  /**
   * Test adding IP address in various fields (correct and incorrect).
   */
  public function testIpAddressEntry() {

    // Add multiple valid IP addresses.
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();
    $edit['ip_ban_readonly_ips'] = ' 192.168.32.60' . PHP_EOL . '156.228.60.110 ';
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertText(t('The configuration options have been saved.'));

    // Try to add an invalid IP address.
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();
    $edit['ip_ban_additional_ips'] = '666.666.666.666';
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertText(t('You have entered one or more incorrect IPV4 addresses.'));

    // Add multiple IP addresses on the same line (error)
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();
    $edit['ip_ban_additional_ips'] = '192.168.32.60, 156.228.60.110';
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertText(t('You have entered one or more incorrect IPV4 addresses.'));
  }

  /**
   * Test adding multiple disabled blocks (correct and incorrect).
   */
  public function testDisabledBlockEntry() {

    // Add a valid block.
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();
    $edit['ip_ban_disabled_blocks'] = 'user,login';
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertText(t('The configuration options have been saved.'));

    // Use incorrect formatting for the blocks.
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();
    $edit['ip_ban_disabled_blocks'] = 'user, login block,11';
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertText(t('You have one or more blocks with an incorrect format; you must enter exactly one module name and delta name per line, separated by a comma.'));

    // Add a block with an incorrect delta.
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();
    $edit['ip_ban_disabled_blocks'] = 'user,login2';
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertText(t('You entered at least one invalid module name or delta; see the help text for how to enter the proper module name and delta.'));
  }

}

/**
 * Tests the complete ban and read only functionality of the IP Ban module.
 */
class IPBanFunctionalTest extends IPBanTestBase {

  /**
   * Implement setUp().
   */
  public function setUp() {

    // Enable any modules required for the test.
    parent::setUp(array(
      'block',
    ));
    $this
      ->drupalLogin($this->adminSitesUser);
    module_disable(array(
      'dashboard',
    ), TRUE);
  }

  /**
   * Implement getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => 'IP Ban functionality tests',
      'description' => 'Testing for banned and read-only users',
      'group' => 'IP Ban',
    );
  }

  /**
   * Test the complete ban functionality.
   */
  public function testCompleteBan() {

    // node/1.
    $this
      ->addBanNode("Read only page");

    // node/2.
    $this
      ->addBanNode("Complete ban page");
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array(
      // Set the United States to Read Only
      // 0 = no action / 1 = Read Only / 2 = Complete Ban.
      'ip_ban_US' => 2,
      // Set the test IP address to a known Google US address.
      'ip_ban_test_ip' => '66.249.84.22',
      // Configure the paths for read-only and complete ban.
      // Todo: use $node = $this->drupalGetNodeByTitle($node_title) to get nids.
      'ip_ban_readonly_path' => 'node/1',
      'ip_ban_completeban_path' => 'node/2',
    );
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertOptionSelected('edit-ip-ban-us', 2, 'Complete ban set correctly in country list table.');

    // Attempt to access read only page after logging privileged user out.
    $this
      ->drupalGet('user/logout');
    $this
      ->drupalGet('node/1');

    // Should be redirected to complete ban page.
    $this
      ->assertUrl('node/2');
  }

  /**
   * Test the read-only and complete ban functionality.
   */
  public function testReadOnlyWithDisabledBlock() {

    // node/1.
    $this
      ->addBanNode("Read only page");
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array(
      // Set the United States to Read Only
      // 0 = no action / 1 = Read Only / 2 = Complete Ban.
      'ip_ban_US' => 1,
      // Set the test IP address to a known Google US address.
      'ip_ban_test_ip' => '66.249.84.22',
      // Configure the path for read-only.
      'ip_ban_readonly_path' => 'node/1',
      // Disable user login and powered by Drupal blocks for read-only users.
      // These blocks are set by default to the left sidebar and footer for
      // fresh installs, so no need to configure below.
      'ip_ban_disabled_blocks' => 'system,powered-by' . PHP_EOL . 'user,login',
    );
    $this
      ->drupalPost(IP_BAN_ADMIN_FORM_PATH, $edit, t('Save configuration'));
    $this
      ->assertOptionSelected('edit-ip-ban-us', 1, 'Read only set correctly in country list table.');

    // Set block title to confirm that the interface is available.
    // No need to do the following on D7 sites since the user login and powered
    // by Drupal are enabled by default.
    // $edit = array(
    // 'title' => $this->randomName(),
    // 'pages' => 'node/1',
    // );
    // $this->drupalPost('admin/structure/block/manage/search/form/configure', $edit, t('Save block'));
    // $this->assertText(t('The block configuration has been saved.'), 'Block configuration set.');
    // // Set the block to a region to confirm block is available.
    // $edit = array();
    // $edit['blocks[search_form][region]'] = 'footer';
    // $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));
    // $this->assertText(t('The block settings have been updated.'), 'Block successfully move to footer region.');
    // Attempt to access user page after logging privileged user out.
    $this
      ->drupalGet('node/1');
    $theme = \Drupal::theme()
      ->getActiveTheme()
      ->getName();
    $all_regions = system_region_list($theme);
    debug($all_regions);
    $blocks = block_list('footer');
    debug($blocks);
    $this
      ->drupalGet('user/logout');
    $this
      ->drupalGet('user/password');

    // Should be redirected to read only page.
    $this
      ->assertUrl('node/1');

    // Not sure how to actually test if a block is not visible as I would have
    // thought the following would work, but I only see an empty array (even
    // when run before the privileged user is logged out).
    // $blocks = block_list('footer');
    // debug($blocks);
    // Note: a simple visual inspection confirms both the user login and powered
    // by Drupal blocks are missing when viewing the test results.
  }

}

Constants

Namesort descending Description
IP_BAN_ADMIN_FORM_PATH @file Tests for ip_ban.module.

Classes

Namesort descending Description
IPBanFormTest Tests the IP Ban admin page form.
IPBanFunctionalTest Tests the complete ban and read only functionality of the IP Ban module.
IPBanTestBase Base class for common test configuration.