xref: /plugin/struct/_test/SearchConfigTest.php (revision 8b7e143ca626b542f3ba39c97bd65e44ed408e48)
18fed17f3SAndreas Gohr<?php
28fed17f3SAndreas Gohr
38fed17f3SAndreas Gohrnamespace dokuwiki\plugin\struct\test;
48fed17f3SAndreas Gohr
58fed17f3SAndreas Gohruse dokuwiki\plugin\struct\meta;
68fed17f3SAndreas Gohruse dokuwiki\plugin\struct\test\mock\SearchConfig;
78fed17f3SAndreas Gohr
88fed17f3SAndreas Gohr/**
98fed17f3SAndreas Gohr * @group plugin_struct
108fed17f3SAndreas Gohr * @group plugins
118fed17f3SAndreas Gohr *
128fed17f3SAndreas Gohr */
138fed17f3SAndreas Gohrclass SearchConfigTest extends StructTest
148fed17f3SAndreas Gohr{
158fed17f3SAndreas Gohr
168fed17f3SAndreas Gohr    public function test_filtervars_simple()
178fed17f3SAndreas Gohr    {
188fed17f3SAndreas Gohr        global $INFO;
198fed17f3SAndreas Gohr        $INFO['id'] = 'foo:bar:baz';
208fed17f3SAndreas Gohr
218fed17f3SAndreas Gohr        $searchConfig = new SearchConfig([]);
228fed17f3SAndreas Gohr
238fed17f3SAndreas Gohr        $this->assertEquals('foo:bar:baz', $searchConfig->applyFilterVars('$ID$'));
248fed17f3SAndreas Gohr        $this->assertEquals('baz', $searchConfig->applyFilterVars('$PAGE$'));
258fed17f3SAndreas Gohr        $this->assertEquals('foo:bar', $searchConfig->applyFilterVars('$NS$'));
268fed17f3SAndreas Gohr        $this->assertEquals(date('Y-m-d'), $searchConfig->applyFilterVars('$TODAY$'));
278fed17f3SAndreas Gohr        $this->assertEquals('', $searchConfig->applyFilterVars('$USER$'));
288fed17f3SAndreas Gohr        $_SERVER['REMOTE_USER'] = 'user';
298fed17f3SAndreas Gohr        $this->assertEquals('user', $searchConfig->applyFilterVars('$USER$'));
308fed17f3SAndreas Gohr
318fed17f3SAndreas Gohr        $this->assertEquals('user baz', $searchConfig->applyFilterVars('$USER$ $PAGE$'));
328fed17f3SAndreas Gohr        $this->assertEquals('$user', $searchConfig->applyFilterVars('$user'));
33c1bfd816SAnna Dabrowska        $this->assertEquals(date('Y-m-d'), $searchConfig->applyFilterVars('$DATE(now)$'));
348fed17f3SAndreas Gohr    }
358fed17f3SAndreas Gohr
36*8b7e143cSAndreas Gohr    public function test_filtervars_nsorid()
37*8b7e143cSAndreas Gohr    {
38*8b7e143cSAndreas Gohr        global $INFO;
39*8b7e143cSAndreas Gohr
40*8b7e143cSAndreas Gohr
41*8b7e143cSAndreas Gohr        $searchConfig = new SearchConfig([]);
42*8b7e143cSAndreas Gohr
43*8b7e143cSAndreas Gohr        // normal page
44*8b7e143cSAndreas Gohr        $INFO['id'] = 'foo:bar:baz';
45*8b7e143cSAndreas Gohr        $this->assertEquals('foo:bar:baz', $searchConfig->applyFilterVars('$NSORID$'));
46*8b7e143cSAndreas Gohr
47*8b7e143cSAndreas Gohr        // start page: start in namespace
48*8b7e143cSAndreas Gohr        $INFO['id'] = 'foo:bar:start';
49*8b7e143cSAndreas Gohr        saveWikiText($INFO['id'], 'start page', 'start created');
50*8b7e143cSAndreas Gohr        $this->assertEquals('foo:bar', $searchConfig->applyFilterVars('$NSORID$'));
51*8b7e143cSAndreas Gohr        saveWikiText($INFO['id'], '', 'start page deleted');
52*8b7e143cSAndreas Gohr        clearstatcache();
53*8b7e143cSAndreas Gohr
54*8b7e143cSAndreas Gohr        // start page: same as namespace in namespace
55*8b7e143cSAndreas Gohr        $INFO['id'] = 'foo:bar:bar';
56*8b7e143cSAndreas Gohr        saveWikiText($INFO['id'], 'start page', 'start created');
57*8b7e143cSAndreas Gohr        $this->assertEquals('foo:bar', $searchConfig->applyFilterVars('$NSORID$'));
58*8b7e143cSAndreas Gohr        saveWikiText($INFO['id'], '', 'start page deleted');
59*8b7e143cSAndreas Gohr        clearstatcache();
60*8b7e143cSAndreas Gohr
61*8b7e143cSAndreas Gohr        // start page: same as namespace in above namespace
62*8b7e143cSAndreas Gohr        // incidally this is the same as a normal page
63*8b7e143cSAndreas Gohr        $INFO['id'] = 'foo:bar';
64*8b7e143cSAndreas Gohr        saveWikiText($INFO['id'], 'start page', 'start created');
65*8b7e143cSAndreas Gohr        $this->assertEquals('foo:bar', $searchConfig->applyFilterVars('$NSORID$'));
66*8b7e143cSAndreas Gohr        saveWikiText($INFO['id'], '', 'start page deleted');
67*8b7e143cSAndreas Gohr        clearstatcache();
68*8b7e143cSAndreas Gohr    }
69*8b7e143cSAndreas Gohr
708fed17f3SAndreas Gohr    public function test_filtervars_struct()
718fed17f3SAndreas Gohr    {
728fed17f3SAndreas Gohr        global $INFO;
738fed17f3SAndreas Gohr        $INFO['id'] = 'foo:bar:baz';
748fed17f3SAndreas Gohr
758fed17f3SAndreas Gohr        // prepare some struct data
768fed17f3SAndreas Gohr        $sb = new meta\SchemaImporter('schema1', file_get_contents(__DIR__ . '/json/schema1.struct.json'));
778fed17f3SAndreas Gohr        $sb->build();
788fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess('schema1', $INFO['id'], time());
798fed17f3SAndreas Gohr        $schemaData->saveData(
808fed17f3SAndreas Gohr            [
818fed17f3SAndreas Gohr                'first' => 'test',
828fed17f3SAndreas Gohr                'second' => ['multi1', 'multi2']
838fed17f3SAndreas Gohr            ]
848fed17f3SAndreas Gohr        );
858fed17f3SAndreas Gohr
868fed17f3SAndreas Gohr        $searchConfig = new SearchConfig(['schemas' => [['schema1', 'alias']]]);
878fed17f3SAndreas Gohr        $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.first$'));
888fed17f3SAndreas Gohr        $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.alias.first$'));
898fed17f3SAndreas Gohr        $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.schema1.first$'));
908fed17f3SAndreas Gohr
918fed17f3SAndreas Gohr        $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.first$post'));
928fed17f3SAndreas Gohr        $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.alias.first$post'));
938fed17f3SAndreas Gohr        $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.schema1.first$post'));
948fed17f3SAndreas Gohr
958fed17f3SAndreas Gohr        $this->assertEquals(['multi1', 'multi2'], $searchConfig->applyFilterVars('$STRUCT.second$'));
968fed17f3SAndreas Gohr        $this->assertEquals(['multi1', 'multi2'], $searchConfig->applyFilterVars('$STRUCT.alias.second$'));
978fed17f3SAndreas Gohr        $this->assertEquals(['multi1', 'multi2'], $searchConfig->applyFilterVars('$STRUCT.schema1.second$'));
988fed17f3SAndreas Gohr
998fed17f3SAndreas Gohr        $this->assertEquals(['premulti1post', 'premulti2post'], $searchConfig->applyFilterVars('pre$STRUCT.second$post'));
1008fed17f3SAndreas Gohr        $this->assertEquals(['premulti1post', 'premulti2post'], $searchConfig->applyFilterVars('pre$STRUCT.alias.second$post'));
1018fed17f3SAndreas Gohr        $this->assertEquals(['premulti1post', 'premulti2post'], $searchConfig->applyFilterVars('pre$STRUCT.schema1.second$post'));
1028fed17f3SAndreas Gohr
1038fed17f3SAndreas Gohr        $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.notexisting$'));
1048fed17f3SAndreas Gohr    }
1058fed17f3SAndreas Gohr
1068fed17f3SAndreas Gohr    public function test_filtervars_struct_other()
1078fed17f3SAndreas Gohr    {
1088fed17f3SAndreas Gohr        global $INFO;
1098fed17f3SAndreas Gohr        $INFO['id'] = 'foo:bar:baz';
1108fed17f3SAndreas Gohr
1118fed17f3SAndreas Gohr        // prepare some struct data
1128fed17f3SAndreas Gohr        $sb = new meta\SchemaImporter('schema2', file_get_contents(__DIR__ . '/json/schema2.struct.json'));
1138fed17f3SAndreas Gohr        $sb->build();
1148fed17f3SAndreas Gohr        $sb = new meta\SchemaImporter('schema3', file_get_contents(__DIR__ . '/json/schema2int.struct.json'));
1158fed17f3SAndreas Gohr        $sb->build();
1168fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess('schema2', $INFO['id'], time());
1178fed17f3SAndreas Gohr        $schemaData->saveData(
1188fed17f3SAndreas Gohr            [
1198fed17f3SAndreas Gohr                'afirst' => 'test',
1208fed17f3SAndreas Gohr                'asecond' => ['multi1', 'multi2']
1218fed17f3SAndreas Gohr            ]
1228fed17f3SAndreas Gohr        );
1238fed17f3SAndreas Gohr        $schemaData = meta\AccessTable::getPageAccess('schema3', 'foo:test:baz', time());
1248fed17f3SAndreas Gohr        $schemaData->saveData(
1258fed17f3SAndreas Gohr            [
1268fed17f3SAndreas Gohr                'afirst' => 'test1',
1278fed17f3SAndreas Gohr                'asecond' => ['multi1a', 'multi2a']
1288fed17f3SAndreas Gohr            ]
1298fed17f3SAndreas Gohr        );
1308fed17f3SAndreas Gohr
1318fed17f3SAndreas Gohr        $searchConfig = new SearchConfig(['schemas' => [['schema3', 'alias']]]);
1328fed17f3SAndreas Gohr        $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.afirst$'));
1338fed17f3SAndreas Gohr        $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.schema2.afirst$'));
1348fed17f3SAndreas Gohr
1358fed17f3SAndreas Gohr        $this->assertEquals('prepost', $searchConfig->applyFilterVars('pre$STRUCT.afirst$post'));
1368fed17f3SAndreas Gohr        $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.schema2.afirst$post'));
1378fed17f3SAndreas Gohr
1388fed17f3SAndreas Gohr        $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.asecond$'));
1398fed17f3SAndreas Gohr        $this->assertEquals(['multi1', 'multi2'], $searchConfig->applyFilterVars('$STRUCT.schema2.asecond$'));
1408fed17f3SAndreas Gohr
1418fed17f3SAndreas Gohr        $this->assertEquals('prepost', $searchConfig->applyFilterVars('pre$STRUCT.asecond$post'));
1428fed17f3SAndreas Gohr        $this->assertEquals(['premulti1post', 'premulti2post'], $searchConfig->applyFilterVars('pre$STRUCT.schema2.asecond$post'));
1438fed17f3SAndreas Gohr
1448fed17f3SAndreas Gohr        $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.notexisting$'));
1458fed17f3SAndreas Gohr
1468fed17f3SAndreas Gohr        $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.afirst$'));
1478fed17f3SAndreas Gohr        $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.schema2.afirst$'));
1488fed17f3SAndreas Gohr    }
1498fed17f3SAndreas Gohr
1508fed17f3SAndreas Gohr    public function test_filtervars_user()
1518fed17f3SAndreas Gohr    {
1528fed17f3SAndreas Gohr        global $INFO, $USERINFO;
1538fed17f3SAndreas Gohr
1548fed17f3SAndreas Gohr        $searchConfig = new SearchConfig([]);
1558fed17f3SAndreas Gohr
1568fed17f3SAndreas Gohr        $_SERVER['REMOTE_USER'] = 'john';
1578fed17f3SAndreas Gohr        $USERINFO['name'] = 'John Smith';
1588fed17f3SAndreas Gohr        $USERINFO['mail'] = 'john.smith@example.com';
1598fed17f3SAndreas Gohr        $USERINFO['grps'] = ['user', 'test'];
1608fed17f3SAndreas Gohr        //update info array
1618fed17f3SAndreas Gohr        $INFO['userinfo'] = $USERINFO;
1628fed17f3SAndreas Gohr
1638fed17f3SAndreas Gohr        $this->assertEquals('John Smith', $searchConfig->applyFilterVars('$USER.name$'));
1648fed17f3SAndreas Gohr        $this->assertEquals('john.smith@example.com', $searchConfig->applyFilterVars('$USER.mail$'));
1658fed17f3SAndreas Gohr        $this->assertEquals(['user', 'test'], $searchConfig->applyFilterVars('$USER.grps$'));
1668fed17f3SAndreas Gohr    }
1678fed17f3SAndreas Gohr
1688fed17f3SAndreas Gohr    public function test_cacheflags()
1698fed17f3SAndreas Gohr    {
1708fed17f3SAndreas Gohr        $searchConfig = new SearchConfig([]);
1718fed17f3SAndreas Gohr
1728fed17f3SAndreas Gohr        $flag = $searchConfig->determineCacheFlag(['foo', 'bar']);
1738fed17f3SAndreas Gohr        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DEFAULT));
1748fed17f3SAndreas Gohr        $this->assertFalse((bool)($flag & SearchConfig::$CACHE_USER));
1758fed17f3SAndreas Gohr        $this->assertFalse((bool)($flag & SearchConfig::$CACHE_DATE));
1768fed17f3SAndreas Gohr
1778fed17f3SAndreas Gohr        $flag = $searchConfig->determineCacheFlag(['foo', '$USER$']);
1788fed17f3SAndreas Gohr        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DEFAULT));
1798fed17f3SAndreas Gohr        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_USER));
1808fed17f3SAndreas Gohr        $this->assertFalse((bool)($flag & SearchConfig::$CACHE_DATE));
1818fed17f3SAndreas Gohr
1828fed17f3SAndreas Gohr        $flag = $searchConfig->determineCacheFlag(['foo', '$TODAY$']);
1838fed17f3SAndreas Gohr        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DEFAULT));
1848fed17f3SAndreas Gohr        $this->assertFalse((bool)($flag & SearchConfig::$CACHE_USER));
1858fed17f3SAndreas Gohr        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DATE));
1868fed17f3SAndreas Gohr
1878fed17f3SAndreas Gohr        $flag = $searchConfig->determineCacheFlag(['foo', '$TODAY$', '$USER$']);
1888fed17f3SAndreas Gohr        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DEFAULT));
1898fed17f3SAndreas Gohr        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_USER));
1908fed17f3SAndreas Gohr        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DATE));
1918fed17f3SAndreas Gohr    }
1928fed17f3SAndreas Gohr}
193