xref: /dokuwiki/_test/tests/Search/Index/MemoryIndexTest.php (revision 2d05a06d7893e8c100d5357c6301ffae08b00522)
1<?php
2
3namespace dokuwiki\test\Search\Index;
4
5use dokuwiki\Search\Index\Lock;
6use dokuwiki\Search\Index\MemoryIndex;
7
8class MemoryIndexTest extends AbstractIndexTestCase
9{
10    protected function getIndex()
11    {
12        static $count = 0;
13        return new MemoryIndex('index', $count++, true);
14    }
15
16    public function tearDown(): void
17    {
18        Lock::releaseAll();
19        parent::tearDown();
20    }
21
22    public function testChangeRow()
23    {
24        $index = $this->getIndex();
25
26        $index->changeRow(5, 'test');
27        $full = $this->getInaccessibleProperty($index, 'data');
28        $this->assertEquals(6, count($full));
29
30        $index->changeRow(3, 'foo');
31        $full = $this->getInaccessibleProperty($index, 'data');
32        $this->assertEquals(6, count($full));
33
34        $index->changeRow(5, 'bar');
35        $index->changeRow(7, 'bang');
36
37        $full = $this->getInaccessibleProperty($index, 'data');
38        $this->assertEquals(['', '', '', 'foo', '', 'bar', '', 'bang'], $full);
39
40        $index->save();
41        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
42        $this->assertEquals(['', '', '', 'foo', '', 'bar', '', 'bang'], $full);
43    }
44
45    public function testRetrieveRow()
46    {
47        $index = $this->getIndex();
48        $index->changeRow(5, 'test');
49        $this->assertEquals('test', $index->retrieveRow(5));
50
51        // out of bounds line should be empty and not modify the index
52        $this->assertEquals('', $index->retrieveRow(10));
53        $index->save();
54        $full = file($index->getFilename(), FILE_IGNORE_NEW_LINES);
55        $this->assertEquals(6, count($full));
56    }
57
58    public function testSave()
59    {
60        $index = $this->getIndex();
61        $this->assertFileDoesNotExist($index->getFilename());
62        $this->assertFalse($index->isDirty());
63
64        $index->changeRow(0, '');
65        $this->assertTrue($index->isDirty());
66        $index->save();
67        $this->assertFalse($index->isDirty());
68        $this->assertEquals(1, filesize($index->getFilename())); // new line
69
70        $index->changeRow(3, 'test');
71        $this->assertTrue($index->isDirty());
72        $index->save();
73        $this->assertFalse($index->isDirty());
74        $this->assertEquals(8, filesize($index->getFilename())); // 4 new lines + test
75
76        $index->getRowID('test'); // existing entry
77        $index->save();
78        $this->assertFalse($index->isDirty());
79    }
80
81    public function testMaxReturnsFullNumericSuffix()
82    {
83        foreach ([9, 10, 12] as $suffix) {
84            $index = new MemoryIndex('indexmax', (string)$suffix, true);
85            $index->changeRow(0, 'test');
86            $index->save();
87            $index->unlock();
88        }
89
90        $index = new MemoryIndex('indexmax');
91        $this->assertEquals(12, $index->max());
92    }
93
94    public function testMaxIgnoresOtherIndexesWithSamePrefix()
95    {
96        $index = new MemoryIndex('w', '10', true);
97        $index->changeRow(0, 'test');
98        $index->save();
99        $index->unlock();
100
101        $other = new MemoryIndex('wiki', '2', true);
102        $other->changeRow(0, 'test');
103        $other->save();
104        $other->unlock();
105
106        $index = new MemoryIndex('w');
107        $this->assertEquals(10, $index->max());
108    }
109
110}
111