You are here

class CdnGeoLocatorTest in GeoIP API 8.2

Tests the CDN locator.

@coversDefaultClass \Drupal\geoip\Plugin\GeoLocator\Cdn

@group GeoIP

Hierarchy

Expanded class hierarchy of CdnGeoLocatorTest

File

tests/src/Unit/CdnGeoLocatorTest.php, line 18

Namespace

Drupal\Tests\geoip\Unit
View source
class CdnGeoLocatorTest extends UnitTestCase {

  /**
   * Test the geolocate method for Cdn plugin.
   *
   * @covers ::geolocate
   * @backupGlobals disabled
   */
  public function testGeolocate() {
    $config = $this
      ->prophesize(ImmutableConfig::class);
    $config
      ->get('debug')
      ->willReturn(FALSE);
    $config_factory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $config_factory
      ->get('geoip.geolocation')
      ->willReturn($config
      ->reveal());
    $logger = $this
      ->prophesize(LoggerInterface::class);
    $locator = new Cdn([], 'cdn', [
      'label' => 'CDN',
      'description' => 'Checks for geolocation headers sent by CDN services',
      'weight' => 10,
    ], $config_factory
      ->reveal(), $logger
      ->reveal());
    $this
      ->assertEquals(NULL, $locator
      ->geolocate('127.0.0.1'));
    $_SERVER['HTTP_CF_IPCOUNTRY'] = 'US';
    $this
      ->assertEquals('US', $locator
      ->geolocate('127.0.0.1'));
    $_SERVER['HTTP_CLOUDFRONT_VIEWER_COUNTRY'] = 'CA';

    // We can't equal CA since we manually check Cloudflare first.
    $this
      ->assertNotEquals('CA', $locator
      ->geolocate('127.0.0.1'));
    unset($_SERVER['HTTP_CF_IPCOUNTRY']);
    $this
      ->assertEquals('CA', $locator
      ->geolocate('127.0.0.1'));
    unset($_SERVER['HTTP_CLOUDFRONT_VIEWER_COUNTRY']);
    $_SERVER['HTTP_MY_CUSTOM_HEADER'] = 'FR';

    // @todo this needs to be updated when custom header implemented.
    $this
      ->assertEquals(NULL, $locator
      ->geolocate('127.0.0.1'));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CdnGeoLocatorTest::testGeolocate public function Test the geolocate method for Cdn plugin.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340