More updates to stop using dev or alpha or beta versions.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Plugin / Context / LazyContextRepositoryTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Plugin\Context;
4
5 use Drupal\Core\Plugin\Context\Context;
6 use Drupal\Core\Plugin\Context\ContextDefinition;
7 use Drupal\Core\Plugin\Context\LazyContextRepository;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Component\DependencyInjection\ContainerBuilder;
10
11 /**
12  * @coversDefaultClass \Drupal\Core\Plugin\Context\LazyContextRepository
13  * @group context
14  */
15 class LazyContextRepositoryTest extends UnitTestCase {
16
17   /**
18    * The container.
19    *
20    * @var \Symfony\Component\DependencyInjection\ContainerBuilder
21    */
22   protected $container;
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     $this->container = new ContainerBuilder();
31   }
32
33   /**
34    * @covers ::getRuntimeContexts
35    */
36   public function testGetRuntimeContextsSingle() {
37     $contexts = $this->setupContextAndProvider('test_provider', ['test_context']);
38
39     $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
40     $run_time_contexts = $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context']);
41     $this->assertEquals(['@test_provider:test_context' => $contexts[0]], $run_time_contexts);
42   }
43
44   /**
45    * @covers ::getRuntimeContexts
46    */
47   public function testGetRuntimeMultipleContextsPerService() {
48     $contexts = $this->setupContextAndProvider('test_provider', ['test_context0', 'test_context1']);
49
50     $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
51     $run_time_contexts = $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context0', '@test_provider:test_context1']);
52     $this->assertEquals(['@test_provider:test_context0' => $contexts[0], '@test_provider:test_context1' => $contexts[1]], $run_time_contexts);
53   }
54
55   /**
56    * @covers ::getRuntimeContexts
57    */
58   public function testGetRuntimeMultipleContextProviders() {
59     $contexts0 = $this->setupContextAndProvider('test_provider', ['test_context0', 'test_context1'], ['test_context0']);
60     $contexts1 = $this->setupContextAndProvider('test_provider2', ['test1_context0', 'test1_context1'], ['test1_context0']);
61
62     $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
63     $run_time_contexts = $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context0', '@test_provider2:test1_context0']);
64     $this->assertEquals(['@test_provider:test_context0' => $contexts0[0], '@test_provider2:test1_context0' => $contexts1[1]], $run_time_contexts);
65   }
66
67   /**
68    * @covers ::getRuntimeContexts
69    */
70   public function testInvalidContextId() {
71     $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
72     $this->setExpectedException(\InvalidArgumentException::class, 'You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
73     $lazy_context_repository->getRuntimeContexts(['test_context', '@test_provider:test_context1']);
74   }
75
76   /**
77    * @covers ::getRuntimeContexts
78    */
79   public function testGetRuntimeStaticCache() {
80     $context0 = new Context(new ContextDefinition('example'));
81     $context1 = new Context(new ContextDefinition('example'));
82
83     $context_provider = $this->prophesize('\Drupal\Core\Plugin\Context\ContextProviderInterface');
84     $context_provider->getRuntimeContexts(['test_context0', 'test_context1'])
85       ->shouldBeCalledTimes(1)
86       ->willReturn(['test_context0' => $context0, 'test_context1' => $context1]);
87     $context_provider = $context_provider->reveal();
88     $this->container->set('test_provider', $context_provider);
89
90     $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
91     $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context0', '@test_provider:test_context1']);
92     $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context0', '@test_provider:test_context1']);
93   }
94
95   /**
96    * @covers ::getAvailableContexts
97    */
98   public function testGetAvailableContexts() {
99     $contexts0 = $this->setupContextAndProvider('test_provider0', ['test0_context0', 'test0_context1']);
100     $contexts1 = $this->setupContextAndProvider('test_provider1', ['test1_context0', 'test1_context1']);
101
102     $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider0', 'test_provider1']);
103     $contexts = $lazy_context_repository->getAvailableContexts();
104
105     $this->assertEquals([
106       '@test_provider0:test0_context0' => $contexts0[0],
107       '@test_provider0:test0_context1' => $contexts0[1],
108       '@test_provider1:test1_context0' => $contexts1[0],
109       '@test_provider1:test1_context1' => $contexts1[1],
110     ], $contexts);
111
112   }
113
114   /**
115    * Sets up contexts and context providers.
116    *
117    * @param string $service_id
118    *   The service ID of the service provider.
119    * @param string[] $unqualified_context_ids
120    *   An array of context slot names.
121    * @param string[] $expected_unqualified_context_ids
122    *   The expected unqualified context IDs passed to getRuntimeContexts.
123    *
124    * @return array
125    *   An array of set up contexts.
126    */
127   protected function setupContextAndProvider($service_id, array $unqualified_context_ids, array $expected_unqualified_context_ids = []) {
128     $contexts = [];
129     for ($i = 0; $i < count($unqualified_context_ids); $i++) {
130       $contexts[] = new Context(new ContextDefinition('example'));
131     }
132
133     $expected_unqualified_context_ids = $expected_unqualified_context_ids ?: $unqualified_context_ids;
134
135     $context_provider = $this->prophesize('\Drupal\Core\Plugin\Context\ContextProviderInterface');
136     $context_provider->getRuntimeContexts($expected_unqualified_context_ids)
137       ->willReturn(array_combine($unqualified_context_ids, $contexts));
138     $context_provider->getAvailableContexts()
139       ->willReturn(array_combine($unqualified_context_ids, $contexts));
140     $context_provider = $context_provider->reveal();
141     $this->container->set($service_id, $context_provider);
142
143     return $contexts;
144   }
145
146 }