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