xref: /plugin/struct/_test/SearchTest.php (revision 8fed17f342cc190557a6ce94d1787f9e2f63cb6c)
1*8fed17f3SAndreas Gohr<?php
2*8fed17f3SAndreas Gohr
3*8fed17f3SAndreas Gohrnamespace dokuwiki\plugin\struct\test;
4*8fed17f3SAndreas Gohr
5*8fed17f3SAndreas Gohruse dokuwiki\plugin\struct\meta;
6*8fed17f3SAndreas Gohr
7*8fed17f3SAndreas Gohr/**
8*8fed17f3SAndreas Gohr * Tests for the building of SQL-Queries for the struct plugin
9*8fed17f3SAndreas Gohr *
10*8fed17f3SAndreas Gohr * @group plugin_struct
11*8fed17f3SAndreas Gohr * @group plugins
12*8fed17f3SAndreas Gohr *
13*8fed17f3SAndreas Gohr */
14*8fed17f3SAndreas Gohrclass SearchTest extends StructTest
15*8fed17f3SAndreas Gohr{
16*8fed17f3SAndreas Gohr
17*8fed17f3SAndreas Gohr    public function setUp(): void
18*8fed17f3SAndreas Gohr    {
19*8fed17f3SAndreas Gohr        parent::setUp();
20*8fed17f3SAndreas Gohr
21*8fed17f3SAndreas Gohr        $this->loadSchemaJSON('schema1');
22*8fed17f3SAndreas Gohr        $this->loadSchemaJSON('schema2');
23*8fed17f3SAndreas Gohr        $_SERVER['REMOTE_USER'] = 'testuser';
24*8fed17f3SAndreas Gohr
25*8fed17f3SAndreas Gohr        $as = mock\Assignments::getInstance();
26*8fed17f3SAndreas Gohr        $page = 'page01';
27*8fed17f3SAndreas Gohr        $as->assignPageSchema($page, 'schema1');
28*8fed17f3SAndreas Gohr        $as->assignPageSchema($page, 'schema2');
29*8fed17f3SAndreas Gohr        saveWikiText($page, "===== TestTitle =====\nabc", "Summary");
30*8fed17f3SAndreas Gohr        p_get_metadata($page);
31*8fed17f3SAndreas Gohr        $now = time();
32*8fed17f3SAndreas Gohr        $this->saveData(
33*8fed17f3SAndreas Gohr            $page,
34*8fed17f3SAndreas Gohr            'schema1',
35*8fed17f3SAndreas Gohr            [
36*8fed17f3SAndreas Gohr                'first' => 'first data',
37*8fed17f3SAndreas Gohr                'second' => ['second data', 'more data', 'even more'],
38*8fed17f3SAndreas Gohr                'third' => 'third data',
39*8fed17f3SAndreas Gohr                'fourth' => 'fourth data'
40*8fed17f3SAndreas Gohr            ],
41*8fed17f3SAndreas Gohr            $now
42*8fed17f3SAndreas Gohr        );
43*8fed17f3SAndreas Gohr        $this->saveData(
44*8fed17f3SAndreas Gohr            $page,
45*8fed17f3SAndreas Gohr            'schema2',
46*8fed17f3SAndreas Gohr            [
47*8fed17f3SAndreas Gohr                'afirst' => 'first data',
48*8fed17f3SAndreas Gohr                'asecond' => ['second data', 'more data', 'even more'],
49*8fed17f3SAndreas Gohr                'athird' => 'third data',
50*8fed17f3SAndreas Gohr                'afourth' => 'fourth data'
51*8fed17f3SAndreas Gohr            ],
52*8fed17f3SAndreas Gohr            $now
53*8fed17f3SAndreas Gohr        );
54*8fed17f3SAndreas Gohr
55*8fed17f3SAndreas Gohr        $as->assignPageSchema('test:document', 'schema1');
56*8fed17f3SAndreas Gohr        $as->assignPageSchema('test:document', 'schema2');
57*8fed17f3SAndreas Gohr        $this->saveData(
58*8fed17f3SAndreas Gohr            'test:document',
59*8fed17f3SAndreas Gohr            'schema1',
60*8fed17f3SAndreas Gohr            [
61*8fed17f3SAndreas Gohr                'first' => 'document first data',
62*8fed17f3SAndreas Gohr                'second' => ['second', 'more'],
63*8fed17f3SAndreas Gohr                'third' => '',
64*8fed17f3SAndreas Gohr                'fourth' => 'fourth data'
65*8fed17f3SAndreas Gohr            ],
66*8fed17f3SAndreas Gohr            $now
67*8fed17f3SAndreas Gohr        );
68*8fed17f3SAndreas Gohr        $this->saveData(
69*8fed17f3SAndreas Gohr            'test:document',
70*8fed17f3SAndreas Gohr            'schema2',
71*8fed17f3SAndreas Gohr            [
72*8fed17f3SAndreas Gohr                'afirst' => 'first data',
73*8fed17f3SAndreas Gohr                'asecond' => ['second data', 'more data', 'even more'],
74*8fed17f3SAndreas Gohr                'athird' => 'third data',
75*8fed17f3SAndreas Gohr                'afourth' => 'fourth data'
76*8fed17f3SAndreas Gohr            ],
77*8fed17f3SAndreas Gohr            $now
78*8fed17f3SAndreas Gohr        );
79*8fed17f3SAndreas Gohr
80*8fed17f3SAndreas Gohr        for ($i = 10; $i <= 20; $i++) {
81*8fed17f3SAndreas Gohr            $this->saveData(
82*8fed17f3SAndreas Gohr                "page$i",
83*8fed17f3SAndreas Gohr                'schema2',
84*8fed17f3SAndreas Gohr                [
85*8fed17f3SAndreas Gohr                    'afirst' => "page$i first data",
86*8fed17f3SAndreas Gohr                    'asecond' => ["page$i second data"],
87*8fed17f3SAndreas Gohr                    'athird' => "page$i third data",
88*8fed17f3SAndreas Gohr                    'afourth' => "page$i fourth data"
89*8fed17f3SAndreas Gohr                ],
90*8fed17f3SAndreas Gohr                $now
91*8fed17f3SAndreas Gohr            );
92*8fed17f3SAndreas Gohr            $as->assignPageSchema("page$i", 'schema2');
93*8fed17f3SAndreas Gohr        }
94*8fed17f3SAndreas Gohr    }
95*8fed17f3SAndreas Gohr
96*8fed17f3SAndreas Gohr    public function test_simple()
97*8fed17f3SAndreas Gohr    {
98*8fed17f3SAndreas Gohr        $search = new mock\Search();
99*8fed17f3SAndreas Gohr
100*8fed17f3SAndreas Gohr        $search->addSchema('schema1');
101*8fed17f3SAndreas Gohr        $search->addColumn('%pageid%');
102*8fed17f3SAndreas Gohr        $search->addColumn('first');
103*8fed17f3SAndreas Gohr        $search->addColumn('second');
104*8fed17f3SAndreas Gohr
105*8fed17f3SAndreas Gohr        /** @var meta\Value[][] $result */
106*8fed17f3SAndreas Gohr        $result = $search->execute();
107*8fed17f3SAndreas Gohr
108*8fed17f3SAndreas Gohr        $this->assertCount(2, $result, 'result rows');
109*8fed17f3SAndreas Gohr        $this->assertCount(3, $result[0], 'result columns');
110*8fed17f3SAndreas Gohr        $this->assertEquals('page01', $result[0][0]->getValue());
111*8fed17f3SAndreas Gohr        $this->assertEquals('first data', $result[0][1]->getValue());
112*8fed17f3SAndreas Gohr        $this->assertEquals(['second data', 'more data', 'even more'], $result[0][2]->getValue());
113*8fed17f3SAndreas Gohr    }
114*8fed17f3SAndreas Gohr
115*8fed17f3SAndreas Gohr    public function test_simple_title()
116*8fed17f3SAndreas Gohr    {
117*8fed17f3SAndreas Gohr        $search = new mock\Search();
118*8fed17f3SAndreas Gohr
119*8fed17f3SAndreas Gohr        $search->addSchema('schema1');
120*8fed17f3SAndreas Gohr        $search->addColumn('%title%');
121*8fed17f3SAndreas Gohr        $search->addColumn('first');
122*8fed17f3SAndreas Gohr        $search->addColumn('second');
123*8fed17f3SAndreas Gohr
124*8fed17f3SAndreas Gohr        /** @var meta\Value[][] $result */
125*8fed17f3SAndreas Gohr        $result = $search->execute();
126*8fed17f3SAndreas Gohr
127*8fed17f3SAndreas Gohr        $this->assertCount(2, $result, 'result rows');
128*8fed17f3SAndreas Gohr        $this->assertCount(3, $result[0], 'result columns');
129*8fed17f3SAndreas Gohr        $this->assertEquals('["page01","TestTitle"]', $result[0][0]->getValue());
130*8fed17f3SAndreas Gohr        $this->assertEquals('first data', $result[0][1]->getValue());
131*8fed17f3SAndreas Gohr        $this->assertEquals(['second data', 'more data', 'even more'], $result[0][2]->getValue());
132*8fed17f3SAndreas Gohr    }
133*8fed17f3SAndreas Gohr
134*8fed17f3SAndreas Gohr    public function test_search_published()
135*8fed17f3SAndreas Gohr    {
136*8fed17f3SAndreas Gohr        $search = new mock\Search();
137*8fed17f3SAndreas Gohr        $search->isNotPublisher();
138*8fed17f3SAndreas Gohr
139*8fed17f3SAndreas Gohr        $search->addSchema('schema1');
140*8fed17f3SAndreas Gohr        $search->addColumn('%pageid%');
141*8fed17f3SAndreas Gohr        $search->addColumn('first');
142*8fed17f3SAndreas Gohr        $search->addColumn('second');
143*8fed17f3SAndreas Gohr
144*8fed17f3SAndreas Gohr        /** @var meta\Value[][] $result */
145*8fed17f3SAndreas Gohr        $result = $search->execute();
146*8fed17f3SAndreas Gohr
147*8fed17f3SAndreas Gohr        $this->assertCount(0, $result, 'result rows');
148*8fed17f3SAndreas Gohr    }
149*8fed17f3SAndreas Gohr
150*8fed17f3SAndreas Gohr    public function test_search_lasteditor()
151*8fed17f3SAndreas Gohr    {
152*8fed17f3SAndreas Gohr        $search = new mock\Search();
153*8fed17f3SAndreas Gohr
154*8fed17f3SAndreas Gohr        $search->addSchema('schema1');
155*8fed17f3SAndreas Gohr        $search->addColumn('%title%');
156*8fed17f3SAndreas Gohr        $search->addColumn('%lasteditor%');
157*8fed17f3SAndreas Gohr        $search->addColumn('first');
158*8fed17f3SAndreas Gohr        $search->addColumn('second');
159*8fed17f3SAndreas Gohr
160*8fed17f3SAndreas Gohr        /** @var meta\Value[][] $result */
161*8fed17f3SAndreas Gohr        $result = $search->execute();
162*8fed17f3SAndreas Gohr
163*8fed17f3SAndreas Gohr        $this->assertCount(2, $result, 'result rows');
164*8fed17f3SAndreas Gohr        $this->assertCount(4, $result[0], 'result columns');
165*8fed17f3SAndreas Gohr        $this->assertEquals('testuser', $result[0][1]->getValue());
166*8fed17f3SAndreas Gohr        $this->assertEquals(['second data', 'more data', 'even more'], $result[0][3]->getValue());
167*8fed17f3SAndreas Gohr    }
168*8fed17f3SAndreas Gohr
169*8fed17f3SAndreas Gohr
170*8fed17f3SAndreas Gohr    /**
171*8fed17f3SAndreas Gohr     * @group slow
172*8fed17f3SAndreas Gohr     */
173*8fed17f3SAndreas Gohr    public function test_search_lastupdate()
174*8fed17f3SAndreas Gohr    {
175*8fed17f3SAndreas Gohr        sleep(1);
176*8fed17f3SAndreas Gohr        saveWikiText('page01', "===== TestTitle =====\nabcd", "Summary");
177*8fed17f3SAndreas Gohr        p_get_metadata('page01');
178*8fed17f3SAndreas Gohr
179*8fed17f3SAndreas Gohr        $search = new mock\Search();
180*8fed17f3SAndreas Gohr
181*8fed17f3SAndreas Gohr        $search->addSchema('schema1');
182*8fed17f3SAndreas Gohr        $search->addColumn('%pageid%');
183*8fed17f3SAndreas Gohr        $search->addColumn('%lastupdate%');
184*8fed17f3SAndreas Gohr        $search->addColumn('first');
185*8fed17f3SAndreas Gohr        $search->addColumn('second');
186*8fed17f3SAndreas Gohr
187*8fed17f3SAndreas Gohr        /** @var meta\Value[][] $result */
188*8fed17f3SAndreas Gohr        $result = $search->execute();
189*8fed17f3SAndreas Gohr
190*8fed17f3SAndreas Gohr        $expected_time = dformat(filemtime(wikiFN('page01')), '%Y-%m-%d %H:%M:%S');
191*8fed17f3SAndreas Gohr
192*8fed17f3SAndreas Gohr        $this->assertCount(2, $result, 'result rows');
193*8fed17f3SAndreas Gohr        $this->assertCount(4, $result[0], 'result columns');
194*8fed17f3SAndreas Gohr        $this->assertEquals($expected_time, $result[0][1]->getValue());
195*8fed17f3SAndreas Gohr        $this->assertEquals(['second data', 'more data', 'even more'], $result[0][3]->getValue());
196*8fed17f3SAndreas Gohr    }
197*8fed17f3SAndreas Gohr
198*8fed17f3SAndreas Gohr    /**
199*8fed17f3SAndreas Gohr     * @group slow
200*8fed17f3SAndreas Gohr     */
201*8fed17f3SAndreas Gohr    public function test_search_lastsummary()
202*8fed17f3SAndreas Gohr    {
203*8fed17f3SAndreas Gohr        sleep(1);
204*8fed17f3SAndreas Gohr        $summary = 'Summary';
205*8fed17f3SAndreas Gohr        saveWikiText('page01', "===== TestTitle =====\nabcd", $summary);
206*8fed17f3SAndreas Gohr        p_get_metadata('page01');
207*8fed17f3SAndreas Gohr
208*8fed17f3SAndreas Gohr        $search = new mock\Search();
209*8fed17f3SAndreas Gohr
210*8fed17f3SAndreas Gohr        $search->addSchema('schema1');
211*8fed17f3SAndreas Gohr        $search->addColumn('%pageid%');
212*8fed17f3SAndreas Gohr        $search->addColumn('%lastsummary%');
213*8fed17f3SAndreas Gohr        $search->addColumn('first');
214*8fed17f3SAndreas Gohr        $search->addColumn('second');
215*8fed17f3SAndreas Gohr
216*8fed17f3SAndreas Gohr        /** @var meta\Value[][] $result */
217*8fed17f3SAndreas Gohr        $result = $search->execute();
218*8fed17f3SAndreas Gohr
219*8fed17f3SAndreas Gohr        $this->assertCount(2, $result, 'result rows');
220*8fed17f3SAndreas Gohr        $this->assertCount(4, $result[0], 'result columns');
221*8fed17f3SAndreas Gohr        $this->assertEquals($summary, $result[0][1]->getValue());
222*8fed17f3SAndreas Gohr        $this->assertEquals(array('second data', 'more data', 'even more'), $result[0][3]->getValue());
223*8fed17f3SAndreas Gohr    }
224*8fed17f3SAndreas Gohr
225*8fed17f3SAndreas Gohr    public function test_search()
226*8fed17f3SAndreas Gohr    {
227*8fed17f3SAndreas Gohr        $search = new mock\Search();
228*8fed17f3SAndreas Gohr
229*8fed17f3SAndreas Gohr        $search->addSchema('schema1');
230*8fed17f3SAndreas Gohr        $search->addSchema('schema2', 'foo');
231*8fed17f3SAndreas Gohr        $this->assertCount(2, $search->schemas);
232*8fed17f3SAndreas Gohr
233*8fed17f3SAndreas Gohr        $search->addColumn('first');
234*8fed17f3SAndreas Gohr        $this->assertEquals('schema1', $search->columns[0]->getTable());
235*8fed17f3SAndreas Gohr        $this->assertEquals(1, $search->columns[0]->getColref());
236*8fed17f3SAndreas Gohr
237*8fed17f3SAndreas Gohr        $search->addColumn('afirst');
238*8fed17f3SAndreas Gohr        $this->assertEquals('schema2', $search->columns[1]->getTable());
239*8fed17f3SAndreas Gohr        $this->assertEquals(1, $search->columns[1]->getColref());
240*8fed17f3SAndreas Gohr
241*8fed17f3SAndreas Gohr        $search->addColumn('schema1.third');
242*8fed17f3SAndreas Gohr        $this->assertEquals('schema1', $search->columns[2]->getTable());
243*8fed17f3SAndreas Gohr        $this->assertEquals(3, $search->columns[2]->getColref());
244*8fed17f3SAndreas Gohr
245*8fed17f3SAndreas Gohr        $search->addColumn('foo.athird');
246*8fed17f3SAndreas Gohr        $this->assertEquals('schema2', $search->columns[3]->getTable());
247*8fed17f3SAndreas Gohr        $this->assertEquals(3, $search->columns[3]->getColref());
248*8fed17f3SAndreas Gohr
249*8fed17f3SAndreas Gohr        $search->addColumn('asecond');
250*8fed17f3SAndreas Gohr        $this->assertEquals('schema2', $search->columns[4]->getTable());
251*8fed17f3SAndreas Gohr        $this->assertEquals(2, $search->columns[4]->getColref());
252*8fed17f3SAndreas Gohr
253*8fed17f3SAndreas Gohr        $search->addColumn('doesntexist');
254*8fed17f3SAndreas Gohr        $this->assertEquals(5, count($search->columns));
255*8fed17f3SAndreas Gohr
256*8fed17f3SAndreas Gohr        $search->addColumn('%pageid%');
257*8fed17f3SAndreas Gohr        $this->assertEquals('schema1', $search->columns[5]->getTable());
258*8fed17f3SAndreas Gohr        $exception = false;
259*8fed17f3SAndreas Gohr        try {
260*8fed17f3SAndreas Gohr            $search->columns[5]->getColref();
261*8fed17f3SAndreas Gohr        } catch (meta\StructException $e) {
262*8fed17f3SAndreas Gohr            $exception = true;
263*8fed17f3SAndreas Gohr        }
264*8fed17f3SAndreas Gohr        $this->assertTrue($exception, "Struct exception expected for accesing colref of PageColumn");
265*8fed17f3SAndreas Gohr
266*8fed17f3SAndreas Gohr        $search->addSort('first', false);
267*8fed17f3SAndreas Gohr        $this->assertCount(1, $search->sortby);
268*8fed17f3SAndreas Gohr
269*8fed17f3SAndreas Gohr        $search->addFilter('%pageid%', '%ag%', '~', 'AND');
270*8fed17f3SAndreas Gohr        $search->addFilter('second', '%sec%', '~', 'AND');
271*8fed17f3SAndreas Gohr        $search->addFilter('first', '%rst%', '~', 'AND');
272*8fed17f3SAndreas Gohr
273*8fed17f3SAndreas Gohr        $result = $search->execute();
274*8fed17f3SAndreas Gohr        $count = $search->getCount();
275*8fed17f3SAndreas Gohr
276*8fed17f3SAndreas Gohr        $this->assertEquals(1, $count, 'result count');
277*8fed17f3SAndreas Gohr        $this->assertCount(1, $result, 'result rows');
278*8fed17f3SAndreas Gohr        $this->assertCount(6, $result[0], 'result columns');
279*8fed17f3SAndreas Gohr
280*8fed17f3SAndreas Gohr        // sort by multi-column
281*8fed17f3SAndreas Gohr        $search->addSort('second');
282*8fed17f3SAndreas Gohr        $this->assertCount(2, $search->sortby);
283*8fed17f3SAndreas Gohr        $result = $search->execute();
284*8fed17f3SAndreas Gohr        $count = $search->getCount();
285*8fed17f3SAndreas Gohr        $this->assertEquals(1, $count, 'result count');
286*8fed17f3SAndreas Gohr        $this->assertCount(1, $result, 'result rows');
287*8fed17f3SAndreas Gohr        $this->assertCount(6, $result[0], 'result columns');
288*8fed17f3SAndreas Gohr
289*8fed17f3SAndreas Gohr        /*
290*8fed17f3SAndreas Gohr        {#debugging
291*8fed17f3SAndreas Gohr            list($sql, $opts) = $search->getSQL();
292*8fed17f3SAndreas Gohr            print "\n";
293*8fed17f3SAndreas Gohr            print_r($sql);
294*8fed17f3SAndreas Gohr            print "\n";
295*8fed17f3SAndreas Gohr            print_r($opts);
296*8fed17f3SAndreas Gohr            print "\n";
297*8fed17f3SAndreas Gohr            #print_r($result);
298*8fed17f3SAndreas Gohr        }
299*8fed17f3SAndreas Gohr        */
300*8fed17f3SAndreas Gohr    }
301*8fed17f3SAndreas Gohr
302*8fed17f3SAndreas Gohr    public function test_ranges()
303*8fed17f3SAndreas Gohr    {
304*8fed17f3SAndreas Gohr        $search = new mock\Search();
305*8fed17f3SAndreas Gohr        $search->addSchema('schema2');
306*8fed17f3SAndreas Gohr
307*8fed17f3SAndreas Gohr        $search->addColumn('%pageid%');
308*8fed17f3SAndreas Gohr        $search->addColumn('afirst');
309*8fed17f3SAndreas Gohr        $search->addColumn('asecond');
310*8fed17f3SAndreas Gohr
311*8fed17f3SAndreas Gohr        $search->addFilter('%pageid%', '%ag%', '~', 'AND');
312*8fed17f3SAndreas Gohr
313*8fed17f3SAndreas Gohr        $search->addSort('%pageid%', false);
314*8fed17f3SAndreas Gohr
315*8fed17f3SAndreas Gohr        /** @var meta\Value[][] $result */
316*8fed17f3SAndreas Gohr        $result = $search->execute();
317*8fed17f3SAndreas Gohr        $count = $search->getCount();
318*8fed17f3SAndreas Gohr
319*8fed17f3SAndreas Gohr        // check result dimensions
320*8fed17f3SAndreas Gohr        $this->assertEquals(12, $count, 'result count');
321*8fed17f3SAndreas Gohr        $this->assertCount(12, $result, 'result rows');
322*8fed17f3SAndreas Gohr        $this->assertCount(3, $result[0], 'result columns');
323*8fed17f3SAndreas Gohr
324*8fed17f3SAndreas Gohr        // check sorting
325*8fed17f3SAndreas Gohr        $this->assertEquals('page20', $result[0][0]->getValue());
326*8fed17f3SAndreas Gohr        $this->assertEquals('page19', $result[1][0]->getValue());
327*8fed17f3SAndreas Gohr        $this->assertEquals('page18', $result[2][0]->getValue());
328*8fed17f3SAndreas Gohr
329*8fed17f3SAndreas Gohr        // now add limit
330*8fed17f3SAndreas Gohr        $search->setLimit(5);
331*8fed17f3SAndreas Gohr        $result = $search->execute();
332*8fed17f3SAndreas Gohr        $count = $search->getCount();
333*8fed17f3SAndreas Gohr
334*8fed17f3SAndreas Gohr        // check result dimensions
335*8fed17f3SAndreas Gohr        $this->assertEquals(12, $count, 'result count'); // full result set
336*8fed17f3SAndreas Gohr        $this->assertCount(5, $result, 'result rows'); // wanted result set
337*8fed17f3SAndreas Gohr
338*8fed17f3SAndreas Gohr        // check the values
339*8fed17f3SAndreas Gohr        $this->assertEquals('page20', $result[0][0]->getValue());
340*8fed17f3SAndreas Gohr        $this->assertEquals('page16', $result[4][0]->getValue());
341*8fed17f3SAndreas Gohr
342*8fed17f3SAndreas Gohr        // now add offset
343*8fed17f3SAndreas Gohr        $search->setOffset(5);
344*8fed17f3SAndreas Gohr        $result = $search->execute();
345*8fed17f3SAndreas Gohr        $count = $search->getCount();
346*8fed17f3SAndreas Gohr
347*8fed17f3SAndreas Gohr        // check result dimensions
348*8fed17f3SAndreas Gohr        $this->assertEquals(12, $count, 'result count'); // full result set
349*8fed17f3SAndreas Gohr        $this->assertCount(5, $result, 'result rows'); // wanted result set
350*8fed17f3SAndreas Gohr
351*8fed17f3SAndreas Gohr        // check the values
352*8fed17f3SAndreas Gohr        $this->assertEquals('page15', $result[0][0]->getValue());
353*8fed17f3SAndreas Gohr        $this->assertEquals('page11', $result[4][0]->getValue());
354*8fed17f3SAndreas Gohr    }
355*8fed17f3SAndreas Gohr
356*8fed17f3SAndreas Gohr    public static function addFilter_testdata()
357*8fed17f3SAndreas Gohr    {
358*8fed17f3SAndreas Gohr        return [
359*8fed17f3SAndreas Gohr            ['%pageid%', 'val', '<>', 'OR', [['%pageid%', 'val', '!=', 'OR']], false, 'replace <> comp'],
360*8fed17f3SAndreas Gohr            ['%pageid%', 'val', '*~', 'OR', [['%pageid%', '%val%', 'LIKE', 'OR']], false, 'replace *~ comp'],
361*8fed17f3SAndreas Gohr            ['%pageid%', 'val*', '~', 'OR', [['%pageid%', 'val%', 'LIKE', 'OR']], false, 'replace * in value'],
362*8fed17f3SAndreas Gohr            ['%pageid%', 'val.*', '=*', 'OR', [['%pageid%', 'val.*', 'REGEXP', 'OR']], false, 'replace * in value'],
363*8fed17f3SAndreas Gohr            ['nonexisting', 'val', '~', 'OR', [], false, 'ignore missing columns'],
364*8fed17f3SAndreas Gohr            ['%pageid%', 'val', '?', 'OR', [], '\dokuwiki\plugin\struct\meta\StructException', 'wrong comperator'],
365*8fed17f3SAndreas Gohr            ['%pageid%', 'val', '=', 'NOT', [], '\dokuwiki\plugin\struct\meta\StructException', 'wrong type']
366*8fed17f3SAndreas Gohr        ];
367*8fed17f3SAndreas Gohr    }
368*8fed17f3SAndreas Gohr
369*8fed17f3SAndreas Gohr    /**
370*8fed17f3SAndreas Gohr     * @dataProvider addFilter_testdata
371*8fed17f3SAndreas Gohr     *
372*8fed17f3SAndreas Gohr     */
373*8fed17f3SAndreas Gohr    public function test_addFilter($colname, $value, $comp, $type, $expected_filter, $expectException, $msg)
374*8fed17f3SAndreas Gohr    {
375*8fed17f3SAndreas Gohr        $search = new mock\Search();
376*8fed17f3SAndreas Gohr        $search->addSchema('schema2');
377*8fed17f3SAndreas Gohr        $search->addColumn('%pageid%');
378*8fed17f3SAndreas Gohr        if ($expectException !== false) $this->setExpectedException($expectException);
379*8fed17f3SAndreas Gohr
380*8fed17f3SAndreas Gohr        $search->addFilter($colname, $value, $comp, $type);
381*8fed17f3SAndreas Gohr
382*8fed17f3SAndreas Gohr        if (count($expected_filter) === 0) {
383*8fed17f3SAndreas Gohr            $this->assertCount(0, $search->filter, $msg);
384*8fed17f3SAndreas Gohr            return;
385*8fed17f3SAndreas Gohr        }
386*8fed17f3SAndreas Gohr        $this->assertEquals($expected_filter[0][0], $search->filter[0][0]->getLabel(), $msg);
387*8fed17f3SAndreas Gohr        $this->assertEquals($expected_filter[0][1], $search->filter[0][1], $msg);
388*8fed17f3SAndreas Gohr        $this->assertEquals($expected_filter[0][2], $search->filter[0][2], $msg);
389*8fed17f3SAndreas Gohr        $this->assertEquals($expected_filter[0][3], $search->filter[0][3], $msg);
390*8fed17f3SAndreas Gohr    }
391*8fed17f3SAndreas Gohr
392*8fed17f3SAndreas Gohr    public function test_wildcard()
393*8fed17f3SAndreas Gohr    {
394*8fed17f3SAndreas Gohr        $search = new mock\Search();
395*8fed17f3SAndreas Gohr        $search->addSchema('schema2', 'alias');
396*8fed17f3SAndreas Gohr        $search->addColumn('*');
397*8fed17f3SAndreas Gohr        $this->assertCount(4, $search->getColumns());
398*8fed17f3SAndreas Gohr
399*8fed17f3SAndreas Gohr        $search = new mock\Search();
400*8fed17f3SAndreas Gohr        $search->addSchema('schema2', 'alias');
401*8fed17f3SAndreas Gohr        $search->addColumn('schema2.*');
402*8fed17f3SAndreas Gohr        $this->assertCount(4, $search->getColumns());
403*8fed17f3SAndreas Gohr
404*8fed17f3SAndreas Gohr        $search = new mock\Search();
405*8fed17f3SAndreas Gohr        $search->addSchema('schema2', 'alias');
406*8fed17f3SAndreas Gohr        $search->addColumn('alias.*');
407*8fed17f3SAndreas Gohr        $this->assertCount(4, $search->getColumns());
408*8fed17f3SAndreas Gohr
409*8fed17f3SAndreas Gohr        $search = new mock\Search();
410*8fed17f3SAndreas Gohr        $search->addSchema('schema2', 'alias');
411*8fed17f3SAndreas Gohr        $search->addColumn('nope.*');
412*8fed17f3SAndreas Gohr        $this->assertCount(0, $search->getColumns());
413*8fed17f3SAndreas Gohr    }
414*8fed17f3SAndreas Gohr
415*8fed17f3SAndreas Gohr    public function test_filterValueList()
416*8fed17f3SAndreas Gohr    {
417*8fed17f3SAndreas Gohr        $search = new mock\Search();
418*8fed17f3SAndreas Gohr
419*8fed17f3SAndreas Gohr        //simple - single quote
420*8fed17f3SAndreas Gohr        $this->assertEquals(array('test'),
421*8fed17f3SAndreas Gohr            $this->callInaccessibleMethod($search, 'parseFilterValueList', array('("test")')));
422*8fed17f3SAndreas Gohr
423*8fed17f3SAndreas Gohr        //simple - double quote
424*8fed17f3SAndreas Gohr        $this->assertEquals(array('test'),
425*8fed17f3SAndreas Gohr            $this->callInaccessibleMethod($search, 'parseFilterValueList', array("('test')")));
426*8fed17f3SAndreas Gohr
427*8fed17f3SAndreas Gohr        //many elements
428*8fed17f3SAndreas Gohr        $this->assertEquals(array('test', 'test2', '18'),
429*8fed17f3SAndreas Gohr            $this->callInaccessibleMethod($search, 'parseFilterValueList', array('("test", \'test2\', 18)')));
430*8fed17f3SAndreas Gohr
431*8fed17f3SAndreas Gohr        $str = <<<'EOD'
432*8fed17f3SAndreas Gohr("t\"est", 't\'est2', 18)
433*8fed17f3SAndreas GohrEOD;
434*8fed17f3SAndreas Gohr        //escape sequences
435*8fed17f3SAndreas Gohr        $this->assertEquals(array('t"est', "t'est2", '18'),
436*8fed17f3SAndreas Gohr            $this->callInaccessibleMethod($search, 'parseFilterValueList', array($str)));
437*8fed17f3SAndreas Gohr
438*8fed17f3SAndreas Gohr        //numbers
439*8fed17f3SAndreas Gohr        $this->assertEquals(array('18.7', '10e5', '-100'),
440*8fed17f3SAndreas Gohr            $this->callInaccessibleMethod($search, 'parseFilterValueList', array('(18.7, 10e5, -100)')));
441*8fed17f3SAndreas Gohr
442*8fed17f3SAndreas Gohr    }
443*8fed17f3SAndreas Gohr}
444