diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-01-16 12:31:08 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2017-03-21 20:29:20 +0100 |
commit | 4306b184c4471825f916d895b047ed03fdf58985 (patch) | |
tree | ae4ffd760d74e58bf469f743076aecf838f634f2 /tests/HistoryTest.php | |
parent | b2306b0c783365e3f8110ae25bc93f2630b8b2c8 (diff) | |
download | Shaarli-4306b184c4471825f916d895b047ed03fdf58985.tar.gz Shaarli-4306b184c4471825f916d895b047ed03fdf58985.tar.zst Shaarli-4306b184c4471825f916d895b047ed03fdf58985.zip |
History mechanism
Use case: rest API service
* saved by default in data/history
* same format as datastore.php
* traced events:
* save/edit/delete link
* change settings or plugins settings
* rename tag
Diffstat (limited to 'tests/HistoryTest.php')
-rw-r--r-- | tests/HistoryTest.php | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/tests/HistoryTest.php b/tests/HistoryTest.php new file mode 100644 index 00000000..79322249 --- /dev/null +++ b/tests/HistoryTest.php | |||
@@ -0,0 +1,195 @@ | |||
1 | <?php | ||
2 | |||
3 | require_once 'application/History.php'; | ||
4 | |||
5 | |||
6 | class HistoryTest extends PHPUnit_Framework_TestCase | ||
7 | { | ||
8 | /** | ||
9 | * @var string History file path | ||
10 | */ | ||
11 | protected static $historyFilePath = 'sandbox/history.php'; | ||
12 | |||
13 | /** | ||
14 | * Delete history file. | ||
15 | */ | ||
16 | public function tearDown() | ||
17 | { | ||
18 | @unlink(self::$historyFilePath); | ||
19 | } | ||
20 | |||
21 | /** | ||
22 | * Test that the history file is created if it doesn't exist. | ||
23 | */ | ||
24 | public function testConstructFileCreated() | ||
25 | { | ||
26 | new History(self::$historyFilePath); | ||
27 | $this->assertFileExists(self::$historyFilePath); | ||
28 | } | ||
29 | |||
30 | /** | ||
31 | * Not writable history file: raise an exception. | ||
32 | * | ||
33 | * @expectedException Exception | ||
34 | * @expectedExceptionMessage History file isn't readable or writable | ||
35 | */ | ||
36 | public function testConstructNotWritable() | ||
37 | { | ||
38 | touch(self::$historyFilePath); | ||
39 | chmod(self::$historyFilePath, 0440); | ||
40 | new History(self::$historyFilePath); | ||
41 | } | ||
42 | |||
43 | /** | ||
44 | * Not parsable history file: raise an exception. | ||
45 | * | ||
46 | * @expectedException Exception | ||
47 | * @expectedExceptionMessageRegExp /Could not parse history file/ | ||
48 | */ | ||
49 | public function testConstructNotParsable() | ||
50 | { | ||
51 | file_put_contents(self::$historyFilePath, 'not parsable'); | ||
52 | // gzinflate generates a warning | ||
53 | @new History(self::$historyFilePath); | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * Test add link event | ||
58 | */ | ||
59 | public function testAddLink() | ||
60 | { | ||
61 | $history = new History(self::$historyFilePath); | ||
62 | $history->addLink(['id' => 0]); | ||
63 | $actual = $history->getHistory()[0]; | ||
64 | $this->assertEquals(History::CREATED, $actual['event']); | ||
65 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
66 | $this->assertEquals(0, $actual['id']); | ||
67 | |||
68 | $history = new History(self::$historyFilePath); | ||
69 | $history->addLink(['id' => 1]); | ||
70 | $actual = $history->getHistory()[0]; | ||
71 | $this->assertEquals(History::CREATED, $actual['event']); | ||
72 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
73 | $this->assertEquals(1, $actual['id']); | ||
74 | |||
75 | $history = new History(self::$historyFilePath); | ||
76 | $history->addLink(['id' => 'str']); | ||
77 | $actual = $history->getHistory()[0]; | ||
78 | $this->assertEquals(History::CREATED, $actual['event']); | ||
79 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
80 | $this->assertEquals('str', $actual['id']); | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * Test updated link event | ||
85 | */ | ||
86 | public function testUpdateLink() | ||
87 | { | ||
88 | $history = new History(self::$historyFilePath); | ||
89 | $history->updateLink(['id' => 1]); | ||
90 | $actual = $history->getHistory()[0]; | ||
91 | $this->assertEquals(History::UPDATED, $actual['event']); | ||
92 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
93 | $this->assertEquals(1, $actual['id']); | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * Test delete link event | ||
98 | */ | ||
99 | public function testDeleteLink() | ||
100 | { | ||
101 | $history = new History(self::$historyFilePath); | ||
102 | $history->deleteLink(['id' => 1]); | ||
103 | $actual = $history->getHistory()[0]; | ||
104 | $this->assertEquals(History::DELETED, $actual['event']); | ||
105 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
106 | $this->assertEquals(1, $actual['id']); | ||
107 | } | ||
108 | |||
109 | /** | ||
110 | * Test updated settings event | ||
111 | */ | ||
112 | public function testUpdateSettings() | ||
113 | { | ||
114 | $history = new History(self::$historyFilePath); | ||
115 | $history->updateSettings(); | ||
116 | $actual = $history->getHistory()[0]; | ||
117 | $this->assertEquals(History::SETTINGS, $actual['event']); | ||
118 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
119 | $this->assertEmpty($actual['id']); | ||
120 | } | ||
121 | |||
122 | /** | ||
123 | * Make sure that new items are stored at the beginning | ||
124 | */ | ||
125 | public function testHistoryOrder() | ||
126 | { | ||
127 | $history = new History(self::$historyFilePath); | ||
128 | $history->updateLink(['id' => 1]); | ||
129 | $actual = $history->getHistory()[0]; | ||
130 | $this->assertEquals(History::UPDATED, $actual['event']); | ||
131 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
132 | $this->assertEquals(1, $actual['id']); | ||
133 | |||
134 | $history->addLink(['id' => 1]); | ||
135 | $actual = $history->getHistory()[0]; | ||
136 | $this->assertEquals(History::CREATED, $actual['event']); | ||
137 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
138 | $this->assertEquals(1, $actual['id']); | ||
139 | } | ||
140 | |||
141 | /** | ||
142 | * Re-read history from file after writing an event | ||
143 | */ | ||
144 | public function testHistoryRead() | ||
145 | { | ||
146 | $history = new History(self::$historyFilePath); | ||
147 | $history->updateLink(['id' => 1]); | ||
148 | $history = new History(self::$historyFilePath); | ||
149 | $actual = $history->getHistory()[0]; | ||
150 | $this->assertEquals(History::UPDATED, $actual['event']); | ||
151 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
152 | $this->assertEquals(1, $actual['id']); | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * Re-read history from file after writing an event and make sure that the order is correct | ||
157 | */ | ||
158 | public function testHistoryOrderRead() | ||
159 | { | ||
160 | $history = new History(self::$historyFilePath); | ||
161 | $history->updateLink(['id' => 1]); | ||
162 | $history->addLink(['id' => 1]); | ||
163 | |||
164 | $history = new History(self::$historyFilePath); | ||
165 | $actual = $history->getHistory()[0]; | ||
166 | $this->assertEquals(History::CREATED, $actual['event']); | ||
167 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
168 | $this->assertEquals(1, $actual['id']); | ||
169 | |||
170 | $actual = $history->getHistory()[1]; | ||
171 | $this->assertEquals(History::UPDATED, $actual['event']); | ||
172 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
173 | $this->assertEquals(1, $actual['id']); | ||
174 | } | ||
175 | |||
176 | /** | ||
177 | * Test retention time: delete old entries. | ||
178 | */ | ||
179 | public function testHistoryRententionTime() | ||
180 | { | ||
181 | $history = new History(self::$historyFilePath, 5); | ||
182 | $history->updateLink(['id' => 1]); | ||
183 | $this->assertEquals(1, count($history->getHistory())); | ||
184 | $arr = $history->getHistory(); | ||
185 | $arr[0]['datetime'] = (new DateTime('-1 hour'))->format(DateTime::ATOM); | ||
186 | FileUtils::writeFlatDB(self::$historyFilePath, $arr); | ||
187 | |||
188 | $history = new History(self::$historyFilePath, 60); | ||
189 | $this->assertEquals(1, count($history->getHistory())); | ||
190 | $this->assertEquals(1, $history->getHistory()[0]['id']); | ||
191 | $history->updateLink(['id' => 2]); | ||
192 | $this->assertEquals(1, count($history->getHistory())); | ||
193 | $this->assertEquals(2, $history->getHistory()[0]['id']); | ||
194 | } | ||
195 | } | ||