]>
Commit | Line | Data |
---|---|---|
4306b184 A |
1 | <?php |
2 | ||
bdc5152d | 3 | namespace Shaarli; |
4306b184 | 4 | |
bdc5152d V |
5 | use DateTime; |
6 | use Exception; | |
4306b184 | 7 | |
bdc5152d | 8 | class HistoryTest extends \PHPUnit\Framework\TestCase |
4306b184 A |
9 | { |
10 | /** | |
11 | * @var string History file path | |
12 | */ | |
13 | protected static $historyFilePath = 'sandbox/history.php'; | |
14 | ||
15 | /** | |
16 | * Delete history file. | |
17 | */ | |
18 | public function tearDown() | |
19 | { | |
20 | @unlink(self::$historyFilePath); | |
21 | } | |
22 | ||
23 | /** | |
24 | * Test that the history file is created if it doesn't exist. | |
25 | */ | |
d16ca2e2 | 26 | public function testConstructLazyLoading() |
4306b184 A |
27 | { |
28 | new History(self::$historyFilePath); | |
d16ca2e2 A |
29 | $this->assertFileNotExists(self::$historyFilePath); |
30 | } | |
31 | ||
32 | /** | |
33 | * Test that the history file is created if it doesn't exist. | |
34 | */ | |
35 | public function testAddEventCreateFile() | |
36 | { | |
37 | $history = new History(self::$historyFilePath); | |
38 | $history->updateSettings(); | |
4306b184 A |
39 | $this->assertFileExists(self::$historyFilePath); |
40 | } | |
41 | ||
42 | /** | |
43 | * Not writable history file: raise an exception. | |
44 | * | |
45 | * @expectedException Exception | |
46 | * @expectedExceptionMessage History file isn't readable or writable | |
47 | */ | |
48 | public function testConstructNotWritable() | |
49 | { | |
50 | touch(self::$historyFilePath); | |
51 | chmod(self::$historyFilePath, 0440); | |
d16ca2e2 A |
52 | $history = new History(self::$historyFilePath); |
53 | $history->updateSettings(); | |
4306b184 A |
54 | } |
55 | ||
56 | /** | |
57 | * Not parsable history file: raise an exception. | |
58 | * | |
59 | * @expectedException Exception | |
60 | * @expectedExceptionMessageRegExp /Could not parse history file/ | |
61 | */ | |
62 | public function testConstructNotParsable() | |
63 | { | |
64 | file_put_contents(self::$historyFilePath, 'not parsable'); | |
d16ca2e2 | 65 | $history = new History(self::$historyFilePath); |
4306b184 | 66 | // gzinflate generates a warning |
d16ca2e2 | 67 | @$history->updateSettings(); |
4306b184 A |
68 | } |
69 | ||
70 | /** | |
71 | * Test add link event | |
72 | */ | |
73 | public function testAddLink() | |
74 | { | |
75 | $history = new History(self::$historyFilePath); | |
76 | $history->addLink(['id' => 0]); | |
77 | $actual = $history->getHistory()[0]; | |
78 | $this->assertEquals(History::CREATED, $actual['event']); | |
813849e5 | 79 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
80 | $this->assertEquals(0, $actual['id']); |
81 | ||
82 | $history = new History(self::$historyFilePath); | |
83 | $history->addLink(['id' => 1]); | |
84 | $actual = $history->getHistory()[0]; | |
85 | $this->assertEquals(History::CREATED, $actual['event']); | |
813849e5 | 86 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
87 | $this->assertEquals(1, $actual['id']); |
88 | ||
89 | $history = new History(self::$historyFilePath); | |
90 | $history->addLink(['id' => 'str']); | |
91 | $actual = $history->getHistory()[0]; | |
92 | $this->assertEquals(History::CREATED, $actual['event']); | |
813849e5 | 93 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
94 | $this->assertEquals('str', $actual['id']); |
95 | } | |
96 | ||
97 | /** | |
98 | * Test updated link event | |
99 | */ | |
100 | public function testUpdateLink() | |
101 | { | |
102 | $history = new History(self::$historyFilePath); | |
103 | $history->updateLink(['id' => 1]); | |
104 | $actual = $history->getHistory()[0]; | |
105 | $this->assertEquals(History::UPDATED, $actual['event']); | |
813849e5 | 106 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
107 | $this->assertEquals(1, $actual['id']); |
108 | } | |
109 | ||
110 | /** | |
111 | * Test delete link event | |
112 | */ | |
113 | public function testDeleteLink() | |
114 | { | |
115 | $history = new History(self::$historyFilePath); | |
116 | $history->deleteLink(['id' => 1]); | |
117 | $actual = $history->getHistory()[0]; | |
118 | $this->assertEquals(History::DELETED, $actual['event']); | |
813849e5 | 119 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
120 | $this->assertEquals(1, $actual['id']); |
121 | } | |
122 | ||
123 | /** | |
124 | * Test updated settings event | |
125 | */ | |
126 | public function testUpdateSettings() | |
127 | { | |
128 | $history = new History(self::$historyFilePath); | |
129 | $history->updateSettings(); | |
130 | $actual = $history->getHistory()[0]; | |
131 | $this->assertEquals(History::SETTINGS, $actual['event']); | |
813849e5 | 132 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
133 | $this->assertEmpty($actual['id']); |
134 | } | |
135 | ||
136 | /** | |
137 | * Make sure that new items are stored at the beginning | |
138 | */ | |
139 | public function testHistoryOrder() | |
140 | { | |
141 | $history = new History(self::$historyFilePath); | |
142 | $history->updateLink(['id' => 1]); | |
143 | $actual = $history->getHistory()[0]; | |
144 | $this->assertEquals(History::UPDATED, $actual['event']); | |
813849e5 | 145 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
146 | $this->assertEquals(1, $actual['id']); |
147 | ||
148 | $history->addLink(['id' => 1]); | |
149 | $actual = $history->getHistory()[0]; | |
150 | $this->assertEquals(History::CREATED, $actual['event']); | |
813849e5 | 151 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
152 | $this->assertEquals(1, $actual['id']); |
153 | } | |
154 | ||
155 | /** | |
156 | * Re-read history from file after writing an event | |
157 | */ | |
158 | public function testHistoryRead() | |
159 | { | |
160 | $history = new History(self::$historyFilePath); | |
161 | $history->updateLink(['id' => 1]); | |
162 | $history = new History(self::$historyFilePath); | |
163 | $actual = $history->getHistory()[0]; | |
164 | $this->assertEquals(History::UPDATED, $actual['event']); | |
813849e5 | 165 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
166 | $this->assertEquals(1, $actual['id']); |
167 | } | |
168 | ||
169 | /** | |
170 | * Re-read history from file after writing an event and make sure that the order is correct | |
171 | */ | |
172 | public function testHistoryOrderRead() | |
173 | { | |
174 | $history = new History(self::$historyFilePath); | |
175 | $history->updateLink(['id' => 1]); | |
176 | $history->addLink(['id' => 1]); | |
177 | ||
178 | $history = new History(self::$historyFilePath); | |
179 | $actual = $history->getHistory()[0]; | |
180 | $this->assertEquals(History::CREATED, $actual['event']); | |
813849e5 | 181 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
182 | $this->assertEquals(1, $actual['id']); |
183 | ||
184 | $actual = $history->getHistory()[1]; | |
185 | $this->assertEquals(History::UPDATED, $actual['event']); | |
813849e5 | 186 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
187 | $this->assertEquals(1, $actual['id']); |
188 | } | |
189 | ||
190 | /** | |
191 | * Test retention time: delete old entries. | |
192 | */ | |
193 | public function testHistoryRententionTime() | |
194 | { | |
195 | $history = new History(self::$historyFilePath, 5); | |
196 | $history->updateLink(['id' => 1]); | |
197 | $this->assertEquals(1, count($history->getHistory())); | |
198 | $arr = $history->getHistory(); | |
813849e5 | 199 | $arr[0]['datetime'] = new DateTime('-1 hour'); |
4306b184 A |
200 | FileUtils::writeFlatDB(self::$historyFilePath, $arr); |
201 | ||
202 | $history = new History(self::$historyFilePath, 60); | |
203 | $this->assertEquals(1, count($history->getHistory())); | |
204 | $this->assertEquals(1, $history->getHistory()[0]['id']); | |
205 | $history->updateLink(['id' => 2]); | |
206 | $this->assertEquals(1, count($history->getHistory())); | |
207 | $this->assertEquals(2, $history->getHistory()[0]['id']); | |
208 | } | |
209 | } |