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