aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/HistoryTest.php
blob: 6dc0e5b7aa112e19f3dd7cbc6f820356885f3088 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php

namespace Shaarli;

use DateTime;
use Shaarli\Bookmark\Bookmark;

class HistoryTest extends \Shaarli\TestCase
{
    /**
     * @var string History file path
     */
    protected static $historyFilePath = 'sandbox/history.php';

    /**
     * Delete history file.
     */
    protected function setUp(): void
    {
        if (file_exists(self::$historyFilePath)) {
            unlink(self::$historyFilePath);
        }
    }

    /**
     * Test that the history file is created if it doesn't exist.
     */
    public function testConstructLazyLoading()
    {
        new History(self::$historyFilePath);
        $this->assertFileNotExists(self::$historyFilePath);
    }

    /**
     * Test that the history file is created if it doesn't exist.
     */
    public function testAddEventCreateFile()
    {
        $history = new History(self::$historyFilePath);
        $history->updateSettings();
        $this->assertFileExists(self::$historyFilePath);
    }

    /**
     * Not writable history file: raise an exception.
     */
    public function testConstructNotWritable()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('History file isn\'t readable or writable');

        touch(self::$historyFilePath);
        chmod(self::$historyFilePath, 0440);
        $history = new History(self::$historyFilePath);
        $history->updateSettings();
    }

    /**
     * Not parsable history file: raise an exception.
     */
    public function testConstructNotParsable()
    {
        $this->expectException(\Exception::class);
        $this->expectExceptionMessageRegExp('/Could not parse history file/');

        file_put_contents(self::$historyFilePath, 'not parsable');
        $history = new History(self::$historyFilePath);
        // gzinflate generates a warning
        @$history->updateSettings();
    }

    /**
     * Test add link event
     */
    public function testAddLink()
    {
        $history = new History(self::$historyFilePath);
        $bookmark = (new Bookmark())->setId(0);
        $history->addLink($bookmark);
        $actual = $history->getHistory()[0];
        $this->assertEquals(History::CREATED, $actual['event']);
        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
        $this->assertEquals(0, $actual['id']);

        $history = new History(self::$historyFilePath);
        $bookmark = (new Bookmark())->setId(1);
        $history->addLink($bookmark);
        $actual = $history->getHistory()[0];
        $this->assertEquals(History::CREATED, $actual['event']);
        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
        $this->assertEquals(1, $actual['id']);

        $history = new History(self::$historyFilePath);
        $bookmark = (new Bookmark())->setId('str');
        $history->addLink($bookmark);
        $actual = $history->getHistory()[0];
        $this->assertEquals(History::CREATED, $actual['event']);
        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
        $this->assertEquals('str', $actual['id']);
    }

//    /**
//     * Test updated link event
//     */
//    public function testUpdateLink()
//    {
//        $history = new History(self::$historyFilePath);
//        $history->updateLink(['id' => 1]);
//        $actual = $history->getHistory()[0];
//        $this->assertEquals(History::UPDATED, $actual['event']);
//        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
//        $this->assertEquals(1, $actual['id']);
//    }
//
//    /**
//     * Test delete link event
//     */
//    public function testDeleteLink()
//    {
//        $history = new History(self::$historyFilePath);
//        $history->deleteLink(['id' => 1]);
//        $actual = $history->getHistory()[0];
//        $this->assertEquals(History::DELETED, $actual['event']);
//        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
//        $this->assertEquals(1, $actual['id']);
//    }
//
//    /**
//     * Test updated settings event
//     */
//    public function testUpdateSettings()
//    {
//        $history = new History(self::$historyFilePath);
//        $history->updateSettings();
//        $actual = $history->getHistory()[0];
//        $this->assertEquals(History::SETTINGS, $actual['event']);
//        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
//        $this->assertEmpty($actual['id']);
//    }
//
//    /**
//     * Make sure that new items are stored at the beginning
//     */
//    public function testHistoryOrder()
//    {
//        $history = new History(self::$historyFilePath);
//        $history->updateLink(['id' => 1]);
//        $actual = $history->getHistory()[0];
//        $this->assertEquals(History::UPDATED, $actual['event']);
//        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
//        $this->assertEquals(1, $actual['id']);
//
//        $history->addLink(['id' => 1]);
//        $actual = $history->getHistory()[0];
//        $this->assertEquals(History::CREATED, $actual['event']);
//        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
//        $this->assertEquals(1, $actual['id']);
//    }
//
//    /**
//     * Re-read history from file after writing an event
//     */
//    public function testHistoryRead()
//    {
//        $history = new History(self::$historyFilePath);
//        $history->updateLink(['id' => 1]);
//        $history = new History(self::$historyFilePath);
//        $actual = $history->getHistory()[0];
//        $this->assertEquals(History::UPDATED, $actual['event']);
//        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
//        $this->assertEquals(1, $actual['id']);
//    }
//
//    /**
//     * Re-read history from file after writing an event and make sure that the order is correct
//     */
//    public function testHistoryOrderRead()
//    {
//        $history = new History(self::$historyFilePath);
//        $history->updateLink(['id' => 1]);
//        $history->addLink(['id' => 1]);
//
//        $history = new History(self::$historyFilePath);
//        $actual = $history->getHistory()[0];
//        $this->assertEquals(History::CREATED, $actual['event']);
//        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
//        $this->assertEquals(1, $actual['id']);
//
//        $actual = $history->getHistory()[1];
//        $this->assertEquals(History::UPDATED, $actual['event']);
//        $this->assertTrue(new DateTime('-2 seconds') < $actual['datetime']);
//        $this->assertEquals(1, $actual['id']);
//    }
//
//    /**
//     * Test retention time: delete old entries.
//     */
//    public function testHistoryRententionTime()
//    {
//        $history = new History(self::$historyFilePath, 5);
//        $history->updateLink(['id' => 1]);
//        $this->assertEquals(1, count($history->getHistory()));
//        $arr = $history->getHistory();
//        $arr[0]['datetime'] = new DateTime('-1 hour');
//        FileUtils::writeFlatDB(self::$historyFilePath, $arr);
//
//        $history = new History(self::$historyFilePath, 60);
//        $this->assertEquals(1, count($history->getHistory()));
//        $this->assertEquals(1, $history->getHistory()[0]['id']);
//        $history->updateLink(['id' => 2]);
//        $this->assertEquals(1, count($history->getHistory()));
//        $this->assertEquals(2, $history->getHistory()[0]['id']);
//    }
}