1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\PageMeta;
6use dokuwiki\plugin\struct\test\mock\AccessTable;
7use dokuwiki\plugin\struct\test\mock\Lookup;
8
9/**
10 * Testing the Dropdown Type
11 *
12 * @group plugin_struct
13 * @group plugins
14 */
15class Type_Lookup_struct_test extends StructTest
16{
17
18    protected function prepareLookup()
19    {
20        saveWikiText('title1', 'test', 'test');
21        $pageMeta = new PageMeta('title1');
22        $pageMeta->setTitle('This is a title');
23
24        saveWikiText('title2', 'test', 'test');
25        $pageMeta = new PageMeta('title2');
26        $pageMeta->setTitle('This is a 2nd title');
27
28        saveWikiText('title3', 'test', 'test');
29        $pageMeta = new PageMeta('title3');
30        $pageMeta->setTitle('Another Title');
31
32        $this->loadSchemaJSON('pageschema', '', 0);
33        $access = AccessTable::getGlobalAccess('pageschema');
34        $access->saveData(
35            array(
36                'singlepage' => 'title1',
37                'multipage' => array('title1'),
38                'singletitle' => 'title1',
39                'multititle' => array('title1'),
40            )
41        );
42        $access = AccessTable::getGlobalAccess('pageschema');
43        $access->saveData(
44            array(
45                'singlepage' => 'title2',
46                'multipage' => array('title2'),
47                'singletitle' => 'title2',
48                'multititle' => array('title2'),
49            )
50        );
51        $access = AccessTable::getGlobalAccess('pageschema');
52        $access->saveData(
53            array(
54                'singlepage' => 'title3',
55                'multipage' => array('title3'),
56                'singletitle' => 'title3',
57                'multititle' => array('title3'),
58            )
59        );
60    }
61
62    protected function prepareTranslation()
63    {
64        $this->loadSchemaJSON('translation', '', 0);
65        $access = AccessTable::getGlobalAccess('translation');
66        $access->saveData(
67            array(
68                'en' => 'shoe',
69                'de' => 'Schuh',
70                'fr' => 'chaussure'
71            )
72        );
73
74        $access = AccessTable::getGlobalAccess('translation');
75        $access->saveData(
76            array(
77                'en' => 'dog',
78                'de' => 'Hund',
79                'fr' => 'chien'
80            )
81        );
82
83        $access = AccessTable::getGlobalAccess('translation');
84        $access->saveData(
85            array(
86                'en' => 'cat',
87                'de' => 'Katze',
88                'fr' => 'Chat'
89            )
90        );
91    }
92
93    protected function preparePages()
94    {
95        $this->loadSchemaJSON('dropdowns');
96        $this->saveData(
97            'test1', 'dropdowns', array(
98            'drop1' => json_encode(['', 1]), 'drop2' => json_encode(['', 1]), 'drop3' => 'John'
99        ), time()
100        );
101        $this->saveData(
102            'test2', 'dropdowns', array(
103            'drop1' => json_encode(['', 2]), 'drop2' => json_encode(['', 2]), 'drop3' => 'Jane'
104        ),
105            time()
106        );
107        $this->saveData(
108            'test3', 'dropdowns', array(
109            'drop1' => json_encode(['', 3]), 'drop2' => json_encode(['', 3]), 'drop3' => 'Tarzan'
110        ),
111            time()
112        );
113    }
114
115    public function test_data()
116    {
117        $this->prepareLookup();
118        $this->preparePages();
119
120        $access = AccessTable::getPageAccess('dropdowns', 'test1', time());
121        $data = $access->getData();
122
123        $this->assertEquals('["[\\"\\",1]","[\\"title1\\",\\"This is a title\\"]"]', $data['drop1']->getValue());
124        $this->assertEquals('["[\\"\\",1]","title1"]', $data['drop2']->getValue());
125
126        $this->assertEquals('["",1]', $data['drop1']->getRawValue());
127        $this->assertEquals('["",1]', $data['drop2']->getRawValue());
128
129        $this->assertEquals('This is a title', $data['drop1']->getDisplayValue());
130        $this->assertEquals('title1', $data['drop2']->getDisplayValue());
131
132        $R = new \Doku_Renderer_xhtml();
133        $data['drop1']->render($R, 'xhtml');
134        $pq = \phpQuery::newDocument($R->doc);
135        $this->assertEquals('This is a title', $pq->find('a')->text());
136        $this->assertContains('title1', $pq->find('a')->attr('href'));
137
138        $R = new \Doku_Renderer_xhtml();
139        $data['drop2']->render($R, 'xhtml');
140        $pq = \phpQuery::newDocument($R->doc);
141        $this->assertEquals('title1', $pq->find('a')->text());
142        $this->assertContains('title1', $pq->find('a')->attr('href'));
143    }
144
145    public function test_translation()
146    {
147        global $conf;
148        $this->prepareTranslation();
149
150        // lookup in english
151        $dropdown = new Lookup(
152            array(
153                'schema' => 'translation',
154                'field' => '$LANG'
155            ),
156            'test',
157            false,
158            0
159        );
160        $expect = array(
161            '' => '',
162            json_encode(['', 3]) => 'cat',
163            json_encode(['', 2]) => 'dog',
164            json_encode(['', 1]) => 'shoe',
165        );
166        $this->assertEquals($expect, $dropdown->getOptions());
167
168        // fallback to english
169        $conf['lang'] = 'zh';
170        $dropdown = new Lookup(
171            array(
172                'schema' => 'translation',
173                'field' => '$LANG'
174            ),
175            'test',
176            false,
177            0
178        );
179        $this->assertEquals($expect, $dropdown->getOptions());
180
181        // german
182        $conf['lang'] = 'de';
183        $dropdown = new Lookup(
184            array(
185                'schema' => 'translation',
186                'field' => '$LANG'
187            ),
188            'test',
189            false,
190            0
191        );
192        $expect = array(
193            '' => '',
194            json_encode(['', 2]) => 'Hund',
195            json_encode(['', 3]) => 'Katze',
196            json_encode(['', 1]) => 'Schuh',
197        );
198        $this->assertEquals($expect, $dropdown->getOptions());
199
200        // french
201        $conf['lang'] = 'fr';
202        $dropdown = new Lookup(
203            array(
204                'schema' => 'translation',
205                'field' => '$LANG'
206            ),
207            'test',
208            false,
209            0
210        );
211        $expect = array(
212            '' => '',
213            json_encode(['', 2]) => 'chien',
214            json_encode(['', 3]) => 'Chat',
215            json_encode(['', 1]) => 'chaussure',
216        );
217        $this->assertEquals($expect, $dropdown->getOptions());
218    }
219
220    public function test_getOptions()
221    {
222        $this->prepareLookup();
223
224        // lookup with titles
225        $dropdown = new Lookup(
226            array(
227                'schema' => 'pageschema',
228                'field' => 'singletitle'
229            ),
230            'test',
231            false,
232            0
233        );
234        $expect = array(
235            '' => '',
236            json_encode(['', 3]) => 'Another Title',
237            json_encode(['', 2]) => 'This is a 2nd title',
238            json_encode(['', 1]) => 'This is a title',
239        );
240        $this->assertEquals($expect, $dropdown->getOptions());
241
242        // lookup with pages
243        $dropdown = new Lookup(
244            array(
245                'schema' => 'pageschema',
246                'field' => 'singlepage'
247            ),
248            'test',
249            false,
250            0
251        );
252        $expect = array(
253            '' => '',
254            json_encode(['', 1]) => 'title1',
255            json_encode(['', 2]) => 'title2',
256            json_encode(['', 3]) => 'title3',
257        );
258        $this->assertEquals($expect, $dropdown->getOptions());
259
260    }
261
262}
263