1<?php
2
3/**
4 * General tests for the cleanoldips plugin
5 *
6 * @group plugin_cleanoldips
7 * @group plugins
8 */
9class cleaning_plugin_cleanoldips_test extends DokuWikiTest
10{
11    protected $pluginsEnabled = ['cleanoldips'];
12
13    protected $yesterday;
14
15    public function setUp()
16    {
17        parent::setUp();
18
19        global $ID;
20        $ID = 'some:page';
21        $changelogFN = metaFN($ID, '.changes');
22        io_makeFileDir($changelogFN);
23        $this->yesterday = time() - 60 * 60 * 24;
24        file_put_contents($changelogFN, '1522767335	192.168.0.105	C	sidebar		created		36
251522767349	192.168.0.105	E	sidebar				12
261523956708	192.168.0.105	E	sidebar	admin			23
271524145287	192.168.0.105	E	sidebar	admin			19
281524464616	192.168.0.105	E	sidebar	admin	ok		0
29');
30        $handle = fopen($changelogFN, 'ab');
31        $recentChangelogLine = $this->yesterday . "	192.168.0.105	E	sidebar	admin	ok		0\n";
32        fwrite($handle, $recentChangelogLine);
33        fclose($handle);
34    }
35
36    public function testCleaningChangelog()
37    {
38        global $ID;
39        trigger_event('INDEXER_TASKS_RUN', $data);
40        $actualChangelogContent = file_get_contents(metaFN($ID, '.changes'));
41
42        $expectedChangelogContent = '1522767335	             	C	sidebar		created		36
431522767349	             	E	sidebar				12
441523956708	             	E	sidebar	admin			23
451524145287	             	E	sidebar	admin			19
461524464616	             	E	sidebar	admin	ok		0
47' . $this->yesterday . "	192.168.0.105	E	sidebar	admin	ok		0\n";
48        $this->assertEquals($expectedChangelogContent, $actualChangelogContent);
49
50    }
51
52    public function dataProvider_validateStartPosition()
53    {
54        return [
55            [
56                0,
57                0,
58                'Startposition 0 should remain 0',
59            ],
60            [
61                48,
62                48,
63                'valid start position should remain unchanged',
64            ],
65            [
66                55,
67                0,
68                'Startposition in the middle of a string should become 0',
69            ],
70            [
71                350,
72                0,
73                'Startposition that is outside the file should become 0',
74            ],
75        ];
76    }
77
78    /**
79     * @dataProvider dataProvider_validateStartPosition
80     *
81     * @param $inputStartPosition
82     * @param $expectedStartPosition
83     * @param $msg
84     */
85    public function test_validateStartPosition($inputStartPosition, $expectedStartPosition, $msg)
86    {
87        global $ID;
88        /** @var action_plugin_cleanoldips $changelogCleaner */
89        $changelogCleaner = plugin_load('action', 'cleanoldips');
90
91        $actualStartPosition = $changelogCleaner->validateStartPosition($inputStartPosition, metaFN($ID, '.changes'));
92
93        $this->assertEquals($expectedStartPosition, $actualStartPosition, $msg);
94    }
95
96}
97