diff options
author | ArthurHoaro <arthur@hoa.ro> | 2017-05-06 17:12:06 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-06 17:12:06 +0200 |
commit | f9ff7f1b69f19b42569ffa67280807ba56f5d48a (patch) | |
tree | 22126f1de7aac6c03a0b2c609380057740b840ac /tests/HistoryTest.php | |
parent | a271c5f34f99bab38a167d491b69e5942cd6da94 (diff) | |
parent | d16ca2e22f3d7325fc9593fccd8523eebe226567 (diff) | |
download | Shaarli-f9ff7f1b69f19b42569ffa67280807ba56f5d48a.tar.gz Shaarli-f9ff7f1b69f19b42569ffa67280807ba56f5d48a.tar.zst Shaarli-f9ff7f1b69f19b42569ffa67280807ba56f5d48a.zip |
Merge pull request #764 from ArthurHoaro/feature/history
History mechanism
Diffstat (limited to 'tests/HistoryTest.php')
-rw-r--r-- | tests/HistoryTest.php | 207 |
1 files changed, 207 insertions, 0 deletions
diff --git a/tests/HistoryTest.php b/tests/HistoryTest.php new file mode 100644 index 00000000..91525845 --- /dev/null +++ b/tests/HistoryTest.php | |||
@@ -0,0 +1,207 @@ | |||
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 testConstructLazyLoading() | ||
25 | { | ||
26 | new History(self::$historyFilePath); | ||
27 | $this->assertFileNotExists(self::$historyFilePath); | ||
28 | } | ||
29 | |||
30 | /** | ||
31 | * Test that the history file is created if it doesn't exist. | ||
32 | */ | ||
33 | public function testAddEventCreateFile() | ||
34 | { | ||
35 | $history = new History(self::$historyFilePath); | ||
36 | $history->updateSettings(); | ||
37 | $this->assertFileExists(self::$historyFilePath); | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Not writable history file: raise an exception. | ||
42 | * | ||
43 | * @expectedException Exception | ||
44 | * @expectedExceptionMessage History file isn't readable or writable | ||
45 | */ | ||
46 | public function testConstructNotWritable() | ||
47 | { | ||
48 | touch(self::$historyFilePath); | ||
49 | chmod(self::$historyFilePath, 0440); | ||
50 | $history = new History(self::$historyFilePath); | ||
51 | $history->updateSettings(); | ||
52 | } | ||
53 | |||
54 | /** | ||
55 | * Not parsable history file: raise an exception. | ||
56 | * | ||
57 | * @expectedException Exception | ||
58 | * @expectedExceptionMessageRegExp /Could not parse history file/ | ||
59 | */ | ||
60 | public function testConstructNotParsable() | ||
61 | { | ||
62 | file_put_contents(self::$historyFilePath, 'not parsable'); | ||
63 | $history = new History(self::$historyFilePath); | ||
64 | // gzinflate generates a warning | ||
65 | @$history->updateSettings(); | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Test add link event | ||
70 | */ | ||
71 | public function testAddLink() | ||
72 | { | ||
73 | $history = new History(self::$historyFilePath); | ||
74 | $history->addLink(['id' => 0]); | ||
75 | $actual = $history->getHistory()[0]; | ||
76 | $this->assertEquals(History::CREATED, $actual['event']); | ||
77 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
78 | $this->assertEquals(0, $actual['id']); | ||
79 | |||
80 | $history = new History(self::$historyFilePath); | ||
81 | $history->addLink(['id' => 1]); | ||
82 | $actual = $history->getHistory()[0]; | ||
83 | $this->assertEquals(History::CREATED, $actual['event']); | ||
84 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
85 | $this->assertEquals(1, $actual['id']); | ||
86 | |||
87 | $history = new History(self::$historyFilePath); | ||
88 | $history->addLink(['id' => 'str']); | ||
89 | $actual = $history->getHistory()[0]; | ||
90 | $this->assertEquals(History::CREATED, $actual['event']); | ||
91 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
92 | $this->assertEquals('str', $actual['id']); | ||
93 | } | ||
94 | |||
95 | /** | ||
96 | * Test updated link event | ||
97 | */ | ||
98 | public function testUpdateLink() | ||
99 | { | ||
100 | $history = new History(self::$historyFilePath); | ||
101 | $history->updateLink(['id' => 1]); | ||
102 | $actual = $history->getHistory()[0]; | ||
103 | $this->assertEquals(History::UPDATED, $actual['event']); | ||
104 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
105 | $this->assertEquals(1, $actual['id']); | ||
106 | } | ||
107 | |||
108 | /** | ||
109 | * Test delete link event | ||
110 | */ | ||
111 | public function testDeleteLink() | ||
112 | { | ||
113 | $history = new History(self::$historyFilePath); | ||
114 | $history->deleteLink(['id' => 1]); | ||
115 | $actual = $history->getHistory()[0]; | ||
116 | $this->assertEquals(History::DELETED, $actual['event']); | ||
117 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
118 | $this->assertEquals(1, $actual['id']); | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * Test updated settings event | ||
123 | */ | ||
124 | public function testUpdateSettings() | ||
125 | { | ||
126 | $history = new History(self::$historyFilePath); | ||
127 | $history->updateSettings(); | ||
128 | $actual = $history->getHistory()[0]; | ||
129 | $this->assertEquals(History::SETTINGS, $actual['event']); | ||
130 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
131 | $this->assertEmpty($actual['id']); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Make sure that new items are stored at the beginning | ||
136 | */ | ||
137 | public function testHistoryOrder() | ||
138 | { | ||
139 | $history = new History(self::$historyFilePath); | ||
140 | $history->updateLink(['id' => 1]); | ||
141 | $actual = $history->getHistory()[0]; | ||
142 | $this->assertEquals(History::UPDATED, $actual['event']); | ||
143 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
144 | $this->assertEquals(1, $actual['id']); | ||
145 | |||
146 | $history->addLink(['id' => 1]); | ||
147 | $actual = $history->getHistory()[0]; | ||
148 | $this->assertEquals(History::CREATED, $actual['event']); | ||
149 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
150 | $this->assertEquals(1, $actual['id']); | ||
151 | } | ||
152 | |||
153 | /** | ||
154 | * Re-read history from file after writing an event | ||
155 | */ | ||
156 | public function testHistoryRead() | ||
157 | { | ||
158 | $history = new History(self::$historyFilePath); | ||
159 | $history->updateLink(['id' => 1]); | ||
160 | $history = new History(self::$historyFilePath); | ||
161 | $actual = $history->getHistory()[0]; | ||
162 | $this->assertEquals(History::UPDATED, $actual['event']); | ||
163 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
164 | $this->assertEquals(1, $actual['id']); | ||
165 | } | ||
166 | |||
167 | /** | ||
168 | * Re-read history from file after writing an event and make sure that the order is correct | ||
169 | */ | ||
170 | public function testHistoryOrderRead() | ||
171 | { | ||
172 | $history = new History(self::$historyFilePath); | ||
173 | $history->updateLink(['id' => 1]); | ||
174 | $history->addLink(['id' => 1]); | ||
175 | |||
176 | $history = new History(self::$historyFilePath); | ||
177 | $actual = $history->getHistory()[0]; | ||
178 | $this->assertEquals(History::CREATED, $actual['event']); | ||
179 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
180 | $this->assertEquals(1, $actual['id']); | ||
181 | |||
182 | $actual = $history->getHistory()[1]; | ||
183 | $this->assertEquals(History::UPDATED, $actual['event']); | ||
184 | $this->assertTrue(new DateTime('-2 seconds') < DateTime::createFromFormat(DateTime::ATOM, $actual['datetime'])); | ||
185 | $this->assertEquals(1, $actual['id']); | ||
186 | } | ||
187 | |||
188 | /** | ||
189 | * Test retention time: delete old entries. | ||
190 | */ | ||
191 | public function testHistoryRententionTime() | ||
192 | { | ||
193 | $history = new History(self::$historyFilePath, 5); | ||
194 | $history->updateLink(['id' => 1]); | ||
195 | $this->assertEquals(1, count($history->getHistory())); | ||
196 | $arr = $history->getHistory(); | ||
197 | $arr[0]['datetime'] = (new DateTime('-1 hour'))->format(DateTime::ATOM); | ||
198 | FileUtils::writeFlatDB(self::$historyFilePath, $arr); | ||
199 | |||
200 | $history = new History(self::$historyFilePath, 60); | ||
201 | $this->assertEquals(1, count($history->getHistory())); | ||
202 | $this->assertEquals(1, $history->getHistory()[0]['id']); | ||
203 | $history->updateLink(['id' => 2]); | ||
204 | $this->assertEquals(1, count($history->getHistory())); | ||
205 | $this->assertEquals(2, $history->getHistory()[0]['id']); | ||
206 | } | ||
207 | } | ||