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