You are here

protected function AnonymousLoginSubscriberTest::callOnKernelRequestCheckRedirect in Anonymous login 8.2

Same name and namespace in other branches
  1. 8 tests/src/Unit/AnonymousLoginSubscriberTest.php \Drupal\Tests\anonymous_login\Unit\AnonymousLoginSubscriberTest::callOnKernelRequestCheckRedirect()

Instantiates the subscriber and runs redirect().

Parameters

string $request_uri: The URI of the request.

Return value

\Symfony\Component\HttpKernel\Event\GetResponseEvent THe response event.

1 call to AnonymousLoginSubscriberTest::callOnKernelRequestCheckRedirect()
AnonymousLoginSubscriberTest::testRedirectLogic in tests/src/Unit/AnonymousLoginSubscriberTest.php
@covers ::redirect @dataProvider getRedirectData

File

tests/src/Unit/AnonymousLoginSubscriberTest.php, line 72

Class

AnonymousLoginSubscriberTest
Tests the redirect logic.

Namespace

Drupal\Tests\anonymous_login\Unit

Code

protected function callOnKernelRequestCheckRedirect($request_uri) {
  $event = $this
    ->getGetResponseEventStub($request_uri);
  $request = $event
    ->getRequest();
  $state = $this
    ->getMockBuilder('Drupal\\Core\\State\\StateInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $state
    ->expects($this
    ->any())
    ->method('get')
    ->with('system.maintenance_mode')
    ->will($this
    ->returnValue(FALSE));
  $current_user = $this
    ->getMockBuilder('Drupal\\Core\\Session\\AccountProxyInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $current_user
    ->expects($this
    ->any())
    ->method('isAnonymous')
    ->will($this
    ->returnValue(TRUE));
  $alias_manager = $this
    ->getMockBuilder('Drupal\\path_alias\\AliasManagerInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $alias_manager
    ->expects($this
    ->any())
    ->method('getPathByAlias')
    ->with($this
    ->anything())
    ->will($this
    ->returnCallback(function ($alias) {
    switch ($alias) {
      case '/node-1-alias':
        $path = '/node/1';
        break;
      case '/node-2-alias':
        $path = '/node/2';
        break;
      default:
        $path = $alias;
    }
    return $path;
  }));
  $alias_manager
    ->expects($this
    ->any())
    ->method('getAliasByPath')
    ->with($this
    ->anything())
    ->will($this
    ->returnCallback(function ($path) {
    switch ($path) {
      case '/node/1':
        $alias = '/node-1-alias';
        break;
      case '/node/2':
        $alias = '/node-2-alias';
        break;
      default:
        $alias = $path;
    }
    return $alias;
  }));
  $paths = [
    'include' => [
      '*',
    ],
    'exclude' => [
      'node/2',
      'user/reset/*',
      'cron/*',
      'sites/default/files/*',
    ],
  ];
  $path_matcher = $this
    ->getMockBuilder('Drupal\\Core\\Path\\PathMatcherInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $path_matcher
    ->expects($this
    ->any())
    ->method('matchPath')
    ->with($this
    ->anything(), $this
    ->anything())
    ->will($this
    ->returnCallback(function ($path, $patterns) {
    $to_replace = [
      '/(\\r\\n?|\\n)/',
      '/\\\\\\*/',
      '/(^|\\|)\\\\<front\\\\>($|\\|)/',
    ];
    $replacements = [
      '|',
      '.*',
      '\\1' . preg_quote('<front>', '/') . '\\2',
    ];
    $patterns_quoted = preg_quote($patterns, '/');
    $search = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
    return (bool) preg_match($search, $path);
  }));
  $module_handler = $this
    ->getMockBuilder('Drupal\\Core\\Extension\\ModuleHandlerInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $module_handler
    ->expects($this
    ->any())
    ->method('alter')
    ->will($this
    ->returnValue($paths));
  $path_validator = $this
    ->getMockBuilder('Drupal\\Core\\Path\\PathValidatorInterface')
    ->disableOriginalConstructor()
    ->getMock();
  $path_validator
    ->expects($this
    ->any())
    ->method('getUrlIfValidWithoutAccessCheck')
    ->will($this
    ->returnValue(FALSE));
  $current_path = $this
    ->getMockBuilder('Drupal\\Core\\Path\\CurrentPathStack')
    ->disableOriginalConstructor()
    ->getMock();
  $current_path
    ->expects($this
    ->any())
    ->method('getPath')
    ->with($request)
    ->will($this
    ->returnValue($request
    ->getPathInfo()));
  $subscriber = new AnonymousLoginSubscriber($this
    ->getConfigFactoryStub([
    'anonymous_login.settings' => [
      'paths' => '*' . PHP_EOL . '~/node/2',
      'login_path' => '/user/login',
    ],
    'system.site' => [
      'page.front' => '<front>',
    ],
  ]), $state, $current_user, $path_matcher, $alias_manager, $module_handler, $path_validator, $current_path, FALSE);

  // Run the main redirect method.
  $subscriber
    ->redirect($event);
  return $event;
}