You are here

public function ip2countryTestCase::testUserAccess in IP-based Determination of a Visitor's Country 7

Same name and namespace in other branches
  1. 6 ip2country.test \ip2countryTestCase::testUserAccess()

Tests module permissions / access to configuration page.

File

tests/ip2country.test, line 158
Tests suite for the ip2country module.

Class

ip2countryTestCase
Tests operations of the IP to Country module.

Code

public function testUserAccess() {

  //
  // Test as anonymous user.
  //
  $this
    ->drupalGet('admin/config');
  $this
    ->assertResponse(403);
  $this
    ->assertText(t('Access denied'));
  $this
    ->assertText(t('You are not authorized to access this page.'));
  $this
    ->drupalGet('admin/config/people/ip2country');
  $this
    ->assertResponse(403);

  // Try to trigger DB update as anonymous.
  $this
    ->drupalGet('admin/config/people/ip2country/update/arin');
  $this
    ->assertResponse(403);

  //
  // Test as authenticated but unprivileged user.
  //
  $this
    ->drupalLogin($this->unprivUser);
  $this
    ->drupalGet('admin/config');
  $this
    ->assertResponse(403);
  $this
    ->drupalGet('admin/config/people/ip2country');
  $this
    ->assertResponse(403);
  $this
    ->drupalLogout();

  //
  // As admin user.
  //
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('admin/config');
  $this
    ->assertResponse(200);
  $this
    ->assertText(t('User location'));
  $this
    ->assertText(t('Settings for determining user location from IP address.'));
  $this
    ->drupalGet('admin/config/people/ip2country');
  $this
    ->assertResponse(200);
  $this
    ->assertText(t('User location'));
  $this
    ->assertText(t('Configuration settings for the ip2country module.'));
  $this
    ->assertText(t('Database is empty.'));
  $this
    ->assertFieldByName('ip2country_watchdog', 1, t('Database updates are being logged to watchdog.'));
  $this
    ->assertFieldByName('ip2country_rir', 'arin', t('Database updates from arin.'));

  // Update database via UI - choose a random RIR.
  // (Actually short-circuiting the UI here because of the Ajax call).
  $rir = array_rand(array(
    'afrinic' => 'AFRINIC',
    'arin' => 'ARIN',
    'apnic' => 'APNIC',
    'lacnic' => 'LACNIC',
    'ripe' => 'RIPE',
  ));
  $this
    ->drupalGet('admin/config/people/ip2country/update/' . $rir);
  $this
    ->assertText(t('The IP to Country database has been updated from @rir.', array(
    '@rir' => strtoupper($rir),
  )));

  // Check watchdog.
  $this
    ->drupalGet('admin/reports/dblog');
  $this
    ->assertText(t('Recent log messages'));
  $this
    ->assertText('ip2country');
  $this
    ->assertLink(t('Manual database update from @rir server.', array(
    '@rir' => strtoupper($rir),
  )));

  // Drill down.
  $this
    ->clickLink(t('Manual database update from @rir server.', array(
    '@rir' => strtoupper($rir),
  )));
  $this
    ->assertText(t('Details'));
  $this
    ->assertText('ip2country');
  $this
    ->assertText(t('Manual database update from @rir server.', array(
    '@rir' => strtoupper($rir),
  )));
  $this
    ->drupalLogout();
}