function SectionStorageManagerTest::testFindByContext

Same name in other branches
  1. 8.9.x 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 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())->setClass(SectionStorageInterface::class),
        'missing_contexts' => (new SectionStorageDefinition())->setClass(SectionStorageInterface::class),
        'provider_access' => (new SectionStorageDefinition())->setClass(SectionStorageInterface::class),
    ];
    $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.