ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
SessionRepositoryTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use PHPUnit\Framework\Attributes\BackupGlobals;
26use PHPUnit\Framework\TestCase;
27
28#[BackupGlobals(true)]
29class SessionRepositoryTest extends TestCase
30{
32
34
35 protected function setUp(): void
36 {
37 $_SESSION = [];
38 $this->repository = new SessionRepository();
39 $this->namespace = new StorageNamespace(['my_component', 'view_state']);
40 }
41
43 {
44 $this->repository->write($this->namespace, 'sort', '"title"');
45
46 $this->assertSame(['kvs:my_component.view_state:sort' => '"title"'], $_SESSION);
47 }
48
49 public function testWhatWasWrittenCanBeReadBack(): void
50 {
51 $this->repository->write($this->namespace, 'sort', '"title"');
52
53 $this->assertTrue($this->repository->has($this->namespace, 'sort'));
54 $this->assertSame('"title"', $this->repository->read($this->namespace, 'sort'));
55 }
56
57 public function testAnAbsentEntryReadsAsNull(): void
58 {
59 $this->assertFalse($this->repository->has($this->namespace, 'sort'));
60 $this->assertNull($this->repository->read($this->namespace, 'sort'));
61 }
62
64 {
65 $_SESSION['kvs:my_component.view_state:sort'] = ['not', 'a', 'string'];
66
67 $this->assertNull($this->repository->read($this->namespace, 'sort'));
68 }
69
70 public function testRemoveDropsOneEntry(): void
71 {
72 $this->repository->write($this->namespace, 'a', '1');
73 $this->repository->write($this->namespace, 'b', '2');
74
75 $this->repository->remove($this->namespace, 'a');
76
77 $this->assertFalse($this->repository->has($this->namespace, 'a'));
78 $this->assertTrue($this->repository->has($this->namespace, 'b'));
79 }
80
82 {
83 $nested = new StorageNamespace(['my_component', 'view_state', 'details']);
84 $sibling = new StorageNamespace(['other_component']);
85 $this->repository->write($this->namespace, 'a', '1');
86 $this->repository->write($nested, 'a', '2');
87 $this->repository->write($sibling, 'a', '3');
88 $_SESSION['AccountId'] = 6;
89
90 $this->repository->removeAll($this->namespace);
91
92 $this->assertFalse($this->repository->has($this->namespace, 'a'));
93 $this->assertTrue($this->repository->has($nested, 'a'));
94 $this->assertTrue($this->repository->has($sibling, 'a'));
95 $this->assertSame(6, $_SESSION['AccountId']);
96 }
97
99 {
100 $this->repository->removeAll($this->namespace);
101
102 $this->assertSame([], $_SESSION);
103 }
104
106 {
107 $_SESSION['AccountId'] = 6;
108 $_SESSION['_authsession'] = ['user' => 'root'];
109
110 $this->repository->removeAll($this->namespace);
111
112 $this->assertSame(['AccountId' => 6, '_authsession' => ['user' => 'root']], $_SESSION);
113 }
114
116 {
117 $this->repository->write(new StorageNamespace(['a', 'b']), 'c', 'nested');
118 $this->repository->write(new StorageNamespace(['a']), 'b.c', 'flat');
119
120 $this->assertSame('nested', $this->repository->read(new StorageNamespace(['a', 'b']), 'c'));
121 $this->assertSame('flat', $this->repository->read(new StorageNamespace(['a']), 'b.c'));
122 }
123
125 {
126 $this->repository->write($this->namespace, 'sort', '"title"');
127 $this->repository->write($this->namespace, 'limit', '10');
128
129 $this->assertSame(
130 ['sort' => '"title"', 'limit' => '10'],
131 $this->repository->readAll($this->namespace)
132 );
133 }
134
136 {
137 $this->repository->write($this->namespace, 'a', '1');
138 $this->repository->write($this->namespace, 'ab', '2');
139
140 $this->assertSame(['a' => '1', 'ab' => '2'], $this->repository->readAll($this->namespace));
141 }
142
144 {
145 $nested = new StorageNamespace(['my_component', 'view_state', 'details']);
146 $sibling = new StorageNamespace(['other_component']);
147 $this->repository->write($this->namespace, 'a', '1');
148 $this->repository->write($nested, 'a', '2');
149 $this->repository->write($sibling, 'a', '3');
150 $_SESSION['AccountId'] = 6;
151 $_SESSION['kvs:my_component.view_state:sort'] = ['not', 'a', 'string'];
152
153 $this->assertSame(['a' => '1'], $this->repository->readAll($this->namespace));
154 $this->assertSame(6, $_SESSION['AccountId']);
155 }
156
158 {
159 $this->assertSame([], $this->repository->readAll($this->namespace));
160 }
161}
Keeps key-value storage in the ILIAS session.
Isolates the keys of one consumer from every other consumer of a storage.