Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / tests / src / Unit / SectionStorageManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Unit;
4
5 use Drupal\Component\Plugin\Factory\FactoryInterface;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\layout_builder\SectionListInterface;
9 use Drupal\layout_builder\SectionStorage\SectionStorageManager;
10 use Drupal\layout_builder\SectionStorageInterface;
11 use Drupal\Tests\UnitTestCase;
12
13 /**
14  * @coversDefaultClass \Drupal\layout_builder\SectionStorage\SectionStorageManager
15  *
16  * @group layout_builder
17  */
18 class SectionStorageManagerTest extends UnitTestCase {
19
20   /**
21    * The section storage manager.
22    *
23    * @var \Drupal\layout_builder\SectionStorage\SectionStorageManager
24    */
25   protected $manager;
26
27   /**
28    * The plugin.
29    *
30    * @var \Drupal\layout_builder\SectionStorageInterface
31    */
32   protected $plugin;
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39
40     $cache = $this->prophesize(CacheBackendInterface::class);
41     $module_handler = $this->prophesize(ModuleHandlerInterface::class);
42     $this->manager = new SectionStorageManager(new \ArrayObject(), $cache->reveal(), $module_handler->reveal());
43
44     $this->plugin = $this->prophesize(SectionStorageInterface::class);
45
46     $factory = $this->prophesize(FactoryInterface::class);
47     $factory->createInstance('the_plugin_id', [])->willReturn($this->plugin->reveal());
48     $reflection_property = new \ReflectionProperty($this->manager, 'factory');
49     $reflection_property->setAccessible(TRUE);
50     $reflection_property->setValue($this->manager, $factory->reveal());
51   }
52
53   /**
54    * @covers ::loadEmpty
55    */
56   public function testLoadEmpty() {
57     $result = $this->manager->loadEmpty('the_plugin_id');
58     $this->assertInstanceOf(SectionStorageInterface::class, $result);
59   }
60
61   /**
62    * @covers ::loadFromStorageId
63    */
64   public function testLoadFromStorageId() {
65     $section_list = $this->prophesize(SectionListInterface::class);
66     $this->plugin->setSectionList($section_list->reveal())->will(function () {
67       return $this;
68     });
69     $this->plugin->getSectionListFromId('the_storage_id')->willReturn($section_list->reveal());
70
71     $result = $this->manager->loadFromStorageId('the_plugin_id', 'the_storage_id');
72     $this->assertInstanceOf(SectionStorageInterface::class, $result);
73   }
74
75   /**
76    * @covers ::loadFromRoute
77    */
78   public function testLoadFromRoute() {
79     $section_list = $this->prophesize(SectionListInterface::class);
80     $this->plugin->extractIdFromRoute('the_value', [], 'the_parameter_name', [])->willReturn('the_storage_id');
81     $this->plugin->getSectionListFromId('the_storage_id')->willReturn($section_list->reveal());
82     $this->plugin->setSectionList($section_list->reveal())->will(function () {
83       return $this;
84     });
85
86     $result = $this->manager->loadFromRoute('the_plugin_id', 'the_value', [], 'the_parameter_name', []);
87     $this->assertInstanceOf(SectionStorageInterface::class, $result);
88   }
89
90   /**
91    * @covers ::loadFromRoute
92    */
93   public function testLoadFromRouteNull() {
94     $this->plugin->extractIdFromRoute('the_value', [], 'the_parameter_name', ['_route' => 'the_route_name'])->willReturn(NULL);
95
96     $result = $this->manager->loadFromRoute('the_plugin_id', 'the_value', [], 'the_parameter_name', ['_route' => 'the_route_name']);
97     $this->assertNull($result);
98   }
99
100 }