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