You are here

public function SectionStorageManagerTest::testFindByContext in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php \Drupal\Tests\layout_builder\Unit\SectionStorageManagerTest::testFindByContext()

@covers ::findByContext

@dataProvider providerTestFindByContext

Parameters

bool $plugin_is_applicable: The result for the plugin's isApplicable() method to return.

File

core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php, line 153

Class

SectionStorageManagerTest
@coversDefaultClass \Drupal\layout_builder\SectionStorage\SectionStorageManager

Namespace

Drupal\Tests\layout_builder\Unit

Code

public function testFindByContext($plugin_is_applicable) {
  $cacheability = new CacheableMetadata();
  $contexts = [
    'foo' => new Context(new ContextDefinition('foo')),
  ];
  $definitions = [
    'no_access' => new SectionStorageDefinition(),
    'missing_contexts' => new SectionStorageDefinition(),
    'provider_access' => new SectionStorageDefinition(),
  ];
  $this->discovery
    ->getDefinitions()
    ->willReturn($definitions);
  $provider_access = $this
    ->prophesize(SectionStorageInterface::class);
  $provider_access
    ->isApplicable($cacheability)
    ->willReturn($plugin_is_applicable);
  $no_access = $this
    ->prophesize(SectionStorageInterface::class);
  $no_access
    ->isApplicable($cacheability)
    ->willReturn(FALSE);
  $missing_contexts = $this
    ->prophesize(SectionStorageInterface::class);

  // Do not do any filtering based on context.
  $this->contextHandler
    ->filterPluginDefinitionsByContexts($contexts, $definitions)
    ->willReturnArgument(1);
  $this->contextHandler
    ->applyContextMapping($no_access, $contexts)
    ->shouldBeCalled();
  $this->contextHandler
    ->applyContextMapping($provider_access, $contexts)
    ->shouldBeCalled();
  $this->contextHandler
    ->applyContextMapping($missing_contexts, $contexts)
    ->willThrow(new ContextException());
  $this->factory
    ->createInstance('no_access', [])
    ->willReturn($no_access
    ->reveal());
  $this->factory
    ->createInstance('missing_contexts', [])
    ->willReturn($missing_contexts
    ->reveal());
  $this->factory
    ->createInstance('provider_access', [])
    ->willReturn($provider_access
    ->reveal());
  $result = $this->manager
    ->findByContext($contexts, $cacheability);
  if ($plugin_is_applicable) {
    $this
      ->assertSame($provider_access
      ->reveal(), $result);
  }
  else {
    $this
      ->assertNull($result);
  }
}