xref: /plugin/struct/_test/StructTest.php (revision 8ec87814898d9c0f4ea1082a8bacaa17ac6b1bf7)
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     * @param int $rid
96     */
97    protected function saveData($page, $table, $data, $rev = 0, $rid = 0) {
98        saveWikiText($page, "test for $page", "saved for testing");
99        if (AccessTable::isTypePage($page, $rev)) {
100            $access = AccessTable::getPageAccess($table, $page, $rev);
101        } elseif(AccessTable::isTypeSerial($page, $rev)) {
102            $access = AccessTable::getSerialAccess($table, $page);
103        } else {
104            $access = AccessTable::getGlobalAccess($table, $rid);
105        }
106        $access->saveData($data);
107        $assignments = Assignments::getInstance();
108        $assignments->assignPageSchema($page, $table);
109    }
110
111    /**
112     * Access the plugin's English language strings
113     *
114     * @param string $key
115     * @return string
116     */
117    protected function getLang($key) {
118        static $lang = null;
119        if(is_null($lang)) {
120            $lang = array();
121            include(DOKU_PLUGIN . 'struct/lang/en/lang.php');
122        }
123        return $lang[$key];
124    }
125
126    /**
127     * Removes Whitespace
128     *
129     * Makes comparing sql statements a bit simpler as it ignores formatting
130     *
131     * @param $string
132     * @return string
133     */
134    protected function cleanWS($string) {
135        return preg_replace('/\s+/s', '', $string);
136    }
137}
138