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