xref: /dokuwiki/_test/tests/Search/Collection/DirectCollectionTest.php (revision 6734bb8cef71e8b4af23e627d4db5430304d55a2)
1<?php
2
3namespace dokuwiki\test\Search\Collection;
4
5use dokuwiki\Search\Collection\PageTitleCollection;
6use dokuwiki\Search\Exception\IndexLockException;
7use dokuwiki\Search\Index\MemoryIndex;
8
9class DirectCollectionTest extends \DokuWikiTest
10{
11    /**
12     * Add a token and verify it's stored at the entity's position
13     */
14    public function testAddEntity()
15    {
16        $index = new MockDirectCollection('a_entity', 'a_token');
17        $index->lock();
18        $index->addEntity('wiki:start', ['Welcome to DokuWiki']);
19        $index->unlock();
20
21        $idxEntity = new MemoryIndex('a_entity');
22        $this->assertEquals('wiki:start', $idxEntity->retrieveRow(0));
23
24        $idxToken = new MemoryIndex('a_token');
25        $this->assertEquals('Welcome to DokuWiki', $idxToken->retrieveRow(0));
26    }
27
28    /**
29     * Updating an entity should overwrite the previous token
30     */
31    public function testUpdateEntity()
32    {
33        $index = new MockDirectCollection('b_entity', 'b_token');
34
35        $index->lock();
36        $index->addEntity('wiki:start', ['Old Title']);
37        $index->unlock();
38
39        $index->lock();
40        $index->addEntity('wiki:start', ['New Title']);
41        $index->unlock();
42
43        $idxToken = new MemoryIndex('b_token');
44        $this->assertEquals('New Title', $idxToken->retrieveRow(0));
45    }
46
47    /**
48     * Empty token list should store empty string
49     */
50    public function testEmptyToken()
51    {
52        $index = new MockDirectCollection('c_entity', 'c_token');
53        $index->lock();
54        $index->addEntity('wiki:start', []);
55        $index->unlock();
56
57        $idxToken = new MemoryIndex('c_token');
58        $this->assertEquals('', $idxToken->retrieveRow(0));
59    }
60
61    /**
62     * getToken should return the stored value
63     */
64    public function testGetToken()
65    {
66        $index = new MockDirectCollection('d_entity', 'd_token');
67        $index->lock();
68        $index->addEntity('wiki:start', ['My Page Title']);
69        $index->unlock();
70
71        $this->assertEquals('My Page Title', $index->getToken('wiki:start'));
72    }
73
74    /**
75     * Adding entity without lock should throw exception
76     */
77    public function testAddEntityWithoutLock()
78    {
79        $this->expectException(IndexLockException::class);
80
81        $index = new MockDirectCollection();
82        $index->addEntity('wiki:start', ['Title']);
83    }
84
85    /**
86     * Test that PageTitleCollection uses correct index names
87     */
88    public function testPageTitleCollection()
89    {
90        $index = new PageTitleCollection();
91        $index->lock();
92        $index->addEntity('wiki:start', ['Welcome']);
93        $index->unlock();
94
95        $idxToken = new MemoryIndex('title');
96        $this->assertEquals('Welcome', $idxToken->retrieveRow(0));
97
98        $this->assertEquals('Welcome', $index->getToken('wiki:start'));
99    }
100
101    /**
102     * resolveTokenFrequencies maps token RID = entity RID with frequency 1
103     */
104    public function testResolveTokenFrequencies()
105    {
106        $index = new MockDirectCollection('rtf_entity', 'rtf_token');
107        $index->lock();
108        $index->addEntity('wiki:start', ['Title One']);
109        $index->addEntity('wiki:syntax', ['Title Two']);
110        $index->unlock();
111
112        $result = $index->resolveTokenFrequencies(0, [0, 1]);
113        $this->assertEquals([
114            0 => [0 => 1],
115            1 => [1 => 1],
116        ], $result);
117    }
118
119    /**
120     * getEntitiesWithData returns entities that have non-empty tokens
121     */
122    public function testGetEntitiesWithData()
123    {
124        $index = new MockDirectCollection('ewd_entity', 'ewd_token');
125        $index->lock();
126        $index->addEntity('wiki:start', ['Title One']);
127        $index->addEntity('wiki:empty', []);
128        $index->addEntity('wiki:syntax', ['Title Two']);
129        $index->unlock();
130
131        $result = $index->getEntitiesWithData();
132        sort($result);
133        $this->assertEquals(['wiki:start', 'wiki:syntax'], $result);
134    }
135}
136