Pull merge.
[yaffs-website] / web / core / modules / layout_builder / tests / src / Unit / LayoutTempstoreRepositoryTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Unit;
4
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;
10
11 /**
12  * @coversDefaultClass \Drupal\layout_builder\LayoutTempstoreRepository
13  * @group layout_builder
14  */
15 class LayoutTempstoreRepositoryTest extends UnitTestCase {
16
17   /**
18    * @covers ::get
19    * @covers ::has
20    */
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');
25
26     $tempstore = $this->prophesize(SharedTempStore::class);
27     $tempstore->get('my_storage_id')->shouldBeCalled();
28
29     $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
30     $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());
31
32     $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
33
34     $this->assertFalse($repository->has($section_storage->reveal()));
35
36     $result = $repository->get($section_storage->reveal());
37     $this->assertSame($section_storage->reveal(), $result);
38   }
39
40   /**
41    * @covers ::get
42    * @covers ::has
43    */
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');
48
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());
54
55     $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
56
57     $this->assertTrue($repository->has($section_storage->reveal()));
58
59     $result = $repository->get($section_storage->reveal());
60     $this->assertSame($tempstore_section_storage->reveal(), $result);
61     $this->assertNotSame($section_storage->reveal(), $result);
62   }
63
64   /**
65    * @covers ::get
66    */
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');
71
72     $tempstore = $this->prophesize(SharedTempStore::class);
73     $tempstore->get('my_storage_id')->willReturn(['section_storage' => 'this_is_not_an_entity']);
74
75     $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
76     $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());
77
78     $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
79
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());
82   }
83
84 }