xref: /plugin/struct/_test/StructTest.php (revision 0549dcc5bc88d4f9d923acdd09931d8d51be7097)
128318177SAndreas Gohr<?php
228318177SAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\test;
428318177SAndreas Gohr
5ea9843dcSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
6ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter;
7*0549dcc5SAndreas Gohruse dokuwiki\plugin\struct\test\mock\Assignments;
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 */
16*0549dcc5SAndreas Gohrabstract class StructTest extends \DokuWikiTest
17*0549dcc5SAndreas Gohr{
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     */
27*0549dcc5SAndreas Gohr    protected function tearDown(): void
28*0549dcc5SAndreas Gohr    {
2928318177SAndreas Gohr        parent::tearDown();
309d580426SAndreas Gohr        /** @var \helper_plugin_struct_db $db */
319d580426SAndreas Gohr        $db = plugin_load('helper', 'struct_db');
329d580426SAndreas Gohr        $db->resetDB();
33025cb9daSAndreas Gohr        Assignments::reset();
3428318177SAndreas Gohr    }
3528318177SAndreas Gohr
3628318177SAndreas Gohr    /**
3728318177SAndreas Gohr     * Creates a schema from one of the available schema files
3828318177SAndreas Gohr     *
3928318177SAndreas Gohr     * @param string $schema
4028318177SAndreas Gohr     * @param string $json base name of the JSON file optional, defaults to $schema
419d580426SAndreas Gohr     * @param int $rev allows to create schemas back in time
426f023786SAndreas Gohr     * @param bool $lookup create as a lookup schema
4328318177SAndreas Gohr     */
44*0549dcc5SAndreas Gohr    protected function loadSchemaJSON($schema, $json = '', $rev = 0)
45*0549dcc5SAndreas Gohr    {
4628318177SAndreas Gohr        if (!$json) $json = $schema;
4728318177SAndreas Gohr        $file = __DIR__ . "/json/$json.struct.json";
4828318177SAndreas Gohr        if (!file_exists($file)) {
4928318177SAndreas Gohr            throw new \RuntimeException("$file does not exist");
5028318177SAndreas Gohr        }
5128318177SAndreas Gohr
527f803aa8SAnna Dabrowska        $importer = new SchemaImporter($schema, file_get_contents($file));
5328318177SAndreas Gohr
549d580426SAndreas Gohr        if (!$importer->build($rev)) {
5528318177SAndreas Gohr            throw new \RuntimeException("build of $schema from $file failed");
5628318177SAndreas Gohr        }
5728318177SAndreas Gohr    }
5828318177SAndreas Gohr
5928318177SAndreas Gohr    /**
6028318177SAndreas Gohr     * This waits until a new second has passed
6128318177SAndreas Gohr     *
6228318177SAndreas Gohr     * The very first call will return immeadiately, proceeding calls will return
639d580426SAndreas Gohr     * only after at least 1 second after the last call has passed.
6428318177SAndreas Gohr     *
659d580426SAndreas Gohr     * When passing $init=true it will not return immeadiately but use the current
669d580426SAndreas Gohr     * second as initialization. It might still return faster than a second.
679d580426SAndreas Gohr     *
689d580426SAndreas Gohr     * @param bool $init wait from now on, not from last time
6928318177SAndreas Gohr     * @return int new timestamp
7028318177SAndreas Gohr     */
71*0549dcc5SAndreas Gohr    protected function waitForTick($init = false)
72*0549dcc5SAndreas Gohr    {
739f6c16baSAndreas Gohr        // this will be in DokuWiki soon
749f6c16baSAndreas Gohr        if (is_callable('parent::waitForTick')) {
759f6c16baSAndreas Gohr            return parent::waitForTick($init);
769f6c16baSAndreas Gohr        }
779f6c16baSAndreas Gohr
7828318177SAndreas Gohr        static $last = 0;
799d580426SAndreas Gohr        if ($init) $last = time();
809d580426SAndreas Gohr
8128318177SAndreas Gohr        while ($last === $now = time()) {
8228318177SAndreas Gohr            usleep(100000); //recheck in a 10th of a second
8328318177SAndreas Gohr        }
8428318177SAndreas Gohr        $last = $now;
8528318177SAndreas Gohr        return $now;
8628318177SAndreas Gohr    }
8728318177SAndreas Gohr
8828318177SAndreas Gohr    /**
8928318177SAndreas Gohr     * Saves struct data for given page and schema
9028318177SAndreas Gohr     *
919d580426SAndreas Gohr     * Please note that setting the $rev only influences the struct data timestamp,
929d580426SAndreas Gohr     * not the page and changelog entries.
939d580426SAndreas Gohr     *
9428318177SAndreas Gohr     * @param string $page
954cd5cc28SAnna Dabrowska     * @param string $table
9628318177SAndreas Gohr     * @param array $data
979d580426SAndreas Gohr     * @param int $rev allows to override the revision timestamp
98777be84fSAnna Dabrowska     * @param int $rid
9928318177SAndreas Gohr     */
100*0549dcc5SAndreas Gohr    protected function saveData($page, $table, $data, $rev = 0, $rid = 0)
101*0549dcc5SAndreas Gohr    {
10228318177SAndreas Gohr        saveWikiText($page, "test for $page", "saved for testing");
1034cd5cc28SAnna Dabrowska        if (AccessTable::isTypePage($page, $rev)) {
1044cd5cc28SAnna Dabrowska            $access = AccessTable::getPageAccess($table, $page, $rev);
1054cd5cc28SAnna Dabrowska        } elseif (AccessTable::isTypeSerial($page, $rev)) {
1064cd5cc28SAnna Dabrowska            $access = AccessTable::getSerialAccess($table, $page);
1074cd5cc28SAnna Dabrowska        } else {
108777be84fSAnna Dabrowska            $access = AccessTable::getGlobalAccess($table, $rid);
1094cd5cc28SAnna Dabrowska        }
1104cd5cc28SAnna Dabrowska        $access->saveData($data);
111025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
1124cd5cc28SAnna Dabrowska        $assignments->assignPageSchema($page, $table);
11328318177SAndreas Gohr    }
1149d580426SAndreas Gohr
1159d580426SAndreas Gohr    /**
1164cd5cc28SAnna Dabrowska     * Access the plugin's English language strings
1179d580426SAndreas Gohr     *
1189d580426SAndreas Gohr     * @param string $key
1199d580426SAndreas Gohr     * @return string
1209d580426SAndreas Gohr     */
121*0549dcc5SAndreas Gohr    protected function getLang($key)
122*0549dcc5SAndreas Gohr    {
1239d580426SAndreas Gohr        static $lang = null;
1249d580426SAndreas Gohr        if (is_null($lang)) {
1259d580426SAndreas Gohr            $lang = array();
1269d580426SAndreas Gohr            include(DOKU_PLUGIN . 'struct/lang/en/lang.php');
1279d580426SAndreas Gohr        }
1289d580426SAndreas Gohr        return $lang[$key];
1299d580426SAndreas Gohr    }
130df30dbf7SAndreas Gohr
131df30dbf7SAndreas Gohr    /**
132df30dbf7SAndreas Gohr     * Removes Whitespace
133df30dbf7SAndreas Gohr     *
134df30dbf7SAndreas Gohr     * Makes comparing sql statements a bit simpler as it ignores formatting
135df30dbf7SAndreas Gohr     *
136df30dbf7SAndreas Gohr     * @param $string
137df30dbf7SAndreas Gohr     * @return string
138df30dbf7SAndreas Gohr     */
139*0549dcc5SAndreas Gohr    protected function cleanWS($string)
140*0549dcc5SAndreas Gohr    {
141df30dbf7SAndreas Gohr        return preg_replace('/\s+/s', '', $string);
142df30dbf7SAndreas Gohr    }
14328318177SAndreas Gohr}
144