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