]>
Commit | Line | Data |
---|---|---|
4306b184 A |
1 | <?php |
2 | ||
bdc5152d | 3 | namespace Shaarli; |
4306b184 | 4 | |
bdc5152d V |
5 | use DateTime; |
6 | use Exception; | |
e26e2060 | 7 | use Shaarli\Bookmark\Bookmark; |
4306b184 | 8 | |
bdc5152d | 9 | class HistoryTest extends \PHPUnit\Framework\TestCase |
4306b184 A |
10 | { |
11 | /** | |
12 | * @var string History file path | |
13 | */ | |
14 | protected static $historyFilePath = 'sandbox/history.php'; | |
15 | ||
16 | /** | |
17 | * Delete history file. | |
18 | */ | |
e26e2060 | 19 | public function setUp() |
4306b184 | 20 | { |
e26e2060 A |
21 | if (file_exists(self::$historyFilePath)) { |
22 | unlink(self::$historyFilePath); | |
23 | } | |
4306b184 A |
24 | } |
25 | ||
26 | /** | |
27 | * Test that the history file is created if it doesn't exist. | |
28 | */ | |
d16ca2e2 | 29 | public function testConstructLazyLoading() |
4306b184 A |
30 | { |
31 | new History(self::$historyFilePath); | |
d16ca2e2 A |
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(); | |
4306b184 A |
42 | $this->assertFileExists(self::$historyFilePath); |
43 | } | |
44 | ||
45 | /** | |
46 | * Not writable history file: raise an exception. | |
47 | * | |
48 | * @expectedException Exception | |
49 | * @expectedExceptionMessage History file isn't readable or writable | |
50 | */ | |
51 | public function testConstructNotWritable() | |
52 | { | |
53 | touch(self::$historyFilePath); | |
54 | chmod(self::$historyFilePath, 0440); | |
d16ca2e2 A |
55 | $history = new History(self::$historyFilePath); |
56 | $history->updateSettings(); | |
4306b184 A |
57 | } |
58 | ||
59 | /** | |
60 | * Not parsable history file: raise an exception. | |
61 | * | |
62 | * @expectedException Exception | |
63 | * @expectedExceptionMessageRegExp /Could not parse history file/ | |
64 | */ | |
65 | public function testConstructNotParsable() | |
66 | { | |
67 | file_put_contents(self::$historyFilePath, 'not parsable'); | |
d16ca2e2 | 68 | $history = new History(self::$historyFilePath); |
4306b184 | 69 | // gzinflate generates a warning |
d16ca2e2 | 70 | @$history->updateSettings(); |
4306b184 A |
71 | } |
72 | ||
73 | /** | |
74 | * Test add link event | |
75 | */ | |
76 | public function testAddLink() | |
77 | { | |
78 | $history = new History(self::$historyFilePath); | |
e26e2060 A |
79 | $bookmark = (new Bookmark())->setId(0); |
80 | $history->addLink($bookmark); | |
4306b184 A |
81 | $actual = $history->getHistory()[0]; |
82 | $this->assertEquals(History::CREATED, $actual['event']); | |
813849e5 | 83 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
84 | $this->assertEquals(0, $actual['id']); |
85 | ||
86 | $history = new History(self::$historyFilePath); | |
e26e2060 A |
87 | $bookmark = (new Bookmark())->setId(1); |
88 | $history->addLink($bookmark); | |
4306b184 A |
89 | $actual = $history->getHistory()[0]; |
90 | $this->assertEquals(History::CREATED, $actual['event']); | |
813849e5 | 91 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
92 | $this->assertEquals(1, $actual['id']); |
93 | ||
94 | $history = new History(self::$historyFilePath); | |
e26e2060 A |
95 | $bookmark = (new Bookmark())->setId('str'); |
96 | $history->addLink($bookmark); | |
4306b184 A |
97 | $actual = $history->getHistory()[0]; |
98 | $this->assertEquals(History::CREATED, $actual['event']); | |
813849e5 | 99 | $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']); |
4306b184 A |
100 | $this->assertEquals('str', $actual['id']); |
101 | } | |
102 | ||
e26e2060 A |
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 | // } | |
4306b184 | 215 | } |