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