5 * Contains \Drupal\Tests\Core\Authentication\AuthenticationCollectorTest.
8 namespace Drupal\Tests\Core\Authentication;
10 use Drupal\Core\Authentication\AuthenticationCollector;
11 use Drupal\Core\Authentication\AuthenticationProviderInterface;
12 use Drupal\Tests\UnitTestCase;
13 use Symfony\Component\HttpFoundation\Request;
16 * @coversDefaultClass \Drupal\Core\Authentication\AuthenticationCollector
17 * @group Authentication
19 class AuthenticationCollectorTest extends UnitTestCase {
22 * Tests adding, getting, and order of priorities.
24 * @covers ::addProvider
25 * @covers ::getSortedProviders
26 * @covers ::getProvider
29 public function testAuthenticationCollector() {
32 $authentication_collector = new AuthenticationCollector();
33 $priorities = [2, 0, -8, 10, 1, 3, -5, 0, 6, -10, -4];
34 foreach ($priorities as $priority) {
35 $provider_id = $this->randomMachineName();
36 $provider = new TestAuthenticationProvider($provider_id);
37 $providers[$priority][$provider_id] = $provider;
38 $global[$provider_id] = rand(0, 1) > 0.5;
39 $authentication_collector->addProvider($provider, $provider_id, $priority, $global[$provider_id]);
41 // Sort the $providers array by priority (highest number is lowest priority)
42 // and compare with AuthenticationCollector::getSortedProviders().
45 // Merge nested providers from $providers into $sorted_providers.
46 $sorted_providers = [];
47 foreach ($providers as $providers_priority) {
48 $sorted_providers = array_merge($sorted_providers, $providers_priority);
50 $this->assertEquals($sorted_providers, $authentication_collector->getSortedProviders());
52 // Test AuthenticationCollector::getProvider() and
53 // AuthenticationCollector::isGlobal().
54 foreach ($sorted_providers as $provider) {
55 $this->assertEquals($provider, $authentication_collector->getProvider($provider->providerId));
56 $this->assertEquals($global[$provider->providerId], $authentication_collector->isGlobal($provider->providerId));
63 * A simple provider for unit testing AuthenticationCollector.
65 class TestAuthenticationProvider implements AuthenticationProviderInterface {
77 public function __construct($provider_id) {
78 $this->providerId = $provider_id;
84 public function applies(Request $request) {
91 public function authenticate(Request $request) {