]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/HistoryTest.php
Fix an issue truncating extracted metadata content
[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
94 // /**
95 // * Test updated link event
96 // */
97 // public function testUpdateLink()
98 // {
99 // $history = new History(self::$historyFilePath);
100 // $history->updateLink(['id' => 1]);
101 // $actual = $history->getHistory()[0];
102 // $this->assertEquals(History::UPDATED, $actual['event']);
103 // $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
104 // $this->assertEquals(1, $actual['id']);
105 // }
106 //
107 // /**
108 // * Test delete link event
109 // */
110 // public function testDeleteLink()
111 // {
112 // $history = new History(self::$historyFilePath);
113 // $history->deleteLink(['id' => 1]);
114 // $actual = $history->getHistory()[0];
115 // $this->assertEquals(History::DELETED, $actual['event']);
116 // $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
117 // $this->assertEquals(1, $actual['id']);
118 // }
119 //
120 // /**
121 // * Test updated settings event
122 // */
123 // public function testUpdateSettings()
124 // {
125 // $history = new History(self::$historyFilePath);
126 // $history->updateSettings();
127 // $actual = $history->getHistory()[0];
128 // $this->assertEquals(History::SETTINGS, $actual['event']);
129 // $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
130 // $this->assertEmpty($actual['id']);
131 // }
132 //
133 // /**
134 // * Make sure that new items are stored at the beginning
135 // */
136 // public function testHistoryOrder()
137 // {
138 // $history = new History(self::$historyFilePath);
139 // $history->updateLink(['id' => 1]);
140 // $actual = $history->getHistory()[0];
141 // $this->assertEquals(History::UPDATED, $actual['event']);
142 // $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
143 // $this->assertEquals(1, $actual['id']);
144 //
145 // $history->addLink(['id' => 1]);
146 // $actual = $history->getHistory()[0];
147 // $this->assertEquals(History::CREATED, $actual['event']);
148 // $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
149 // $this->assertEquals(1, $actual['id']);
150 // }
151 //
152 // /**
153 // * Re-read history from file after writing an event
154 // */
155 // public function testHistoryRead()
156 // {
157 // $history = new History(self::$historyFilePath);
158 // $history->updateLink(['id' => 1]);
159 // $history = new History(self::$historyFilePath);
160 // $actual = $history->getHistory()[0];
161 // $this->assertEquals(History::UPDATED, $actual['event']);
162 // $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
163 // $this->assertEquals(1, $actual['id']);
164 // }
165 //
166 // /**
167 // * Re-read history from file after writing an event and make sure that the order is correct
168 // */
169 // public function testHistoryOrderRead()
170 // {
171 // $history = new History(self::$historyFilePath);
172 // $history->updateLink(['id' => 1]);
173 // $history->addLink(['id' => 1]);
174 //
175 // $history = new History(self::$historyFilePath);
176 // $actual = $history->getHistory()[0];
177 // $this->assertEquals(History::CREATED, $actual['event']);
178 // $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
179 // $this->assertEquals(1, $actual['id']);
180 //
181 // $actual = $history->getHistory()[1];
182 // $this->assertEquals(History::UPDATED, $actual['event']);
183 // $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
184 // $this->assertEquals(1, $actual['id']);
185 // }
186 //
187 // /**
188 // * Test retention time: delete old entries.
189 // */
190 // public function testHistoryRententionTime()
191 // {
192 // $history = new History(self::$historyFilePath, 5);
193 // $history->updateLink(['id' => 1]);
194 // $this->assertEquals(1, count($history->getHistory()));
195 // $arr = $history->getHistory();
196 // $arr[0]['datetime'] = new DateTime('-1 hour');
197 // FileUtils::writeFlatDB(self::$historyFilePath, $arr);
198 //
199 // $history = new History(self::$historyFilePath, 60);
200 // $this->assertEquals(1, count($history->getHistory()));
201 // $this->assertEquals(1, $history->getHistory()[0]['id']);
202 // $history->updateLink(['id' => 2]);
203 // $this->assertEquals(1, count($history->getHistory()));
204 // $this->assertEquals(2, $history->getHistory()[0]['id']);
205 // }
206 }