xref: /plugin/struct/_test/StructTest.php (revision 7f803aa873894c400484b80e8b543a49fdf1f9d5)
128318177SAndreas Gohr<?php
228318177SAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\test;
428318177SAndreas Gohr
5ea9843dcSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
6025cb9daSAndreas Gohruse dokuwiki\plugin\struct\test\mock\Assignments;
7ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter;
828318177SAndreas Gohr
928318177SAndreas Gohr/**
1028318177SAndreas Gohr * Base class for all struct tests
1128318177SAndreas Gohr *
1228318177SAndreas Gohr * It cleans up the database in teardown and provides some useful helper methods
1328318177SAndreas Gohr *
14ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\test
1528318177SAndreas Gohr */
1628318177SAndreas Gohrabstract class StructTest extends \DokuWikiTest {
1728318177SAndreas Gohr
1828318177SAndreas Gohr    /** @var array alway enable the needed plugins */
1928318177SAndreas Gohr    protected $pluginsEnabled = array('struct', 'sqlite');
2028318177SAndreas Gohr
2128318177SAndreas Gohr    /**
2228318177SAndreas Gohr     * Default teardown
2328318177SAndreas Gohr     *
2428318177SAndreas Gohr     * we always make sure the database is clear
2528318177SAndreas Gohr     */
2628318177SAndreas Gohr    protected function tearDown() {
2728318177SAndreas Gohr        parent::tearDown();
289d580426SAndreas Gohr        /** @var \helper_plugin_struct_db $db */
299d580426SAndreas Gohr        $db = plugin_load('helper', 'struct_db');
309d580426SAndreas Gohr        $db->resetDB();
31025cb9daSAndreas Gohr        Assignments::reset();
3228318177SAndreas Gohr    }
3328318177SAndreas Gohr
3428318177SAndreas Gohr    /**
3528318177SAndreas Gohr     * Creates a schema from one of the available schema files
3628318177SAndreas Gohr     *
3728318177SAndreas Gohr     * @param string $schema
3828318177SAndreas Gohr     * @param string $json base name of the JSON file optional, defaults to $schema
399d580426SAndreas Gohr     * @param int $rev allows to create schemas back in time
406f023786SAndreas Gohr     * @param bool $lookup create as a lookup schema
4128318177SAndreas Gohr     */
42*7f803aa8SAnna Dabrowska    protected function loadSchemaJSON($schema, $json = '', $rev = 0) {
4328318177SAndreas Gohr        if(!$json) $json = $schema;
4428318177SAndreas Gohr        $file = __DIR__ . "/json/$json.struct.json";
4528318177SAndreas Gohr        if(!file_exists($file)) {
4628318177SAndreas Gohr            throw new \RuntimeException("$file does not exist");
4728318177SAndreas Gohr        }
4828318177SAndreas Gohr
49*7f803aa8SAnna Dabrowska        $importer = new SchemaImporter($schema, file_get_contents($file));
5028318177SAndreas Gohr
519d580426SAndreas Gohr        if(!$importer->build($rev)) {
5228318177SAndreas Gohr            throw new \RuntimeException("build of $schema from $file failed");
5328318177SAndreas Gohr        }
5428318177SAndreas Gohr    }
5528318177SAndreas Gohr
5628318177SAndreas Gohr    /**
5728318177SAndreas Gohr     * This waits until a new second has passed
5828318177SAndreas Gohr     *
5928318177SAndreas Gohr     * The very first call will return immeadiately, proceeding calls will return
609d580426SAndreas Gohr     * only after at least 1 second after the last call has passed.
6128318177SAndreas Gohr     *
629d580426SAndreas Gohr     * When passing $init=true it will not return immeadiately but use the current
639d580426SAndreas Gohr     * second as initialization. It might still return faster than a second.
649d580426SAndreas Gohr     *
659d580426SAndreas Gohr     * @param bool $init wait from now on, not from last time
6628318177SAndreas Gohr     * @return int new timestamp
6728318177SAndreas Gohr     */
689d580426SAndreas Gohr    protected function waitForTick($init = false) {
699f6c16baSAndreas Gohr        // this will be in DokuWiki soon
709f6c16baSAndreas Gohr        if (is_callable('parent::waitForTick')) {
719f6c16baSAndreas Gohr            return parent::waitForTick($init);
729f6c16baSAndreas Gohr        }
739f6c16baSAndreas Gohr
7428318177SAndreas Gohr        static $last = 0;
759d580426SAndreas Gohr        if($init) $last = time();
769d580426SAndreas Gohr
7728318177SAndreas Gohr        while($last === $now = time()) {
7828318177SAndreas Gohr            usleep(100000); //recheck in a 10th of a second
7928318177SAndreas Gohr        }
8028318177SAndreas Gohr        $last = $now;
8128318177SAndreas Gohr        return $now;
8228318177SAndreas Gohr    }
8328318177SAndreas Gohr
8428318177SAndreas Gohr    /**
8528318177SAndreas Gohr     * Saves struct data for given page and schema
8628318177SAndreas Gohr     *
879d580426SAndreas Gohr     * Please note that setting the $rev only influences the struct data timestamp,
889d580426SAndreas Gohr     * not the page and changelog entries.
899d580426SAndreas Gohr     *
9028318177SAndreas Gohr     * @param string $page
9128318177SAndreas Gohr     * @param string $schema
9228318177SAndreas Gohr     * @param array $data
939d580426SAndreas Gohr     * @param int $rev allows to override the revision timestamp
9428318177SAndreas Gohr     */
959d580426SAndreas Gohr    protected function saveData($page, $schema, $data, $rev = 0) {
969d580426SAndreas Gohr        if(!$rev) $rev = time();
979d580426SAndreas Gohr
9828318177SAndreas Gohr        saveWikiText($page, "test for $page", "saved for testing");
99ea9843dcSAndreas Gohr        $schemaData = AccessTable::byTableName($schema, $page, $rev);
10028318177SAndreas Gohr        $schemaData->saveData($data);
101025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
102add73e0aSAndreas Gohr        $assignments->assignPageSchema($page, $schema);
10328318177SAndreas Gohr    }
1049d580426SAndreas Gohr
1059d580426SAndreas Gohr    /**
1069d580426SAndreas Gohr     * Access the plugin's english language strings
1079d580426SAndreas Gohr     *
1089d580426SAndreas Gohr     * @param string $key
1099d580426SAndreas Gohr     * @return string
1109d580426SAndreas Gohr     */
1119d580426SAndreas Gohr    protected function getLang($key) {
1129d580426SAndreas Gohr        static $lang = null;
1139d580426SAndreas Gohr        if(is_null($lang)) {
1149d580426SAndreas Gohr            $lang = array();
1159d580426SAndreas Gohr            include(DOKU_PLUGIN . 'struct/lang/en/lang.php');
1169d580426SAndreas Gohr        }
1179d580426SAndreas Gohr        return $lang[$key];
1189d580426SAndreas Gohr    }
119df30dbf7SAndreas Gohr
120df30dbf7SAndreas Gohr    /**
121df30dbf7SAndreas Gohr     * Removes Whitespace
122df30dbf7SAndreas Gohr     *
123df30dbf7SAndreas Gohr     * Makes comparing sql statements a bit simpler as it ignores formatting
124df30dbf7SAndreas Gohr     *
125df30dbf7SAndreas Gohr     * @param $string
126df30dbf7SAndreas Gohr     * @return string
127df30dbf7SAndreas Gohr     */
128df30dbf7SAndreas Gohr    protected function cleanWS($string) {
129df30dbf7SAndreas Gohr        return preg_replace('/\s+/s', '', $string);
130df30dbf7SAndreas Gohr    }
13128318177SAndreas Gohr}
132