class ClientIpRestoreTest in CloudFlare 8
Tests functionality of CloudFlareState object.
@group cloudflare
@covers \Drupal\cloudflare\EventSubscriber\ClientIpRestffore
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\cloudflare\Unit\ClientIpRestoreTest uses StringTranslationTrait
Expanded class hierarchy of ClientIpRestoreTest
File
- tests/
src/ Unit/ ClientIpRestoreTest.php, line 23
Namespace
Drupal\Tests\cloudflare\UnitView source
class ClientIpRestoreTest extends UnitTestCase {
use StringTranslationTrait;
protected $container;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->container = new ContainerBuilder();
$this->container
->set('string_translation', $this
->getStringTranslationStub());
$this->urlGenerator = $this
->createMock('Drupal\\Core\\Routing\\UrlGeneratorInterface');
$this->urlGenerator
->expects($this
->any())
->method('getPathFromRoute')
->with('cloudflare.admin_settings_form', [])
->willReturn('/admin/config/services/cloudflare');
$this->container
->set('url_generator', $this->urlGenerator);
\Drupal::setContainer($this->container);
}
/**
* Test ClientIpRestoreProvider functionality.
*
* @param bool $client_ip_restore_enabled
* Bool to indicate if client ip restore is enabled.
* @param string $host_header
* Host header to send as part of request.
* @param string $remote_header_ip
* The server server ip.
* @param string $cf_header
* CloudFlare header with the originating user's IP.
* @param string $bypass_host
* Host/Domain that is expected to bypass CloudFlare.
* @param string $expected_message
* The expected message to be logged.
* @param string $expected_client_ip
* The expected clientIp address after ClientIpRestore has been run.
*
* @dataProvider requestProvider
*/
public function testEnabledClientIpRestoreProvider($client_ip_restore_enabled, $host_header, $remote_header_ip, $cf_header, $bypass_host, $expected_message, $expected_client_ip) {
$logger = $this
->createMock(LoggerInterface::class);
if (empty($expected_message)) {
$logger
->expects($this
->never())
->method('warning')
->with($expected_message);
}
else {
$logger
->expects($this
->once())
->method('warning')
->with((string) $expected_message);
}
$config_factory = $this
->createMock('\\Drupal\\Core\\Config\\ConfigFactoryInterface');
$config = $this
->getMockBuilder('Drupal\\Core\\Config\\Config')
->disableOriginalConstructor()
->getMock();
// Create a map of arguments to return values.
$map = [
[
ClientIpRestore::CLOUDFLARE_BYPASS_HOST,
$bypass_host,
],
[
ClientIpRestore::CLOUDFLARE_CLIENT_IP_RESTORE_ENABLED,
$client_ip_restore_enabled,
],
];
$config
->expects($this
->atLeastOnce())
->method('get')
->will($this
->returnValueMap($map));
$config_factory
->expects($this
->once())
->method('get')
->will($this
->returnValue($config));
$cf_ips = explode("\n", "103.21.244.0/22\n 103.22.200.0/22\n 103.31.4.0/22\n 104.16.0.0/12\n 108.162.192.0/18\n 141.101.64.0/18\n 162.158.0.0/15\n 172.64.0.0/13\n 173.245.48.0/20\n 188.114.96.0/20\n 190.93.240.0/20\n 197.234.240.0/22\n 198.41.128.0/17\n 199.27.128.0/21");
$cf_ips = array_map('trim', $cf_ips);
$cache_backend = new MemoryBackend('foo');
$cache_backend
->set(ClientIpRestore::CLOUDFLARE_RANGE_KEY, $cf_ips);
$client_ip_restore = new ClientIpRestore($config_factory, $cache_backend, $this
->createMock(ClientInterface::class), $logger);
$request = Request::create('/test', 'get');
$kernel = $this
->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
if (!empty($cf_header)) {
$request->server
->set('HTTP_CF_CONNECTING_IP', $cf_header);
}
if (!empty($host_header)) {
$request->headers
->set('host', $host_header);
}
if (!empty($remote_header_ip)) {
$request->server
->set('REMOTE_ADDR', $remote_header_ip);
}
$request
->overrideGlobals();
$event = new GetResponseEvent($kernel, $request, 'foo', new NotFoundHttpException('foo'));
$client_ip_restore
->onRequest($event);
$this
->assertEquals($expected_client_ip, $request
->getClientIp());
}
/**
* Provider for testing ClientIpRestoreProvider.
*
* @return array
* Test Data to simulate incoming request and the expected results..
*/
public function requestProvider() {
// The setup container is not yet available.
$this->container = new ContainerBuilder();
$this->container
->set('string_translation', $this
->getStringTranslationStub());
\Drupal::setContainer($this->container);
$message0 = $this
->t('Request came through without being routed through CloudFlare.');
$message1 = $this
->t("Client IP of 192.168.2.203 does not match a known CloudFlare IP but there is HTTP_CF_CONNECTING_IP of 103.21.244.0.");
$message2 = $this
->t('Request has already been updated. This functionality should be deactivated. Please go <a href="@link_to_settings">here</a> to disable "Restore Client Ip Address".', [
'@link_to_settings' => "/admin/config/services/cloudflare",
]);
$test0 = [
FALSE,
'cftest.dev',
'192.168.2.201',
NULL,
'edit.cftest.dev',
'',
'192.168.2.201',
];
$test1 = [
TRUE,
'cftest.dev',
'192.168.2.202',
'',
'edit.cftest.dev',
$message0,
'192.168.2.202',
];
$test2 = [
TRUE,
'cftest.dev',
'192.168.2.203',
'103.21.244.0',
'edit.cftest.dev',
$message1,
'192.168.2.203',
];
$test3 = [
TRUE,
'cftest.dev',
'103.21.244.0',
'103.21.244.0',
'edit.cftest.dev',
$message2,
'103.21.244.0',
];
$test4 = [
TRUE,
'cftest.dev',
'103.21.244.0',
'5.5.5.5',
'edit.cftest.dev',
'',
'5.5.5.5',
];
$test5 = [
FALSE,
'edit.cftest.dev',
'192.168.2.201',
NULL,
'edit.cftest.dev',
'',
'192.168.2.201',
];
$test6 = [
TRUE,
'edit.cftest.dev',
'192.168.2.202',
'',
'edit.cftest.dev',
'',
'192.168.2.202',
];
$test7 = [
TRUE,
'edit.cftest.dev',
'192.168.2.203',
'103.21.244.0',
'edit.cftest.dev',
'',
'192.168.2.203',
];
$test8 = [
TRUE,
'edit.cftest.dev',
'103.21.244.0',
'103.21.244.0',
'edit.cftest.dev',
'',
'103.21.244.0',
];
$test9 = [
TRUE,
'edit.cftest.dev',
'103.21.244.0',
'5.5.5.5',
'edit.cftest.dev',
'',
'103.21.244.0',
];
$test10 = [
FALSE,
'',
'192.168.2.201',
NULL,
'',
'',
'192.168.2.201',
];
$test11 = [
TRUE,
'',
'192.168.2.202',
'',
'',
$message0,
'192.168.2.202',
];
$test12 = [
TRUE,
'',
'192.168.2.203',
'103.21.244.0',
'',
$message1,
'192.168.2.203',
];
$test13 = [
TRUE,
'',
'103.21.244.0',
'103.21.244.0',
'',
$message2,
'103.21.244.0',
];
$test14 = [
TRUE,
'',
'103.21.244.0',
'5.5.5.5',
'',
'',
'5.5.5.5',
];
return [
$test0,
$test1,
$test2,
$test3,
$test4,
$test5,
$test6,
$test7,
$test8,
$test9,
$test10,
$test11,
$test12,
$test13,
$test14,
];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ClientIpRestoreTest:: |
protected | property | ||
ClientIpRestoreTest:: |
public | function | Provider for testing ClientIpRestoreProvider. | |
ClientIpRestoreTest:: |
public | function |
Overrides UnitTestCase:: |
|
ClientIpRestoreTest:: |
public | function | Test ClientIpRestoreProvider functionality. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |