xref: /plugin/struct/_test/StructTest.php (revision fc26989e4442674ded604c12f445cb8ca3510cf9)
1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5
6use dokuwiki\plugin\struct\meta\AccessTable;
7use dokuwiki\plugin\struct\meta\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    }
33
34    /**
35     * Creates a schema from one of the available schema files
36     *
37     * @param string $schema
38     * @param string $json base name of the JSON file optional, defaults to $schema
39     * @param int $rev allows to create schemas back in time
40     */
41    protected function loadSchemaJSON($schema, $json = '', $rev = 0) {
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));
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