function SectionStorageManagerTest::testFindByContext

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php \Drupal\Tests\layout_builder\Unit\SectionStorageManagerTest::testFindByContext()
  2. 10 core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php \Drupal\Tests\layout_builder\Unit\SectionStorageManagerTest::testFindByContext()
  3. 11.x 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 211

Class

SectionStorageManagerTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21layout_builder%21src%21SectionStorage%21SectionStorageManager.php/class/SectionStorageManager/8.9.x" title="Provides the Section Storage type plugin manager." class="local">\Drupal\layout_builder\SectionStorage\SectionStorageManager</a>

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.