5 * Contains \Drupal\Tests\Core\Authentication\AuthenticationManagerTest.
8 namespace Drupal\Tests\Core\Authentication;
10 use Drupal\Core\Authentication\AuthenticationCollector;
11 use Drupal\Core\Authentication\AuthenticationManager;
12 use Drupal\Core\Authentication\AuthenticationProviderFilterInterface;
13 use Drupal\Core\Authentication\AuthenticationProviderInterface;
14 use Drupal\Tests\UnitTestCase;
15 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\Routing\Route;
20 * @coversDefaultClass \Drupal\Core\Authentication\AuthenticationManager
21 * @group Authentication
23 class AuthenticationManagerTest extends UnitTestCase {
26 * @covers ::defaultFilter
27 * @covers ::applyFilter
29 * @dataProvider providerTestDefaultFilter
31 public function testDefaultFilter($applies, $has_route, $auth_option, $provider_id, $global) {
32 $auth_provider = $this->getMock('Drupal\Core\Authentication\AuthenticationProviderInterface');
33 $auth_collector = new AuthenticationCollector();
34 $auth_collector->addProvider($auth_provider, $provider_id, 0, $global);
35 $authentication_manager = new AuthenticationManager($auth_collector);
37 $request = new Request();
39 $route = new Route('/example');
41 $route->setOption('_auth', $auth_option);
43 $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, $route);
46 $this->assertSame($applies, $authentication_manager->appliesToRoutedRequest($request, FALSE));
50 * @covers ::applyFilter
52 public function testApplyFilterWithFilterprovider() {
53 $auth_provider = $this->getMock('Drupal\Tests\Core\Authentication\TestAuthenticationProviderInterface');
54 $auth_provider->expects($this->once())
55 ->method('appliesToRoutedRequest')
58 $authentication_collector = new AuthenticationCollector();
59 $authentication_collector->addProvider($auth_provider, 'filtered', 0);
61 $authentication_manager = new AuthenticationManager($authentication_collector);
63 $request = new Request();
64 $this->assertTrue($authentication_manager->appliesToRoutedRequest($request, FALSE));
68 * Provides data to self::testDefaultFilter().
70 public function providerTestDefaultFilter() {
72 // No route, cookie is global, should apply.
73 $data[] = [TRUE, FALSE, [], 'cookie', TRUE];
74 // No route, cookie is not global, should not apply.
75 $data[] = [FALSE, FALSE, [], 'cookie', FALSE];
76 // Route, no _auth, cookie is global, should apply.
77 $data[] = [TRUE, TRUE, [], 'cookie', TRUE];
78 // Route, no _auth, cookie is not global, should not apply.
79 $data[] = [FALSE, TRUE, [], 'cookie', FALSE];
80 // Route, with _auth and non-matching provider, should not apply.
81 $data[] = [FALSE, TRUE, ['basic_auth'], 'cookie', TRUE];
82 // Route, with _auth and matching provider should not apply.
83 $data[] = [TRUE, TRUE, ['basic_auth'], 'basic_auth', TRUE];
90 * Helper interface to mock two interfaces at once.
92 interface TestAuthenticationProviderInterface extends AuthenticationProviderFilterInterface, AuthenticationProviderInterface {}