xref: /plugin/struct/_test/StructTest.php (revision 8a701f071409959636f09cd7b057c9a9431b602c)
1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\Assignments;
6use dokuwiki\plugin\struct\meta\SchemaData;
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     */
40    protected function loadSchemaJSON($schema, $json = '', $rev = 0) {
41        if(!$json) $json = $schema;
42        $file = __DIR__ . "/json/$json.struct.json";
43        if(!file_exists($file)) {
44            throw new \RuntimeException("$file does not exist");
45        }
46
47        $importer = new SchemaImporter($schema, file_get_contents($file));
48
49        if(!$importer->build($rev)) {
50            throw new \RuntimeException("build of $schema from $file failed");
51        }
52    }
53
54    /**
55     * This waits until a new second has passed
56     *
57     * The very first call will return immeadiately, proceeding calls will return
58     * only after at least 1 second after the last call has passed.
59     *
60     * When passing $init=true it will not return immeadiately but use the current
61     * second as initialization. It might still return faster than a second.
62     *
63     * @param bool $init wait from now on, not from last time
64     * @return int new timestamp
65     */
66    protected function waitForTick($init = false) {
67        static $last = 0;
68        if($init) $last = time();
69
70        while($last === $now = time()) {
71            usleep(100000); //recheck in a 10th of a second
72        }
73        $last = $now;
74        return $now;
75    }
76
77    /**
78     * Saves struct data for given page and schema
79     *
80     * Please note that setting the $rev only influences the struct data timestamp,
81     * not the page and changelog entries.
82     *
83     * @param string $page
84     * @param string $schema
85     * @param array $data
86     * @param int $rev allows to override the revision timestamp
87     */
88    protected function saveData($page, $schema, $data, $rev = 0) {
89        if(!$rev) $rev = time();
90
91        saveWikiText($page, "test for $page", "saved for testing");
92        $schemaData = new SchemaData($schema, $page, $rev);
93        $schemaData->saveData($data);
94        $assignments = new Assignments();
95        $assignments->assignPageSchema($page, $schema);
96    }
97
98    /**
99     * Access the plugin's english language strings
100     *
101     * @param string $key
102     * @return string
103     */
104    protected function getLang($key) {
105        static $lang = null;
106        if(is_null($lang)) {
107            $lang = array();
108            include(DOKU_PLUGIN . 'struct/lang/en/lang.php');
109        }
110        return $lang[$key];
111    }
112
113    /**
114     * Removes Whitespace
115     *
116     * Makes comparing sql statements a bit simpler as it ignores formatting
117     *
118     * @param $string
119     * @return string
120     */
121    protected function cleanWS($string) {
122        return preg_replace('/\s+/s', '', $string);
123    }
124}
125