1<?php 2 3namespace dokuwiki\plugin\struct\test; 4 5use dokuwiki\plugin\struct\meta\AccessTable; 6use dokuwiki\plugin\struct\meta\SchemaImporter; 7use dokuwiki\plugin\struct\test\mock\Assignments; 8 9/** 10 * Base class for all struct tests 11 * 12 * It cleans up the database in teardown and provides some useful helper methods 13 * 14 * @package dokuwiki\plugin\struct\test 15 */ 16abstract class StructTest extends \DokuWikiTest 17{ 18 19 /** @var array alway enable the needed plugins */ 20 protected $pluginsEnabled = array('struct', 'sqlite'); 21 22 /** 23 * Default teardown 24 * 25 * we always make sure the database is clear 26 */ 27 protected function tearDown(): void 28 { 29 parent::tearDown(); 30 /** @var \helper_plugin_struct_db $db */ 31 $db = plugin_load('helper', 'struct_db'); 32 $db->resetDB(); 33 Assignments::reset(); 34 } 35 36 /** 37 * Creates a schema from one of the available schema files 38 * 39 * @param string $schema 40 * @param string $json base name of the JSON file optional, defaults to $schema 41 * @param int $rev allows to create schemas back in time 42 */ 43 protected function loadSchemaJSON($schema, $json = '', $rev = 0) 44 { 45 if (!$json) $json = $schema; 46 $file = __DIR__ . "/json/$json.struct.json"; 47 if (!file_exists($file)) { 48 throw new \RuntimeException("$file does not exist"); 49 } 50 51 $importer = new SchemaImporter($schema, file_get_contents($file)); 52 53 if (!$importer->build($rev)) { 54 throw new \RuntimeException("build of $schema from $file failed"); 55 } 56 } 57 58 /** 59 * Saves struct data for given page and schema 60 * 61 * Please note that setting the $rev only influences the struct data timestamp, 62 * not the page and changelog entries. 63 * 64 * @param string $page 65 * @param string $table 66 * @param array $data 67 * @param int $rev allows to override the revision timestamp 68 * @param int $rid 69 */ 70 protected function saveData($page, $table, $data, $rev = 0, $rid = 0) 71 { 72 saveWikiText($page, "test for $page", "saved for testing"); 73 if (AccessTable::isTypePage($page, $rev)) { 74 $access = AccessTable::getPageAccess($table, $page, $rev); 75 } elseif (AccessTable::isTypeSerial($page, $rev)) { 76 $access = AccessTable::getSerialAccess($table, $page); 77 } else { 78 $access = AccessTable::getGlobalAccess($table, $rid); 79 } 80 $access->saveData($data); 81 $assignments = Assignments::getInstance(); 82 $assignments->assignPageSchema($page, $table); 83 } 84 85 /** 86 * Access the plugin's English language strings 87 * 88 * @param string $key 89 * @return string 90 */ 91 protected function getLang($key) 92 { 93 static $lang = null; 94 if (is_null($lang)) { 95 $lang = array(); 96 include(DOKU_PLUGIN . 'struct/lang/en/lang.php'); 97 } 98 return $lang[$key]; 99 } 100 101 /** 102 * Removes Whitespace 103 * 104 * Makes comparing sql statements a bit simpler as it ignores formatting 105 * 106 * @param $string 107 * @return string 108 */ 109 protected function cleanWS($string) 110 { 111 return preg_replace('/\s+/s', '', $string); 112 } 113} 114