Version 1
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Authentication / AuthenticationCollectorTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\Core\Authentication\AuthenticationCollectorTest.
6  */
7
8 namespace Drupal\Tests\Core\Authentication;
9
10 use Drupal\Core\Authentication\AuthenticationCollector;
11 use Drupal\Core\Authentication\AuthenticationProviderInterface;
12 use Drupal\Tests\UnitTestCase;
13 use Symfony\Component\HttpFoundation\Request;
14
15 /**
16  * @coversDefaultClass \Drupal\Core\Authentication\AuthenticationCollector
17  * @group Authentication
18  */
19 class AuthenticationCollectorTest extends UnitTestCase {
20
21   /**
22    * Tests adding, getting, and order of priorities.
23    *
24    * @covers ::addProvider
25    * @covers ::getSortedProviders
26    * @covers ::getProvider
27    * @covers ::isGlobal
28    */
29   public function testAuthenticationCollector() {
30     $providers = [];
31     $global = [];
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]);
40     }
41     // Sort the $providers array by priority (highest number is lowest priority)
42     // and compare with AuthenticationCollector::getSortedProviders().
43     krsort($providers);
44
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);
49     }
50     $this->assertEquals($sorted_providers, $authentication_collector->getSortedProviders());
51
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));
57     }
58   }
59
60 }
61
62 /**
63  * A simple provider for unit testing AuthenticationCollector.
64  */
65 class TestAuthenticationProvider implements AuthenticationProviderInterface {
66
67   /**
68    * The provider id.
69    *
70    * @var string
71    */
72   public $providerId;
73
74   /**
75    * Constructor.
76    */
77   public function __construct($provider_id) {
78     $this->providerId = $provider_id;
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function applies(Request $request) {
85     return TRUE;
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function authenticate(Request $request) {
92     return NULL;
93   }
94
95 }