128318177SAndreas Gohr<?php 228318177SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\test; 428318177SAndreas Gohr 5ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaData; 6ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter; 728318177SAndreas Gohr 828318177SAndreas Gohr/** 928318177SAndreas Gohr * Base class for all struct tests 1028318177SAndreas Gohr * 1128318177SAndreas Gohr * It cleans up the database in teardown and provides some useful helper methods 1228318177SAndreas Gohr * 13ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\test 1428318177SAndreas Gohr */ 1528318177SAndreas Gohrabstract class StructTest extends \DokuWikiTest { 1628318177SAndreas Gohr 1728318177SAndreas Gohr /** @var array alway enable the needed plugins */ 1828318177SAndreas Gohr protected $pluginsEnabled = array('struct', 'sqlite'); 1928318177SAndreas Gohr 2028318177SAndreas Gohr /** 2128318177SAndreas Gohr * Default teardown 2228318177SAndreas Gohr * 2328318177SAndreas Gohr * we always make sure the database is clear 2428318177SAndreas Gohr */ 2528318177SAndreas Gohr protected function tearDown() { 2628318177SAndreas Gohr parent::tearDown(); 279d580426SAndreas Gohr /** @var \helper_plugin_struct_db $db */ 289d580426SAndreas Gohr $db = plugin_load('helper', 'struct_db'); 299d580426SAndreas Gohr $db->resetDB(); 3028318177SAndreas Gohr } 3128318177SAndreas Gohr 3228318177SAndreas Gohr /** 3328318177SAndreas Gohr * Creates a schema from one of the available schema files 3428318177SAndreas Gohr * 3528318177SAndreas Gohr * @param string $schema 3628318177SAndreas Gohr * @param string $json base name of the JSON file optional, defaults to $schema 379d580426SAndreas Gohr * @param int $rev allows to create schemas back in time 3828318177SAndreas Gohr */ 399d580426SAndreas Gohr protected function loadSchemaJSON($schema, $json = '', $rev = 0) { 4028318177SAndreas Gohr if(!$json) $json = $schema; 4128318177SAndreas Gohr $file = __DIR__ . "/json/$json.struct.json"; 4228318177SAndreas Gohr if(!file_exists($file)) { 4328318177SAndreas Gohr throw new \RuntimeException("$file does not exist"); 4428318177SAndreas Gohr } 4528318177SAndreas Gohr 4628318177SAndreas Gohr $importer = new SchemaImporter($schema, file_get_contents($file)); 4728318177SAndreas Gohr 489d580426SAndreas Gohr if(!$importer->build($rev)) { 4928318177SAndreas Gohr throw new \RuntimeException("build of $schema from $file failed"); 5028318177SAndreas Gohr } 5128318177SAndreas Gohr } 5228318177SAndreas Gohr 5328318177SAndreas Gohr /** 5428318177SAndreas Gohr * This waits until a new second has passed 5528318177SAndreas Gohr * 5628318177SAndreas Gohr * The very first call will return immeadiately, proceeding calls will return 579d580426SAndreas Gohr * only after at least 1 second after the last call has passed. 5828318177SAndreas Gohr * 599d580426SAndreas Gohr * When passing $init=true it will not return immeadiately but use the current 609d580426SAndreas Gohr * second as initialization. It might still return faster than a second. 619d580426SAndreas Gohr * 629d580426SAndreas Gohr * @param bool $init wait from now on, not from last time 6328318177SAndreas Gohr * @return int new timestamp 6428318177SAndreas Gohr */ 659d580426SAndreas Gohr protected function waitForTick($init = false) { 6628318177SAndreas Gohr static $last = 0; 679d580426SAndreas Gohr if($init) $last = time(); 689d580426SAndreas Gohr 6928318177SAndreas Gohr while($last === $now = time()) { 7028318177SAndreas Gohr usleep(100000); //recheck in a 10th of a second 7128318177SAndreas Gohr } 7228318177SAndreas Gohr $last = $now; 7328318177SAndreas Gohr return $now; 7428318177SAndreas Gohr } 7528318177SAndreas Gohr 7628318177SAndreas Gohr /** 7728318177SAndreas Gohr * Saves struct data for given page and schema 7828318177SAndreas Gohr * 799d580426SAndreas Gohr * Please note that setting the $rev only influences the struct data timestamp, 809d580426SAndreas Gohr * not the page and changelog entries. 819d580426SAndreas Gohr * 8228318177SAndreas Gohr * @param string $page 8328318177SAndreas Gohr * @param string $schema 8428318177SAndreas Gohr * @param array $data 859d580426SAndreas Gohr * @param int $rev allows to override the revision timestamp 8628318177SAndreas Gohr */ 879d580426SAndreas Gohr protected function saveData($page, $schema, $data, $rev = 0) { 889d580426SAndreas Gohr if(!$rev) $rev = time(); 899d580426SAndreas Gohr 9028318177SAndreas Gohr saveWikiText($page, "test for $page", "saved for testing"); 919d580426SAndreas Gohr $schemaData = new SchemaData($schema, $page, $rev); 9228318177SAndreas Gohr $schemaData->saveData($data); 9328318177SAndreas Gohr } 949d580426SAndreas Gohr 959d580426SAndreas Gohr /** 969d580426SAndreas Gohr * Access the plugin's english language strings 979d580426SAndreas Gohr * 989d580426SAndreas Gohr * @param string $key 999d580426SAndreas Gohr * @return string 1009d580426SAndreas Gohr */ 1019d580426SAndreas Gohr protected function getLang($key) { 1029d580426SAndreas Gohr static $lang = null; 1039d580426SAndreas Gohr if(is_null($lang)) { 1049d580426SAndreas Gohr $lang = array(); 1059d580426SAndreas Gohr include(DOKU_PLUGIN . 'struct/lang/en/lang.php'); 1069d580426SAndreas Gohr } 1079d580426SAndreas Gohr return $lang[$key]; 1089d580426SAndreas Gohr } 109*df30dbf7SAndreas Gohr 110*df30dbf7SAndreas Gohr /** 111*df30dbf7SAndreas Gohr * Removes Whitespace 112*df30dbf7SAndreas Gohr * 113*df30dbf7SAndreas Gohr * Makes comparing sql statements a bit simpler as it ignores formatting 114*df30dbf7SAndreas Gohr * 115*df30dbf7SAndreas Gohr * @param $string 116*df30dbf7SAndreas Gohr * @return string 117*df30dbf7SAndreas Gohr */ 118*df30dbf7SAndreas Gohr protected function cleanWS($string) { 119*df30dbf7SAndreas Gohr return preg_replace('/\s+/s', '', $string); 120*df30dbf7SAndreas Gohr } 12128318177SAndreas Gohr} 122