Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[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    */
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');
24
25     $tempstore = $this->prophesize(SharedTempStore::class);
26     $tempstore->get('my_storage_id')->shouldBeCalled();
27
28     $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
29     $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());
30
31     $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
32
33     $result = $repository->get($section_storage->reveal());
34     $this->assertSame($section_storage->reveal(), $result);
35   }
36
37   /**
38    * @covers ::get
39    */
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');
44
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());
50
51     $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
52
53     $result = $repository->get($section_storage->reveal());
54     $this->assertSame($tempstore_section_storage->reveal(), $result);
55     $this->assertNotSame($section_storage->reveal(), $result);
56   }
57
58   /**
59    * @covers ::get
60    */
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');
65
66     $tempstore = $this->prophesize(SharedTempStore::class);
67     $tempstore->get('my_storage_id')->willReturn(['section_storage' => 'this_is_not_an_entity']);
68
69     $tempstore_factory = $this->prophesize(SharedTempStoreFactory::class);
70     $tempstore_factory->get('layout_builder.section_storage.my_storage_type')->willReturn($tempstore->reveal());
71
72     $repository = new LayoutTempstoreRepository($tempstore_factory->reveal());
73
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());
76   }
77
78 }