You are here

public function Ip2CountryTest::testUserAccess in IP-based Determination of a Visitor's Country 8

Tests module permissions / access to configuration page.

File

tests/src/Functional/Ip2CountryTest.php, line 150

Class

Ip2CountryTest
Tests operations of the IP to Country module.

Namespace

Drupal\Tests\ip2country\Functional

Code

public function testUserAccess() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();

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

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

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

  //
  // As admin user.
  //
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('admin/config');
  $assert
    ->statusCodeEquals(200);
  $assert
    ->pageTextContains('User location');
  $assert
    ->pageTextContains('Settings for determining user location from IP address.');
  $this
    ->drupalGet('admin/config/people/ip2country');
  $assert
    ->statusCodeEquals(200);
  $assert
    ->pageTextContains('User location');

  // This is output by ip2country_help().
  $assert
    ->pageTextContains('Configuration settings for the ip2country module.');
  $assert
    ->pageTextContains('Database is empty.');

  // Check that database updates are being logged to watchdog.
  $assert
    ->fieldValueEquals('ip2country_watchdog', 1);

  // Check that database updates are using arin.
  $assert
    ->fieldValueEquals('ip2country_rir', 'arin');

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

  // Check watchdog.
  $this
    ->drupalGet('admin/reports/dblog');
  $assert
    ->pageTextContains('Recent log messages');
  $assert
    ->pageTextContains('ip2country');
  $assert
    ->linkExists('Manual database update from ' . mb_strtoupper($rir) . ' server.');

  // Drill down.
  $this
    ->clickLink('Manual database update from ' . mb_strtoupper($rir) . ' server.');
  $assert
    ->pageTextContains('Details');
  $assert
    ->pageTextContains('ip2country');
  $assert
    ->pageTextContains('Manual database update from ' . mb_strtoupper($rir) . ' server.');
  $this
    ->drupalLogout();
}