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 {
20 public function testGetEmptyTempstore() {
21 $section_storage = $this->prophesize(SectionStorageInterface::class);
22 $section_storage->getStorageType()->willReturn('my_storage_type');
23 $section_storage->getStorageId()->willReturn('my_storage_id');
25 $tempstore = $this->prophesize(SharedTempStore::class);
26 $tempstore->get('my_storage_id')->shouldBeCalled();
28 $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
29 $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());
31 $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
33 $result = $repository->get($section_storage->reveal());
34 $this->assertSame($section_storage->reveal(), $result);
40 public function testGetLoadedTempstore() {
41 $section_storage = $this->prophesize(SectionStorageInterface::class);
42 $section_storage->getStorageType()->willReturn('my_storage_type');
43 $section_storage->getStorageId()->willReturn('my_storage_id');
45 $tempstore_section_storage = $this->prophesize(SectionStorageInterface::class);
46 $tempstore = $this->prophesize(SharedTempStore::class);
47 $tempstore->get('my_storage_id')->willReturn(['section_storage' => $tempstore_section_storage->reveal()]);
48 $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
49 $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());
51 $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
53 $result = $repository->get($section_storage->reveal());
54 $this->assertSame($tempstore_section_storage->reveal(), $result);
55 $this->assertNotSame($section_storage->reveal(), $result);
61 public function testGetInvalidEntry() {
62 $section_storage = $this->prophesize(SectionStorageInterface::class);
63 $section_storage->getStorageType()->willReturn('my_storage_type');
64 $section_storage->getStorageId()->willReturn('my_storage_id');
66 $tempstore = $this->prophesize(SharedTempStore::class);
67 $tempstore->get('my_storage_id')->willReturn(['section_storage' => 'this_is_not_an_entity']);
69 $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
70 $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());
72 $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
74 $this->setExpectedException(\UnexpectedValueException::class, 'The entry with storage type "my_storage_type" and ID "my_storage_id" is invalid');
75 $repository->get($section_storage->reveal());