1*ede46466SAndreas Gohr<?php 2*ede46466SAndreas Gohr 3*ede46466SAndreas Gohrnamespace dokuwiki\test\Search; 4*ede46466SAndreas Gohr 5*ede46466SAndreas Gohruse dokuwiki\Search\Indexer; 6*ede46466SAndreas Gohruse dokuwiki\Search\Index\FileIndex; 7*ede46466SAndreas Gohr 8*ede46466SAndreas Gohr/** 9*ede46466SAndreas Gohr * Tests the Indexer class 10*ede46466SAndreas Gohr */ 11*ede46466SAndreas Gohrclass IndexerTest extends \DokuWikiTest 12*ede46466SAndreas Gohr{ 13*ede46466SAndreas Gohr /** 14*ede46466SAndreas Gohr * Test basic page indexing via addPage 15*ede46466SAndreas Gohr */ 16*ede46466SAndreas Gohr public function testAddPage() 17*ede46466SAndreas Gohr { 18*ede46466SAndreas Gohr $indexer = new Indexer(); 19*ede46466SAndreas Gohr 20*ede46466SAndreas Gohr saveWikiText('testpage', 'Foo bar baz.', 'Test initialization'); 21*ede46466SAndreas Gohr $indexer->addPage('testpage'); 22*ede46466SAndreas Gohr 23*ede46466SAndreas Gohr // page should be in the entity index 24*ede46466SAndreas Gohr $pageIndex = new FileIndex('page'); 25*ede46466SAndreas Gohr $result = $pageIndex->search('/^testpage$/'); 26*ede46466SAndreas Gohr $this->assertNotEmpty($result, 'testpage not found in page.idx'); 27*ede46466SAndreas Gohr } 28*ede46466SAndreas Gohr 29*ede46466SAndreas Gohr /** 30*ede46466SAndreas Gohr * Test that deletePage clears data 31*ede46466SAndreas Gohr */ 32*ede46466SAndreas Gohr public function testDeletePage() 33*ede46466SAndreas Gohr { 34*ede46466SAndreas Gohr $indexer = new Indexer(); 35*ede46466SAndreas Gohr 36*ede46466SAndreas Gohr saveWikiText('delpage', 'Delete me content.', 'Test initialization'); 37*ede46466SAndreas Gohr $indexer->addPage('delpage'); 38*ede46466SAndreas Gohr $indexer->deletePage('delpage', true); 39*ede46466SAndreas Gohr 40*ede46466SAndreas Gohr // page entity persists in page.idx but data is cleared 41*ede46466SAndreas Gohr $pageIndex = new FileIndex('page'); 42*ede46466SAndreas Gohr $result = $pageIndex->search('/^delpage$/'); 43*ede46466SAndreas Gohr $this->assertNotEmpty($result, 'delpage should persist in page.idx'); 44*ede46466SAndreas Gohr } 45*ede46466SAndreas Gohr 46*ede46466SAndreas Gohr /** 47*ede46466SAndreas Gohr * Test renamePage clears old and indexes new 48*ede46466SAndreas Gohr */ 49*ede46466SAndreas Gohr public function testRenamePage() 50*ede46466SAndreas Gohr { 51*ede46466SAndreas Gohr $indexer = new Indexer(); 52*ede46466SAndreas Gohr 53*ede46466SAndreas Gohr saveWikiText('old_name', 'Old page content words.', 'Test initialization'); 54*ede46466SAndreas Gohr $indexer->addPage('old_name'); 55*ede46466SAndreas Gohr 56*ede46466SAndreas Gohr // move the page on disk 57*ede46466SAndreas Gohr io_rename(wikiFN('old_name'), wikiFN('new_name')); 58*ede46466SAndreas Gohr saveWikiText('new_name', 'Old page content words.', 'Renamed'); 59*ede46466SAndreas Gohr 60*ede46466SAndreas Gohr $indexer->renamePage('old_name', 'new_name'); 61*ede46466SAndreas Gohr 62*ede46466SAndreas Gohr // new page should be indexed 63*ede46466SAndreas Gohr $pageIndex = new FileIndex('page'); 64*ede46466SAndreas Gohr $result = $pageIndex->search('/^new_name$/'); 65*ede46466SAndreas Gohr $this->assertNotEmpty($result, 'new_name not found in page.idx after rename'); 66*ede46466SAndreas Gohr } 67*ede46466SAndreas Gohr 68*ede46466SAndreas Gohr /** 69*ede46466SAndreas Gohr * Test that clear removes all index files 70*ede46466SAndreas Gohr */ 71*ede46466SAndreas Gohr public function testClear() 72*ede46466SAndreas Gohr { 73*ede46466SAndreas Gohr global $conf; 74*ede46466SAndreas Gohr $indexer = new Indexer(); 75*ede46466SAndreas Gohr 76*ede46466SAndreas Gohr saveWikiText('clearpage', 'Some words to index.', 'Test initialization'); 77*ede46466SAndreas Gohr $indexer->addPage('clearpage'); 78*ede46466SAndreas Gohr 79*ede46466SAndreas Gohr $this->assertFileExists($conf['indexdir'] . '/page.idx'); 80*ede46466SAndreas Gohr 81*ede46466SAndreas Gohr $indexer->clear(); 82*ede46466SAndreas Gohr 83*ede46466SAndreas Gohr $this->assertFileDoesNotExist($conf['indexdir'] . '/page.idx'); 84*ede46466SAndreas Gohr } 85*ede46466SAndreas Gohr 86*ede46466SAndreas Gohr /** 87*ede46466SAndreas Gohr * Test that getVersion returns a version string 88*ede46466SAndreas Gohr */ 89*ede46466SAndreas Gohr public function testGetVersion() 90*ede46466SAndreas Gohr { 91*ede46466SAndreas Gohr $indexer = new Indexer(); 92*ede46466SAndreas Gohr $version = $indexer->getVersion(); 93*ede46466SAndreas Gohr $this->assertNotEmpty($version); 94*ede46466SAndreas Gohr $this->assertIsString((string)$version); 95*ede46466SAndreas Gohr } 96*ede46466SAndreas Gohr 97*ede46466SAndreas Gohr /** 98*ede46466SAndreas Gohr * Test needsIndexing returns true for new pages 99*ede46466SAndreas Gohr */ 100*ede46466SAndreas Gohr public function testNeedsIndexing() 101*ede46466SAndreas Gohr { 102*ede46466SAndreas Gohr $indexer = new Indexer(); 103*ede46466SAndreas Gohr 104*ede46466SAndreas Gohr saveWikiText('needsidx', 'Some content.', 'Test initialization'); 105*ede46466SAndreas Gohr $this->assertTrue($indexer->needsIndexing('needsidx')); 106*ede46466SAndreas Gohr 107*ede46466SAndreas Gohr $indexer->addPage('needsidx'); 108*ede46466SAndreas Gohr // ensure the .indexed tag is strictly newer than the wiki file 109*ede46466SAndreas Gohr touch(metaFN('needsidx', '.indexed'), time() + 1); 110*ede46466SAndreas Gohr $this->assertFalse($indexer->needsIndexing('needsidx')); 111*ede46466SAndreas Gohr $this->assertTrue($indexer->needsIndexing('needsidx', true)); // force 112*ede46466SAndreas Gohr } 113*ede46466SAndreas Gohr 114*ede46466SAndreas Gohr /** 115*ede46466SAndreas Gohr * Test the logger callback 116*ede46466SAndreas Gohr */ 117*ede46466SAndreas Gohr public function testLogger() 118*ede46466SAndreas Gohr { 119*ede46466SAndreas Gohr $messages = []; 120*ede46466SAndreas Gohr $indexer = (new Indexer())->setLogger(function ($msg) use (&$messages) { 121*ede46466SAndreas Gohr $messages[] = $msg; 122*ede46466SAndreas Gohr }); 123*ede46466SAndreas Gohr 124*ede46466SAndreas Gohr saveWikiText('logpage', 'Log test content.', 'Test initialization'); 125*ede46466SAndreas Gohr $indexer->addPage('logpage'); 126*ede46466SAndreas Gohr 127*ede46466SAndreas Gohr // ensure the .indexed tag is strictly newer so second call detects "up to date" 128*ede46466SAndreas Gohr touch(metaFN('logpage', '.indexed'), time() + 1); 129*ede46466SAndreas Gohr 130*ede46466SAndreas Gohr $indexer->addPage('logpage'); 131*ede46466SAndreas Gohr $this->assertNotEmpty($messages); 132*ede46466SAndreas Gohr $this->assertStringContainsString('up to date', end($messages)); 133*ede46466SAndreas Gohr } 134*ede46466SAndreas Gohr} 135