View source
<?php
namespace Drupal\Tests\ip2country\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
use Drupal\Tests\rest\Functional\ResourceTestBase;
class Ip2CountryResourceTest extends ResourceTestBase {
use CookieResourceTestTrait;
protected $defaultTheme = 'stark';
protected static $format = 'hal_json';
protected static $mimeType = 'application/hal+json';
protected static $auth = 'cookie';
protected static $resourceConfigId = 'ip_lookup';
public static $modules = [
'hal',
'ip2country',
];
public function setUp() {
parent::setUp();
$auth = isset(static::$auth) ? [
static::$auth,
] : [];
$this
->provisionResource([
static::$format,
], $auth);
}
public function testIpLookup() {
$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',
];
$this
->initAuthentication();
$url = Url::fromRoute('rest.ip_lookup.GET', [
'ip_address' => '127.0.0.1',
'_format' => static::$format,
]);
$request_options = $this
->getAuthenticationRequestOptions('GET');
$response = $this
->request('GET', $url, $request_options);
$this
->assertResourceErrorResponse(403, "The 'restful get ip_lookup' permission is required.", $response, [
'4xx-response',
'http_response',
], [
'user.permissions',
], FALSE, FALSE);
$this
->setUpAuthorization('GET');
if (version_compare(\Drupal::VERSION, '9.0', '>=')) {
$expected_cache_tags = [
'config:rest.resource.ip_lookup',
'http_response',
];
}
else {
$expected_cache_tags = [
'config:rest.resource.ip_lookup',
'config:rest.settings',
'http_response',
];
}
foreach ($ip_array as $ip => $country) {
$url
->setRouteParameter('ip_address', $ip);
$response = $this
->request('GET', $url, $request_options);
$this
->assertResourceResponse(200, FALSE, $response, $expected_cache_tags, [
'user.permissions',
], FALSE, 'MISS');
$lookup_result = Json::decode((string) $response
->getBody());
$this
->assertEquals($country, $lookup_result, 'Country code is correct.');
}
$url
->setRouteParameter('ip_address', '203.0.113.0');
$response = $this
->request('GET', $url, $request_options);
$this
->assertResourceErrorResponse(404, 'IP Address 203.0.113.0 is not assigned to a country.', $response);
$url
->setRouteParameter('ip_address', 0);
$response = $this
->request('GET', $url, $request_options);
$this
->assertResourceErrorResponse(400, 'The IP address you entered is invalid. Please enter an address in the form xxx.xxx.xxx.xxx where xxx is between 0 and 255 inclusive.', $response);
}
protected function setUpAuthorization($method) {
switch ($method) {
case 'GET':
$this
->grantPermissionsToTestedRole([
'restful get ip_lookup',
]);
break;
default:
throw new \UnexpectedValueException();
}
}
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
}
protected function getExpectedUnauthorizedAccessCacheability() {
}
protected function getExpectedUnauthorizedAccessMessage($method) {
}
}