xref: /dokuwiki/_test/tests/Search/Index/TupleOpsTest.php (revision 6734bb8cef71e8b4af23e627d4db5430304d55a2)
1<?php
2
3namespace dokuwiki\test\Search\Index;
4
5use dokuwiki\Search\Index\TupleOps;
6
7class TupleOpsTest extends \DokuWikiTest
8{
9    /**
10     * @see testUpdateTuple
11     */
12    public function provideUpdateTuple()
13    {
14        return [
15            ['', 'foo', 3, 'foo*3'],
16            ['', 17, 3, '17*3'],
17            ['foo*2', 'foo', 3, 'foo*3'],
18            ['17*2', 17, 3, '17*3'],
19            ['bar*2:foo*2', 'foo', 3, 'foo*3:bar*2'],
20            ['18*2:17*2', 17, 3, '17*3:18*2'],
21            // Test tuples without explicit count (implicit count of 1)
22            ['foo', 'foo', 3, 'foo*3'],
23            ['17', 17, 3, '17*3'],
24            ['bar:foo', 'foo', 3, 'foo*3:bar'],
25            ['bar*2:foo', 'foo', 5, 'foo*5:bar*2'],
26            ['18:17', 17, 3, '17*3:18'],
27            ['18:17', 19, 1, '19:18:17'],
28            // existing 1 counts are not updated unless touched directly
29            ['foo*4:bar*1:baz*3', 'uff', 2, 'uff*2:foo*4:bar*1:baz*3'],
30            ['foo*4:bar*1:baz*3', 'bar', 1, 'bar:foo*4:baz*3'],
31            // 0 is a valid entity
32            ['', 0, 1, '0'],
33            ['0*7', 0, 6, '0*6'],
34            ['foo:bar*3', 0, 1, '0:foo:bar*3'],
35            // frequency of 0 deletes
36            ['', 7, 0, ''],
37            ['0', 0, 0, ''],
38            ['foo*6:bar*3:baz', 'bar', 0, 'foo*6:baz'],
39        ];
40    }
41
42    /**
43     * @dataProvider provideUpdateTuple
44     */
45    public function testUpdateTuple($record, $key, $count, $expect)
46    {
47        $result = TupleOps::updateTuple($record, $key, $count);
48        $this->assertEquals($result, $expect);
49    }
50
51    /**
52     * @see testAggregateTupleCounts
53     */
54    public function provideAggregateTupleCounts()
55    {
56        return [
57            ['5*10:foo*2:14*100::bar*7', 119],
58            // Test with tuples without explicit count (implicit count of 1)
59            ['5:foo:14::bar', 4],
60            ['5*10:foo:14*100', 111],
61            ['key1:key2:key3', 3],
62            ['', 0],
63            ['single', 1],
64        ];
65    }
66
67    /**
68     * @dataProvider provideAggregateTupleCounts
69     */
70    public function testAggregateTupleCounts($record, $expected)
71    {
72        $result = TupleOps::aggregateTupleCounts($record);
73        $this->assertEquals($expected, $result);
74    }
75
76    /**
77     * @see testParseTuples
78     */
79    public function provideParseTuples()
80    {
81        return [
82            // Original test case
83            [
84                '5*10:foo*2:14*100::bar*7',
85                [5 => 'first', 'bar' => 'second', 'foo' => null],
86                ['first' => 10, 'second' => 7]
87            ],
88            // Test with tuples without explicit count (implicit count of 1)
89            [
90                '5:foo:14::bar',
91                [5 => 'first', 'bar' => 'second', 'foo' => null],
92                ['first' => 1, 'second' => 1]
93            ],
94            // Mixed: some with count, some without
95            [
96                '5*10:foo:14*100',
97                [5 => 'first', 'foo' => 'second', 14 => 'third'],
98                ['first' => 10, 'second' => 1, 'third' => 100]
99            ],
100            // No filter map (returns all with original keys)
101            [
102                '5*10:foo*2:bar',
103                null,
104                [5 => 10, 'foo' => 2, 'bar' => 1]
105            ],
106            // Empty record
107            [
108                '',
109                [5 => 'first'],
110                []
111            ],
112        ];
113    }
114
115    /**
116     * @dataProvider provideParseTuples
117     */
118    public function testParseTuples($record, $keys, $expect)
119    {
120        $result = TupleOps::parseTuples($record, $keys);
121        $this->assertEquals($expect, $result);
122    }
123
124}
125