xref: /plugin/statistics/_test/LoggerTest.php (revision 41d1fffc4a3b58bed7f96d983ba8317fe9e225a5)
15cc1319aSAndreas Gohr<?php
25cc1319aSAndreas Gohr
35cc1319aSAndreas Gohrnamespace dokuwiki\plugin\statistics\test;
45cc1319aSAndreas Gohr
5696a1b1bSAndreas Gohruse dokuwiki\plugin\statistics\Logger;
6*41d1fffcSAndreas Gohruse DokuWikiTest;
7696a1b1bSAndreas Gohruse helper_plugin_statistics;
85cc1319aSAndreas Gohr
95cc1319aSAndreas Gohr/**
10696a1b1bSAndreas Gohr * Tests for the statistics plugin Logger class
115cc1319aSAndreas Gohr *
125cc1319aSAndreas Gohr * @group plugin_statistics
135cc1319aSAndreas Gohr * @group plugins
145cc1319aSAndreas Gohr */
155cc1319aSAndreas Gohrclass LoggerTest extends DokuWikiTest
165cc1319aSAndreas Gohr{
17de1daf8cSAndreas Gohr    protected $pluginsEnabled = ['statistics', 'sqlite'];
18de1daf8cSAndreas Gohr
19696a1b1bSAndreas Gohr    /** @var helper_plugin_statistics */
20696a1b1bSAndreas Gohr    protected $helper;
215cc1319aSAndreas Gohr
22*41d1fffcSAndreas Gohr    const SESSION_ID = 'test-session-12345';
23*41d1fffcSAndreas Gohr    const USER_ID = 'test-uid-12345';
24*41d1fffcSAndreas Gohr    const USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36';
25696a1b1bSAndreas Gohr
26696a1b1bSAndreas Gohr    public function setUp(): void
27696a1b1bSAndreas Gohr    {
28696a1b1bSAndreas Gohr        parent::setUp();
29696a1b1bSAndreas Gohr
30696a1b1bSAndreas Gohr        // Load the helper plugin
31696a1b1bSAndreas Gohr        $this->helper = plugin_load('helper', 'statistics');
32696a1b1bSAndreas Gohr
33*41d1fffcSAndreas Gohr        // set default user agent
34*41d1fffcSAndreas Gohr        $_SERVER['HTTP_USER_AGENT'] = self::USER_AGENT;
35696a1b1bSAndreas Gohr
36*41d1fffcSAndreas Gohr        // Set up session data that Logger expects
37*41d1fffcSAndreas Gohr        $_SESSION[DOKU_COOKIE]['statistics']['uid'] = self::USER_ID;
38*41d1fffcSAndreas Gohr        $_SESSION[DOKU_COOKIE]['statistics']['id'] = self::SESSION_ID;
39696a1b1bSAndreas Gohr    }
40696a1b1bSAndreas Gohr
41696a1b1bSAndreas Gohr    public function tearDown(): void
42696a1b1bSAndreas Gohr    {
43696a1b1bSAndreas Gohr        unset($_SERVER['HTTP_USER_AGENT']);
44*41d1fffcSAndreas Gohr        unset($_SESSION[DOKU_COOKIE]['statistics']);
45696a1b1bSAndreas Gohr        parent::tearDown();
46696a1b1bSAndreas Gohr    }
47696a1b1bSAndreas Gohr
48696a1b1bSAndreas Gohr    /**
49696a1b1bSAndreas Gohr     * Test constructor initializes properties correctly
50696a1b1bSAndreas Gohr     */
51696a1b1bSAndreas Gohr    public function testConstructor()
52696a1b1bSAndreas Gohr    {
53*41d1fffcSAndreas Gohr        $this->assertInstanceOf(Logger::class, $this->helper->getLogger());
54696a1b1bSAndreas Gohr
55696a1b1bSAndreas Gohr        // Test that bot user agents throw exception
56696a1b1bSAndreas Gohr        $_SERVER['HTTP_USER_AGENT'] = 'Googlebot/2.1 (+http://www.google.com/bot.html)';
57696a1b1bSAndreas Gohr
58*41d1fffcSAndreas Gohr        $this->expectException(\dokuwiki\plugin\statistics\IgnoreException::class);
59696a1b1bSAndreas Gohr        $this->expectExceptionMessage('Bot detected, not logging');
60696a1b1bSAndreas Gohr        new Logger($this->helper);
61696a1b1bSAndreas Gohr    }
62696a1b1bSAndreas Gohr
63696a1b1bSAndreas Gohr    /**
64696a1b1bSAndreas Gohr     * Test begin and end transaction methods
65696a1b1bSAndreas Gohr     */
66696a1b1bSAndreas Gohr    public function testBeginEnd()
67696a1b1bSAndreas Gohr    {
68*41d1fffcSAndreas Gohr        $this->helper->getLogger()->begin();
69696a1b1bSAndreas Gohr
70696a1b1bSAndreas Gohr        // Verify transaction is active by checking PDO
71696a1b1bSAndreas Gohr        $pdo = $this->helper->getDB()->getPdo();
72696a1b1bSAndreas Gohr        $this->assertTrue($pdo->inTransaction());
73696a1b1bSAndreas Gohr
74*41d1fffcSAndreas Gohr        $this->helper->getLogger()->end();
75696a1b1bSAndreas Gohr
76696a1b1bSAndreas Gohr        // Verify transaction is committed
77696a1b1bSAndreas Gohr        $this->assertFalse($pdo->inTransaction());
78696a1b1bSAndreas Gohr    }
79696a1b1bSAndreas Gohr
80696a1b1bSAndreas Gohr    /**
81*41d1fffcSAndreas Gohr     * Test user logging
82696a1b1bSAndreas Gohr     */
83*41d1fffcSAndreas Gohr    public function testLogUser()
84696a1b1bSAndreas Gohr    {
85696a1b1bSAndreas Gohr        // Test with no user (should not log)
86*41d1fffcSAndreas Gohr        $_SERVER['REMOTE_USER'] = '';
87*41d1fffcSAndreas Gohr        $this->helper->getLogger()->begin();
88*41d1fffcSAndreas Gohr        $this->helper->getLogger()->end();
89696a1b1bSAndreas Gohr
90*41d1fffcSAndreas Gohr        $count = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM users');
91696a1b1bSAndreas Gohr        $this->assertEquals(0, $count);
92696a1b1bSAndreas Gohr
93696a1b1bSAndreas Gohr        // Test with user
94*41d1fffcSAndreas Gohr        $_SERVER['REMOTE_USER'] = 'testuser';
95*41d1fffcSAndreas Gohr        $this->helper->getLogger()->begin();
96*41d1fffcSAndreas Gohr        $this->helper->getLogger()->end();
97696a1b1bSAndreas Gohr
98*41d1fffcSAndreas Gohr        $count = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM users');
99696a1b1bSAndreas Gohr        $this->assertEquals(1, $count);
100696a1b1bSAndreas Gohr
101*41d1fffcSAndreas Gohr        $user = $this->helper->getDB()->queryValue('SELECT user FROM users WHERE user = ?', ['testuser']);
102696a1b1bSAndreas Gohr        $this->assertEquals('testuser', $user);
103696a1b1bSAndreas Gohr    }
104696a1b1bSAndreas Gohr
105696a1b1bSAndreas Gohr    /**
106696a1b1bSAndreas Gohr     * Data provider for logGroups test
107696a1b1bSAndreas Gohr     */
108696a1b1bSAndreas Gohr    public function logGroupsProvider()
109696a1b1bSAndreas Gohr    {
110696a1b1bSAndreas Gohr        return [
111*41d1fffcSAndreas Gohr            'empty groups' => [[], 0],
112*41d1fffcSAndreas Gohr            'single group' => [['admin'], 1],
113*41d1fffcSAndreas Gohr            'multiple groups' => [['admin', 'user'], 2],
114*41d1fffcSAndreas Gohr            'filtered groups' => [['admin', 'nonexistent'], 2], // all groups are logged
115696a1b1bSAndreas Gohr        ];
116696a1b1bSAndreas Gohr    }
117696a1b1bSAndreas Gohr
118696a1b1bSAndreas Gohr    /**
119696a1b1bSAndreas Gohr     * Test logGroups method
120696a1b1bSAndreas Gohr     * @dataProvider logGroupsProvider
121696a1b1bSAndreas Gohr     */
122*41d1fffcSAndreas Gohr    public function testLogGroups($groups, $expectedCount)
123696a1b1bSAndreas Gohr    {
124*41d1fffcSAndreas Gohr        global $USERINFO;
125696a1b1bSAndreas Gohr
126*41d1fffcSAndreas Gohr        // Set up a test user and groups
127*41d1fffcSAndreas Gohr        $_SERVER['REMOTE_USER'] = 'testuser';
128*41d1fffcSAndreas Gohr        $USERINFO = ['grps' => $groups];
12923e0cc03SAndreas Gohr
130696a1b1bSAndreas Gohr
131*41d1fffcSAndreas Gohr        $this->helper->getLogger()->begin();
132*41d1fffcSAndreas Gohr        $this->helper->getLogger()->end();
133*41d1fffcSAndreas Gohr
134*41d1fffcSAndreas Gohr        $count = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM groups WHERE user = ?', ['testuser']);
135696a1b1bSAndreas Gohr        $this->assertEquals($expectedCount, $count);
136696a1b1bSAndreas Gohr
137696a1b1bSAndreas Gohr        if ($expectedCount > 0) {
138*41d1fffcSAndreas Gohr            $loggedGroups = $this->helper->getDB()->queryAll('SELECT `group` FROM groups WHERE user = ?', ['testuser']);
139696a1b1bSAndreas Gohr            $this->assertCount($expectedCount, $loggedGroups);
140696a1b1bSAndreas Gohr        }
141696a1b1bSAndreas Gohr    }
142696a1b1bSAndreas Gohr
143696a1b1bSAndreas Gohr    /**
144*41d1fffcSAndreas Gohr     * Data provider fortestLogReferer test
145696a1b1bSAndreas Gohr     */
146*41d1fffcSAndreas Gohr    public function logRefererProvider()
147696a1b1bSAndreas Gohr    {
148696a1b1bSAndreas Gohr        return [
149696a1b1bSAndreas Gohr            'google search' => [
150696a1b1bSAndreas Gohr                'https://www.google.com/search?q=dokuwiki+test',
151de1daf8cSAndreas Gohr                'google'
152696a1b1bSAndreas Gohr            ],
153696a1b1bSAndreas Gohr            'non-search referer' => [
154696a1b1bSAndreas Gohr                'https://example.com/page',
155696a1b1bSAndreas Gohr                null,
156696a1b1bSAndreas Gohr            ],
157696a1b1bSAndreas Gohr        ];
158696a1b1bSAndreas Gohr    }
159696a1b1bSAndreas Gohr
160696a1b1bSAndreas Gohr    /**
161*41d1fffcSAndreas Gohr     * Test logReferer method
162*41d1fffcSAndreas Gohr     * @dataProvider logRefererProvider
163696a1b1bSAndreas Gohr     */
164*41d1fffcSAndreas Gohr    public function testLogReferer($referer, $expectedEngine)
165696a1b1bSAndreas Gohr    {
166*41d1fffcSAndreas Gohr        $refId = $this->helper->getLogger()->logReferer($referer);
167*41d1fffcSAndreas Gohr        $this->assertNotNull($refId);
168*41d1fffcSAndreas Gohr        $refererRecord = $this->helper->getDB()->queryRecord('SELECT * FROM referers WHERE id = ?', [$refId]);
169*41d1fffcSAndreas Gohr        $this->assertEquals($expectedEngine, $refererRecord['engine']);
170696a1b1bSAndreas Gohr    }
171696a1b1bSAndreas Gohr
172696a1b1bSAndreas Gohr    /**
173696a1b1bSAndreas Gohr     * Test logSearch method
174696a1b1bSAndreas Gohr     */
175696a1b1bSAndreas Gohr    public function testLogSearch()
176696a1b1bSAndreas Gohr    {
177696a1b1bSAndreas Gohr        $query = 'test search query';
178696a1b1bSAndreas Gohr        $words = ['test', 'search', 'query'];
179696a1b1bSAndreas Gohr
180*41d1fffcSAndreas Gohr        $this->helper->getLogger()->logSearch($query, $words);
181696a1b1bSAndreas Gohr
182696a1b1bSAndreas Gohr        // Check search table
183696a1b1bSAndreas Gohr        $search = $this->helper->getDB()->queryRecord('SELECT * FROM search ORDER BY dt DESC LIMIT 1');
184696a1b1bSAndreas Gohr        $this->assertEquals($query, $search['query']);
185696a1b1bSAndreas Gohr
186696a1b1bSAndreas Gohr        // Check searchwords table
187de1daf8cSAndreas Gohr        $wordCount = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM searchwords WHERE sid = ?', [$search['id']]);
188696a1b1bSAndreas Gohr        $this->assertEquals(3, $wordCount);
189696a1b1bSAndreas Gohr
190de1daf8cSAndreas Gohr        $loggedWords = $this->helper->getDB()->queryAll('SELECT word FROM searchwords WHERE sid = ? ORDER BY word', [$search['id']]);
191696a1b1bSAndreas Gohr        $this->assertEquals(['query', 'search', 'test'], array_column($loggedWords, 'word'));
192696a1b1bSAndreas Gohr    }
193696a1b1bSAndreas Gohr
194696a1b1bSAndreas Gohr    /**
195696a1b1bSAndreas Gohr     * Test logSession method
196696a1b1bSAndreas Gohr     */
197696a1b1bSAndreas Gohr    public function testLogSession()
198696a1b1bSAndreas Gohr    {
199*41d1fffcSAndreas Gohr        $_SERVER['REMOTE_USER'] = 'testuser';
200696a1b1bSAndreas Gohr
201*41d1fffcSAndreas Gohr        // Test session creation
202*41d1fffcSAndreas Gohr        $logger = $this->helper->getLogger();
203*41d1fffcSAndreas Gohr
204*41d1fffcSAndreas Gohr        $logger->begin();
205*41d1fffcSAndreas Gohr        $logger->end();
206*41d1fffcSAndreas Gohr
207*41d1fffcSAndreas Gohr        $sessionCount = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM sessions');
208696a1b1bSAndreas Gohr        $this->assertEquals(1, $sessionCount);
209696a1b1bSAndreas Gohr
210*41d1fffcSAndreas Gohr        $session = $this->helper->getDB()->queryRecord('SELECT * FROM sessions LIMIT 1');
211*41d1fffcSAndreas Gohr        $this->assertIsArray($session);
212*41d1fffcSAndreas Gohr        $this->assertEquals('testuser', $session['user']);
213*41d1fffcSAndreas Gohr        $this->assertEquals(self::SESSION_ID, $session['session']);
214*41d1fffcSAndreas Gohr        $this->assertEquals(self::USER_ID, $session['uid']);
215*41d1fffcSAndreas Gohr        $this->assertEquals(self::USER_AGENT, $session['ua']);
216*41d1fffcSAndreas Gohr        $this->assertEquals('Chrome', $session['ua_info']);
217*41d1fffcSAndreas Gohr        $this->assertEquals('browser', $session['ua_type']);
218*41d1fffcSAndreas Gohr        $this->assertEquals('91', $session['ua_ver']);
219*41d1fffcSAndreas Gohr        $this->assertEquals('Windows', $session['os']);
220696a1b1bSAndreas Gohr
221696a1b1bSAndreas Gohr    }
222696a1b1bSAndreas Gohr
223696a1b1bSAndreas Gohr    /**
224696a1b1bSAndreas Gohr     * Test logIp method
225696a1b1bSAndreas Gohr     */
226696a1b1bSAndreas Gohr    public function testLogIp()
227696a1b1bSAndreas Gohr    {
228696a1b1bSAndreas Gohr        $ip = '8.8.8.8';
229*41d1fffcSAndreas Gohr        $_SERVER['REMOTE_ADDR'] = $ip;
230696a1b1bSAndreas Gohr
231c7cad24dSAndreas Gohr        // Create a mock HTTP client
232c7cad24dSAndreas Gohr        $mockHttpClient = $this->createMock(\dokuwiki\HTTP\DokuHTTPClient::class);
2336c24c4b8SAndreas Gohr
234c7cad24dSAndreas Gohr        // Mock the API response
235c7cad24dSAndreas Gohr        $mockResponse = json_encode([
236c7cad24dSAndreas Gohr            'status' => 'success',
237c7cad24dSAndreas Gohr            'country' => 'United States',
238c7cad24dSAndreas Gohr            'countryCode' => 'US',
239c7cad24dSAndreas Gohr            'city' => 'Ashburn',
240c7cad24dSAndreas Gohr            'query' => $ip
241c7cad24dSAndreas Gohr        ]);
2426c24c4b8SAndreas Gohr
243c7cad24dSAndreas Gohr        $mockHttpClient->expects($this->once())
244c7cad24dSAndreas Gohr            ->method('get')
245c7cad24dSAndreas Gohr            ->with('http://ip-api.com/json/' . $ip)
246c7cad24dSAndreas Gohr            ->willReturn($mockResponse);
247696a1b1bSAndreas Gohr
248c7cad24dSAndreas Gohr        // Set timeout property
249c7cad24dSAndreas Gohr        $mockHttpClient->timeout = 10;
250c7cad24dSAndreas Gohr
251c7cad24dSAndreas Gohr        // Create logger with mock HTTP client
252c7cad24dSAndreas Gohr        $logger = new Logger($this->helper, $mockHttpClient);
253c7cad24dSAndreas Gohr
254c7cad24dSAndreas Gohr        // Test with IP that doesn't exist in database
255*41d1fffcSAndreas Gohr        $logger->logIp();
256c7cad24dSAndreas Gohr
257c7cad24dSAndreas Gohr        // Verify the IP was logged
258c7cad24dSAndreas Gohr        $ipRecord = $this->helper->getDB()->queryRecord('SELECT * FROM iplocation WHERE ip = ?', [$ip]);
259c7cad24dSAndreas Gohr        $this->assertNotNull($ipRecord);
260c7cad24dSAndreas Gohr        $this->assertEquals($ip, $ipRecord['ip']);
261c7cad24dSAndreas Gohr        $this->assertEquals('United States', $ipRecord['country']);
262c7cad24dSAndreas Gohr        $this->assertEquals('US', $ipRecord['code']);
263c7cad24dSAndreas Gohr        $this->assertEquals('Ashburn', $ipRecord['city']);
264c7cad24dSAndreas Gohr        $this->assertNotEmpty($ipRecord['host']); // gethostbyaddr result
265c7cad24dSAndreas Gohr
266c7cad24dSAndreas Gohr        // Test with IP that already exists and is recent (should not make HTTP call)
267c7cad24dSAndreas Gohr        $mockHttpClient2 = $this->createMock(\dokuwiki\HTTP\DokuHTTPClient::class);
268c7cad24dSAndreas Gohr        $mockHttpClient2->expects($this->never())->method('get');
269c7cad24dSAndreas Gohr
270c7cad24dSAndreas Gohr        $logger2 = new Logger($this->helper, $mockHttpClient2);
271*41d1fffcSAndreas Gohr        $logger2->logIp(); // Should not trigger HTTP call
272696a1b1bSAndreas Gohr    }
273696a1b1bSAndreas Gohr
274696a1b1bSAndreas Gohr    /**
275696a1b1bSAndreas Gohr     * Test logOutgoing method
276696a1b1bSAndreas Gohr     */
277696a1b1bSAndreas Gohr    public function testLogOutgoing()
278696a1b1bSAndreas Gohr    {
279696a1b1bSAndreas Gohr        global $INPUT;
280696a1b1bSAndreas Gohr
281696a1b1bSAndreas Gohr        // Test without outgoing link
282696a1b1bSAndreas Gohr        $INPUT->set('ol', '');
283*41d1fffcSAndreas Gohr        $this->helper->getLogger()->logOutgoing();
284696a1b1bSAndreas Gohr
285696a1b1bSAndreas Gohr        $count = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM outlinks');
286696a1b1bSAndreas Gohr        $this->assertEquals(0, $count);
287696a1b1bSAndreas Gohr
288696a1b1bSAndreas Gohr        // Test with outgoing link
289696a1b1bSAndreas Gohr        $link = 'https://example.com';
290696a1b1bSAndreas Gohr        $page = 'test:page';
291696a1b1bSAndreas Gohr        $INPUT->set('ol', $link);
292696a1b1bSAndreas Gohr        $INPUT->set('p', $page);
293696a1b1bSAndreas Gohr
294*41d1fffcSAndreas Gohr        $this->helper->getLogger()->logOutgoing();
295696a1b1bSAndreas Gohr
296696a1b1bSAndreas Gohr        $count = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM outlinks');
297696a1b1bSAndreas Gohr        $this->assertEquals(1, $count);
298696a1b1bSAndreas Gohr
299696a1b1bSAndreas Gohr        $outlink = $this->helper->getDB()->queryRecord('SELECT * FROM outlinks ORDER BY dt DESC LIMIT 1');
300696a1b1bSAndreas Gohr        $this->assertEquals($link, $outlink['link']);
301696a1b1bSAndreas Gohr        $this->assertEquals($page, $outlink['page']);
302696a1b1bSAndreas Gohr    }
303696a1b1bSAndreas Gohr
304696a1b1bSAndreas Gohr    /**
305*41d1fffcSAndreas Gohr     * Test logPageView method
306696a1b1bSAndreas Gohr     */
307*41d1fffcSAndreas Gohr    public function testLogPageView()
308696a1b1bSAndreas Gohr    {
309de1daf8cSAndreas Gohr        global $INPUT, $USERINFO, $conf;
310de1daf8cSAndreas Gohr
311de1daf8cSAndreas Gohr        $conf['plugin']['statistics']['loggroups'] = ['admin', 'user'];
312696a1b1bSAndreas Gohr
313696a1b1bSAndreas Gohr        $page = 'test:page';
314696a1b1bSAndreas Gohr        $referer = 'https://example.com';
315696a1b1bSAndreas Gohr        $user = 'testuser';
316696a1b1bSAndreas Gohr
317696a1b1bSAndreas Gohr        $INPUT->set('p', $page);
318696a1b1bSAndreas Gohr        $INPUT->set('r', $referer);
319696a1b1bSAndreas Gohr        $INPUT->set('sx', 1920);
320696a1b1bSAndreas Gohr        $INPUT->set('sy', 1080);
321696a1b1bSAndreas Gohr        $INPUT->set('vx', 1200);
322696a1b1bSAndreas Gohr        $INPUT->set('vy', 800);
323696a1b1bSAndreas Gohr        $INPUT->server->set('REMOTE_USER', $user);
324696a1b1bSAndreas Gohr
325696a1b1bSAndreas Gohr        $USERINFO = ['grps' => ['admin', 'user']];
326696a1b1bSAndreas Gohr
327*41d1fffcSAndreas Gohr        $logger = $this->helper->getLogger();
328*41d1fffcSAndreas Gohr        $logger->begin();
329*41d1fffcSAndreas Gohr        $logger->logPageView();
330*41d1fffcSAndreas Gohr        $logger->end();
331696a1b1bSAndreas Gohr
332*41d1fffcSAndreas Gohr        // Check pageviews table
333*41d1fffcSAndreas Gohr        $pageview = $this->helper->getDB()->queryRecord('SELECT * FROM pageviews ORDER BY dt DESC LIMIT 1');
334*41d1fffcSAndreas Gohr        $this->assertEquals($page, $pageview['page']);
335*41d1fffcSAndreas Gohr        $this->assertEquals(1920, $pageview['screen_x']);
336*41d1fffcSAndreas Gohr        $this->assertEquals(1080, $pageview['screen_y']);
337*41d1fffcSAndreas Gohr        $this->assertEquals(1200, $pageview['view_x']);
338*41d1fffcSAndreas Gohr        $this->assertEquals(800, $pageview['view_y']);
339*41d1fffcSAndreas Gohr        $this->assertEquals(self::SESSION_ID, $pageview['session']);
340696a1b1bSAndreas Gohr    }
341696a1b1bSAndreas Gohr
342696a1b1bSAndreas Gohr    /**
343696a1b1bSAndreas Gohr     * Data provider for logMedia test
344696a1b1bSAndreas Gohr     */
345696a1b1bSAndreas Gohr    public function logMediaProvider()
346696a1b1bSAndreas Gohr    {
347696a1b1bSAndreas Gohr        return [
348696a1b1bSAndreas Gohr            'image inline' => ['test.jpg', 'image/jpeg', true, 1024],
349696a1b1bSAndreas Gohr            'video not inline' => ['test.mp4', 'video/mp4', false, 2048],
350696a1b1bSAndreas Gohr            'document' => ['test.pdf', 'application/pdf', false, 512],
351696a1b1bSAndreas Gohr        ];
352696a1b1bSAndreas Gohr    }
353696a1b1bSAndreas Gohr
354696a1b1bSAndreas Gohr    /**
355696a1b1bSAndreas Gohr     * Test logMedia method
356696a1b1bSAndreas Gohr     * @dataProvider logMediaProvider
357696a1b1bSAndreas Gohr     */
358696a1b1bSAndreas Gohr    public function testLogMedia($media, $mime, $inline, $size)
359696a1b1bSAndreas Gohr    {
360696a1b1bSAndreas Gohr        global $INPUT;
361696a1b1bSAndreas Gohr
362696a1b1bSAndreas Gohr        $user = 'testuser';
363696a1b1bSAndreas Gohr        $INPUT->server->set('REMOTE_USER', $user);
364696a1b1bSAndreas Gohr
365*41d1fffcSAndreas Gohr        $this->helper->getLogger()->logMedia($media, $mime, $inline, $size);
366696a1b1bSAndreas Gohr
367696a1b1bSAndreas Gohr        $mediaLog = $this->helper->getDB()->queryRecord('SELECT * FROM media ORDER BY dt DESC LIMIT 1');
368696a1b1bSAndreas Gohr        $this->assertEquals($media, $mediaLog['media']);
369696a1b1bSAndreas Gohr        $this->assertEquals($size, $mediaLog['size']);
370696a1b1bSAndreas Gohr        $this->assertEquals($inline ? 1 : 0, $mediaLog['inline']);
371696a1b1bSAndreas Gohr
372696a1b1bSAndreas Gohr        [$mime1, $mime2] = explode('/', strtolower($mime));
373696a1b1bSAndreas Gohr        $this->assertEquals($mime1, $mediaLog['mime1']);
374696a1b1bSAndreas Gohr        $this->assertEquals($mime2, $mediaLog['mime2']);
375696a1b1bSAndreas Gohr    }
376696a1b1bSAndreas Gohr
377696a1b1bSAndreas Gohr    /**
378696a1b1bSAndreas Gohr     * Data provider for logEdit test
379696a1b1bSAndreas Gohr     */
380696a1b1bSAndreas Gohr    public function logEditProvider()
381696a1b1bSAndreas Gohr    {
382696a1b1bSAndreas Gohr        return [
383696a1b1bSAndreas Gohr            'create page' => ['new:page', 'create'],
384696a1b1bSAndreas Gohr            'edit page' => ['existing:page', 'edit'],
385696a1b1bSAndreas Gohr            'delete page' => ['old:page', 'delete'],
386696a1b1bSAndreas Gohr        ];
387696a1b1bSAndreas Gohr    }
388696a1b1bSAndreas Gohr
389696a1b1bSAndreas Gohr    /**
390696a1b1bSAndreas Gohr     * Test logEdit method
391696a1b1bSAndreas Gohr     * @dataProvider logEditProvider
392696a1b1bSAndreas Gohr     */
393696a1b1bSAndreas Gohr    public function testLogEdit($page, $type)
394696a1b1bSAndreas Gohr    {
395*41d1fffcSAndreas Gohr        global $INPUT, $USERINFO;
396de1daf8cSAndreas Gohr
39723e0cc03SAndreas Gohr
398696a1b1bSAndreas Gohr        $user = 'testuser';
399696a1b1bSAndreas Gohr        $INPUT->server->set('REMOTE_USER', $user);
400696a1b1bSAndreas Gohr        $USERINFO = ['grps' => ['admin']];
401696a1b1bSAndreas Gohr
402*41d1fffcSAndreas Gohr        $this->helper->getLogger()->logEdit($page, $type);
403696a1b1bSAndreas Gohr
404696a1b1bSAndreas Gohr        // Check edits table
405696a1b1bSAndreas Gohr        $edit = $this->helper->getDB()->queryRecord('SELECT * FROM edits ORDER BY dt DESC LIMIT 1');
406696a1b1bSAndreas Gohr        $this->assertEquals($page, $edit['page']);
407696a1b1bSAndreas Gohr        $this->assertEquals($type, $edit['type']);
408696a1b1bSAndreas Gohr    }
409696a1b1bSAndreas Gohr
410696a1b1bSAndreas Gohr    /**
411696a1b1bSAndreas Gohr     * Data provider for logLogin test
412696a1b1bSAndreas Gohr     */
413696a1b1bSAndreas Gohr    public function logLoginProvider()
414696a1b1bSAndreas Gohr    {
415696a1b1bSAndreas Gohr        return [
416696a1b1bSAndreas Gohr            'login' => ['login', 'testuser'],
417696a1b1bSAndreas Gohr            'logout' => ['logout', 'testuser'],
418696a1b1bSAndreas Gohr            'create' => ['create', 'newuser'],
419696a1b1bSAndreas Gohr        ];
420696a1b1bSAndreas Gohr    }
421696a1b1bSAndreas Gohr
422696a1b1bSAndreas Gohr    /**
423696a1b1bSAndreas Gohr     * Test logLogin method
424696a1b1bSAndreas Gohr     * @dataProvider logLoginProvider
425696a1b1bSAndreas Gohr     */
426696a1b1bSAndreas Gohr    public function testLogLogin($type, $user)
427696a1b1bSAndreas Gohr    {
428*41d1fffcSAndreas Gohr        $this->helper->getLogger()->logLogin($type, $user);
429696a1b1bSAndreas Gohr        $login = $this->helper->getDB()->queryRecord('SELECT * FROM logins ORDER BY dt DESC LIMIT 1');
430696a1b1bSAndreas Gohr        $this->assertEquals($type, $login['type']);
431696a1b1bSAndreas Gohr        $this->assertEquals($user, $login['user']);
432696a1b1bSAndreas Gohr    }
433696a1b1bSAndreas Gohr
434696a1b1bSAndreas Gohr    /**
435696a1b1bSAndreas Gohr     * Test logHistoryPages method
436696a1b1bSAndreas Gohr     */
437696a1b1bSAndreas Gohr    public function testLogHistoryPages()
438696a1b1bSAndreas Gohr    {
439*41d1fffcSAndreas Gohr        $this->helper->getLogger()->logHistoryPages();
440696a1b1bSAndreas Gohr
441696a1b1bSAndreas Gohr        // Check that both page_count and page_size entries were created
442696a1b1bSAndreas Gohr        $pageCount = $this->helper->getDB()->queryValue('SELECT value FROM history WHERE info = ?', ['page_count']);
443696a1b1bSAndreas Gohr        $pageSize = $this->helper->getDB()->queryValue('SELECT value FROM history WHERE info = ?', ['page_size']);
444696a1b1bSAndreas Gohr
445696a1b1bSAndreas Gohr        $this->assertIsNumeric($pageCount);
446696a1b1bSAndreas Gohr        $this->assertIsNumeric($pageSize);
447696a1b1bSAndreas Gohr        $this->assertGreaterThanOrEqual(0, $pageCount);
448696a1b1bSAndreas Gohr        $this->assertGreaterThanOrEqual(0, $pageSize);
449696a1b1bSAndreas Gohr    }
450696a1b1bSAndreas Gohr
451696a1b1bSAndreas Gohr    /**
452696a1b1bSAndreas Gohr     * Test logHistoryMedia method
453696a1b1bSAndreas Gohr     */
454696a1b1bSAndreas Gohr    public function testLogHistoryMedia()
455696a1b1bSAndreas Gohr    {
456*41d1fffcSAndreas Gohr        $this->helper->getLogger()->logHistoryMedia();
457696a1b1bSAndreas Gohr
458696a1b1bSAndreas Gohr        // Check that both media_count and media_size entries were created
459696a1b1bSAndreas Gohr        $mediaCount = $this->helper->getDB()->queryValue('SELECT value FROM history WHERE info = ?', ['media_count']);
460696a1b1bSAndreas Gohr        $mediaSize = $this->helper->getDB()->queryValue('SELECT value FROM history WHERE info = ?', ['media_size']);
461696a1b1bSAndreas Gohr
462696a1b1bSAndreas Gohr        $this->assertIsNumeric($mediaCount);
463696a1b1bSAndreas Gohr        $this->assertIsNumeric($mediaSize);
464696a1b1bSAndreas Gohr        $this->assertGreaterThanOrEqual(0, $mediaCount);
465696a1b1bSAndreas Gohr        $this->assertGreaterThanOrEqual(0, $mediaSize);
466696a1b1bSAndreas Gohr    }
467696a1b1bSAndreas Gohr
468696a1b1bSAndreas Gohr    /**
469696a1b1bSAndreas Gohr     * Test that feedreader user agents are handled correctly
470696a1b1bSAndreas Gohr     */
471696a1b1bSAndreas Gohr    public function testFeedReaderUserAgent()
472696a1b1bSAndreas Gohr    {
47300f786d8SAndreas Gohr        // Use a user agent that DeviceDetector recognizes as a feedreader
47400f786d8SAndreas Gohr        $_SERVER['HTTP_USER_AGENT'] = 'BashPodder/1.0 (http://bashpodder.sourceforge.net/)';
475696a1b1bSAndreas Gohr
476696a1b1bSAndreas Gohr        $logger = new Logger($this->helper);
477696a1b1bSAndreas Gohr
478696a1b1bSAndreas Gohr        // Use reflection to access protected property
479696a1b1bSAndreas Gohr        $reflection = new \ReflectionClass($logger);
480696a1b1bSAndreas Gohr        $uaTypeProperty = $reflection->getProperty('uaType');
481696a1b1bSAndreas Gohr        $uaTypeProperty->setAccessible(true);
482696a1b1bSAndreas Gohr
483696a1b1bSAndreas Gohr        $this->assertEquals('feedreader', $uaTypeProperty->getValue($logger));
484696a1b1bSAndreas Gohr    }
4855cc1319aSAndreas Gohr}
486