Repository navigation
fix: copy labels to runtime contexts #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| namespace Drupal\Tests\proxy_block\Unit; | ||
|
|
||
| use Drupal\Component\Plugin\Context\ContextDefinitionInterface as ComponentContextDefinitionInterface; | ||
| use Drupal\Core\Plugin\Context\ContextDefinitionInterface; | ||
| use Drupal\Core\Plugin\Context\ContextHandlerInterface; | ||
| use Drupal\Core\Plugin\Context\ContextInterface; | ||
|
|
@@ -52,28 +53,66 @@ protected function setUp(): void { | |
| $this->testContextManager->setStringTranslation($this->stringTranslation); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an array of available context mocks keyed by context ID. | ||
| * | ||
| * @param array $labels | ||
| * Associative array of context_id => label. | ||
| * | ||
| * @return \Drupal\Core\Plugin\Context\ContextInterface[] | ||
| * Mock context objects with labeled definitions. | ||
| */ | ||
| private function createAvailableContextMocks(array $labels): array { | ||
| $contexts = []; | ||
| foreach ($labels as $id => $label) { | ||
| $definition = $this->createMock(ComponentContextDefinitionInterface::class); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this service interacts with Drupal Core's context system, it is better to use $definition = $this->createMock(ContextDefinitionInterface::class); |
||
| $definition->method('getLabel')->willReturn($label); | ||
| $context = $this->createMock(ContextInterface::class); | ||
| $context->method('getContextDefinition')->willReturn($definition); | ||
| $contexts[$id] = $context; | ||
| } | ||
| return $contexts; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a runtime context mock with a labeled definition. | ||
| * | ||
| * @param string|null $label | ||
| * The label for the context definition, or NULL for no label. | ||
| * | ||
| * @return \Drupal\Core\Plugin\Context\ContextInterface|\PHPUnit\Framework\MockObject\MockObject | ||
| * A mock context. | ||
| */ | ||
| private function createRuntimeContextMock(?string $label = 'Test'): ContextInterface|MockObject { | ||
| $definition = $this->createMock(ComponentContextDefinitionInterface::class); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| $definition->method('getLabel')->willReturn($label); | ||
| $context = $this->createMock(ContextInterface::class); | ||
| $context->method('getContextDefinition')->willReturn($definition); | ||
| return $context; | ||
| } | ||
|
|
||
| /** | ||
| * Tests getGatheredContexts with available contexts. | ||
| * | ||
| * @covers ::getGatheredContexts | ||
| */ | ||
| public function testGetGatheredContextsWithAvailableContexts(): void { | ||
| $available_context_ids = [ | ||
| $available_contexts = $this->createAvailableContextMocks([ | ||
| 'node' => 'Current node', | ||
| 'user' => 'Current user', | ||
| 'view_mode' => 'View mode', | ||
| ]; | ||
| ]); | ||
|
|
||
| $runtime_contexts = [ | ||
| 'node' => $this->createMock(ContextInterface::class), | ||
| 'user' => $this->createMock(ContextInterface::class), | ||
| 'view_mode' => $this->createMock(ContextInterface::class), | ||
| 'node' => $this->createRuntimeContextMock('Node'), | ||
| 'user' => $this->createRuntimeContextMock('User'), | ||
| 'view_mode' => $this->createRuntimeContextMock('View mode'), | ||
| ]; | ||
|
|
||
| $this->contextRepository | ||
| ->expects($this->once()) | ||
| ->method('getAvailableContexts') | ||
| ->willReturn($available_context_ids); | ||
| ->willReturn($available_contexts); | ||
|
|
||
| $this->contextRepository | ||
| ->expects($this->once()) | ||
|
|
@@ -96,14 +135,14 @@ public function testGetGatheredContextsWithAvailableContexts(): void { | |
| * @covers ::createDefaultViewModeContext | ||
| */ | ||
| public function testGetGatheredContextsCreatesDefaultViewModeContext(): void { | ||
| $available_context_ids = [ | ||
| $available_contexts = $this->createAvailableContextMocks([ | ||
| 'node' => 'Current node', | ||
| 'user' => 'Current user', | ||
| ]; | ||
| ]); | ||
|
|
||
| $runtime_contexts = [ | ||
| 'node' => $this->createMock(ContextInterface::class), | ||
| 'user' => $this->createMock(ContextInterface::class), | ||
| 'node' => $this->createRuntimeContextMock('Node'), | ||
| 'user' => $this->createRuntimeContextMock('User'), | ||
| ]; | ||
|
|
||
| $mock_view_mode_context = $this->createMock(ContextInterface::class); | ||
|
|
@@ -112,7 +151,7 @@ public function testGetGatheredContextsCreatesDefaultViewModeContext(): void { | |
| $this->contextRepository | ||
| ->expects($this->once()) | ||
| ->method('getAvailableContexts') | ||
| ->willReturn($available_context_ids); | ||
| ->willReturn($available_contexts); | ||
|
|
||
| $this->contextRepository | ||
| ->expects($this->once()) | ||
|
|
@@ -138,21 +177,21 @@ public function testGetGatheredContextsCreatesDefaultViewModeContext(): void { | |
| * @covers ::getGatheredContexts | ||
| */ | ||
| public function testGetGatheredContextsWithExistingViewModeContext(): void { | ||
| $available_context_ids = [ | ||
| $available_contexts = $this->createAvailableContextMocks([ | ||
| 'node' => 'Current node', | ||
| 'view_mode' => 'View mode', | ||
| ]; | ||
| ]); | ||
|
|
||
| $existing_view_mode_context = $this->createMock(ContextInterface::class); | ||
| $existing_view_mode_context = $this->createRuntimeContextMock('View mode'); | ||
| $runtime_contexts = [ | ||
| 'node' => $this->createMock(ContextInterface::class), | ||
| 'node' => $this->createRuntimeContextMock('Node'), | ||
| 'view_mode' => $existing_view_mode_context, | ||
| ]; | ||
|
|
||
| $this->contextRepository | ||
| ->expects($this->once()) | ||
| ->method('getAvailableContexts') | ||
| ->willReturn($available_context_ids); | ||
| ->willReturn($available_contexts); | ||
|
|
||
| $this->contextRepository | ||
| ->expects($this->once()) | ||
|
|
@@ -204,6 +243,70 @@ public function testGetGatheredContextsWithEmptyContexts(): void { | |
| $this->assertSame($mock_view_mode_context, $view_mode_context); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that runtime contexts without labels get labels from available ones. | ||
| * | ||
| * @covers ::getGatheredContexts | ||
| */ | ||
| public function testGetGatheredContextsCopiesLabelsFromAvailableContexts(): void { | ||
| $available_contexts = $this->createAvailableContextMocks([ | ||
| '@node.node_route_context:node' => 'Node from URL', | ||
| ]); | ||
|
|
||
| // Runtime context has a definition that starts with a NULL label. | ||
| $runtime_definition = $this->createMock(ComponentContextDefinitionInterface::class); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| $runtime_definition->method('getLabel')->willReturn(NULL); | ||
| $runtime_definition->expects($this->once()) | ||
| ->method('setLabel') | ||
| ->with('Node from URL'); | ||
| $runtime_context = $this->createMock(ContextInterface::class); | ||
| $runtime_context->method('getContextDefinition')->willReturn($runtime_definition); | ||
|
|
||
| $mock_view_mode_context = $this->createMock(ContextInterface::class); | ||
| $this->testContextManager->setMockViewModeContext($mock_view_mode_context); | ||
|
|
||
| $this->contextRepository | ||
| ->method('getAvailableContexts') | ||
| ->willReturn($available_contexts); | ||
| $this->contextRepository | ||
| ->method('getRuntimeContexts') | ||
| ->willReturn(['@node.node_route_context:node' => $runtime_context]); | ||
|
|
||
| $result = $this->testContextManager->getGatheredContexts(); | ||
|
|
||
| $this->assertArrayHasKey('@node.node_route_context:node', $result); | ||
| } | ||
|
|
||
| /** | ||
| * Tests that runtime contexts with existing labels are not overwritten. | ||
| * | ||
| * @covers ::getGatheredContexts | ||
| */ | ||
| public function testGetGatheredContextsPreservesExistingLabels(): void { | ||
| $available_contexts = $this->createAvailableContextMocks([ | ||
| 'node' => 'Node from URL', | ||
| ]); | ||
|
|
||
| // Runtime context already has a label. | ||
| $runtime_definition = $this->createMock(ComponentContextDefinitionInterface::class); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| $runtime_definition->method('getLabel')->willReturn('Existing label'); | ||
| $runtime_definition->expects($this->never())->method('setLabel'); | ||
| $runtime_context = $this->createMock(ContextInterface::class); | ||
| $runtime_context->method('getContextDefinition')->willReturn($runtime_definition); | ||
|
|
||
| $mock_view_mode_context = $this->createMock(ContextInterface::class); | ||
| $this->testContextManager->setMockViewModeContext($mock_view_mode_context); | ||
|
|
||
| $this->contextRepository | ||
| ->method('getAvailableContexts') | ||
| ->willReturn($available_contexts); | ||
| $this->contextRepository | ||
| ->method('getRuntimeContexts') | ||
| ->willReturn(['node' => $runtime_context]); | ||
|
|
||
| $this->testContextManager->getGatheredContexts(); | ||
| } | ||
|
|
||
| /** | ||
| * Tests applyContextsToTargetBlock with block having no context requirements. | ||
| * | ||
|
|
@@ -217,16 +320,16 @@ public function testApplyContextsToTargetBlockNoContextRequirements(): void { | |
| ->willReturn([]); | ||
|
|
||
| $gathered_contexts = [ | ||
| 'node' => $this->createMock(ContextInterface::class), | ||
| 'user' => $this->createMock(ContextInterface::class), | ||
| 'node' => $this->createRuntimeContextMock('Node'), | ||
| 'user' => $this->createRuntimeContextMock('User'), | ||
| ]; | ||
|
|
||
| $mock_view_mode_context = $this->createMock(ContextInterface::class); | ||
| $this->testContextManager->setMockViewModeContext($mock_view_mode_context); | ||
|
|
||
| $this->contextRepository | ||
| ->method('getAvailableContexts') | ||
| ->willReturn(['node' => 'Node', 'user' => 'User']); | ||
| ->willReturn($this->createAvailableContextMocks(['node' => 'Node', 'user' => 'User'])); | ||
| $this->contextRepository | ||
| ->method('getRuntimeContexts') | ||
| ->willReturn($gathered_contexts); | ||
|
|
@@ -256,16 +359,16 @@ public function testApplyContextsToTargetBlockWithSatisfiedMapping(): void { | |
| ->willReturn(['node' => 'current_node']); | ||
|
|
||
| $gathered_contexts = [ | ||
| 'current_node' => $this->createMock(ContextInterface::class), | ||
| 'user' => $this->createMock(ContextInterface::class), | ||
| 'current_node' => $this->createRuntimeContextMock('Node'), | ||
| 'user' => $this->createRuntimeContextMock('User'), | ||
| ]; | ||
|
|
||
| $mock_view_mode_context = $this->createMock(ContextInterface::class); | ||
| $this->testContextManager->setMockViewModeContext($mock_view_mode_context); | ||
|
|
||
| $this->contextRepository | ||
| ->method('getAvailableContexts') | ||
| ->willReturn(['current_node' => 'Node', 'user' => 'User']); | ||
| ->willReturn($this->createAvailableContextMocks(['current_node' => 'Node', 'user' => 'User'])); | ||
| $this->contextRepository | ||
| ->method('getRuntimeContexts') | ||
| ->willReturn($gathered_contexts); | ||
|
|
@@ -304,16 +407,16 @@ public function testApplyContextsToTargetBlockWithAutomaticMapping(): void { | |
| ->willReturn([]); | ||
|
|
||
| $gathered_contexts = [ | ||
| 'current_node' => $this->createMock(ContextInterface::class), | ||
| 'user' => $this->createMock(ContextInterface::class), | ||
| 'current_node' => $this->createRuntimeContextMock('Node'), | ||
| 'user' => $this->createRuntimeContextMock('User'), | ||
| ]; | ||
|
|
||
| $mock_view_mode_context = $this->createMock(ContextInterface::class); | ||
| $this->testContextManager->setMockViewModeContext($mock_view_mode_context); | ||
|
|
||
| $this->contextRepository | ||
| ->method('getAvailableContexts') | ||
| ->willReturn(['current_node' => 'Node', 'user' => 'User']); | ||
| ->willReturn($this->createAvailableContextMocks(['current_node' => 'Node', 'user' => 'User'])); | ||
| $this->contextRepository | ||
| ->method('getRuntimeContexts') | ||
| ->willReturn($gathered_contexts); | ||
|
|
@@ -326,7 +429,7 @@ public function testApplyContextsToTargetBlockWithAutomaticMapping(): void { | |
| ->expects($this->once()) | ||
| ->method('getMatchingContexts') | ||
| ->with($expected_contexts, $context_definition) | ||
| ->willReturn(['current_node' => $this->createMock(ContextInterface::class)]); | ||
| ->willReturn(['current_node' => $this->createRuntimeContextMock('Node')]); | ||
|
|
||
| $target_block | ||
| ->expects($this->once()) | ||
|
|
@@ -362,16 +465,16 @@ public function testApplyContextsToTargetBlockWithAtPrefixResolution(): void { | |
| ->willReturn(['node' => '@current_node']); | ||
|
|
||
| $gathered_contexts = [ | ||
| 'current_node' => $this->createMock(ContextInterface::class), | ||
| 'user' => $this->createMock(ContextInterface::class), | ||
| 'current_node' => $this->createRuntimeContextMock('Node'), | ||
| 'user' => $this->createRuntimeContextMock('User'), | ||
| ]; | ||
|
|
||
| $mock_view_mode_context = $this->createMock(ContextInterface::class); | ||
| $this->testContextManager->setMockViewModeContext($mock_view_mode_context); | ||
|
|
||
| $this->contextRepository | ||
| ->method('getAvailableContexts') | ||
| ->willReturn(['current_node' => 'Node', 'user' => 'User']); | ||
| ->willReturn($this->createAvailableContextMocks(['current_node' => 'Node', 'user' => 'User'])); | ||
| $this->contextRepository | ||
| ->method('getRuntimeContexts') | ||
| ->willReturn($gathered_contexts); | ||
|
|
@@ -409,15 +512,15 @@ public function testApplyContextsToTargetBlockWithFallbackResolution(): void { | |
| ->willReturn(['node' => '@missing_context']); | ||
|
|
||
| $gathered_contexts = [ | ||
| '@missing_context' => $this->createMock(ContextInterface::class), | ||
| '@missing_context' => $this->createRuntimeContextMock('Missing Context'), | ||
| ]; | ||
|
|
||
| $mock_view_mode_context = $this->createMock(ContextInterface::class); | ||
| $this->testContextManager->setMockViewModeContext($mock_view_mode_context); | ||
|
|
||
| $this->contextRepository | ||
| ->method('getAvailableContexts') | ||
| ->willReturn(['@missing_context' => 'Missing Context']); | ||
| ->willReturn($this->createAvailableContextMocks(['@missing_context' => 'Missing Context'])); | ||
| $this->contextRepository | ||
| ->method('getRuntimeContexts') | ||
| ->willReturn($gathered_contexts); | ||
|
|
@@ -478,15 +581,15 @@ public function testApplyContextsToTargetBlockExceptionDuringApplication(): void | |
| ->willReturn(['node' => 'current_node']); | ||
|
|
||
| $gathered_contexts = [ | ||
| 'current_node' => $this->createMock(ContextInterface::class), | ||
| 'current_node' => $this->createRuntimeContextMock('Node'), | ||
| ]; | ||
|
|
||
| $mock_view_mode_context = $this->createMock(ContextInterface::class); | ||
| $this->testContextManager->setMockViewModeContext($mock_view_mode_context); | ||
|
|
||
| $this->contextRepository | ||
| ->method('getAvailableContexts') | ||
| ->willReturn(['current_node' => 'Node']); | ||
| ->willReturn($this->createAvailableContextMocks(['current_node' => 'Node'])); | ||
| $this->contextRepository | ||
| ->method('getRuntimeContexts') | ||
| ->willReturn($gathered_contexts); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loop performs multiple calls to
getContextDefinition()on the same object. Assigning the definition to a variable improves readability and slightly improves performance by avoiding redundant method calls.