View source  
  <?php
define('IP_BAN_ADMIN_FORM_PATH', 'admin/config/ip_ban');
class IPBanTestBase extends DrupalWebTestCase {
  protected $adminSitesUser;
  protected $ipBanType;
  
  public function setUp() {
    
    parent::setUp(array(
      'ip_ban',
      'ip2country',
    ));
    
    $this->ipBanType = $this
      ->drupalCreateContentType(array(
      'name' => 'IP Ban Node',
      'type' => 'ipban_node',
    ));
    
    $this->adminSitesUser = $this
      ->drupalCreateUser(array(
      'administer site configuration',
      'create ipban_node content',
      'edit any ipban_node content',
      'access content',
      'bypass node access',
      'administer blocks',
      
      'ignore ip_ban',
    ));
  }
  
  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');
  }
}
class IPBanFormTest extends IPBanTestBase {
  
  public function setUp() {
    
    parent::setUp(array());
    $this
      ->drupalLogin($this->adminSitesUser);
  }
  
  public static function getInfo() {
    return array(
      'name' => 'IP Ban admin form test',
      'description' => 'Basic testing for the admin form settings.',
      'group' => 'IP Ban',
    );
  }
  
  public function testBanPaths() {
    
    $this
      ->addBanNode("Test page");
    
    $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'));
    $readOnlyPath = variable_get('ip_ban_readonly_path');
    $this
      ->assertIdentical($readOnlyPath, 'node/1');
    
    $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.'));
    
    $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.'));
  }
  
  public function testSetCountry() {
    
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array();
    
    $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.');
  }
  
  public function testIpAddressEntry() {
    
    $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.'));
    
    $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.'));
    
    $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.'));
  }
  
  public function testDisabledBlockEntry() {
    
    $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.'));
    
    $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.'));
    
    $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.'));
  }
}
class IPBanFunctionalTest extends IPBanTestBase {
  
  public function setUp() {
    
    parent::setUp(array(
      'block',
    ));
    $this
      ->drupalLogin($this->adminSitesUser);
    module_disable(array(
      'dashboard',
    ), TRUE);
  }
  
  public static function getInfo() {
    return array(
      'name' => 'IP Ban functionality tests',
      'description' => 'Testing for banned and read-only users',
      'group' => 'IP Ban',
    );
  }
  
  public function testCompleteBan() {
    
    $this
      ->addBanNode("Read only page");
    
    $this
      ->addBanNode("Complete ban page");
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array(
      
      'ip_ban_US' => 2,
      
      'ip_ban_test_ip' => '66.249.84.22',
      
      '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.');
    
    $this
      ->drupalGet('user/logout');
    $this
      ->drupalGet('node/1');
    
    $this
      ->assertUrl('node/2');
  }
  
  public function testReadOnlyWithDisabledBlock() {
    
    $this
      ->addBanNode("Read only page");
    $this
      ->drupalGet(IP_BAN_ADMIN_FORM_PATH);
    $edit = array(
      
      'ip_ban_US' => 1,
      
      'ip_ban_test_ip' => '66.249.84.22',
      
      'ip_ban_readonly_path' => 'node/1',
      
      '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.');
    
    $this
      ->drupalGet('node/1');
    global $theme;
    $all_regions = system_region_list($theme);
    debug($all_regions);
    $blocks = block_list('footer');
    debug($blocks);
    $this
      ->drupalGet('user/logout');
    $this
      ->drupalGet('user/password');
    
    $this
      ->assertUrl('node/1');
    
  }
}