xref: /dokuwiki/_test/tests/Search/Collection/DirectCollectionTest.php (revision ede4646658cf51245060332d97a319a39c788ea1)
1*ede46466SAndreas Gohr<?php
2*ede46466SAndreas Gohr
3*ede46466SAndreas Gohrnamespace dokuwiki\test\Search\Collection;
4*ede46466SAndreas Gohr
5*ede46466SAndreas Gohruse dokuwiki\Search\Collection\PageTitleCollection;
6*ede46466SAndreas Gohruse dokuwiki\Search\Exception\IndexLockException;
7*ede46466SAndreas Gohruse dokuwiki\Search\Index\MemoryIndex;
8*ede46466SAndreas Gohr
9*ede46466SAndreas Gohrclass DirectCollectionTest extends \DokuWikiTest
10*ede46466SAndreas Gohr{
11*ede46466SAndreas Gohr    /**
12*ede46466SAndreas Gohr     * Add a token and verify it's stored at the entity's position
13*ede46466SAndreas Gohr     */
14*ede46466SAndreas Gohr    public function testAddEntity()
15*ede46466SAndreas Gohr    {
16*ede46466SAndreas Gohr        $index = new MockDirectCollection('a_entity', 'a_token');
17*ede46466SAndreas Gohr        $index->lock();
18*ede46466SAndreas Gohr        $index->addEntity('wiki:start', ['Welcome to DokuWiki']);
19*ede46466SAndreas Gohr        $index->unlock();
20*ede46466SAndreas Gohr
21*ede46466SAndreas Gohr        $idxEntity = new MemoryIndex('a_entity');
22*ede46466SAndreas Gohr        $this->assertEquals('wiki:start', $idxEntity->retrieveRow(0));
23*ede46466SAndreas Gohr
24*ede46466SAndreas Gohr        $idxToken = new MemoryIndex('a_token');
25*ede46466SAndreas Gohr        $this->assertEquals('Welcome to DokuWiki', $idxToken->retrieveRow(0));
26*ede46466SAndreas Gohr    }
27*ede46466SAndreas Gohr
28*ede46466SAndreas Gohr    /**
29*ede46466SAndreas Gohr     * Updating an entity should overwrite the previous token
30*ede46466SAndreas Gohr     */
31*ede46466SAndreas Gohr    public function testUpdateEntity()
32*ede46466SAndreas Gohr    {
33*ede46466SAndreas Gohr        $index = new MockDirectCollection('b_entity', 'b_token');
34*ede46466SAndreas Gohr
35*ede46466SAndreas Gohr        $index->lock();
36*ede46466SAndreas Gohr        $index->addEntity('wiki:start', ['Old Title']);
37*ede46466SAndreas Gohr        $index->unlock();
38*ede46466SAndreas Gohr
39*ede46466SAndreas Gohr        $index->lock();
40*ede46466SAndreas Gohr        $index->addEntity('wiki:start', ['New Title']);
41*ede46466SAndreas Gohr        $index->unlock();
42*ede46466SAndreas Gohr
43*ede46466SAndreas Gohr        $idxToken = new MemoryIndex('b_token');
44*ede46466SAndreas Gohr        $this->assertEquals('New Title', $idxToken->retrieveRow(0));
45*ede46466SAndreas Gohr    }
46*ede46466SAndreas Gohr
47*ede46466SAndreas Gohr    /**
48*ede46466SAndreas Gohr     * Empty token list should store empty string
49*ede46466SAndreas Gohr     */
50*ede46466SAndreas Gohr    public function testEmptyToken()
51*ede46466SAndreas Gohr    {
52*ede46466SAndreas Gohr        $index = new MockDirectCollection('c_entity', 'c_token');
53*ede46466SAndreas Gohr        $index->lock();
54*ede46466SAndreas Gohr        $index->addEntity('wiki:start', []);
55*ede46466SAndreas Gohr        $index->unlock();
56*ede46466SAndreas Gohr
57*ede46466SAndreas Gohr        $idxToken = new MemoryIndex('c_token');
58*ede46466SAndreas Gohr        $this->assertEquals('', $idxToken->retrieveRow(0));
59*ede46466SAndreas Gohr    }
60*ede46466SAndreas Gohr
61*ede46466SAndreas Gohr    /**
62*ede46466SAndreas Gohr     * getToken should return the stored value
63*ede46466SAndreas Gohr     */
64*ede46466SAndreas Gohr    public function testGetToken()
65*ede46466SAndreas Gohr    {
66*ede46466SAndreas Gohr        $index = new MockDirectCollection('d_entity', 'd_token');
67*ede46466SAndreas Gohr        $index->lock();
68*ede46466SAndreas Gohr        $index->addEntity('wiki:start', ['My Page Title']);
69*ede46466SAndreas Gohr        $index->unlock();
70*ede46466SAndreas Gohr
71*ede46466SAndreas Gohr        $this->assertEquals('My Page Title', $index->getToken('wiki:start'));
72*ede46466SAndreas Gohr    }
73*ede46466SAndreas Gohr
74*ede46466SAndreas Gohr    /**
75*ede46466SAndreas Gohr     * Adding entity without lock should throw exception
76*ede46466SAndreas Gohr     */
77*ede46466SAndreas Gohr    public function testAddEntityWithoutLock()
78*ede46466SAndreas Gohr    {
79*ede46466SAndreas Gohr        $this->expectException(IndexLockException::class);
80*ede46466SAndreas Gohr
81*ede46466SAndreas Gohr        $index = new MockDirectCollection();
82*ede46466SAndreas Gohr        $index->addEntity('wiki:start', ['Title']);
83*ede46466SAndreas Gohr    }
84*ede46466SAndreas Gohr
85*ede46466SAndreas Gohr    /**
86*ede46466SAndreas Gohr     * Test that PageTitleCollection uses correct index names
87*ede46466SAndreas Gohr     */
88*ede46466SAndreas Gohr    public function testPageTitleCollection()
89*ede46466SAndreas Gohr    {
90*ede46466SAndreas Gohr        $index = new PageTitleCollection();
91*ede46466SAndreas Gohr        $index->lock();
92*ede46466SAndreas Gohr        $index->addEntity('wiki:start', ['Welcome']);
93*ede46466SAndreas Gohr        $index->unlock();
94*ede46466SAndreas Gohr
95*ede46466SAndreas Gohr        $idxToken = new MemoryIndex('title');
96*ede46466SAndreas Gohr        $this->assertEquals('Welcome', $idxToken->retrieveRow(0));
97*ede46466SAndreas Gohr
98*ede46466SAndreas Gohr        $this->assertEquals('Welcome', $index->getToken('wiki:start'));
99*ede46466SAndreas Gohr    }
100*ede46466SAndreas Gohr}
101