3 namespace Drupal\Tests\layout_builder\Unit;
5 use Drupal\Core\TempStore\SharedTempStore;
6 use Drupal\Core\TempStore\SharedTempStoreFactory;
7 use Drupal\layout_builder\LayoutTempstoreRepository;
8 use Drupal\layout_builder\SectionStorageInterface;
9 use Drupal\Tests\UnitTestCase;
12 * @coversDefaultClass \Drupal\layout_builder\LayoutTempstoreRepository
13 * @group layout_builder
15 class LayoutTempstoreRepositoryTest extends UnitTestCase {
21 public function testGetEmptyTempstore() {
22 $section_storage = $this->prophesize(SectionStorageInterface::class);
23 $section_storage->getStorageType()->willReturn('my_storage_type');
24 $section_storage->getStorageId()->willReturn('my_storage_id');
26 $tempstore = $this->prophesize(SharedTempStore::class);
27 $tempstore->get('my_storage_id')->shouldBeCalled();
29 $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
30 $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());
32 $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
34 $this->assertFalse($repository->has($section_storage->reveal()));
36 $result = $repository->get($section_storage->reveal());
37 $this->assertSame($section_storage->reveal(), $result);
44 public function testGetLoadedTempstore() {
45 $section_storage = $this->prophesize(SectionStorageInterface::class);
46 $section_storage->getStorageType()->willReturn('my_storage_type');
47 $section_storage->getStorageId()->willReturn('my_storage_id');
49 $tempstore_section_storage = $this->prophesize(SectionStorageInterface::class);
50 $tempstore = $this->prophesize(SharedTempStore::class);
51 $tempstore->get('my_storage_id')->willReturn(['section_storage' => $tempstore_section_storage->reveal()]);
52 $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
53 $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());
55 $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
57 $this->assertTrue($repository->has($section_storage->reveal()));
59 $result = $repository->get($section_storage->reveal());
60 $this->assertSame($tempstore_section_storage->reveal(), $result);
61 $this->assertNotSame($section_storage->reveal(), $result);
67 public function testGetInvalidEntry() {
68 $section_storage = $this->prophesize(SectionStorageInterface::class);
69 $section_storage->getStorageType()->willReturn('my_storage_type');
70 $section_storage->getStorageId()->willReturn('my_storage_id');
72 $tempstore = $this->prophesize(SharedTempStore::class);
73 $tempstore->get('my_storage_id')->willReturn(['section_storage' => 'this_is_not_an_entity']);
75 $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
76 $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());
78 $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
80 $this->setExpectedException(\UnexpectedValueException::class, 'The entry with storage type "my_storage_type" and ID "my_storage_id" is invalid');
81 $repository->get($section_storage->reveal());