18fed17f3SAndreas Gohr<?php 28fed17f3SAndreas Gohr 38fed17f3SAndreas Gohrnamespace dokuwiki\plugin\struct\test; 48fed17f3SAndreas Gohr 58fed17f3SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable; 68fed17f3SAndreas Gohr 78fed17f3SAndreas Gohr/** 88fed17f3SAndreas Gohr * Tests for the integration with Bureaucracy plugin 98fed17f3SAndreas Gohr * 10*30ad7b71SAnna Dabrowska * @group plugin_struct 118fed17f3SAndreas Gohr * @group plugins 128fed17f3SAndreas Gohr * 138fed17f3SAndreas Gohr */ 148fed17f3SAndreas Gohrclass BureaucracyTest extends StructTest 158fed17f3SAndreas Gohr{ 168fed17f3SAndreas Gohr 178fed17f3SAndreas Gohr /** @var array always enable the needed plugins */ 188fed17f3SAndreas Gohr protected $pluginsEnabled = ['struct', 'sqlite', 'bureaucracy']; 198fed17f3SAndreas Gohr 208fed17f3SAndreas Gohr /** @var array of lookup data */ 218fed17f3SAndreas Gohr protected $lookup = []; 228fed17f3SAndreas Gohr 238fed17f3SAndreas Gohr public function setUp(): void 248fed17f3SAndreas Gohr { 258fed17f3SAndreas Gohr parent::setUp(); 268fed17f3SAndreas Gohr 278fed17f3SAndreas Gohr if (!class_exists('\dokuwiki\plugin\bureaucracy\test\BureaucracyTest')) { 288fed17f3SAndreas Gohr $this->markTestSkipped('bureaucracy plugin not available'); 298fed17f3SAndreas Gohr } 308fed17f3SAndreas Gohr 318fed17f3SAndreas Gohr $this->loadSchemaJSON('bureaucracy_lookup'); 328fed17f3SAndreas Gohr $this->loadSchemaJSON('bureaucracy'); 338fed17f3SAndreas Gohr 348fed17f3SAndreas Gohr //insert some data to lookup 358fed17f3SAndreas Gohr for ($i = 1; $i <= 10; ++$i) { 368fed17f3SAndreas Gohr $data = [ 378fed17f3SAndreas Gohr 'lookup_first' => 'value first ' . $i, 388fed17f3SAndreas Gohr 'lookup_second' => 'value second ' . $i 398fed17f3SAndreas Gohr ]; 408fed17f3SAndreas Gohr 418fed17f3SAndreas Gohr $lookupData = AccessTable::getGlobalAccess('bureaucracy_lookup'); 428fed17f3SAndreas Gohr $lookupData->saveData($data); 438fed17f3SAndreas Gohr $this->lookup[] = $lookupData; 448fed17f3SAndreas Gohr } 458fed17f3SAndreas Gohr } 468fed17f3SAndreas Gohr 478fed17f3SAndreas Gohr public function test_bureaucracy_lookup_replacement_empty() 488fed17f3SAndreas Gohr { 498fed17f3SAndreas Gohr //page created by bureaucracy 508fed17f3SAndreas Gohr $id = 'bureaucracy_lookup_replacement_empty'; 518fed17f3SAndreas Gohr //id of template page 528fed17f3SAndreas Gohr $template_id = 'template'; 538fed17f3SAndreas Gohr 548fed17f3SAndreas Gohr //create template 558fed17f3SAndreas Gohr saveWikiText($template_id, 'Value:@@bureaucracy.lookup_select@@', 'summary'); 568fed17f3SAndreas Gohr 578fed17f3SAndreas Gohr //build form 588fed17f3SAndreas Gohr $fields = []; 598fed17f3SAndreas Gohr 608fed17f3SAndreas Gohr /** @var \helper_plugin_struct_field $lookup_field */ 618fed17f3SAndreas Gohr $lookup_field = plugin_load('helper', 'struct_field'); 628fed17f3SAndreas Gohr 638fed17f3SAndreas Gohr $lookup_field->opt['cmd'] = ''; 648fed17f3SAndreas Gohr $lookup_field->opt['label'] = 'bureaucracy.lookup_select'; 658fed17f3SAndreas Gohr 668fed17f3SAndreas Gohr $lookup_field->initialize([$lookup_field->opt['cmd'], $lookup_field->opt['label']]); 678fed17f3SAndreas Gohr 688fed17f3SAndreas Gohr //empty lookup value 698fed17f3SAndreas Gohr $lookup_field->opt['value'] = json_encode([]); 708fed17f3SAndreas Gohr $fields[] = $lookup_field; 718fed17f3SAndreas Gohr 728fed17f3SAndreas Gohr /** @var \helper_plugin_bureaucracy_actiontemplate $actiontemplate */ 738fed17f3SAndreas Gohr $actiontemplate = plugin_load('helper', 'bureaucracy_actiontemplate'); 748fed17f3SAndreas Gohr $actiontemplate->run($fields, '', array($template_id, $id, '_')); 758fed17f3SAndreas Gohr 768fed17f3SAndreas Gohr $page_content = io_readWikiPage(wikiFN($id), $id); 778fed17f3SAndreas Gohr 788fed17f3SAndreas Gohr $this->assertEquals('Value:', $page_content); 798fed17f3SAndreas Gohr } 808fed17f3SAndreas Gohr 818fed17f3SAndreas Gohr public function test_bureaucracy_lookup_replacement() 828fed17f3SAndreas Gohr { 838fed17f3SAndreas Gohr //page created by bureaucracy 848fed17f3SAndreas Gohr $id = 'bureaucracy_lookup_replacement'; 858fed17f3SAndreas Gohr //id of template page 868fed17f3SAndreas Gohr $template_id = 'template'; 878fed17f3SAndreas Gohr //rid of selected value 888fed17f3SAndreas Gohr $lookup_rid = $this->lookup[0]->getRid(); 898fed17f3SAndreas Gohr //selected value 908fed17f3SAndreas Gohr $lookup_value = $this->lookup[0]->getData()['lookup_first']->getValue(); 918fed17f3SAndreas Gohr 928fed17f3SAndreas Gohr //create template 938fed17f3SAndreas Gohr saveWikiText($template_id, 'Value:@@bureaucracy.lookup_select@@', 'summary'); 948fed17f3SAndreas Gohr 958fed17f3SAndreas Gohr //build form 968fed17f3SAndreas Gohr $fields = array(); 978fed17f3SAndreas Gohr 988fed17f3SAndreas Gohr $lookup_field = plugin_load('helper', 'struct_field'); 998fed17f3SAndreas Gohr $lookup_field->opt['cmd'] = ''; 1008fed17f3SAndreas Gohr $lookup_field->opt['label'] = 'bureaucracy.lookup_select'; 1018fed17f3SAndreas Gohr 1028fed17f3SAndreas Gohr $lookup_field->initialize([$lookup_field->opt['cmd'], $lookup_field->opt['label']]); 1038fed17f3SAndreas Gohr $lookup_field->opt['value'] = json_encode(['', $lookup_rid]); 1048fed17f3SAndreas Gohr 1058fed17f3SAndreas Gohr $fields[] = $lookup_field; 1068fed17f3SAndreas Gohr 1078fed17f3SAndreas Gohr /* @var \helper_plugin_bureaucracy_actiontemplate $actiontemplate */ 1088fed17f3SAndreas Gohr $actiontemplate = plugin_load('helper', 'bureaucracy_actiontemplate'); 1098fed17f3SAndreas Gohr $actiontemplate->run($fields, '', array($template_id, $id, '_')); 1108fed17f3SAndreas Gohr 1118fed17f3SAndreas Gohr $page_content = io_readWikiPage(wikiFN($id), $id); 1128fed17f3SAndreas Gohr 1138fed17f3SAndreas Gohr $this->assertEquals('Value:' . $lookup_value, $page_content); 1148fed17f3SAndreas Gohr } 1158fed17f3SAndreas Gohr 1168fed17f3SAndreas Gohr public function test_bureaucracy_multi_field() 1178fed17f3SAndreas Gohr { 1188fed17f3SAndreas Gohr $this->loadSchemaJSON('schema1'); 1198fed17f3SAndreas Gohr 1208fed17f3SAndreas Gohr $formSyntax = [ 1218fed17f3SAndreas Gohr 'struct_field "schema1.first"', 1228fed17f3SAndreas Gohr 'struct_field "schema1.second"', 1238fed17f3SAndreas Gohr ]; 1248fed17f3SAndreas Gohr $templateSyntax = "staticPrefix @@schema1.first@@ staticPostfix\nmulti: @@schema1.second@@ multipost"; 1258fed17f3SAndreas Gohr $values = ['foo', ['bar', 'baz']]; 1268fed17f3SAndreas Gohr 1278fed17f3SAndreas Gohr $errors = []; 1288fed17f3SAndreas Gohr $bTest = new \dokuwiki\plugin\bureaucracy\test\BureaucracyTest(); 1298fed17f3SAndreas Gohr $actualWikitext = $this->callInaccessibleMethod( 1308fed17f3SAndreas Gohr $bTest, 1318fed17f3SAndreas Gohr 'send_form_action_template', 1328fed17f3SAndreas Gohr array_merge( 1338fed17f3SAndreas Gohr [ 1348fed17f3SAndreas Gohr $formSyntax, 1358fed17f3SAndreas Gohr $templateSyntax, 1368fed17f3SAndreas Gohr &$errors 1378fed17f3SAndreas Gohr ], 1388fed17f3SAndreas Gohr $values 1398fed17f3SAndreas Gohr 1408fed17f3SAndreas Gohr ) 1418fed17f3SAndreas Gohr ); 1428fed17f3SAndreas Gohr 1438fed17f3SAndreas Gohr $expectedSyntax = "staticPrefix foo staticPostfix\nmulti: bar, baz multipost"; 1448fed17f3SAndreas Gohr $this->assertEquals($expectedSyntax, $actualWikitext); 1458fed17f3SAndreas Gohr $this->assertEmpty($errors); 1468fed17f3SAndreas Gohr } 1478fed17f3SAndreas Gohr} 148