ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
UiStorageAdapterTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
26use ILIAS\Data\Factory as DataFactory;
33use ILIAS\Refinery\Factory as Refinery;
38use PHPUnit\Framework\Attributes\BackupGlobals;
39use PHPUnit\Framework\Attributes\DataProvider;
40use PHPUnit\Framework\TestCase;
41
42#[BackupGlobals(true)]
43class UiStorageAdapterTest extends TestCase
44{
46
48
49 protected function setUp(): void
50 {
51 $_SESSION = [];
52 $language = $this->getMockBuilder(\ilLanguage::class)
53 ->disableOriginalConstructor()
54 ->getMock();
55 $this->refinery = new Refinery(new DataFactory(), $language);
56 $store = new NamespacedStore(
57 new StorageNamespace(['ui', 'storage']),
59 new KeyRules(),
60 new Values($this->refinery)
61 );
62 $subjects = $this->createStub(AuthenticatedSubjectResolver::class);
63 $subjects->method('supportsPersistentStorage')->willReturn(false);
64 $services = $this->createMock(Services::class);
65 $services->method('session')->willReturn($store);
66 $services->expects($this->never())->method('persistentFor');
67 $this->adapter = new UiStorageAdapter($subjects, $services, $this->refinery);
68 }
69
71 {
72 $this->assertFalse($this->adapter->offsetExists('view_state'));
73
74 $this->adapter->offsetSet('view_state', ['sort' => 'title', 'page' => 2]);
75
76 $this->assertTrue($this->adapter->offsetExists('view_state'));
77 $this->assertSame(['sort' => 'title', 'page' => 2], $this->adapter->offsetGet('view_state'));
78 }
79
80 public function testUnsetRemovesTheEntry(): void
81 {
82 $this->adapter->offsetSet('view_state', ['page' => 2]);
83
84 $this->adapter->offsetUnset('view_state');
85
86 $this->assertFalse($this->adapter->offsetExists('view_state'));
87 $this->assertNull($this->adapter->offsetGet('view_state'));
88 }
89
94 #[DataProvider('storageIdsUsedByTheUi')]
95 public function testTheStorageIdsOfTheUiAreAccepted(string $storage_id): void
96 {
97 $this->adapter->offsetSet($storage_id, ['sort' => 'title']);
98
99 $this->assertSame(['sort' => 'title'], $this->adapter->offsetGet($storage_id));
100 }
101
105 public static function storageIdsUsedByTheUi(): array
106 {
107 return [
108 'data table' => [Data::STORAGE_ID_PREFIX . 'my_table'],
109 'ordering table' => [Ordering::STORAGE_ID_PREFIX . 'my_table'],
110 'sequence' => [Sequence::STORAGE_ID_PREFIX . 'my_sequence'],
111 ];
112 }
113
114 public function testANonStringOffsetIsRejected(): void
115 {
116 $this->expectException(\InvalidArgumentException::class);
117 $this->expectExceptionMessage('Offset needs to be of type string.');
118
119 $this->adapter->offsetExists(42);
120 }
121
123 {
124 $store = $this->createMock(Store::class);
125 $store->expects($this->once())->method('has')->with('view_state')->willReturn(true);
126 $subjects = $this->createStub(AuthenticatedSubjectResolver::class);
127 $subjects->method('supportsPersistentStorage')->willReturn(true);
128 $services = $this->createMock(Services::class);
129 $services->expects($this->once())
130 ->method('persistentFor')
131 ->with($subjects, ['ui', 'storage'])
132 ->willReturn($store);
133 $services->expects($this->never())->method('session');
134
135 self::assertTrue((new UiStorageAdapter($subjects, $services, $this->refinery))->offsetExists('view_state'));
136 }
137
139 {
140 $store = $this->createMock(Store::class);
141 $store->expects($this->once())->method('has')->with('view_state')->willReturn(false);
142 $subjects = $this->createStub(AuthenticatedSubjectResolver::class);
143 $subjects->method('supportsPersistentStorage')->willReturn(false);
144 $services = $this->createMock(Services::class);
145 $services->expects($this->once())->method('session')->with(['ui', 'storage'])->willReturn($store);
146 $services->expects($this->never())->method('persistentFor');
147
148 self::assertFalse((new UiStorageAdapter($subjects, $services, $this->refinery))->offsetExists('view_state'));
149 }
150
152 {
153 $subjects = $this->createMock(AuthenticatedSubjectResolver::class);
154 $subjects->expects($this->never())->method('supportsPersistentStorage');
155 $services = $this->createMock(Services::class);
156 $services->expects($this->never())->method($this->anything());
157
158 new UiStorageAdapter($subjects, $services, $this->refinery);
159 }
160
165 {
166 $session_store = $this->createMock(Store::class);
167 $session_store->expects($this->once())->method('set')->with('view_state', ['page' => 1]);
168 $persistent_store = $this->createMock(Store::class);
169 $persistent_store->expects($this->once())->method('set')->with('view_state', ['page' => 2]);
170 $subjects = $this->createStub(AuthenticatedSubjectResolver::class);
171 $subjects->method('supportsPersistentStorage')->willReturnOnConsecutiveCalls(false, true);
172 $services = $this->createStub(Services::class);
173 $services->method('session')->willReturn($session_store);
174 $services->method('persistentFor')->willReturn($persistent_store);
175
176 $adapter = new UiStorageAdapter($subjects, $services, $this->refinery);
177 $adapter->offsetSet('view_state', ['page' => 1]);
178 $adapter->offsetSet('view_state', ['page' => 2]);
179 }
180
182 {
183 $store = $this->createMock(Store::class);
184 $store->expects($this->once())
185 ->method('get')
186 ->with(
187 'view_state',
188 $this->callback(static function (Transformation $transformation): bool {
189 return $transformation->transform(['sort' => 'title']) === ['sort' => 'title']
190 && $transformation->transform(null) === null;
191 })
192 )
193 ->willReturn(['sort' => 'title']);
194 $subjects = $this->createStub(AuthenticatedSubjectResolver::class);
195 $subjects->method('supportsPersistentStorage')->willReturn(false);
196 $services = $this->createStub(Services::class);
197 $services->method('session')->willReturn($store);
198
199 $adapter = new UiStorageAdapter($subjects, $services, $this->refinery);
200
201 self::assertSame(['sort' => 'title'], $adapter->offsetGet('view_state'));
202 }
203}
Keeps key-value storage in the ILIAS session.
Adapts key-value storage to the UI ArrayAccess contract.
Builds data types.
Definition: Factory.php:36
The rules every storage key has to follow.
Definition: KeyRules.php:37
A store bound to one namespace of one repository.
Isolates the keys of one consumer from every other consumer of a storage.
Translates between the values a consumer works with and the strings a repository stores.
Definition: Values.php:38
testALoginAfterTheAdapterWasBuiltSwitchesToThePersistentStore()
The adapter may be built before the user logs in within the same request.
testTheStorageIdsOfTheUiAreAccepted(string $storage_id)
The UI builds its storage ids from class names, so the storage has to accept backslashes.
Resolves the current fully authenticated ILIAS user to a KeyValueStorage subject.
Entry point to the namespace-scoped key-value storages of ILIAS.
Definition: Services.php:34
Key-value store scoped to one namespace.
Definition: Store.php:32
A transformation is a function from one datatype to another.
transform($from)
Perform the transformation.