128318177SAndreas Gohr<?php 228318177SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\test; 428318177SAndreas Gohr 5ea9843dcSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable; 6*4cd5cc28SAnna Dabrowskause dokuwiki\plugin\struct\test\mock\AccessTableData; 7025cb9daSAndreas Gohruse dokuwiki\plugin\struct\test\mock\Assignments; 8ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter; 928318177SAndreas Gohr 1028318177SAndreas Gohr/** 1128318177SAndreas Gohr * Base class for all struct tests 1228318177SAndreas Gohr * 1328318177SAndreas Gohr * It cleans up the database in teardown and provides some useful helper methods 1428318177SAndreas Gohr * 15ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\test 1628318177SAndreas Gohr */ 1728318177SAndreas Gohrabstract class StructTest extends \DokuWikiTest { 1828318177SAndreas Gohr 1928318177SAndreas Gohr /** @var array alway enable the needed plugins */ 2028318177SAndreas Gohr protected $pluginsEnabled = array('struct', 'sqlite'); 2128318177SAndreas Gohr 2228318177SAndreas Gohr /** 2328318177SAndreas Gohr * Default teardown 2428318177SAndreas Gohr * 2528318177SAndreas Gohr * we always make sure the database is clear 2628318177SAndreas Gohr */ 2728318177SAndreas Gohr protected function tearDown() { 2828318177SAndreas Gohr parent::tearDown(); 299d580426SAndreas Gohr /** @var \helper_plugin_struct_db $db */ 309d580426SAndreas Gohr $db = plugin_load('helper', 'struct_db'); 319d580426SAndreas Gohr $db->resetDB(); 32025cb9daSAndreas Gohr Assignments::reset(); 3328318177SAndreas Gohr } 3428318177SAndreas Gohr 3528318177SAndreas Gohr /** 3628318177SAndreas Gohr * Creates a schema from one of the available schema files 3728318177SAndreas Gohr * 3828318177SAndreas Gohr * @param string $schema 3928318177SAndreas Gohr * @param string $json base name of the JSON file optional, defaults to $schema 409d580426SAndreas Gohr * @param int $rev allows to create schemas back in time 416f023786SAndreas Gohr * @param bool $lookup create as a lookup schema 4228318177SAndreas Gohr */ 437f803aa8SAnna Dabrowska protected function loadSchemaJSON($schema, $json = '', $rev = 0) { 4428318177SAndreas Gohr if(!$json) $json = $schema; 4528318177SAndreas Gohr $file = __DIR__ . "/json/$json.struct.json"; 4628318177SAndreas Gohr if(!file_exists($file)) { 4728318177SAndreas Gohr throw new \RuntimeException("$file does not exist"); 4828318177SAndreas Gohr } 4928318177SAndreas Gohr 507f803aa8SAnna Dabrowska $importer = new SchemaImporter($schema, file_get_contents($file)); 5128318177SAndreas Gohr 529d580426SAndreas Gohr if(!$importer->build($rev)) { 5328318177SAndreas Gohr throw new \RuntimeException("build of $schema from $file failed"); 5428318177SAndreas Gohr } 5528318177SAndreas Gohr } 5628318177SAndreas Gohr 5728318177SAndreas Gohr /** 5828318177SAndreas Gohr * This waits until a new second has passed 5928318177SAndreas Gohr * 6028318177SAndreas Gohr * The very first call will return immeadiately, proceeding calls will return 619d580426SAndreas Gohr * only after at least 1 second after the last call has passed. 6228318177SAndreas Gohr * 639d580426SAndreas Gohr * When passing $init=true it will not return immeadiately but use the current 649d580426SAndreas Gohr * second as initialization. It might still return faster than a second. 659d580426SAndreas Gohr * 669d580426SAndreas Gohr * @param bool $init wait from now on, not from last time 6728318177SAndreas Gohr * @return int new timestamp 6828318177SAndreas Gohr */ 699d580426SAndreas Gohr protected function waitForTick($init = false) { 709f6c16baSAndreas Gohr // this will be in DokuWiki soon 719f6c16baSAndreas Gohr if (is_callable('parent::waitForTick')) { 729f6c16baSAndreas Gohr return parent::waitForTick($init); 739f6c16baSAndreas Gohr } 749f6c16baSAndreas Gohr 7528318177SAndreas Gohr static $last = 0; 769d580426SAndreas Gohr if($init) $last = time(); 779d580426SAndreas Gohr 7828318177SAndreas Gohr while($last === $now = time()) { 7928318177SAndreas Gohr usleep(100000); //recheck in a 10th of a second 8028318177SAndreas Gohr } 8128318177SAndreas Gohr $last = $now; 8228318177SAndreas Gohr return $now; 8328318177SAndreas Gohr } 8428318177SAndreas Gohr 8528318177SAndreas Gohr /** 8628318177SAndreas Gohr * Saves struct data for given page and schema 8728318177SAndreas Gohr * 889d580426SAndreas Gohr * Please note that setting the $rev only influences the struct data timestamp, 899d580426SAndreas Gohr * not the page and changelog entries. 909d580426SAndreas Gohr * 9128318177SAndreas Gohr * @param string $page 92*4cd5cc28SAnna Dabrowska * @param string $table 9328318177SAndreas Gohr * @param array $data 949d580426SAndreas Gohr * @param int $rev allows to override the revision timestamp 9528318177SAndreas Gohr */ 96*4cd5cc28SAnna Dabrowska protected function saveData($page, $table, $data, $rev = 0) { 9728318177SAndreas Gohr saveWikiText($page, "test for $page", "saved for testing"); 98*4cd5cc28SAnna Dabrowska if (AccessTable::isTypePage($page, $rev)) { 99*4cd5cc28SAnna Dabrowska $access = AccessTable::getPageAccess($table, $page, $rev); 100*4cd5cc28SAnna Dabrowska } elseif(AccessTable::isTypeSerial($page, $rev)) { 101*4cd5cc28SAnna Dabrowska $access = AccessTable::getSerialAccess($table, $page); 102*4cd5cc28SAnna Dabrowska } else { 103*4cd5cc28SAnna Dabrowska $access = AccessTable::getLookupAccess($table, $page); 104*4cd5cc28SAnna Dabrowska } 105*4cd5cc28SAnna Dabrowska $access->saveData($data); 106025cb9daSAndreas Gohr $assignments = Assignments::getInstance(); 107*4cd5cc28SAnna Dabrowska $assignments->assignPageSchema($page, $table); 10828318177SAndreas Gohr } 1099d580426SAndreas Gohr 1109d580426SAndreas Gohr /** 111*4cd5cc28SAnna Dabrowska * Access the plugin's English language strings 1129d580426SAndreas Gohr * 1139d580426SAndreas Gohr * @param string $key 1149d580426SAndreas Gohr * @return string 1159d580426SAndreas Gohr */ 1169d580426SAndreas Gohr protected function getLang($key) { 1179d580426SAndreas Gohr static $lang = null; 1189d580426SAndreas Gohr if(is_null($lang)) { 1199d580426SAndreas Gohr $lang = array(); 1209d580426SAndreas Gohr include(DOKU_PLUGIN . 'struct/lang/en/lang.php'); 1219d580426SAndreas Gohr } 1229d580426SAndreas Gohr return $lang[$key]; 1239d580426SAndreas Gohr } 124df30dbf7SAndreas Gohr 125df30dbf7SAndreas Gohr /** 126df30dbf7SAndreas Gohr * Removes Whitespace 127df30dbf7SAndreas Gohr * 128df30dbf7SAndreas Gohr * Makes comparing sql statements a bit simpler as it ignores formatting 129df30dbf7SAndreas Gohr * 130df30dbf7SAndreas Gohr * @param $string 131df30dbf7SAndreas Gohr * @return string 132df30dbf7SAndreas Gohr */ 133df30dbf7SAndreas Gohr protected function cleanWS($string) { 134df30dbf7SAndreas Gohr return preg_replace('/\s+/s', '', $string); 135df30dbf7SAndreas Gohr } 13628318177SAndreas Gohr} 137