xref: /plugin/struct/_test/StructTest.php (revision 854aaac5ec91fc4cdb3c40652a7be2603497fe43)
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, $lookup = false) {
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), $lookup);
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        static $last = 0;
70        if($init) $last = time();
71
72        while($last === $now = time()) {
73            usleep(100000); //recheck in a 10th of a second
74        }
75        $last = $now;
76        return $now;
77    }
78
79    /**
80     * Saves struct data for given page and schema
81     *
82     * Please note that setting the $rev only influences the struct data timestamp,
83     * not the page and changelog entries.
84     *
85     * @param string $page
86     * @param string $schema
87     * @param array $data
88     * @param int $rev allows to override the revision timestamp
89     */
90    protected function saveData($page, $schema, $data, $rev = 0) {
91        if(!$rev) $rev = time();
92
93        saveWikiText($page, "test for $page", "saved for testing");
94        $schemaData = AccessTable::byTableName($schema, $page, $rev);
95        $schemaData->saveData($data);
96        $assignments = Assignments::getInstance();
97        $assignments->assignPageSchema($page, $schema);
98    }
99
100    /**
101     * Access the plugin's english language strings
102     *
103     * @param string $key
104     * @return string
105     */
106    protected function getLang($key) {
107        static $lang = null;
108        if(is_null($lang)) {
109            $lang = array();
110            include(DOKU_PLUGIN . 'struct/lang/en/lang.php');
111        }
112        return $lang[$key];
113    }
114
115    /**
116     * Removes Whitespace
117     *
118     * Makes comparing sql statements a bit simpler as it ignores formatting
119     *
120     * @param $string
121     * @return string
122     */
123    protected function cleanWS($string) {
124        return preg_replace('/\s+/s', '', $string);
125    }
126}
127