View source
<?php
namespace Drupal\Tests\ip2country\Functional;
use Drupal\Component\Utility\Environment;
use Drupal\Tests\BrowserTestBase;
class Ip2CountryTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
protected static $modules = [
'dblog',
'help',
'block',
];
protected $adminUser;
protected $unprivUser;
protected function setUp() {
parent::setUp();
$this->timeLimit = 3 * 60;
Environment::setTimeLimit($this->timeLimit);
\Drupal::state()
->set('ip2country_populate_database_on_install', FALSE);
$status = \Drupal::service('module_installer')
->install([
'ip2country',
], FALSE);
$this
->resetAll();
$this
->assertTrue($status, 'Module ip2country enabled.');
$count = \Drupal::service('ip2country.manager')
->getRowCount();
$this
->assertEquals(0, $count, 'Database is empty.');
$this
->drupalPlaceBlock('help_block', [
'region' => 'help',
]);
$this
->drupalPlaceBlock('page_title_block');
$this->adminUser = $this
->drupalCreateUser([
'administer site configuration',
'access administration pages',
'access site reports',
'administer ip2country',
]);
$this->unprivUser = $this
->drupalCreateUser();
}
public function testIpLookup() {
\Drupal::service('ip2country.manager')
->updateDatabase('arin');
$count = \Drupal::service('ip2country.manager')
->getRowCount();
$this
->assertNotEquals(0, $count, 'Database has been updated with ' . $count . ' rows.');
$ip_array = [
'125.29.33.201' => 'JP',
'212.58.224.138' => 'GB',
'184.51.240.110' => 'US',
'210.87.9.66' => 'AU',
'93.184.216.119' => 'EU',
];
foreach ($ip_array as $ip_address => $country) {
$lookup = \Drupal::service('ip2country.lookup')
->getCountry($ip_address);
$this
->assertEquals($country, $lookup, $ip_address . ' found, resolved to ' . $lookup . '.');
$usl_lookup = \Drupal::service('ip2country.lookup')
->getCountry(ip2long($ip_address));
$this
->assertEquals($lookup, $usl_lookup, 'Unsigned long lookup found same country code.');
$this
->assertTrue(TRUE, 'Valid IP found in database.');
}
$ip_array = [
'127.0.0.1',
'358.1.1.0',
];
foreach ($ip_array as $ip_address) {
$country = \Drupal::service('ip2country.lookup')
->getCountry($ip_address);
$this
->assertFalse($country, $ip_address . ' not found in database.');
$this
->assertTrue(TRUE, 'Invalid IP not found in database.');
}
\Drupal::service('ip2country.manager')
->emptyDatabase();
$count = \Drupal::service('ip2country.manager')
->getRowCount();
$this
->assertEquals(0, $count, 'Database is empty.');
}
public function testAlterHook() {
$this
->assertTrue(TRUE, 'testAlterHook passed.');
}
public function testDefaultCountry() {
$this
->assertTrue(TRUE, 'testDefaultCountry passed.');
}
public function testUserAccess() {
$assert = $this
->assertSession();
$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);
$this
->drupalGet('admin/config/people/ip2country/update/arin');
$assert
->statusCodeEquals(403);
$this
->drupalLogin($this->unprivUser);
$this
->drupalGet('admin/config');
$assert
->statusCodeEquals(403);
$this
->drupalGet('admin/config/people/ip2country');
$assert
->statusCodeEquals(403);
$this
->drupalLogout();
$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');
$assert
->pageTextContains('Configuration settings for the ip2country module.');
$assert
->pageTextContains('Database is empty.');
$assert
->fieldValueEquals('ip2country_watchdog', 1);
$assert
->fieldValueEquals('ip2country_rir', 'arin');
$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) . '.');
$this
->drupalGet('admin/reports/dblog');
$assert
->pageTextContains('Recent log messages');
$assert
->pageTextContains('ip2country');
$assert
->linkExists('Manual database update from ' . mb_strtoupper($rir) . ' server.');
$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();
}
public function testUserObject() {
$this
->assertTrue(TRUE, 'testUserObject passed.');
}
public function testUi() {
$this
->assertTrue(TRUE, 'testUi passed.');
}
public function testIpSpoofing() {
$this
->assertTrue(TRUE, 'testIpSpoofing passed.');
}
public function testCountrySpoofing() {
$this
->assertTrue(TRUE, 'testCountrySpoofing passed.');
}
public function testIpManualLookup() {
$this
->assertTrue(TRUE, 'testIpManualLookup passed.');
}
public function testDbDownload() {
\Drupal::service('ip2country.manager')
->emptyDatabase();
$count = \Drupal::service('ip2country.manager')
->getRowCount();
$this
->assertEquals(0, $count, 'Database is empty.');
$rir = array_rand([
'arin' => 'ARIN',
'apnic' => 'APNIC',
'lacnic' => 'LACNIC',
'ripe' => 'RIPE',
]);
\Drupal::service('ip2country.manager')
->updateDatabase($rir);
$count = \Drupal::service('ip2country.manager')
->getRowCount();
$this
->assertNotEquals(0, $count, 'Database has been updated from ' . mb_strtoupper($rir) . ' with ' . $count . ' rows.');
\Drupal::service('ip2country.manager')
->emptyDatabase();
$count = \Drupal::service('ip2country.manager')
->getRowCount();
$this
->assertEquals(0, $count, 'Database is empty.');
}
public function testDbManualUpdate() {
$connection = \Drupal::database();
$rows = $connection
->select('ip2country')
->countQuery()
->execute()
->fetchField();
$this
->assertTrue(TRUE, 'testDbManualUpdate passed.');
}
public function testDbCronUpdate() {
$this
->assertTrue(TRUE, 'testDbCronUpdate passed.');
}
public function testDbUpdateLogging() {
$edit = [
'ip2country_watchdog' => [
'test' => TRUE,
],
];
$this
->assertTrue(TRUE, 'testDbUpdateLogging passed.');
}
protected function tearDown() {
\Drupal::state()
->delete('ip2country_populate_database_on_install');
parent::tearDown();
}
}