xref: /plugin/struct/_test/StructTest.php (revision 9d58042634fc9c4c71778f7c94d8ff0aa7f72e1a)
128318177SAndreas Gohr<?php
228318177SAndreas Gohr
328318177SAndreas Gohrnamespace plugin\struct\test;
428318177SAndreas Gohr
528318177SAndreas Gohruse plugin\struct\meta\SchemaData;
628318177SAndreas Gohruse plugin\struct\meta\SchemaImporter;
7*9d580426SAndreas Gohr
828318177SAndreas Gohrspl_autoload_register(array('action_plugin_struct_autoloader', 'autoloader'));
928318177SAndreas Gohr
1028318177SAndreas Gohr/**
1128318177SAndreas Gohr * Base class for all struct tests
1228318177SAndreas Gohr *
1328318177SAndreas Gohr * It cleans up the database in teardown and provides some useful helper methods
1428318177SAndreas Gohr *
1528318177SAndreas Gohr * @package plugin\struct\test
1628318177SAndreas Gohr */
1728318177SAndreas Gohrabstract class StructTest extends \DokuWikiTest {
1828318177SAndreas Gohr
1928318177SAndreas Gohr    /** @var array alway enable the needed plugins */
2028318177SAndreas Gohr    protected $pluginsEnabled = array('struct', 'sqlite');
2128318177SAndreas Gohr
2228318177SAndreas Gohr    /**
2328318177SAndreas Gohr     * Default teardown
2428318177SAndreas Gohr     *
2528318177SAndreas Gohr     * we always make sure the database is clear
2628318177SAndreas Gohr     */
2728318177SAndreas Gohr    protected function tearDown() {
2828318177SAndreas Gohr        parent::tearDown();
29*9d580426SAndreas Gohr        /** @var \helper_plugin_struct_db $db */
30*9d580426SAndreas Gohr        $db = plugin_load('helper', 'struct_db');
31*9d580426SAndreas Gohr        $db->resetDB();
3228318177SAndreas Gohr    }
3328318177SAndreas Gohr
3428318177SAndreas Gohr    /**
3528318177SAndreas Gohr     * Creates a schema from one of the available schema files
3628318177SAndreas Gohr     *
3728318177SAndreas Gohr     * @param string $schema
3828318177SAndreas Gohr     * @param string $json base name of the JSON file optional, defaults to $schema
39*9d580426SAndreas Gohr     * @param int $rev allows to create schemas back in time
4028318177SAndreas Gohr     */
41*9d580426SAndreas Gohr    protected function loadSchemaJSON($schema, $json = '', $rev = 0) {
4228318177SAndreas Gohr        if(!$json) $json = $schema;
4328318177SAndreas Gohr        $file = __DIR__ . "/json/$json.struct.json";
4428318177SAndreas Gohr        if(!file_exists($file)) {
4528318177SAndreas Gohr            throw new \RuntimeException("$file does not exist");
4628318177SAndreas Gohr        }
4728318177SAndreas Gohr
4828318177SAndreas Gohr        $importer = new SchemaImporter($schema, file_get_contents($file));
4928318177SAndreas Gohr
50*9d580426SAndreas Gohr        if(!$importer->build($rev)) {
5128318177SAndreas Gohr            throw new \RuntimeException("build of $schema from $file failed");
5228318177SAndreas Gohr        }
5328318177SAndreas Gohr    }
5428318177SAndreas Gohr
5528318177SAndreas Gohr    /**
5628318177SAndreas Gohr     * This waits until a new second has passed
5728318177SAndreas Gohr     *
5828318177SAndreas Gohr     * The very first call will return immeadiately, proceeding calls will return
59*9d580426SAndreas Gohr     * only after at least 1 second after the last call has passed.
6028318177SAndreas Gohr     *
61*9d580426SAndreas Gohr     * When passing $init=true it will not return immeadiately but use the current
62*9d580426SAndreas Gohr     * second as initialization. It might still return faster than a second.
63*9d580426SAndreas Gohr     *
64*9d580426SAndreas Gohr     * @param bool $init wait from now on, not from last time
6528318177SAndreas Gohr     * @return int new timestamp
6628318177SAndreas Gohr     */
67*9d580426SAndreas Gohr    protected function waitForTick($init = false) {
6828318177SAndreas Gohr        static $last = 0;
69*9d580426SAndreas Gohr        if($init) $last = time();
70*9d580426SAndreas Gohr
7128318177SAndreas Gohr        while($last === $now = time()) {
7228318177SAndreas Gohr            usleep(100000); //recheck in a 10th of a second
7328318177SAndreas Gohr        }
7428318177SAndreas Gohr        $last = $now;
7528318177SAndreas Gohr        return $now;
7628318177SAndreas Gohr    }
7728318177SAndreas Gohr
7828318177SAndreas Gohr    /**
7928318177SAndreas Gohr     * Saves struct data for given page and schema
8028318177SAndreas Gohr     *
81*9d580426SAndreas Gohr     * Please note that setting the $rev only influences the struct data timestamp,
82*9d580426SAndreas Gohr     * not the page and changelog entries.
83*9d580426SAndreas Gohr     *
8428318177SAndreas Gohr     * @param string $page
8528318177SAndreas Gohr     * @param string $schema
8628318177SAndreas Gohr     * @param array $data
87*9d580426SAndreas Gohr     * @param int $rev allows to override the revision timestamp
8828318177SAndreas Gohr     */
89*9d580426SAndreas Gohr    protected function saveData($page, $schema, $data, $rev = 0) {
90*9d580426SAndreas Gohr        if(!$rev) $rev = time();
91*9d580426SAndreas Gohr
9228318177SAndreas Gohr        saveWikiText($page, "test for $page", "saved for testing");
93*9d580426SAndreas Gohr        $schemaData = new SchemaData($schema, $page, $rev);
9428318177SAndreas Gohr        $schemaData->saveData($data);
9528318177SAndreas Gohr    }
96*9d580426SAndreas Gohr
97*9d580426SAndreas Gohr    /**
98*9d580426SAndreas Gohr     * Access the plugin's english language strings
99*9d580426SAndreas Gohr     *
100*9d580426SAndreas Gohr     * @param string $key
101*9d580426SAndreas Gohr     * @return string
102*9d580426SAndreas Gohr     */
103*9d580426SAndreas Gohr    protected function getLang($key) {
104*9d580426SAndreas Gohr        static $lang = null;
105*9d580426SAndreas Gohr        if(is_null($lang)) {
106*9d580426SAndreas Gohr            $lang = array();
107*9d580426SAndreas Gohr            include(DOKU_PLUGIN . 'struct/lang/en/lang.php');
108*9d580426SAndreas Gohr        }
109*9d580426SAndreas Gohr        return $lang[$key];
110*9d580426SAndreas Gohr    }
11128318177SAndreas Gohr}
112