15cc1319aSAndreas Gohr<?php 25cc1319aSAndreas Gohr 35cc1319aSAndreas Gohrnamespace dokuwiki\plugin\statistics\test; 45cc1319aSAndreas Gohr 55cc1319aSAndreas Gohruse DokuWikiTest; 6*696a1b1bSAndreas Gohruse dokuwiki\plugin\statistics\Logger; 7*696a1b1bSAndreas Gohruse helper_plugin_statistics; 85cc1319aSAndreas Gohr 95cc1319aSAndreas Gohr/** 10*696a1b1bSAndreas 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{ 17*696a1b1bSAndreas Gohr /** @var helper_plugin_statistics */ 18*696a1b1bSAndreas Gohr protected $helper; 195cc1319aSAndreas Gohr 20*696a1b1bSAndreas Gohr /** @var Logger */ 21*696a1b1bSAndreas Gohr protected $logger; 22*696a1b1bSAndreas Gohr 23*696a1b1bSAndreas Gohr public function setUp(): void 24*696a1b1bSAndreas Gohr { 25*696a1b1bSAndreas Gohr parent::setUp(); 26*696a1b1bSAndreas Gohr 27*696a1b1bSAndreas Gohr // Load the helper plugin 28*696a1b1bSAndreas Gohr $this->helper = plugin_load('helper', 'statistics'); 29*696a1b1bSAndreas Gohr 30*696a1b1bSAndreas Gohr // Mock user agent to avoid bot detection 31*696a1b1bSAndreas Gohr $_SERVER['HTTP_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'; 32*696a1b1bSAndreas Gohr 33*696a1b1bSAndreas Gohr // Initialize logger 34*696a1b1bSAndreas Gohr $this->logger = new Logger($this->helper); 35*696a1b1bSAndreas Gohr } 36*696a1b1bSAndreas Gohr 37*696a1b1bSAndreas Gohr public function tearDown(): void 38*696a1b1bSAndreas Gohr { 39*696a1b1bSAndreas Gohr unset($_SERVER['HTTP_USER_AGENT']); 40*696a1b1bSAndreas Gohr parent::tearDown(); 41*696a1b1bSAndreas Gohr } 42*696a1b1bSAndreas Gohr 43*696a1b1bSAndreas Gohr /** 44*696a1b1bSAndreas Gohr * Test constructor initializes properties correctly 45*696a1b1bSAndreas Gohr */ 46*696a1b1bSAndreas Gohr public function testConstructor() 47*696a1b1bSAndreas Gohr { 48*696a1b1bSAndreas Gohr $this->assertInstanceOf(Logger::class, $this->logger); 49*696a1b1bSAndreas Gohr 50*696a1b1bSAndreas Gohr // Test that bot user agents throw exception 51*696a1b1bSAndreas Gohr $_SERVER['HTTP_USER_AGENT'] = 'Googlebot/2.1 (+http://www.google.com/bot.html)'; 52*696a1b1bSAndreas Gohr 53*696a1b1bSAndreas Gohr $this->expectException(\RuntimeException::class); 54*696a1b1bSAndreas Gohr $this->expectExceptionMessage('Bot detected, not logging'); 55*696a1b1bSAndreas Gohr new Logger($this->helper); 56*696a1b1bSAndreas Gohr } 57*696a1b1bSAndreas Gohr 58*696a1b1bSAndreas Gohr /** 59*696a1b1bSAndreas Gohr * Test begin and end transaction methods 60*696a1b1bSAndreas Gohr */ 61*696a1b1bSAndreas Gohr public function testBeginEnd() 62*696a1b1bSAndreas Gohr { 63*696a1b1bSAndreas Gohr $this->logger->begin(); 64*696a1b1bSAndreas Gohr 65*696a1b1bSAndreas Gohr // Verify transaction is active by checking PDO 66*696a1b1bSAndreas Gohr $pdo = $this->helper->getDB()->getPdo(); 67*696a1b1bSAndreas Gohr $this->assertTrue($pdo->inTransaction()); 68*696a1b1bSAndreas Gohr 69*696a1b1bSAndreas Gohr $this->logger->end(); 70*696a1b1bSAndreas Gohr 71*696a1b1bSAndreas Gohr // Verify transaction is committed 72*696a1b1bSAndreas Gohr $this->assertFalse($pdo->inTransaction()); 73*696a1b1bSAndreas Gohr } 74*696a1b1bSAndreas Gohr 75*696a1b1bSAndreas Gohr /** 76*696a1b1bSAndreas Gohr * Test logLastseen method 77*696a1b1bSAndreas Gohr */ 78*696a1b1bSAndreas Gohr public function testLogLastseen() 79*696a1b1bSAndreas Gohr { 80*696a1b1bSAndreas Gohr global $INPUT; 81*696a1b1bSAndreas Gohr 82*696a1b1bSAndreas Gohr // Test with no user (should not log) 83*696a1b1bSAndreas Gohr $INPUT->server->set('REMOTE_USER', ''); 84*696a1b1bSAndreas Gohr $this->logger->logLastseen(); 85*696a1b1bSAndreas Gohr 86*696a1b1bSAndreas Gohr $count = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM lastseen'); 87*696a1b1bSAndreas Gohr $this->assertEquals(0, $count); 88*696a1b1bSAndreas Gohr 89*696a1b1bSAndreas Gohr // Test with user 90*696a1b1bSAndreas Gohr $INPUT->server->set('REMOTE_USER', 'testuser'); 91*696a1b1bSAndreas Gohr $this->logger->logLastseen(); 92*696a1b1bSAndreas Gohr 93*696a1b1bSAndreas Gohr $count = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM lastseen'); 94*696a1b1bSAndreas Gohr $this->assertEquals(1, $count); 95*696a1b1bSAndreas Gohr 96*696a1b1bSAndreas Gohr $user = $this->helper->getDB()->queryValue('SELECT user FROM lastseen WHERE user = ?', ['testuser']); 97*696a1b1bSAndreas Gohr $this->assertEquals('testuser', $user); 98*696a1b1bSAndreas Gohr } 99*696a1b1bSAndreas Gohr 100*696a1b1bSAndreas Gohr /** 101*696a1b1bSAndreas Gohr * Data provider for logGroups test 102*696a1b1bSAndreas Gohr */ 103*696a1b1bSAndreas Gohr public function logGroupsProvider() 104*696a1b1bSAndreas Gohr { 105*696a1b1bSAndreas Gohr return [ 106*696a1b1bSAndreas Gohr 'empty groups' => [[], 'view', 0], 107*696a1b1bSAndreas Gohr 'single group' => [['admin'], 'view', 1], 108*696a1b1bSAndreas Gohr 'multiple groups' => [['admin', 'user'], 'edit', 2], 109*696a1b1bSAndreas Gohr 'filtered groups' => [['admin', 'nonexistent'], 'view', 1], // assuming only 'admin' is configured 110*696a1b1bSAndreas Gohr ]; 111*696a1b1bSAndreas Gohr } 112*696a1b1bSAndreas Gohr 113*696a1b1bSAndreas Gohr /** 114*696a1b1bSAndreas Gohr * Test logGroups method 115*696a1b1bSAndreas Gohr * @dataProvider logGroupsProvider 116*696a1b1bSAndreas Gohr */ 117*696a1b1bSAndreas Gohr public function testLogGroups($groups, $type, $expectedCount) 118*696a1b1bSAndreas Gohr { 119*696a1b1bSAndreas Gohr // Mock configuration to allow 'admin' and 'user' groups 120*696a1b1bSAndreas Gohr $this->helper->conf['loggroups'] = ['admin', 'user']; 121*696a1b1bSAndreas Gohr 122*696a1b1bSAndreas Gohr $this->logger->logGroups($type, $groups); 123*696a1b1bSAndreas Gohr 124*696a1b1bSAndreas Gohr $count = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM groups WHERE type = ?', [$type]); 125*696a1b1bSAndreas Gohr $this->assertEquals($expectedCount, $count); 126*696a1b1bSAndreas Gohr 127*696a1b1bSAndreas Gohr if ($expectedCount > 0) { 128*696a1b1bSAndreas Gohr $loggedGroups = $this->helper->getDB()->queryAll('SELECT `group` FROM groups WHERE type = ?', [$type]); 129*696a1b1bSAndreas Gohr $this->assertCount($expectedCount, $loggedGroups); 130*696a1b1bSAndreas Gohr } 131*696a1b1bSAndreas Gohr } 132*696a1b1bSAndreas Gohr 133*696a1b1bSAndreas Gohr /** 134*696a1b1bSAndreas Gohr * Data provider for logExternalSearch test 135*696a1b1bSAndreas Gohr */ 136*696a1b1bSAndreas Gohr public function logExternalSearchProvider() 137*696a1b1bSAndreas Gohr { 138*696a1b1bSAndreas Gohr return [ 139*696a1b1bSAndreas Gohr 'google search' => [ 140*696a1b1bSAndreas Gohr 'https://www.google.com/search?q=dokuwiki+test', 141*696a1b1bSAndreas Gohr 'search', 142*696a1b1bSAndreas Gohr 'dokuwiki test', 143*696a1b1bSAndreas Gohr 'Google' 144*696a1b1bSAndreas Gohr ], 145*696a1b1bSAndreas Gohr 'non-search referer' => [ 146*696a1b1bSAndreas Gohr 'https://example.com/page', 147*696a1b1bSAndreas Gohr '', 148*696a1b1bSAndreas Gohr null, 149*696a1b1bSAndreas Gohr null 150*696a1b1bSAndreas Gohr ], 151*696a1b1bSAndreas Gohr ]; 152*696a1b1bSAndreas Gohr } 153*696a1b1bSAndreas Gohr 154*696a1b1bSAndreas Gohr /** 155*696a1b1bSAndreas Gohr * Test logExternalSearch method 156*696a1b1bSAndreas Gohr * @dataProvider logExternalSearchProvider 157*696a1b1bSAndreas Gohr */ 158*696a1b1bSAndreas Gohr public function testLogExternalSearch($referer, $expectedType, $expectedQuery, $expectedEngine) 159*696a1b1bSAndreas Gohr { 160*696a1b1bSAndreas Gohr global $INPUT; 161*696a1b1bSAndreas Gohr $INPUT->set('p', 'test:page'); 162*696a1b1bSAndreas Gohr 163*696a1b1bSAndreas Gohr $type = ''; 164*696a1b1bSAndreas Gohr $this->logger->logExternalSearch($referer, $type); 165*696a1b1bSAndreas Gohr 166*696a1b1bSAndreas Gohr $this->assertEquals($expectedType, $type); 167*696a1b1bSAndreas Gohr 168*696a1b1bSAndreas Gohr if ($expectedType === 'search') { 169*696a1b1bSAndreas Gohr $searchCount = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM search'); 170*696a1b1bSAndreas Gohr $this->assertEquals(1, $searchCount); 171*696a1b1bSAndreas Gohr 172*696a1b1bSAndreas Gohr $search = $this->helper->getDB()->queryRecord('SELECT * FROM search ORDER BY dt DESC LIMIT 1'); 173*696a1b1bSAndreas Gohr $this->assertEquals($expectedQuery, $search['query']); 174*696a1b1bSAndreas Gohr $this->assertEquals($expectedEngine, $search['engine']); 175*696a1b1bSAndreas Gohr } 176*696a1b1bSAndreas Gohr } 177*696a1b1bSAndreas Gohr 178*696a1b1bSAndreas Gohr /** 179*696a1b1bSAndreas Gohr * Test logSearch method 180*696a1b1bSAndreas Gohr */ 181*696a1b1bSAndreas Gohr public function testLogSearch() 182*696a1b1bSAndreas Gohr { 183*696a1b1bSAndreas Gohr $page = 'test:page'; 184*696a1b1bSAndreas Gohr $query = 'test search query'; 185*696a1b1bSAndreas Gohr $words = ['test', 'search', 'query']; 186*696a1b1bSAndreas Gohr $engine = 'Google'; 187*696a1b1bSAndreas Gohr 188*696a1b1bSAndreas Gohr $this->logger->logSearch($page, $query, $words, $engine); 189*696a1b1bSAndreas Gohr 190*696a1b1bSAndreas Gohr // Check search table 191*696a1b1bSAndreas Gohr $search = $this->helper->getDB()->queryRecord('SELECT * FROM search ORDER BY dt DESC LIMIT 1'); 192*696a1b1bSAndreas Gohr $this->assertEquals($page, $search['page']); 193*696a1b1bSAndreas Gohr $this->assertEquals($query, $search['query']); 194*696a1b1bSAndreas Gohr $this->assertEquals($engine, $search['engine']); 195*696a1b1bSAndreas Gohr 196*696a1b1bSAndreas Gohr // Check searchwords table 197*696a1b1bSAndreas Gohr $wordCount = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM searchwords WHERE sid = ?', [$search['rowid']]); 198*696a1b1bSAndreas Gohr $this->assertEquals(3, $wordCount); 199*696a1b1bSAndreas Gohr 200*696a1b1bSAndreas Gohr $loggedWords = $this->helper->getDB()->queryAll('SELECT word FROM searchwords WHERE sid = ? ORDER BY word', [$search['rowid']]); 201*696a1b1bSAndreas Gohr $this->assertEquals(['query', 'search', 'test'], array_column($loggedWords, 'word')); 202*696a1b1bSAndreas Gohr } 203*696a1b1bSAndreas Gohr 204*696a1b1bSAndreas Gohr /** 205*696a1b1bSAndreas Gohr * Test logSession method 206*696a1b1bSAndreas Gohr */ 207*696a1b1bSAndreas Gohr public function testLogSession() 208*696a1b1bSAndreas Gohr { 209*696a1b1bSAndreas Gohr // Test without adding view 210*696a1b1bSAndreas Gohr $this->logger->logSession(0); 211*696a1b1bSAndreas Gohr 212*696a1b1bSAndreas Gohr $sessionCount = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM session'); 213*696a1b1bSAndreas Gohr $this->assertEquals(1, $sessionCount); 214*696a1b1bSAndreas Gohr 215*696a1b1bSAndreas Gohr $session = $this->helper->getDB()->queryRecord('SELECT * FROM session ORDER BY dt DESC LIMIT 1'); 216*696a1b1bSAndreas Gohr $this->assertEquals(0, $session['views']); 217*696a1b1bSAndreas Gohr 218*696a1b1bSAndreas Gohr // Test adding view 219*696a1b1bSAndreas Gohr $this->logger->logSession(1); 220*696a1b1bSAndreas Gohr 221*696a1b1bSAndreas Gohr $session = $this->helper->getDB()->queryRecord('SELECT * FROM session ORDER BY dt DESC LIMIT 1'); 222*696a1b1bSAndreas Gohr $this->assertEquals(1, $session['views']); 223*696a1b1bSAndreas Gohr 224*696a1b1bSAndreas Gohr // Test incrementing views 225*696a1b1bSAndreas Gohr $this->logger->logSession(1); 226*696a1b1bSAndreas Gohr 227*696a1b1bSAndreas Gohr $session = $this->helper->getDB()->queryRecord('SELECT * FROM session ORDER BY dt DESC LIMIT 1'); 228*696a1b1bSAndreas Gohr $this->assertEquals(2, $session['views']); 229*696a1b1bSAndreas Gohr } 230*696a1b1bSAndreas Gohr 231*696a1b1bSAndreas Gohr /** 232*696a1b1bSAndreas Gohr * Test logIp method 233*696a1b1bSAndreas Gohr */ 234*696a1b1bSAndreas Gohr public function testLogIp() 235*696a1b1bSAndreas Gohr { 236*696a1b1bSAndreas Gohr $ip = '8.8.8.8'; 237*696a1b1bSAndreas Gohr 238*696a1b1bSAndreas Gohr // Mock HTTP client response 239*696a1b1bSAndreas Gohr $this->markTestSkipped('Requires mocking HTTP client for external API call'); 240*696a1b1bSAndreas Gohr 241*696a1b1bSAndreas Gohr // This test would need to mock the DokuHTTPClient to avoid actual API calls 242*696a1b1bSAndreas Gohr // For now, we'll skip it as the requirement was not to mock anything 243*696a1b1bSAndreas Gohr } 244*696a1b1bSAndreas Gohr 245*696a1b1bSAndreas Gohr /** 246*696a1b1bSAndreas Gohr * Test logOutgoing method 247*696a1b1bSAndreas Gohr */ 248*696a1b1bSAndreas Gohr public function testLogOutgoing() 249*696a1b1bSAndreas Gohr { 250*696a1b1bSAndreas Gohr global $INPUT; 251*696a1b1bSAndreas Gohr 252*696a1b1bSAndreas Gohr // Test without outgoing link 253*696a1b1bSAndreas Gohr $INPUT->set('ol', ''); 254*696a1b1bSAndreas Gohr $this->logger->logOutgoing(); 255*696a1b1bSAndreas Gohr 256*696a1b1bSAndreas Gohr $count = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM outlinks'); 257*696a1b1bSAndreas Gohr $this->assertEquals(0, $count); 258*696a1b1bSAndreas Gohr 259*696a1b1bSAndreas Gohr // Test with outgoing link 260*696a1b1bSAndreas Gohr $link = 'https://example.com'; 261*696a1b1bSAndreas Gohr $page = 'test:page'; 262*696a1b1bSAndreas Gohr $INPUT->set('ol', $link); 263*696a1b1bSAndreas Gohr $INPUT->set('p', $page); 264*696a1b1bSAndreas Gohr 265*696a1b1bSAndreas Gohr $this->logger->logOutgoing(); 266*696a1b1bSAndreas Gohr 267*696a1b1bSAndreas Gohr $count = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM outlinks'); 268*696a1b1bSAndreas Gohr $this->assertEquals(1, $count); 269*696a1b1bSAndreas Gohr 270*696a1b1bSAndreas Gohr $outlink = $this->helper->getDB()->queryRecord('SELECT * FROM outlinks ORDER BY dt DESC LIMIT 1'); 271*696a1b1bSAndreas Gohr $this->assertEquals($link, $outlink['link']); 272*696a1b1bSAndreas Gohr $this->assertEquals(md5($link), $outlink['link_md5']); 273*696a1b1bSAndreas Gohr $this->assertEquals($page, $outlink['page']); 274*696a1b1bSAndreas Gohr } 275*696a1b1bSAndreas Gohr 276*696a1b1bSAndreas Gohr /** 277*696a1b1bSAndreas Gohr * Test logAccess method 278*696a1b1bSAndreas Gohr */ 279*696a1b1bSAndreas Gohr public function testLogAccess() 280*696a1b1bSAndreas Gohr { 281*696a1b1bSAndreas Gohr global $INPUT, $USERINFO; 282*696a1b1bSAndreas Gohr 283*696a1b1bSAndreas Gohr $page = 'test:page'; 284*696a1b1bSAndreas Gohr $referer = 'https://example.com'; 285*696a1b1bSAndreas Gohr $user = 'testuser'; 286*696a1b1bSAndreas Gohr 287*696a1b1bSAndreas Gohr $INPUT->set('p', $page); 288*696a1b1bSAndreas Gohr $INPUT->set('r', $referer); 289*696a1b1bSAndreas Gohr $INPUT->set('sx', 1920); 290*696a1b1bSAndreas Gohr $INPUT->set('sy', 1080); 291*696a1b1bSAndreas Gohr $INPUT->set('vx', 1200); 292*696a1b1bSAndreas Gohr $INPUT->set('vy', 800); 293*696a1b1bSAndreas Gohr $INPUT->set('js', 1); 294*696a1b1bSAndreas Gohr $INPUT->server->set('REMOTE_USER', $user); 295*696a1b1bSAndreas Gohr 296*696a1b1bSAndreas Gohr $USERINFO = ['grps' => ['admin', 'user']]; 297*696a1b1bSAndreas Gohr $this->helper->conf['loggroups'] = ['admin', 'user']; 298*696a1b1bSAndreas Gohr 299*696a1b1bSAndreas Gohr $this->logger->logAccess(); 300*696a1b1bSAndreas Gohr 301*696a1b1bSAndreas Gohr // Check access table 302*696a1b1bSAndreas Gohr $access = $this->helper->getDB()->queryRecord('SELECT * FROM access ORDER BY dt DESC LIMIT 1'); 303*696a1b1bSAndreas Gohr $this->assertEquals($page, $access['page']); 304*696a1b1bSAndreas Gohr $this->assertEquals($user, $access['user']); 305*696a1b1bSAndreas Gohr $this->assertEquals(1920, $access['screen_x']); 306*696a1b1bSAndreas Gohr $this->assertEquals(1080, $access['screen_y']); 307*696a1b1bSAndreas Gohr $this->assertEquals(1200, $access['view_x']); 308*696a1b1bSAndreas Gohr $this->assertEquals(800, $access['view_y']); 309*696a1b1bSAndreas Gohr $this->assertEquals(1, $access['js']); 310*696a1b1bSAndreas Gohr $this->assertEquals('external', $access['ref_type']); 311*696a1b1bSAndreas Gohr 312*696a1b1bSAndreas Gohr // Check refseen table 313*696a1b1bSAndreas Gohr $refCount = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM refseen WHERE ref_md5 = ?', [md5($referer)]); 314*696a1b1bSAndreas Gohr $this->assertEquals(1, $refCount); 315*696a1b1bSAndreas Gohr 316*696a1b1bSAndreas Gohr // Check groups table 317*696a1b1bSAndreas Gohr $groupCount = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM groups WHERE type = ?', ['view']); 318*696a1b1bSAndreas Gohr $this->assertEquals(2, $groupCount); 319*696a1b1bSAndreas Gohr } 320*696a1b1bSAndreas Gohr 321*696a1b1bSAndreas Gohr /** 322*696a1b1bSAndreas Gohr * Data provider for logMedia test 323*696a1b1bSAndreas Gohr */ 324*696a1b1bSAndreas Gohr public function logMediaProvider() 325*696a1b1bSAndreas Gohr { 326*696a1b1bSAndreas Gohr return [ 327*696a1b1bSAndreas Gohr 'image inline' => ['test.jpg', 'image/jpeg', true, 1024], 328*696a1b1bSAndreas Gohr 'video not inline' => ['test.mp4', 'video/mp4', false, 2048], 329*696a1b1bSAndreas Gohr 'document' => ['test.pdf', 'application/pdf', false, 512], 330*696a1b1bSAndreas Gohr ]; 331*696a1b1bSAndreas Gohr } 332*696a1b1bSAndreas Gohr 333*696a1b1bSAndreas Gohr /** 334*696a1b1bSAndreas Gohr * Test logMedia method 335*696a1b1bSAndreas Gohr * @dataProvider logMediaProvider 336*696a1b1bSAndreas Gohr */ 337*696a1b1bSAndreas Gohr public function testLogMedia($media, $mime, $inline, $size) 338*696a1b1bSAndreas Gohr { 339*696a1b1bSAndreas Gohr global $INPUT; 340*696a1b1bSAndreas Gohr 341*696a1b1bSAndreas Gohr $user = 'testuser'; 342*696a1b1bSAndreas Gohr $INPUT->server->set('REMOTE_USER', $user); 343*696a1b1bSAndreas Gohr 344*696a1b1bSAndreas Gohr $this->logger->logMedia($media, $mime, $inline, $size); 345*696a1b1bSAndreas Gohr 346*696a1b1bSAndreas Gohr $mediaLog = $this->helper->getDB()->queryRecord('SELECT * FROM media ORDER BY dt DESC LIMIT 1'); 347*696a1b1bSAndreas Gohr $this->assertEquals($media, $mediaLog['media']); 348*696a1b1bSAndreas Gohr $this->assertEquals($user, $mediaLog['user']); 349*696a1b1bSAndreas Gohr $this->assertEquals($size, $mediaLog['size']); 350*696a1b1bSAndreas Gohr $this->assertEquals($inline ? 1 : 0, $mediaLog['inline']); 351*696a1b1bSAndreas Gohr 352*696a1b1bSAndreas Gohr [$mime1, $mime2] = explode('/', strtolower($mime)); 353*696a1b1bSAndreas Gohr $this->assertEquals($mime1, $mediaLog['mime1']); 354*696a1b1bSAndreas Gohr $this->assertEquals($mime2, $mediaLog['mime2']); 355*696a1b1bSAndreas Gohr } 356*696a1b1bSAndreas Gohr 357*696a1b1bSAndreas Gohr /** 358*696a1b1bSAndreas Gohr * Data provider for logEdit test 359*696a1b1bSAndreas Gohr */ 360*696a1b1bSAndreas Gohr public function logEditProvider() 361*696a1b1bSAndreas Gohr { 362*696a1b1bSAndreas Gohr return [ 363*696a1b1bSAndreas Gohr 'create page' => ['new:page', 'create'], 364*696a1b1bSAndreas Gohr 'edit page' => ['existing:page', 'edit'], 365*696a1b1bSAndreas Gohr 'delete page' => ['old:page', 'delete'], 366*696a1b1bSAndreas Gohr ]; 367*696a1b1bSAndreas Gohr } 368*696a1b1bSAndreas Gohr 369*696a1b1bSAndreas Gohr /** 370*696a1b1bSAndreas Gohr * Test logEdit method 371*696a1b1bSAndreas Gohr * @dataProvider logEditProvider 372*696a1b1bSAndreas Gohr */ 373*696a1b1bSAndreas Gohr public function testLogEdit($page, $type) 374*696a1b1bSAndreas Gohr { 375*696a1b1bSAndreas Gohr global $INPUT, $USERINFO; 376*696a1b1bSAndreas Gohr 377*696a1b1bSAndreas Gohr $user = 'testuser'; 378*696a1b1bSAndreas Gohr $INPUT->server->set('REMOTE_USER', $user); 379*696a1b1bSAndreas Gohr $USERINFO = ['grps' => ['admin']]; 380*696a1b1bSAndreas Gohr $this->helper->conf['loggroups'] = ['admin']; 381*696a1b1bSAndreas Gohr 382*696a1b1bSAndreas Gohr $this->logger->logEdit($page, $type); 383*696a1b1bSAndreas Gohr 384*696a1b1bSAndreas Gohr // Check edits table 385*696a1b1bSAndreas Gohr $edit = $this->helper->getDB()->queryRecord('SELECT * FROM edits ORDER BY dt DESC LIMIT 1'); 386*696a1b1bSAndreas Gohr $this->assertEquals($page, $edit['page']); 387*696a1b1bSAndreas Gohr $this->assertEquals($type, $edit['type']); 388*696a1b1bSAndreas Gohr $this->assertEquals($user, $edit['user']); 389*696a1b1bSAndreas Gohr 390*696a1b1bSAndreas Gohr // Check groups table 391*696a1b1bSAndreas Gohr $groupCount = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM groups WHERE type = ?', ['edit']); 392*696a1b1bSAndreas Gohr $this->assertEquals(1, $groupCount); 393*696a1b1bSAndreas Gohr } 394*696a1b1bSAndreas Gohr 395*696a1b1bSAndreas Gohr /** 396*696a1b1bSAndreas Gohr * Data provider for logLogin test 397*696a1b1bSAndreas Gohr */ 398*696a1b1bSAndreas Gohr public function logLoginProvider() 399*696a1b1bSAndreas Gohr { 400*696a1b1bSAndreas Gohr return [ 401*696a1b1bSAndreas Gohr 'login' => ['login', 'testuser'], 402*696a1b1bSAndreas Gohr 'logout' => ['logout', 'testuser'], 403*696a1b1bSAndreas Gohr 'create' => ['create', 'newuser'], 404*696a1b1bSAndreas Gohr ]; 405*696a1b1bSAndreas Gohr } 406*696a1b1bSAndreas Gohr 407*696a1b1bSAndreas Gohr /** 408*696a1b1bSAndreas Gohr * Test logLogin method 409*696a1b1bSAndreas Gohr * @dataProvider logLoginProvider 410*696a1b1bSAndreas Gohr */ 411*696a1b1bSAndreas Gohr public function testLogLogin($type, $user) 412*696a1b1bSAndreas Gohr { 413*696a1b1bSAndreas Gohr global $INPUT; 414*696a1b1bSAndreas Gohr 415*696a1b1bSAndreas Gohr if ($user === 'testuser') { 416*696a1b1bSAndreas Gohr $INPUT->server->set('REMOTE_USER', $user); 417*696a1b1bSAndreas Gohr $this->logger->logLogin($type); 418*696a1b1bSAndreas Gohr } else { 419*696a1b1bSAndreas Gohr $this->logger->logLogin($type, $user); 420*696a1b1bSAndreas Gohr } 421*696a1b1bSAndreas Gohr 422*696a1b1bSAndreas Gohr $login = $this->helper->getDB()->queryRecord('SELECT * FROM logins ORDER BY dt DESC LIMIT 1'); 423*696a1b1bSAndreas Gohr $this->assertEquals($type, $login['type']); 424*696a1b1bSAndreas Gohr $this->assertEquals($user, $login['user']); 425*696a1b1bSAndreas Gohr } 426*696a1b1bSAndreas Gohr 427*696a1b1bSAndreas Gohr /** 428*696a1b1bSAndreas Gohr * Test logHistoryPages method 429*696a1b1bSAndreas Gohr */ 430*696a1b1bSAndreas Gohr public function testLogHistoryPages() 431*696a1b1bSAndreas Gohr { 432*696a1b1bSAndreas Gohr $this->logger->logHistoryPages(); 433*696a1b1bSAndreas Gohr 434*696a1b1bSAndreas Gohr // Check that both page_count and page_size entries were created 435*696a1b1bSAndreas Gohr $pageCount = $this->helper->getDB()->queryValue('SELECT value FROM history WHERE info = ?', ['page_count']); 436*696a1b1bSAndreas Gohr $pageSize = $this->helper->getDB()->queryValue('SELECT value FROM history WHERE info = ?', ['page_size']); 437*696a1b1bSAndreas Gohr 438*696a1b1bSAndreas Gohr $this->assertIsNumeric($pageCount); 439*696a1b1bSAndreas Gohr $this->assertIsNumeric($pageSize); 440*696a1b1bSAndreas Gohr $this->assertGreaterThanOrEqual(0, $pageCount); 441*696a1b1bSAndreas Gohr $this->assertGreaterThanOrEqual(0, $pageSize); 442*696a1b1bSAndreas Gohr } 443*696a1b1bSAndreas Gohr 444*696a1b1bSAndreas Gohr /** 445*696a1b1bSAndreas Gohr * Test logHistoryMedia method 446*696a1b1bSAndreas Gohr */ 447*696a1b1bSAndreas Gohr public function testLogHistoryMedia() 448*696a1b1bSAndreas Gohr { 449*696a1b1bSAndreas Gohr $this->logger->logHistoryMedia(); 450*696a1b1bSAndreas Gohr 451*696a1b1bSAndreas Gohr // Check that both media_count and media_size entries were created 452*696a1b1bSAndreas Gohr $mediaCount = $this->helper->getDB()->queryValue('SELECT value FROM history WHERE info = ?', ['media_count']); 453*696a1b1bSAndreas Gohr $mediaSize = $this->helper->getDB()->queryValue('SELECT value FROM history WHERE info = ?', ['media_size']); 454*696a1b1bSAndreas Gohr 455*696a1b1bSAndreas Gohr $this->assertIsNumeric($mediaCount); 456*696a1b1bSAndreas Gohr $this->assertIsNumeric($mediaSize); 457*696a1b1bSAndreas Gohr $this->assertGreaterThanOrEqual(0, $mediaCount); 458*696a1b1bSAndreas Gohr $this->assertGreaterThanOrEqual(0, $mediaSize); 459*696a1b1bSAndreas Gohr } 460*696a1b1bSAndreas Gohr 461*696a1b1bSAndreas Gohr /** 462*696a1b1bSAndreas Gohr * Test that feedreader user agents are handled correctly 463*696a1b1bSAndreas Gohr */ 464*696a1b1bSAndreas Gohr public function testFeedReaderUserAgent() 465*696a1b1bSAndreas Gohr { 466*696a1b1bSAndreas Gohr $_SERVER['HTTP_USER_AGENT'] = 'FeedBurner/1.0 (http://www.FeedBurner.com)'; 467*696a1b1bSAndreas Gohr 468*696a1b1bSAndreas Gohr $logger = new Logger($this->helper); 469*696a1b1bSAndreas Gohr 470*696a1b1bSAndreas Gohr // Use reflection to access protected property 471*696a1b1bSAndreas Gohr $reflection = new \ReflectionClass($logger); 472*696a1b1bSAndreas Gohr $uaTypeProperty = $reflection->getProperty('uaType'); 473*696a1b1bSAndreas Gohr $uaTypeProperty->setAccessible(true); 474*696a1b1bSAndreas Gohr 475*696a1b1bSAndreas Gohr $this->assertEquals('feedreader', $uaTypeProperty->getValue($logger)); 476*696a1b1bSAndreas Gohr } 477*696a1b1bSAndreas Gohr 478*696a1b1bSAndreas Gohr /** 479*696a1b1bSAndreas Gohr * Test session logging only works for browser type 480*696a1b1bSAndreas Gohr */ 481*696a1b1bSAndreas Gohr public function testLogSessionOnlyForBrowser() 482*696a1b1bSAndreas Gohr { 483*696a1b1bSAndreas Gohr // Change user agent type to feedreader using reflection 484*696a1b1bSAndreas Gohr $reflection = new \ReflectionClass($this->logger); 485*696a1b1bSAndreas Gohr $uaTypeProperty = $reflection->getProperty('uaType'); 486*696a1b1bSAndreas Gohr $uaTypeProperty->setAccessible(true); 487*696a1b1bSAndreas Gohr $uaTypeProperty->setValue($this->logger, 'feedreader'); 488*696a1b1bSAndreas Gohr 489*696a1b1bSAndreas Gohr $this->logger->logSession(1); 490*696a1b1bSAndreas Gohr 491*696a1b1bSAndreas Gohr $sessionCount = $this->helper->getDB()->queryValue('SELECT COUNT(*) FROM session'); 492*696a1b1bSAndreas Gohr $this->assertEquals(0, $sessionCount); 4935cc1319aSAndreas Gohr } 4945cc1319aSAndreas Gohr} 495