class SectionStorageManagerTest

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php \Drupal\Tests\layout_builder\Unit\SectionStorageManagerTest
  2. 8.9.x core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php \Drupal\Tests\layout_builder\Unit\SectionStorageManagerTest
  3. 10 core/modules/layout_builder/tests/src/Unit/SectionStorageManagerTest.php \Drupal\Tests\layout_builder\Unit\SectionStorageManagerTest

@coversDefaultClass \Drupal\layout_builder\SectionStorage\SectionStorageManager

@group layout_builder

Hierarchy

Expanded class hierarchy of SectionStorageManagerTest

File

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

Namespace

Drupal\Tests\layout_builder\Unit
View source
class SectionStorageManagerTest extends UnitTestCase {
    
    /**
     * The section storage manager.
     *
     * @var \Drupal\layout_builder\SectionStorage\SectionStorageManager
     */
    protected $manager;
    
    /**
     * The plugin.
     *
     * @var \Drupal\layout_builder\SectionStorageInterface
     */
    protected $plugin;
    
    /**
     * The plugin discovery.
     *
     * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface
     */
    protected $discovery;
    
    /**
     * The plugin factory.
     *
     * @var \Drupal\Component\Plugin\Factory\FactoryInterface
     */
    protected $factory;
    
    /**
     * The context handler.
     *
     * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
     */
    protected $contextHandler;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $cache = $this->prophesize(CacheBackendInterface::class);
        $module_handler = $this->prophesize(ModuleHandlerInterface::class);
        $this->contextHandler = $this->prophesize(ContextHandlerInterface::class);
        $this->manager = new SectionStorageManager(new \ArrayObject(), $cache->reveal(), $module_handler->reveal(), $this->contextHandler
            ->reveal());
        $this->discovery = $this->prophesize(DiscoveryInterface::class);
        $reflection_property = new \ReflectionProperty($this->manager, 'discovery');
        $reflection_property->setValue($this->manager, $this->discovery
            ->reveal());
        $this->plugin = $this->prophesize(SectionStorageInterface::class);
        $this->factory = $this->prophesize(FactoryInterface::class);
        $this->factory
            ->createInstance('the_plugin_id', [])
            ->willReturn($this->plugin
            ->reveal());
        $reflection_property = new \ReflectionProperty($this->manager, 'factory');
        $reflection_property->setValue($this->manager, $this->factory
            ->reveal());
    }
    
    /**
     * @covers ::loadEmpty
     */
    public function testLoadEmpty() : void {
        $result = $this->manager
            ->loadEmpty('the_plugin_id');
        $this->assertInstanceOf(SectionStorageInterface::class, $result);
        $this->assertSame($this->plugin
            ->reveal(), $result);
    }
    
    /**
     * @covers ::load
     */
    public function testLoad() : void {
        $contexts = [
            'the_context' => $this->prophesize(ContextInterface::class)
                ->reveal(),
        ];
        $this->contextHandler
            ->applyContextMapping($this->plugin, $contexts)
            ->shouldBeCalled();
        $result = $this->manager
            ->load('the_plugin_id', $contexts);
        $this->assertSame($this->plugin
            ->reveal(), $result);
    }
    
    /**
     * @covers ::load
     */
    public function testLoadNull() : void {
        $contexts = [
            'the_context' => $this->prophesize(ContextInterface::class)
                ->reveal(),
        ];
        $this->contextHandler
            ->applyContextMapping($this->plugin, $contexts)
            ->willThrow(new ContextException());
        $result = $this->manager
            ->load('the_plugin_id', $contexts);
        $this->assertNull($result);
    }
    
    /**
     * @covers ::findDefinitions
     */
    public function testFindDefinitions() : void {
        $this->discovery
            ->getDefinitions()
            ->willReturn([
            'plugin1' => (new SectionStorageDefinition())->setClass(SectionStorageInterface::class),
            'plugin2' => (new SectionStorageDefinition([
                'weight' => -5,
            ]))->setClass(SectionStorageInterface::class),
            'plugin3' => (new SectionStorageDefinition([
                'weight' => -5,
            ]))->setClass(SectionStorageInterface::class),
            'plugin4' => (new SectionStorageDefinition([
                'weight' => 10,
            ]))->setClass(SectionStorageInterface::class),
        ]);
        $expected = [
            'plugin2',
            'plugin3',
            'plugin1',
            'plugin4',
        ];
        $result = $this->manager
            ->getDefinitions();
        $this->assertSame($expected, array_keys($result));
    }
    
    /**
     * @covers ::findByContext
     *
     * @dataProvider providerTestFindByContext
     *
     * @param bool $plugin_is_applicable
     *   The result for the plugin's isApplicable() method to return.
     */
    public function testFindByContext($plugin_is_applicable) : void {
        $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);
        }
    }
    
    /**
     * Provides test data for ::testFindByContext().
     */
    public static function providerTestFindByContext() {
        // Data provider values are:
        // - the result for the plugin's isApplicable() method to return.
        $data = [];
        $data['plugin access: true'] = [
            TRUE,
        ];
        $data['plugin access: false'] = [
            FALSE,
        ];
        return $data;
    }
    
    /**
     * @covers ::findByContext
     */
    public function testFindByContextCacheableSectionStorage() : void {
        $cacheability = new CacheableMetadata();
        $contexts = [
            'foo' => new Context(new ContextDefinition('foo')),
        ];
        $definitions = [
            'first' => (new SectionStorageDefinition())->setClass(SectionStorageInterface::class),
            'second' => (new SectionStorageDefinition())->setClass(SectionStorageInterface::class),
        ];
        $this->discovery
            ->getDefinitions()
            ->willReturn($definitions);
        // Create a plugin that has cacheability info itself as a cacheable object
        // and from within ::isApplicable() but is not applicable.
        $first_plugin = $this->prophesize(SectionStorageInterface::class);
        $first_plugin->willImplement(CacheableDependencyInterface::class);
        $first_plugin->getCacheContexts()
            ->shouldNotBeCalled();
        $first_plugin->getCacheTags()
            ->shouldNotBeCalled();
        $first_plugin->getCacheMaxAge()
            ->shouldNotBeCalled();
        $first_plugin->isApplicable($cacheability)
            ->will(function ($arguments) {
            $arguments[0]->addCacheTags([
                'first_plugin',
            ]);
            return FALSE;
        });
        // Create a plugin that adds cacheability info from within ::isApplicable()
        // and is applicable.
        $second_plugin = $this->prophesize(SectionStorageInterface::class);
        $second_plugin->isApplicable($cacheability)
            ->will(function ($arguments) {
            $arguments[0]->addCacheTags([
                'second_plugin',
            ]);
            return TRUE;
        });
        $this->factory
            ->createInstance('first', [])
            ->willReturn($first_plugin->reveal());
        $this->factory
            ->createInstance('second', [])
            ->willReturn($second_plugin->reveal());
        // Do not do any filtering based on context.
        $this->contextHandler
            ->filterPluginDefinitionsByContexts($contexts, $definitions)
            ->willReturnArgument(1);
        $this->contextHandler
            ->applyContextMapping($first_plugin, $contexts)
            ->shouldBeCalled();
        $this->contextHandler
            ->applyContextMapping($second_plugin, $contexts)
            ->shouldBeCalled();
        $result = $this->manager
            ->findByContext($contexts, $cacheability);
        $this->assertSame($second_plugin->reveal(), $result);
        $this->assertSame([
            'first_plugin',
            'second_plugin',
        ], $cacheability->getCacheTags());
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
SectionStorageManagerTest::$contextHandler protected property The context handler.
SectionStorageManagerTest::$discovery protected property The plugin discovery.
SectionStorageManagerTest::$factory protected property The plugin factory.
SectionStorageManagerTest::$manager protected property The section storage manager.
SectionStorageManagerTest::$plugin protected property The plugin.
SectionStorageManagerTest::providerTestFindByContext public static function Provides test data for ::testFindByContext().
SectionStorageManagerTest::setUp protected function Overrides UnitTestCase::setUp
SectionStorageManagerTest::testFindByContext public function @covers ::findByContext
SectionStorageManagerTest::testFindByContextCacheableSectionStorage public function @covers ::findByContext
SectionStorageManagerTest::testFindDefinitions public function @covers ::findDefinitions
SectionStorageManagerTest::testLoad public function @covers ::load
SectionStorageManagerTest::testLoadEmpty public function @covers ::loadEmpty
SectionStorageManagerTest::testLoadNull public function @covers ::load
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function

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