16734bb8cSAndreas Gohr<?php 26734bb8cSAndreas Gohr 36734bb8cSAndreas Gohrnamespace dokuwiki\test\Search; 46734bb8cSAndreas Gohr 5*e1272c08SAndreas Gohruse dokuwiki\Search\Indexer; 66734bb8cSAndreas Gohruse dokuwiki\Search\MetadataSearch; 76734bb8cSAndreas Gohr 86734bb8cSAndreas Gohr/** 96734bb8cSAndreas Gohr * Tests for MetadataSearch utility methods 106734bb8cSAndreas Gohr */ 116734bb8cSAndreas Gohrclass MetadataSearchTest extends \DokuWikiTest 126734bb8cSAndreas Gohr{ 136734bb8cSAndreas Gohr /** 14*e1272c08SAndreas Gohr * pageLookup finds pages by name, respects namespace filters, and searches titles with UTF-8 15*e1272c08SAndreas Gohr */ 16*e1272c08SAndreas Gohr public function testPageLookup() 17*e1272c08SAndreas Gohr { 18*e1272c08SAndreas Gohr $indexer = new Indexer(); 19*e1272c08SAndreas Gohr $search = new MetadataSearch(); 20*e1272c08SAndreas Gohr 21*e1272c08SAndreas Gohr saveWikiText('test:page1', 'Some text', 'Test initialization'); 22*e1272c08SAndreas Gohr $indexer->addPage('test:page1', true); 23*e1272c08SAndreas Gohr saveWikiText('ns:page2', 'Other text', 'Test initialization'); 24*e1272c08SAndreas Gohr $indexer->addPage('ns:page2', true); 25*e1272c08SAndreas Gohr saveWikiText('ns:utf8', '====== Title with ÄöÜ ======', 'Test initialization'); 26*e1272c08SAndreas Gohr $indexer->addPage('ns:utf8', true); 27*e1272c08SAndreas Gohr 28*e1272c08SAndreas Gohr $this->assertEquals(['test:page1' => null, 'ns:page2' => null], $search->pageLookup('page')); 29*e1272c08SAndreas Gohr $this->assertEquals(['test:page1' => null], $search->pageLookup('page @test')); 30*e1272c08SAndreas Gohr $this->assertEquals(['ns:page2' => null], $search->pageLookup('page ^test')); 31*e1272c08SAndreas Gohr 32*e1272c08SAndreas Gohr $this->assertEquals(['ns:utf8' => 'Title with ÄöÜ'], $search->pageLookup('title', false, true)); 33*e1272c08SAndreas Gohr $this->assertEquals(['ns:utf8' => 'Title with ÄöÜ'], $search->pageLookup('äöü', false, true)); 34*e1272c08SAndreas Gohr } 35*e1272c08SAndreas Gohr 36*e1272c08SAndreas Gohr /** 376734bb8cSAndreas Gohr * filterPages removes non-existent pages 386734bb8cSAndreas Gohr */ 396734bb8cSAndreas Gohr public function testFilterPagesRemovesNonExistent() 406734bb8cSAndreas Gohr { 416734bb8cSAndreas Gohr saveWikiText('wiki:existing', 'content', 'init'); 426734bb8cSAndreas Gohr 436734bb8cSAndreas Gohr $pages = ['wiki:existing' => true, 'wiki:nonexistent' => true]; 446734bb8cSAndreas Gohr $result = MetadataSearch::filterPages($pages); 456734bb8cSAndreas Gohr 466734bb8cSAndreas Gohr $this->assertArrayHasKey('wiki:existing', $result); 476734bb8cSAndreas Gohr $this->assertArrayNotHasKey('wiki:nonexistent', $result); 486734bb8cSAndreas Gohr } 496734bb8cSAndreas Gohr 506734bb8cSAndreas Gohr /** 516734bb8cSAndreas Gohr * filterPages respects hidden pages setting 526734bb8cSAndreas Gohr */ 536734bb8cSAndreas Gohr public function testFilterPagesHidden() 546734bb8cSAndreas Gohr { 556734bb8cSAndreas Gohr global $conf; 566734bb8cSAndreas Gohr $conf['hidepages'] = 'hidden:.*'; 576734bb8cSAndreas Gohr 586734bb8cSAndreas Gohr saveWikiText('hidden:page', 'content', 'init'); 596734bb8cSAndreas Gohr saveWikiText('visible:page', 'content', 'init'); 606734bb8cSAndreas Gohr 616734bb8cSAndreas Gohr $pages = ['hidden:page' => true, 'visible:page' => true]; 626734bb8cSAndreas Gohr 636734bb8cSAndreas Gohr // default: hidden pages are filtered 646734bb8cSAndreas Gohr $result = MetadataSearch::filterPages($pages); 656734bb8cSAndreas Gohr $this->assertArrayNotHasKey('hidden:page', $result); 666734bb8cSAndreas Gohr $this->assertArrayHasKey('visible:page', $result); 676734bb8cSAndreas Gohr 686734bb8cSAndreas Gohr // ignorePerms: hidden pages are kept 696734bb8cSAndreas Gohr $result = MetadataSearch::filterPages($pages, true); 706734bb8cSAndreas Gohr $this->assertArrayHasKey('hidden:page', $result); 716734bb8cSAndreas Gohr $this->assertArrayHasKey('visible:page', $result); 726734bb8cSAndreas Gohr } 736734bb8cSAndreas Gohr 746734bb8cSAndreas Gohr /** 756734bb8cSAndreas Gohr * filterPages respects ACL permissions 766734bb8cSAndreas Gohr */ 776734bb8cSAndreas Gohr public function testFilterPagesACL() 786734bb8cSAndreas Gohr { 796734bb8cSAndreas Gohr global $conf; 806734bb8cSAndreas Gohr global $AUTH_ACL; 816734bb8cSAndreas Gohr $conf['superuser'] = 'admin'; 826734bb8cSAndreas Gohr $conf['useacl'] = 1; 836734bb8cSAndreas Gohr 846734bb8cSAndreas Gohr $AUTH_ACL = [ 856734bb8cSAndreas Gohr '* @ALL 8', 866734bb8cSAndreas Gohr 'secret:* @ALL 0', 876734bb8cSAndreas Gohr ]; 886734bb8cSAndreas Gohr 896734bb8cSAndreas Gohr $_SERVER['REMOTE_USER'] = 'user'; 906734bb8cSAndreas Gohr 916734bb8cSAndreas Gohr saveWikiText('public:page', 'content', 'init'); 926734bb8cSAndreas Gohr saveWikiText('secret:page', 'content', 'init'); 936734bb8cSAndreas Gohr 946734bb8cSAndreas Gohr $pages = ['public:page' => true, 'secret:page' => true]; 956734bb8cSAndreas Gohr 966734bb8cSAndreas Gohr $result = MetadataSearch::filterPages($pages); 976734bb8cSAndreas Gohr $this->assertArrayHasKey('public:page', $result); 986734bb8cSAndreas Gohr $this->assertArrayNotHasKey('secret:page', $result); 996734bb8cSAndreas Gohr 1006734bb8cSAndreas Gohr $result = MetadataSearch::filterPages($pages, true); 1016734bb8cSAndreas Gohr $this->assertArrayHasKey('public:page', $result); 1026734bb8cSAndreas Gohr $this->assertArrayHasKey('secret:page', $result); 1036734bb8cSAndreas Gohr } 1046734bb8cSAndreas Gohr 1056734bb8cSAndreas Gohr /** 1066734bb8cSAndreas Gohr * filterPages filters by modification time 1076734bb8cSAndreas Gohr */ 1086734bb8cSAndreas Gohr public function testFilterPagesTime() 1096734bb8cSAndreas Gohr { 1106734bb8cSAndreas Gohr saveWikiText('wiki:timepage', 'content', 'init'); 1116734bb8cSAndreas Gohr $mtime = filemtime(wikiFN('wiki:timepage')); 1126734bb8cSAndreas Gohr 1136734bb8cSAndreas Gohr $pages = ['wiki:timepage' => true]; 1146734bb8cSAndreas Gohr 1156734bb8cSAndreas Gohr // after: page mtime is before the threshold → filtered 1166734bb8cSAndreas Gohr $result = MetadataSearch::filterPages($pages, false, $mtime + 100); 1176734bb8cSAndreas Gohr $this->assertEmpty($result); 1186734bb8cSAndreas Gohr 1196734bb8cSAndreas Gohr // after: page mtime is after the threshold → kept 1206734bb8cSAndreas Gohr $result = MetadataSearch::filterPages($pages, false, $mtime - 100); 1216734bb8cSAndreas Gohr $this->assertArrayHasKey('wiki:timepage', $result); 1226734bb8cSAndreas Gohr 1236734bb8cSAndreas Gohr // before: page mtime is after the threshold → filtered 1246734bb8cSAndreas Gohr $result = MetadataSearch::filterPages($pages, false, null, $mtime - 100); 1256734bb8cSAndreas Gohr $this->assertEmpty($result); 1266734bb8cSAndreas Gohr 1276734bb8cSAndreas Gohr // before: page mtime is before the threshold → kept 1286734bb8cSAndreas Gohr $result = MetadataSearch::filterPages($pages, false, null, $mtime + 100); 1296734bb8cSAndreas Gohr $this->assertArrayHasKey('wiki:timepage', $result); 1306734bb8cSAndreas Gohr } 1316734bb8cSAndreas Gohr 1326734bb8cSAndreas Gohr /** 1336734bb8cSAndreas Gohr * filterPages preserves original array values 1346734bb8cSAndreas Gohr */ 1356734bb8cSAndreas Gohr public function testFilterPagesPreservesValues() 1366734bb8cSAndreas Gohr { 1376734bb8cSAndreas Gohr saveWikiText('wiki:testpage', 'content', 'init'); 1386734bb8cSAndreas Gohr 1396734bb8cSAndreas Gohr $pages = ['wiki:testpage' => 'My Title']; 1406734bb8cSAndreas Gohr $result = MetadataSearch::filterPages($pages); 1416734bb8cSAndreas Gohr 1426734bb8cSAndreas Gohr $this->assertEquals('My Title', $result['wiki:testpage']); 1436734bb8cSAndreas Gohr } 1446734bb8cSAndreas Gohr} 145