]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/HistoryTest.php
Merge pull request #1698 from ArthurHoaro/feature/plugins-search-filter
[github/shaarli/Shaarli.git] / tests / HistoryTest.php
1 <?php
2
3 namespace Shaarli;
4
5 use DateTime;
6 use Exception;
7
8 class HistoryTest extends \PHPUnit\Framework\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 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 */
26 public function testConstructLazyLoading()
27 {
28 new History(self::$historyFilePath);
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();
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);
52 $history = new History(self::$historyFilePath);
53 $history->updateSettings();
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');
65 $history = new History(self::$historyFilePath);
66 // gzinflate generates a warning
67 @$history->updateSettings();
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']);
79 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
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']);
86 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
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']);
93 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
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']);
106 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
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']);
119 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
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']);
132 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
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']);
145 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
146 $this->assertEquals(1, $actual['id']);
147
148 $history->addLink(['id' => 1]);
149 $actual = $history->getHistory()[0];
150 $this->assertEquals(History::CREATED, $actual['event']);
151 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
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']);
165 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
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']);
181 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
182 $this->assertEquals(1, $actual['id']);
183
184 $actual = $history->getHistory()[1];
185 $this->assertEquals(History::UPDATED, $actual['event']);
186 $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
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();
199 $arr[0]['datetime'] = new DateTime('-1 hour');
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 }