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