xref: /plugin/struct/_test/StructTest.php (revision 1dfe5343b719d50bfe1c5e0b85e6cdaa31180e35)
1<?php
2
3namespace plugin\struct\test;
4
5use plugin\struct\meta\SchemaData;
6use plugin\struct\meta\SchemaImporter;
7
8spl_autoload_register(array('action_plugin_struct_autoloader', 'autoloader'));
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 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 = new SchemaData($schema, $page, $rev);
94        $schemaData->saveData($data);
95    }
96
97    /**
98     * Access the plugin's english language strings
99     *
100     * @param string $key
101     * @return string
102     */
103    protected function getLang($key) {
104        static $lang = null;
105        if(is_null($lang)) {
106            $lang = array();
107            include(DOKU_PLUGIN . 'struct/lang/en/lang.php');
108        }
109        return $lang[$key];
110    }
111}
112