Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/Service/TargetBlockContextManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,22 @@ public function __construct(
* Array of available contexts.
*/
public function getGatheredContexts(): array {
$available_context_ids = $this->contextRepository->getAvailableContexts();
$contexts = $this->contextRepository->getRuntimeContexts(array_keys($available_context_ids));
$available_contexts = $this->contextRepository->getAvailableContexts();
$contexts = $this->contextRepository->getRuntimeContexts(array_keys($available_contexts));

// Runtime context definitions may lack labels (e.g. NodeRouteContext).
// Without a label the select option value is NULL, and Drupal's
// FormValidator uses isset() which returns FALSE for NULL, rejecting
// the submitted value. Copy labels from the available context
// definitions which always carry a human-readable label.
foreach ($contexts as $context_id => $context) {
if (empty($context->getContextDefinition()->getLabel()) && isset($available_contexts[$context_id])) {
$available_label = $available_contexts[$context_id]->getContextDefinition()->getLabel();
if (!empty($available_label)) {
$context->getContextDefinition()->setLabel($available_label);
}
}
}
Comment on lines +49 to +56

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

    foreach ($contexts as $context_id => $context) {
      $definition = $context->getContextDefinition();
      if (empty($definition->getLabel()) && isset($available_contexts[$context_id])) {
        $available_label = $available_contexts[$context_id]->getContextDefinition()->getLabel();
        if (!empty($available_label)) {
          $definition->setLabel($available_label);
        }
      }
    }


$populated_contexts = $contexts;

Expand Down
169 changes: 136 additions & 33 deletions tests/src/Unit/TargetBlockContextManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since this service interacts with Drupal Core's context system, it is better to use ContextDefinitionInterface for mocking. This ensures that the mock objects are consistent with the types expected by the Core services and provides access to methods like setLabel() if needed.

      $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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The runtime context definition mock should use ContextDefinitionInterface because the code under test calls setLabel(), which is defined in the Core interface but not in the Component interface.

    $definition = $this->createMock(ContextDefinitionInterface::class);

$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())
Expand All @@ -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);
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This mock specifically expects a call to setLabel(). You should use ContextDefinitionInterface::class here as ComponentContextDefinitionInterface does not define this method.

    $runtime_definition = $this->createMock(ContextDefinitionInterface::class);

$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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with other context definition mocks in this test class, use ContextDefinitionInterface::class instead of the component-level interface.

    $runtime_definition = $this->createMock(ContextDefinitionInterface::class);

$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.
*
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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())
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading