1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta;
6use dokuwiki\plugin\struct\test\mock\SearchConfig;
7
8/**
9 * @group plugin_struct
10 * @group plugins
11 *
12 */
13class SearchConfig_struct_test extends StructTest
14{
15
16    public function test_filtervars_simple()
17    {
18        global $INFO;
19        $INFO['id'] = 'foo:bar:baz';
20
21        $searchConfig = new SearchConfig(array());
22
23        $this->assertEquals('foo:bar:baz', $searchConfig->applyFilterVars('$ID$'));
24        $this->assertEquals('baz', $searchConfig->applyFilterVars('$PAGE$'));
25        $this->assertEquals('foo:bar', $searchConfig->applyFilterVars('$NS$'));
26        $this->assertEquals(date('Y-m-d'), $searchConfig->applyFilterVars('$TODAY$'));
27        $this->assertEquals('', $searchConfig->applyFilterVars('$USER$'));
28        $_SERVER['REMOTE_USER'] = 'user';
29        $this->assertEquals('user', $searchConfig->applyFilterVars('$USER$'));
30
31        $this->assertEquals('user baz', $searchConfig->applyFilterVars('$USER$ $PAGE$'));
32        $this->assertEquals('$user', $searchConfig->applyFilterVars('$user'));
33
34    }
35
36    public function test_filtervars_struct()
37    {
38        global $INFO;
39        $INFO['id'] = 'foo:bar:baz';
40
41        // prepare some struct data
42        $sb = new meta\SchemaImporter('schema1', file_get_contents(__DIR__ . '/json/schema1.struct.json'));
43        $sb->build();
44        $schemaData = meta\AccessTable::getPageAccess('schema1', $INFO['id'], time());
45        $schemaData->saveData(
46            array(
47                'first' => 'test',
48                'second' => array('multi1', 'multi2')
49            )
50        );
51
52        $searchConfig = new SearchConfig(array('schemas' => array(array('schema1', 'alias'))));
53        $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.first$'));
54        $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.alias.first$'));
55        $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.schema1.first$'));
56
57        $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.first$post'));
58        $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.alias.first$post'));
59        $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.schema1.first$post'));
60
61        $this->assertEquals(array('multi1', 'multi2'), $searchConfig->applyFilterVars('$STRUCT.second$'));
62        $this->assertEquals(array('multi1', 'multi2'), $searchConfig->applyFilterVars('$STRUCT.alias.second$'));
63        $this->assertEquals(array('multi1', 'multi2'), $searchConfig->applyFilterVars('$STRUCT.schema1.second$'));
64
65        $this->assertEquals(array('premulti1post', 'premulti2post'), $searchConfig->applyFilterVars('pre$STRUCT.second$post'));
66        $this->assertEquals(array('premulti1post', 'premulti2post'), $searchConfig->applyFilterVars('pre$STRUCT.alias.second$post'));
67        $this->assertEquals(array('premulti1post', 'premulti2post'), $searchConfig->applyFilterVars('pre$STRUCT.schema1.second$post'));
68
69        $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.notexisting$'));
70    }
71
72    public function test_filtervars_struct_other()
73    {
74        global $INFO;
75        $INFO['id'] = 'foo:bar:baz';
76
77        // prepare some struct data
78        $sb = new meta\SchemaImporter('schema2', file_get_contents(__DIR__ . '/json/schema2.struct.json'));
79        $sb->build();
80        $sb = new meta\SchemaImporter('schema3', file_get_contents(__DIR__ . '/json/schema2int.struct.json'));
81        $sb->build();
82        $schemaData = meta\AccessTable::getPageAccess('schema2', $INFO['id'], time());
83        $schemaData->saveData(
84            array(
85                'afirst' => 'test',
86                'asecond' => array('multi1', 'multi2')
87            )
88        );
89        $schemaData = meta\AccessTable::getPageAccess('schema3', 'foo:test:baz', time());
90        $schemaData->saveData(
91            array(
92                'afirst' => 'test1',
93                'asecond' => array('multi1a', 'multi2a')
94            )
95        );
96
97        $searchConfig = new SearchConfig(array('schemas' => array(array('schema3', 'alias'))));
98        $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.afirst$'));
99        $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.schema2.afirst$'));
100
101        $this->assertEquals('prepost', $searchConfig->applyFilterVars('pre$STRUCT.afirst$post'));
102        $this->assertEquals('pretestpost', $searchConfig->applyFilterVars('pre$STRUCT.schema2.afirst$post'));
103
104        $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.asecond$'));
105        $this->assertEquals(array('multi1', 'multi2'), $searchConfig->applyFilterVars('$STRUCT.schema2.asecond$'));
106
107        $this->assertEquals('prepost', $searchConfig->applyFilterVars('pre$STRUCT.asecond$post'));
108        $this->assertEquals(array('premulti1post', 'premulti2post'), $searchConfig->applyFilterVars('pre$STRUCT.schema2.asecond$post'));
109
110        $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.notexisting$'));
111
112        $this->assertEquals('', $searchConfig->applyFilterVars('$STRUCT.afirst$'));
113        $this->assertEquals('test', $searchConfig->applyFilterVars('$STRUCT.schema2.afirst$'));
114    }
115
116    public function test_filtervars_user()
117    {
118        global $INFO, $USERINFO, $conf;
119
120        $searchConfig = new SearchConfig(array());
121
122        $_SERVER['REMOTE_USER'] = 'john';
123        $USERINFO['name'] = 'John Smith';
124        $USERINFO['mail'] = 'john.smith@example.com';
125        $USERINFO['grps'] = array('user', 'test');
126        //update info array
127        $INFO['userinfo'] = $USERINFO;
128
129        $this->assertEquals('John Smith', $searchConfig->applyFilterVars('$USER.name$'));
130        $this->assertEquals('john.smith@example.com', $searchConfig->applyFilterVars('$USER.mail$'));
131        $this->assertEquals(array('user', 'test'), $searchConfig->applyFilterVars('$USER.grps$'));
132    }
133
134    public function test_cacheflags()
135    {
136        $searchConfig = new SearchConfig(array());
137
138        $flag = $searchConfig->determineCacheFlag(array('foo', 'bar'));
139        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DEFAULT));
140        $this->assertFalse((bool)($flag & SearchConfig::$CACHE_USER));
141        $this->assertFalse((bool)($flag & SearchConfig::$CACHE_DATE));
142
143        $flag = $searchConfig->determineCacheFlag(array('foo', '$USER$'));
144        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DEFAULT));
145        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_USER));
146        $this->assertFalse((bool)($flag & SearchConfig::$CACHE_DATE));
147
148        $flag = $searchConfig->determineCacheFlag(array('foo', '$TODAY$'));
149        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DEFAULT));
150        $this->assertFalse((bool)($flag & SearchConfig::$CACHE_USER));
151        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DATE));
152
153        $flag = $searchConfig->determineCacheFlag(array('foo', '$TODAY$', '$USER$'));
154        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DEFAULT));
155        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_USER));
156        $this->assertTrue((bool)($flag & SearchConfig::$CACHE_DATE));
157    }
158}
159