xref: /plugin/struct/_test/StructTest.php (revision 6f023786c1c663521843bb4140ddac5ce47a93b8)
1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\AccessTable;
6use dokuwiki\plugin\struct\meta\Assignments;
7use dokuwiki\plugin\struct\meta\SchemaImporter;
8
9/**
10 * Base class for all struct tests
11 *
12 * It cleans up the database in teardown and provides some useful helper methods
13 *
14 * @package dokuwiki\plugin\struct\test
15 */
16abstract class StructTest extends \DokuWikiTest {
17
18    /** @var array alway enable the needed plugins */
19    protected $pluginsEnabled = array('struct', 'sqlite');
20
21    /**
22     * Default teardown
23     *
24     * we always make sure the database is clear
25     */
26    protected function tearDown() {
27        parent::tearDown();
28        /** @var \helper_plugin_struct_db $db */
29        $db = plugin_load('helper', 'struct_db');
30        $db->resetDB();
31    }
32
33    /**
34     * Creates a schema from one of the available schema files
35     *
36     * @param string $schema
37     * @param string $json base name of the JSON file optional, defaults to $schema
38     * @param int $rev allows to create schemas back in time
39     * @param bool $lookup create as a lookup schema
40     */
41    protected function loadSchemaJSON($schema, $json = '', $rev = 0, $lookup = false) {
42        if(!$json) $json = $schema;
43        $file = __DIR__ . "/json/$json.struct.json";
44        if(!file_exists($file)) {
45            throw new \RuntimeException("$file does not exist");
46        }
47
48        $importer = new SchemaImporter($schema, file_get_contents($file), $lookup);
49
50        if(!$importer->build($rev)) {
51            throw new \RuntimeException("build of $schema from $file failed");
52        }
53    }
54
55    /**
56     * This waits until a new second has passed
57     *
58     * The very first call will return immeadiately, proceeding calls will return
59     * only after at least 1 second after the last call has passed.
60     *
61     * When passing $init=true it will not return immeadiately but use the current
62     * second as initialization. It might still return faster than a second.
63     *
64     * @param bool $init wait from now on, not from last time
65     * @return int new timestamp
66     */
67    protected function waitForTick($init = false) {
68        static $last = 0;
69        if($init) $last = time();
70
71        while($last === $now = time()) {
72            usleep(100000); //recheck in a 10th of a second
73        }
74        $last = $now;
75        return $now;
76    }
77
78    /**
79     * Saves struct data for given page and schema
80     *
81     * Please note that setting the $rev only influences the struct data timestamp,
82     * not the page and changelog entries.
83     *
84     * @param string $page
85     * @param string $schema
86     * @param array $data
87     * @param int $rev allows to override the revision timestamp
88     */
89    protected function saveData($page, $schema, $data, $rev = 0) {
90        if(!$rev) $rev = time();
91
92        saveWikiText($page, "test for $page", "saved for testing");
93        $schemaData = AccessTable::byTableName($schema, $page, $rev);
94        $schemaData->saveData($data);
95        $assignments = new Assignments();
96        $assignments->assignPageSchema($page, $schema);
97    }
98
99    /**
100     * Access the plugin's english language strings
101     *
102     * @param string $key
103     * @return string
104     */
105    protected function getLang($key) {
106        static $lang = null;
107        if(is_null($lang)) {
108            $lang = array();
109            include(DOKU_PLUGIN . 'struct/lang/en/lang.php');
110        }
111        return $lang[$key];
112    }
113
114    /**
115     * Removes Whitespace
116     *
117     * Makes comparing sql statements a bit simpler as it ignores formatting
118     *
119     * @param $string
120     * @return string
121     */
122    protected function cleanWS($string) {
123        return preg_replace('/\s+/s', '', $string);
124    }
125}
126