18fed17f3SAndreas Gohr<?php 28fed17f3SAndreas Gohr 38fed17f3SAndreas Gohrnamespace dokuwiki\plugin\struct\test; 48fed17f3SAndreas Gohr 58fed17f3SAndreas Gohruse dokuwiki\plugin\struct\meta; 68fed17f3SAndreas Gohr 78fed17f3SAndreas Gohr/** 88fed17f3SAndreas Gohr * Tests for the building of SQL-Queries for the struct plugin 98fed17f3SAndreas Gohr * 108fed17f3SAndreas Gohr * @group plugin_struct 118fed17f3SAndreas Gohr * @group plugins 128fed17f3SAndreas Gohr * 138fed17f3SAndreas Gohr */ 148fed17f3SAndreas Gohrclass SearchTest extends StructTest 158fed17f3SAndreas Gohr{ 168fed17f3SAndreas Gohr 178fed17f3SAndreas Gohr public function setUp(): void 188fed17f3SAndreas Gohr { 19*ea625c46SAnna Dabrowska // workaround for recent GitHub disk I/O errors 20*ea625c46SAnna Dabrowska parent::setUpBeforeClass(); 21*ea625c46SAnna Dabrowska 228fed17f3SAndreas Gohr parent::setUp(); 238fed17f3SAndreas Gohr 248fed17f3SAndreas Gohr $this->loadSchemaJSON('schema1'); 258fed17f3SAndreas Gohr $this->loadSchemaJSON('schema2'); 268fed17f3SAndreas Gohr $_SERVER['REMOTE_USER'] = 'testuser'; 278fed17f3SAndreas Gohr 288fed17f3SAndreas Gohr $as = mock\Assignments::getInstance(); 298fed17f3SAndreas Gohr $page = 'page01'; 308fed17f3SAndreas Gohr $as->assignPageSchema($page, 'schema1'); 318fed17f3SAndreas Gohr $as->assignPageSchema($page, 'schema2'); 328fed17f3SAndreas Gohr saveWikiText($page, "===== TestTitle =====\nabc", "Summary"); 338fed17f3SAndreas Gohr p_get_metadata($page); 348fed17f3SAndreas Gohr $now = time(); 358fed17f3SAndreas Gohr $this->saveData( 368fed17f3SAndreas Gohr $page, 378fed17f3SAndreas Gohr 'schema1', 388fed17f3SAndreas Gohr [ 398fed17f3SAndreas Gohr 'first' => 'first data', 408fed17f3SAndreas Gohr 'second' => ['second data', 'more data', 'even more'], 418fed17f3SAndreas Gohr 'third' => 'third data', 428fed17f3SAndreas Gohr 'fourth' => 'fourth data' 438fed17f3SAndreas Gohr ], 448fed17f3SAndreas Gohr $now 458fed17f3SAndreas Gohr ); 468fed17f3SAndreas Gohr $this->saveData( 478fed17f3SAndreas Gohr $page, 488fed17f3SAndreas Gohr 'schema2', 498fed17f3SAndreas Gohr [ 508fed17f3SAndreas Gohr 'afirst' => 'first data', 518fed17f3SAndreas Gohr 'asecond' => ['second data', 'more data', 'even more'], 528fed17f3SAndreas Gohr 'athird' => 'third data', 538fed17f3SAndreas Gohr 'afourth' => 'fourth data' 548fed17f3SAndreas Gohr ], 558fed17f3SAndreas Gohr $now 568fed17f3SAndreas Gohr ); 578fed17f3SAndreas Gohr 588fed17f3SAndreas Gohr $as->assignPageSchema('test:document', 'schema1'); 598fed17f3SAndreas Gohr $as->assignPageSchema('test:document', 'schema2'); 608fed17f3SAndreas Gohr $this->saveData( 618fed17f3SAndreas Gohr 'test:document', 628fed17f3SAndreas Gohr 'schema1', 638fed17f3SAndreas Gohr [ 648fed17f3SAndreas Gohr 'first' => 'document first data', 658fed17f3SAndreas Gohr 'second' => ['second', 'more'], 668fed17f3SAndreas Gohr 'third' => '', 678fed17f3SAndreas Gohr 'fourth' => 'fourth data' 688fed17f3SAndreas Gohr ], 698fed17f3SAndreas Gohr $now 708fed17f3SAndreas Gohr ); 718fed17f3SAndreas Gohr $this->saveData( 728fed17f3SAndreas Gohr 'test:document', 738fed17f3SAndreas Gohr 'schema2', 748fed17f3SAndreas Gohr [ 758fed17f3SAndreas Gohr 'afirst' => 'first data', 768fed17f3SAndreas Gohr 'asecond' => ['second data', 'more data', 'even more'], 778fed17f3SAndreas Gohr 'athird' => 'third data', 788fed17f3SAndreas Gohr 'afourth' => 'fourth data' 798fed17f3SAndreas Gohr ], 808fed17f3SAndreas Gohr $now 818fed17f3SAndreas Gohr ); 828fed17f3SAndreas Gohr 838fed17f3SAndreas Gohr for ($i = 10; $i <= 20; $i++) { 848fed17f3SAndreas Gohr $this->saveData( 858fed17f3SAndreas Gohr "page$i", 868fed17f3SAndreas Gohr 'schema2', 878fed17f3SAndreas Gohr [ 888fed17f3SAndreas Gohr 'afirst' => "page$i first data", 898fed17f3SAndreas Gohr 'asecond' => ["page$i second data"], 908fed17f3SAndreas Gohr 'athird' => "page$i third data", 918fed17f3SAndreas Gohr 'afourth' => "page$i fourth data" 928fed17f3SAndreas Gohr ], 938fed17f3SAndreas Gohr $now 948fed17f3SAndreas Gohr ); 958fed17f3SAndreas Gohr $as->assignPageSchema("page$i", 'schema2'); 968fed17f3SAndreas Gohr } 978fed17f3SAndreas Gohr } 988fed17f3SAndreas Gohr 998fed17f3SAndreas Gohr public function test_simple() 1008fed17f3SAndreas Gohr { 1018fed17f3SAndreas Gohr $search = new mock\Search(); 1028fed17f3SAndreas Gohr 1038fed17f3SAndreas Gohr $search->addSchema('schema1'); 1048fed17f3SAndreas Gohr $search->addColumn('%pageid%'); 1058fed17f3SAndreas Gohr $search->addColumn('first'); 1068fed17f3SAndreas Gohr $search->addColumn('second'); 1078fed17f3SAndreas Gohr 1088fed17f3SAndreas Gohr /** @var meta\Value[][] $result */ 109ba7f5789SAnna Dabrowska $result = $search->getRows(); 1108fed17f3SAndreas Gohr 1118fed17f3SAndreas Gohr $this->assertCount(2, $result, 'result rows'); 1128fed17f3SAndreas Gohr $this->assertCount(3, $result[0], 'result columns'); 1138fed17f3SAndreas Gohr $this->assertEquals('page01', $result[0][0]->getValue()); 1148fed17f3SAndreas Gohr $this->assertEquals('first data', $result[0][1]->getValue()); 1158fed17f3SAndreas Gohr $this->assertEquals(['second data', 'more data', 'even more'], $result[0][2]->getValue()); 1168fed17f3SAndreas Gohr } 1178fed17f3SAndreas Gohr 1188fed17f3SAndreas Gohr public function test_simple_title() 1198fed17f3SAndreas Gohr { 1208fed17f3SAndreas Gohr $search = new mock\Search(); 1218fed17f3SAndreas Gohr 1228fed17f3SAndreas Gohr $search->addSchema('schema1'); 1238fed17f3SAndreas Gohr $search->addColumn('%title%'); 1248fed17f3SAndreas Gohr $search->addColumn('first'); 1258fed17f3SAndreas Gohr $search->addColumn('second'); 1268fed17f3SAndreas Gohr 1278fed17f3SAndreas Gohr /** @var meta\Value[][] $result */ 128ba7f5789SAnna Dabrowska $result = $search->getRows(); 1298fed17f3SAndreas Gohr 1308fed17f3SAndreas Gohr $this->assertCount(2, $result, 'result rows'); 1318fed17f3SAndreas Gohr $this->assertCount(3, $result[0], 'result columns'); 1328fed17f3SAndreas Gohr $this->assertEquals('["page01","TestTitle"]', $result[0][0]->getValue()); 1338fed17f3SAndreas Gohr $this->assertEquals('first data', $result[0][1]->getValue()); 1348fed17f3SAndreas Gohr $this->assertEquals(['second data', 'more data', 'even more'], $result[0][2]->getValue()); 1358fed17f3SAndreas Gohr } 1368fed17f3SAndreas Gohr 1378fed17f3SAndreas Gohr public function test_search_published() 1388fed17f3SAndreas Gohr { 1398fed17f3SAndreas Gohr $search = new mock\Search(); 1408fed17f3SAndreas Gohr $search->isNotPublisher(); 1418fed17f3SAndreas Gohr 1428fed17f3SAndreas Gohr $search->addSchema('schema1'); 1438fed17f3SAndreas Gohr $search->addColumn('%pageid%'); 1448fed17f3SAndreas Gohr $search->addColumn('first'); 1458fed17f3SAndreas Gohr $search->addColumn('second'); 1468fed17f3SAndreas Gohr 1478fed17f3SAndreas Gohr /** @var meta\Value[][] $result */ 148ba7f5789SAnna Dabrowska $result = $search->getRows(); 1498fed17f3SAndreas Gohr 1508fed17f3SAndreas Gohr $this->assertCount(0, $result, 'result rows'); 1518fed17f3SAndreas Gohr } 1528fed17f3SAndreas Gohr 1538fed17f3SAndreas Gohr public function test_search_lasteditor() 1548fed17f3SAndreas Gohr { 1558fed17f3SAndreas Gohr $search = new mock\Search(); 1568fed17f3SAndreas Gohr 1578fed17f3SAndreas Gohr $search->addSchema('schema1'); 1588fed17f3SAndreas Gohr $search->addColumn('%title%'); 1598fed17f3SAndreas Gohr $search->addColumn('%lasteditor%'); 1608fed17f3SAndreas Gohr $search->addColumn('first'); 1618fed17f3SAndreas Gohr $search->addColumn('second'); 1628fed17f3SAndreas Gohr 1638fed17f3SAndreas Gohr /** @var meta\Value[][] $result */ 164ba7f5789SAnna Dabrowska $result = $search->getRows(); 1658fed17f3SAndreas Gohr 1668fed17f3SAndreas Gohr $this->assertCount(2, $result, 'result rows'); 1678fed17f3SAndreas Gohr $this->assertCount(4, $result[0], 'result columns'); 1688fed17f3SAndreas Gohr $this->assertEquals('testuser', $result[0][1]->getValue()); 1698fed17f3SAndreas Gohr $this->assertEquals(['second data', 'more data', 'even more'], $result[0][3]->getValue()); 1708fed17f3SAndreas Gohr } 1718fed17f3SAndreas Gohr 1728fed17f3SAndreas Gohr 1738fed17f3SAndreas Gohr /** 1748fed17f3SAndreas Gohr * @group slow 1758fed17f3SAndreas Gohr */ 1768fed17f3SAndreas Gohr public function test_search_lastupdate() 1778fed17f3SAndreas Gohr { 1788fed17f3SAndreas Gohr sleep(1); 1798fed17f3SAndreas Gohr saveWikiText('page01', "===== TestTitle =====\nabcd", "Summary"); 1808fed17f3SAndreas Gohr p_get_metadata('page01'); 1818fed17f3SAndreas Gohr 1828fed17f3SAndreas Gohr $search = new mock\Search(); 1838fed17f3SAndreas Gohr 1848fed17f3SAndreas Gohr $search->addSchema('schema1'); 1858fed17f3SAndreas Gohr $search->addColumn('%pageid%'); 1868fed17f3SAndreas Gohr $search->addColumn('%lastupdate%'); 1878fed17f3SAndreas Gohr $search->addColumn('first'); 1888fed17f3SAndreas Gohr $search->addColumn('second'); 1898fed17f3SAndreas Gohr 1908fed17f3SAndreas Gohr /** @var meta\Value[][] $result */ 191ba7f5789SAnna Dabrowska $result = $search->getRows(); 1928fed17f3SAndreas Gohr 1938fed17f3SAndreas Gohr $expected_time = dformat(filemtime(wikiFN('page01')), '%Y-%m-%d %H:%M:%S'); 1948fed17f3SAndreas Gohr 1958fed17f3SAndreas Gohr $this->assertCount(2, $result, 'result rows'); 1968fed17f3SAndreas Gohr $this->assertCount(4, $result[0], 'result columns'); 197850ad33eSAndreas Gohr $this->assertEquals($expected_time, $result[0][1]->getValue(), "Is your date.timezone set up in php.ini?"); 1988fed17f3SAndreas Gohr $this->assertEquals(['second data', 'more data', 'even more'], $result[0][3]->getValue()); 1998fed17f3SAndreas Gohr } 2008fed17f3SAndreas Gohr 2018fed17f3SAndreas Gohr /** 2028fed17f3SAndreas Gohr * @group slow 2038fed17f3SAndreas Gohr */ 2048fed17f3SAndreas Gohr public function test_search_lastsummary() 2058fed17f3SAndreas Gohr { 2068fed17f3SAndreas Gohr sleep(1); 2078fed17f3SAndreas Gohr $summary = 'Summary'; 2088fed17f3SAndreas Gohr saveWikiText('page01', "===== TestTitle =====\nabcd", $summary); 2098fed17f3SAndreas Gohr p_get_metadata('page01'); 2108fed17f3SAndreas Gohr 2118fed17f3SAndreas Gohr $search = new mock\Search(); 2128fed17f3SAndreas Gohr 2138fed17f3SAndreas Gohr $search->addSchema('schema1'); 2148fed17f3SAndreas Gohr $search->addColumn('%pageid%'); 2158fed17f3SAndreas Gohr $search->addColumn('%lastsummary%'); 2168fed17f3SAndreas Gohr $search->addColumn('first'); 2178fed17f3SAndreas Gohr $search->addColumn('second'); 2188fed17f3SAndreas Gohr 2198fed17f3SAndreas Gohr /** @var meta\Value[][] $result */ 220ba7f5789SAnna Dabrowska $result = $search->getRows(); 2218fed17f3SAndreas Gohr 2228fed17f3SAndreas Gohr $this->assertCount(2, $result, 'result rows'); 2238fed17f3SAndreas Gohr $this->assertCount(4, $result[0], 'result columns'); 2248fed17f3SAndreas Gohr $this->assertEquals($summary, $result[0][1]->getValue()); 2258fed17f3SAndreas Gohr $this->assertEquals(array('second data', 'more data', 'even more'), $result[0][3]->getValue()); 2268fed17f3SAndreas Gohr } 2278fed17f3SAndreas Gohr 2288fed17f3SAndreas Gohr public function test_search() 2298fed17f3SAndreas Gohr { 2308fed17f3SAndreas Gohr $search = new mock\Search(); 2318fed17f3SAndreas Gohr 2328fed17f3SAndreas Gohr $search->addSchema('schema1'); 2338fed17f3SAndreas Gohr $search->addSchema('schema2', 'foo'); 2348fed17f3SAndreas Gohr $this->assertCount(2, $search->schemas); 2358fed17f3SAndreas Gohr 2368fed17f3SAndreas Gohr $search->addColumn('first'); 2378fed17f3SAndreas Gohr $this->assertEquals('schema1', $search->columns[0]->getTable()); 2388fed17f3SAndreas Gohr $this->assertEquals(1, $search->columns[0]->getColref()); 2398fed17f3SAndreas Gohr 2408fed17f3SAndreas Gohr $search->addColumn('afirst'); 2418fed17f3SAndreas Gohr $this->assertEquals('schema2', $search->columns[1]->getTable()); 2428fed17f3SAndreas Gohr $this->assertEquals(1, $search->columns[1]->getColref()); 2438fed17f3SAndreas Gohr 2448fed17f3SAndreas Gohr $search->addColumn('schema1.third'); 2458fed17f3SAndreas Gohr $this->assertEquals('schema1', $search->columns[2]->getTable()); 2468fed17f3SAndreas Gohr $this->assertEquals(3, $search->columns[2]->getColref()); 2478fed17f3SAndreas Gohr 2488fed17f3SAndreas Gohr $search->addColumn('foo.athird'); 2498fed17f3SAndreas Gohr $this->assertEquals('schema2', $search->columns[3]->getTable()); 2508fed17f3SAndreas Gohr $this->assertEquals(3, $search->columns[3]->getColref()); 2518fed17f3SAndreas Gohr 2528fed17f3SAndreas Gohr $search->addColumn('asecond'); 2538fed17f3SAndreas Gohr $this->assertEquals('schema2', $search->columns[4]->getTable()); 2548fed17f3SAndreas Gohr $this->assertEquals(2, $search->columns[4]->getColref()); 2558fed17f3SAndreas Gohr 2568fed17f3SAndreas Gohr $search->addColumn('doesntexist'); 2578fed17f3SAndreas Gohr $this->assertEquals(5, count($search->columns)); 2588fed17f3SAndreas Gohr 2598fed17f3SAndreas Gohr $search->addColumn('%pageid%'); 2608fed17f3SAndreas Gohr $this->assertEquals('schema1', $search->columns[5]->getTable()); 2618fed17f3SAndreas Gohr $exception = false; 2628fed17f3SAndreas Gohr try { 2638fed17f3SAndreas Gohr $search->columns[5]->getColref(); 2648fed17f3SAndreas Gohr } catch (meta\StructException $e) { 2658fed17f3SAndreas Gohr $exception = true; 2668fed17f3SAndreas Gohr } 2678fed17f3SAndreas Gohr $this->assertTrue($exception, "Struct exception expected for accesing colref of PageColumn"); 2688fed17f3SAndreas Gohr 2698fed17f3SAndreas Gohr $search->addSort('first', false); 2708fed17f3SAndreas Gohr $this->assertCount(1, $search->sortby); 2718fed17f3SAndreas Gohr 2728fed17f3SAndreas Gohr $search->addFilter('%pageid%', '%ag%', '~', 'AND'); 2738fed17f3SAndreas Gohr $search->addFilter('second', '%sec%', '~', 'AND'); 2748fed17f3SAndreas Gohr $search->addFilter('first', '%rst%', '~', 'AND'); 2758fed17f3SAndreas Gohr 276ba7f5789SAnna Dabrowska $result = $search->getRows(); 2778fed17f3SAndreas Gohr $count = $search->getCount(); 2788fed17f3SAndreas Gohr 2798fed17f3SAndreas Gohr $this->assertEquals(1, $count, 'result count'); 2808fed17f3SAndreas Gohr $this->assertCount(1, $result, 'result rows'); 2818fed17f3SAndreas Gohr $this->assertCount(6, $result[0], 'result columns'); 2828fed17f3SAndreas Gohr 2838fed17f3SAndreas Gohr // sort by multi-column 2848fed17f3SAndreas Gohr $search->addSort('second'); 2858fed17f3SAndreas Gohr $this->assertCount(2, $search->sortby); 286ba7f5789SAnna Dabrowska $result = $search->getRows(); 2878fed17f3SAndreas Gohr $count = $search->getCount(); 2888fed17f3SAndreas Gohr $this->assertEquals(1, $count, 'result count'); 2898fed17f3SAndreas Gohr $this->assertCount(1, $result, 'result rows'); 2908fed17f3SAndreas Gohr $this->assertCount(6, $result[0], 'result columns'); 2918fed17f3SAndreas Gohr } 2928fed17f3SAndreas Gohr 2938fed17f3SAndreas Gohr public function test_ranges() 2948fed17f3SAndreas Gohr { 2958fed17f3SAndreas Gohr $search = new mock\Search(); 2968fed17f3SAndreas Gohr $search->addSchema('schema2'); 2978fed17f3SAndreas Gohr $search->addColumn('%pageid%'); 2988fed17f3SAndreas Gohr $search->addColumn('afirst'); 2998fed17f3SAndreas Gohr $search->addColumn('asecond'); 3008fed17f3SAndreas Gohr $search->addFilter('%pageid%', '%ag%', '~', 'AND'); 3018fed17f3SAndreas Gohr $search->addSort('%pageid%', false); 3028fed17f3SAndreas Gohr 3038fed17f3SAndreas Gohr /** @var meta\Value[][] $result */ 304ba7f5789SAnna Dabrowska $result = $search->getRows(); 3058fed17f3SAndreas Gohr $count = $search->getCount(); 3068fed17f3SAndreas Gohr 3078fed17f3SAndreas Gohr // check result dimensions 3088fed17f3SAndreas Gohr $this->assertEquals(12, $count, 'result count'); 3098fed17f3SAndreas Gohr $this->assertCount(12, $result, 'result rows'); 3108fed17f3SAndreas Gohr $this->assertCount(3, $result[0], 'result columns'); 3118fed17f3SAndreas Gohr 3128fed17f3SAndreas Gohr // check sorting 3138fed17f3SAndreas Gohr $this->assertEquals('page20', $result[0][0]->getValue()); 3148fed17f3SAndreas Gohr $this->assertEquals('page19', $result[1][0]->getValue()); 3158fed17f3SAndreas Gohr $this->assertEquals('page18', $result[2][0]->getValue()); 3168fed17f3SAndreas Gohr 317ba7f5789SAnna Dabrowska // now with limit 318ba7f5789SAnna Dabrowska // new search object because result is fetched only once 319ba7f5789SAnna Dabrowska $search = new mock\Search(); 320ba7f5789SAnna Dabrowska $search->addSchema('schema2'); 321ba7f5789SAnna Dabrowska $search->addColumn('%pageid%'); 322ba7f5789SAnna Dabrowska $search->addColumn('afirst'); 323ba7f5789SAnna Dabrowska $search->addColumn('asecond'); 324ba7f5789SAnna Dabrowska $search->addFilter('%pageid%', '%ag%', '~', 'AND'); 325ba7f5789SAnna Dabrowska $search->addSort('%pageid%', false); 3268fed17f3SAndreas Gohr $search->setLimit(5); 327ba7f5789SAnna Dabrowska 328ba7f5789SAnna Dabrowska /** @var meta\Value[][] $result */ 329ba7f5789SAnna Dabrowska $result = $search->getRows(); 3308fed17f3SAndreas Gohr $count = $search->getCount(); 3318fed17f3SAndreas Gohr 3328fed17f3SAndreas Gohr // check result dimensions 3338fed17f3SAndreas Gohr $this->assertEquals(12, $count, 'result count'); // full result set 3348fed17f3SAndreas Gohr $this->assertCount(5, $result, 'result rows'); // wanted result set 3358fed17f3SAndreas Gohr 3368fed17f3SAndreas Gohr // check the values 3378fed17f3SAndreas Gohr $this->assertEquals('page20', $result[0][0]->getValue()); 3388fed17f3SAndreas Gohr $this->assertEquals('page16', $result[4][0]->getValue()); 3398fed17f3SAndreas Gohr 3408fed17f3SAndreas Gohr // now add offset 341ba7f5789SAnna Dabrowska // again a new object 342ba7f5789SAnna Dabrowska $search = new mock\Search(); 343ba7f5789SAnna Dabrowska $search->addSchema('schema2'); 344ba7f5789SAnna Dabrowska $search->addColumn('%pageid%'); 345ba7f5789SAnna Dabrowska $search->addColumn('afirst'); 346ba7f5789SAnna Dabrowska $search->addColumn('asecond'); 347ba7f5789SAnna Dabrowska $search->addFilter('%pageid%', '%ag%', '~', 'AND'); 348ba7f5789SAnna Dabrowska $search->addSort('%pageid%', false); 349ba7f5789SAnna Dabrowska $search->setLimit(5); 3508fed17f3SAndreas Gohr $search->setOffset(5); 351ba7f5789SAnna Dabrowska $result = $search->getRows(); 3528fed17f3SAndreas Gohr $count = $search->getCount(); 3538fed17f3SAndreas Gohr 3548fed17f3SAndreas Gohr // check result dimensions 3558fed17f3SAndreas Gohr $this->assertEquals(12, $count, 'result count'); // full result set 3568fed17f3SAndreas Gohr $this->assertCount(5, $result, 'result rows'); // wanted result set 3578fed17f3SAndreas Gohr 3588fed17f3SAndreas Gohr // check the values 3598fed17f3SAndreas Gohr $this->assertEquals('page15', $result[0][0]->getValue()); 3608fed17f3SAndreas Gohr $this->assertEquals('page11', $result[4][0]->getValue()); 3618fed17f3SAndreas Gohr } 3628fed17f3SAndreas Gohr 3638fed17f3SAndreas Gohr public static function addFilter_testdata() 3648fed17f3SAndreas Gohr { 3658fed17f3SAndreas Gohr return [ 3668fed17f3SAndreas Gohr ['%pageid%', 'val', '<>', 'OR', [['%pageid%', 'val', '!=', 'OR']], false, 'replace <> comp'], 3678fed17f3SAndreas Gohr ['%pageid%', 'val', '*~', 'OR', [['%pageid%', '%val%', 'LIKE', 'OR']], false, 'replace *~ comp'], 3688fed17f3SAndreas Gohr ['%pageid%', 'val*', '~', 'OR', [['%pageid%', 'val%', 'LIKE', 'OR']], false, 'replace * in value'], 3698fed17f3SAndreas Gohr ['%pageid%', 'val.*', '=*', 'OR', [['%pageid%', 'val.*', 'REGEXP', 'OR']], false, 'replace * in value'], 3708fed17f3SAndreas Gohr ['nonexisting', 'val', '~', 'OR', [], false, 'ignore missing columns'], 3718fed17f3SAndreas Gohr ['%pageid%', 'val', '?', 'OR', [], '\dokuwiki\plugin\struct\meta\StructException', 'wrong comperator'], 3728fed17f3SAndreas Gohr ['%pageid%', 'val', '=', 'NOT', [], '\dokuwiki\plugin\struct\meta\StructException', 'wrong type'] 3738fed17f3SAndreas Gohr ]; 3748fed17f3SAndreas Gohr } 3758fed17f3SAndreas Gohr 3768fed17f3SAndreas Gohr /** 3778fed17f3SAndreas Gohr * @dataProvider addFilter_testdata 3788fed17f3SAndreas Gohr * 3798fed17f3SAndreas Gohr */ 3808fed17f3SAndreas Gohr public function test_addFilter($colname, $value, $comp, $type, $expected_filter, $expectException, $msg) 3818fed17f3SAndreas Gohr { 3828fed17f3SAndreas Gohr $search = new mock\Search(); 3838fed17f3SAndreas Gohr $search->addSchema('schema2'); 3848fed17f3SAndreas Gohr $search->addColumn('%pageid%'); 3858fed17f3SAndreas Gohr if ($expectException !== false) $this->setExpectedException($expectException); 3868fed17f3SAndreas Gohr 3878fed17f3SAndreas Gohr $search->addFilter($colname, $value, $comp, $type); 3888fed17f3SAndreas Gohr 3898fed17f3SAndreas Gohr if (count($expected_filter) === 0) { 3908fed17f3SAndreas Gohr $this->assertCount(0, $search->filter, $msg); 3918fed17f3SAndreas Gohr return; 3928fed17f3SAndreas Gohr } 3938fed17f3SAndreas Gohr $this->assertEquals($expected_filter[0][0], $search->filter[0][0]->getLabel(), $msg); 3948fed17f3SAndreas Gohr $this->assertEquals($expected_filter[0][1], $search->filter[0][1], $msg); 3958fed17f3SAndreas Gohr $this->assertEquals($expected_filter[0][2], $search->filter[0][2], $msg); 3968fed17f3SAndreas Gohr $this->assertEquals($expected_filter[0][3], $search->filter[0][3], $msg); 3978fed17f3SAndreas Gohr } 3988fed17f3SAndreas Gohr 3998fed17f3SAndreas Gohr public function test_wildcard() 4008fed17f3SAndreas Gohr { 4018fed17f3SAndreas Gohr $search = new mock\Search(); 4028fed17f3SAndreas Gohr $search->addSchema('schema2', 'alias'); 4038fed17f3SAndreas Gohr $search->addColumn('*'); 4048fed17f3SAndreas Gohr $this->assertCount(4, $search->getColumns()); 4058fed17f3SAndreas Gohr 4068fed17f3SAndreas Gohr $search = new mock\Search(); 4078fed17f3SAndreas Gohr $search->addSchema('schema2', 'alias'); 4088fed17f3SAndreas Gohr $search->addColumn('schema2.*'); 4098fed17f3SAndreas Gohr $this->assertCount(4, $search->getColumns()); 4108fed17f3SAndreas Gohr 4118fed17f3SAndreas Gohr $search = new mock\Search(); 4128fed17f3SAndreas Gohr $search->addSchema('schema2', 'alias'); 4138fed17f3SAndreas Gohr $search->addColumn('alias.*'); 4148fed17f3SAndreas Gohr $this->assertCount(4, $search->getColumns()); 4158fed17f3SAndreas Gohr 4168fed17f3SAndreas Gohr $search = new mock\Search(); 4178fed17f3SAndreas Gohr $search->addSchema('schema2', 'alias'); 4188fed17f3SAndreas Gohr $search->addColumn('nope.*'); 4198fed17f3SAndreas Gohr $this->assertCount(0, $search->getColumns()); 4208fed17f3SAndreas Gohr } 4218fed17f3SAndreas Gohr 4228fed17f3SAndreas Gohr public function test_filterValueList() 4238fed17f3SAndreas Gohr { 4248fed17f3SAndreas Gohr $search = new mock\Search(); 4258fed17f3SAndreas Gohr 4268fed17f3SAndreas Gohr //simple - single quote 4278fed17f3SAndreas Gohr $this->assertEquals(array('test'), 4288fed17f3SAndreas Gohr $this->callInaccessibleMethod($search, 'parseFilterValueList', array('("test")'))); 4298fed17f3SAndreas Gohr 4308fed17f3SAndreas Gohr //simple - double quote 4318fed17f3SAndreas Gohr $this->assertEquals(array('test'), 4328fed17f3SAndreas Gohr $this->callInaccessibleMethod($search, 'parseFilterValueList', array("('test')"))); 4338fed17f3SAndreas Gohr 4348fed17f3SAndreas Gohr //many elements 4358fed17f3SAndreas Gohr $this->assertEquals(array('test', 'test2', '18'), 4368fed17f3SAndreas Gohr $this->callInaccessibleMethod($search, 'parseFilterValueList', array('("test", \'test2\', 18)'))); 4378fed17f3SAndreas Gohr 4388fed17f3SAndreas Gohr $str = <<<'EOD' 4398fed17f3SAndreas Gohr("t\"est", 't\'est2', 18) 4408fed17f3SAndreas GohrEOD; 4418fed17f3SAndreas Gohr //escape sequences 4428fed17f3SAndreas Gohr $this->assertEquals(array('t"est', "t'est2", '18'), 4438fed17f3SAndreas Gohr $this->callInaccessibleMethod($search, 'parseFilterValueList', array($str))); 4448fed17f3SAndreas Gohr 4458fed17f3SAndreas Gohr //numbers 4468fed17f3SAndreas Gohr $this->assertEquals(array('18.7', '10e5', '-100'), 4478fed17f3SAndreas Gohr $this->callInaccessibleMethod($search, 'parseFilterValueList', array('(18.7, 10e5, -100)'))); 4488fed17f3SAndreas Gohr 4498fed17f3SAndreas Gohr } 4508fed17f3SAndreas Gohr} 451