7 use Shaarli\Bookmark\Bookmark
;
9 class HistoryTest
extends \PHPUnit\Framework\TestCase
12 * @var string History file path
14 protected static $historyFilePath = 'sandbox/history.php';
17 * Delete history file.
19 public function setUp()
21 if (file_exists(self
::$historyFilePath)) {
22 unlink(self
::$historyFilePath);
27 * Test that the history file is created if it doesn't exist.
29 public function testConstructLazyLoading()
31 new History(self
::$historyFilePath);
32 $this->assertFileNotExists(self
::$historyFilePath);
36 * Test that the history file is created if it doesn't exist.
38 public function testAddEventCreateFile()
40 $history = new History(self
::$historyFilePath);
41 $history->updateSettings();
42 $this->assertFileExists(self
::$historyFilePath);
46 * Not writable history file: raise an exception.
48 * @expectedException Exception
49 * @expectedExceptionMessage History file isn't readable or writable
51 public function testConstructNotWritable()
53 touch(self
::$historyFilePath);
54 chmod(self
::$historyFilePath, 0440);
55 $history = new History(self
::$historyFilePath);
56 $history->updateSettings();
60 * Not parsable history file: raise an exception.
62 * @expectedException Exception
63 * @expectedExceptionMessageRegExp /Could not parse history file/
65 public function testConstructNotParsable()
67 file_put_contents(self
::$historyFilePath, 'not parsable');
68 $history = new History(self
::$historyFilePath);
69 // gzinflate generates a warning
70 @$history->updateSettings();
76 public function testAddLink()
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']);
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']);
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']);
104 // * Test updated link event
106 // public function testUpdateLink()
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']);
117 // * Test delete link event
119 // public function testDeleteLink()
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']);
130 // * Test updated settings event
132 // public function testUpdateSettings()
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']);
143 // * Make sure that new items are stored at the beginning
145 // public function testHistoryOrder()
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']);
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']);
162 // * Re-read history from file after writing an event
164 // public function testHistoryRead()
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']);
176 // * Re-read history from file after writing an event and make sure that the order is correct
178 // public function testHistoryOrderRead()
180 // $history = new History(self::$historyFilePath);
181 // $history->updateLink(['id' => 1]);
182 // $history->addLink(['id' => 1]);
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']);
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']);
197 // * Test retention time: delete old entries.
199 // public function testHistoryRententionTime()
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);
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']);