Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Authentication / AuthenticationManagerTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Authentication\AuthenticationManagerTest.
6  */
7
8 namespace Drupal\Tests\Core\Authentication;
9
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;
18
19 /**
20  * @coversDefaultClass \Drupal\Core\Authentication\AuthenticationManager
21  * @group Authentication
22  */
23 class AuthenticationManagerTest extends UnitTestCase {
24
25   /**
26    * @covers ::defaultFilter
27    * @covers ::applyFilter
28    *
29    * @dataProvider providerTestDefaultFilter
30    */
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);
36
37     $request = new Request();
38     if ($has_route) {
39       $route = new Route('/example');
40       if ($auth_option) {
41         $route->setOption('_auth', $auth_option);
42       }
43       $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, $route);
44     }
45
46     $this->assertSame($applies, $authentication_manager->appliesToRoutedRequest($request, FALSE));
47   }
48
49   /**
50    * @covers ::applyFilter
51    */
52   public function testApplyFilterWithFilterprovider() {
53     $auth_provider = $this->getMock('Drupal\Tests\Core\Authentication\TestAuthenticationProviderInterface');
54     $auth_provider->expects($this->once())
55       ->method('appliesToRoutedRequest')
56       ->willReturn(TRUE);
57
58     $authentication_collector = new AuthenticationCollector();
59     $authentication_collector->addProvider($auth_provider, 'filtered', 0);
60
61     $authentication_manager = new AuthenticationManager($authentication_collector);
62
63     $request = new Request();
64     $this->assertTrue($authentication_manager->appliesToRoutedRequest($request, FALSE));
65   }
66
67   /**
68    * Provides data to self::testDefaultFilter().
69    */
70   public function providerTestDefaultFilter() {
71     $data = [];
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];
84     return $data;
85   }
86
87 }
88
89 /**
90  * Helper interface to mock two interfaces at once.
91  */
92 interface TestAuthenticationProviderInterface extends AuthenticationProviderFilterInterface, AuthenticationProviderInterface {}