xref: /dokuwiki/_test/tests/Search/Collection/DirectCollectionTest.php (revision ede4646658cf51245060332d97a319a39c788ea1)
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