1<?php 2 3namespace dokuwiki\plugin\struct\test; 4 5use dokuwiki\plugin\struct\meta\AccessTable; 6use dokuwiki\plugin\struct\test\mock\AccessTablePage; 7use dokuwiki\plugin\struct\test\mock\Assignments; 8use dokuwiki\plugin\struct\meta\SchemaImporter; 9 10/** 11 * Base class for all struct tests 12 * 13 * It cleans up the database in teardown and provides some useful helper methods 14 * 15 * @package dokuwiki\plugin\struct\test 16 */ 17abstract class StructTest extends \DokuWikiTest { 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() { 28 parent::tearDown(); 29 /** @var \helper_plugin_struct_db $db */ 30 $db = plugin_load('helper', 'struct_db'); 31 $db->resetDB(); 32 Assignments::reset(); 33 } 34 35 /** 36 * Creates a schema from one of the available schema files 37 * 38 * @param string $schema 39 * @param string $json base name of the JSON file optional, defaults to $schema 40 * @param int $rev allows to create schemas back in time 41 * @param bool $lookup create as a lookup schema 42 */ 43 protected function loadSchemaJSON($schema, $json = '', $rev = 0) { 44 if(!$json) $json = $schema; 45 $file = __DIR__ . "/json/$json.struct.json"; 46 if(!file_exists($file)) { 47 throw new \RuntimeException("$file does not exist"); 48 } 49 50 $importer = new SchemaImporter($schema, file_get_contents($file)); 51 52 if(!$importer->build($rev)) { 53 throw new \RuntimeException("build of $schema from $file failed"); 54 } 55 } 56 57 /** 58 * This waits until a new second has passed 59 * 60 * The very first call will return immeadiately, proceeding calls will return 61 * only after at least 1 second after the last call has passed. 62 * 63 * When passing $init=true it will not return immeadiately but use the current 64 * second as initialization. It might still return faster than a second. 65 * 66 * @param bool $init wait from now on, not from last time 67 * @return int new timestamp 68 */ 69 protected function waitForTick($init = false) { 70 // this will be in DokuWiki soon 71 if (is_callable('parent::waitForTick')) { 72 return parent::waitForTick($init); 73 } 74 75 static $last = 0; 76 if($init) $last = time(); 77 78 while($last === $now = time()) { 79 usleep(100000); //recheck in a 10th of a second 80 } 81 $last = $now; 82 return $now; 83 } 84 85 /** 86 * Saves struct data for given page and schema 87 * 88 * Please note that setting the $rev only influences the struct data timestamp, 89 * not the page and changelog entries. 90 * 91 * @param string $page 92 * @param string $table 93 * @param array $data 94 * @param int $rev allows to override the revision timestamp 95 */ 96 protected function saveData($page, $table, $data, $rev = 0) { 97 saveWikiText($page, "test for $page", "saved for testing"); 98 if (AccessTable::isTypePage($page, $rev)) { 99 $access = AccessTable::getPageAccess($table, $page, $rev); 100 } elseif(AccessTable::isTypeSerial($page, $rev)) { 101 $access = AccessTable::getSerialAccess($table, $page); 102 } else { 103 $access = AccessTable::getGlobalAccess($table, $page); 104 } 105 $access->saveData($data); 106 $assignments = Assignments::getInstance(); 107 $assignments->assignPageSchema($page, $table); 108 } 109 110 /** 111 * Access the plugin's English language strings 112 * 113 * @param string $key 114 * @return string 115 */ 116 protected function getLang($key) { 117 static $lang = null; 118 if(is_null($lang)) { 119 $lang = array(); 120 include(DOKU_PLUGIN . 'struct/lang/en/lang.php'); 121 } 122 return $lang[$key]; 123 } 124 125 /** 126 * Removes Whitespace 127 * 128 * Makes comparing sql statements a bit simpler as it ignores formatting 129 * 130 * @param $string 131 * @return string 132 */ 133 protected function cleanWS($string) { 134 return preg_replace('/\s+/s', '', $string); 135 } 136} 137