1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\AccessTable;
6use dokuwiki\plugin\struct\meta\Column;
7use dokuwiki\plugin\struct\meta\SchemaImporter;
8use dokuwiki\plugin\struct\meta\Value;
9use dokuwiki\plugin\struct\test\mock\Assignments;
10use dokuwiki\plugin\struct\types\Text;
11
12/**
13 * Base class for all struct tests
14 *
15 * It cleans up the database in teardown and provides some useful helper methods
16 *
17 * @package dokuwiki\plugin\struct\test
18 */
19abstract class StructTest extends \DokuWikiTest
20{
21
22    /** @var array alway enable the needed plugins */
23    protected $pluginsEnabled = array('struct', 'sqlite');
24
25    /**
26     * Default teardown
27     *
28     * we always make sure the database is clear
29     */
30    protected function tearDown(): void
31    {
32        parent::tearDown();
33        /** @var \helper_plugin_struct_db $db */
34        $db = plugin_load('helper', 'struct_db');
35        $db->resetDB();
36        Assignments::reset();
37    }
38
39    /**
40     * Creates a schema from one of the available schema files
41     *
42     * @param string $schema
43     * @param string $json base name of the JSON file optional, defaults to $schema
44     * @param int $rev allows to create schemas back in time
45     */
46    protected function loadSchemaJSON($schema, $json = '', $rev = 0)
47    {
48        if (!$json) $json = $schema;
49        $file = __DIR__ . "/json/$json.struct.json";
50        if (!file_exists($file)) {
51            throw new \RuntimeException("$file does not exist");
52        }
53
54        $importer = new SchemaImporter($schema, file_get_contents($file));
55
56        if (!$importer->build($rev)) {
57            throw new \RuntimeException("build of $schema from $file failed");
58        }
59    }
60
61    /**
62     * Saves struct data for given page and schema
63     *
64     * Please note that setting the $rev only influences the struct data timestamp,
65     * not the page and changelog entries.
66     *
67     * @param string $page
68     * @param string $table
69     * @param array $data
70     * @param int $rev allows to override the revision timestamp
71     * @param int $rid
72     */
73    protected function saveData($page, $table, $data, $rev = 0, $rid = 0)
74    {
75        saveWikiText($page, "test for $page", "saved for testing");
76        if (AccessTable::isTypePage($page, $rev)) {
77            $access = AccessTable::getPageAccess($table, $page, $rev);
78        } elseif (AccessTable::isTypeSerial($page, $rev)) {
79            $access = AccessTable::getSerialAccess($table, $page);
80        } else {
81            $access = AccessTable::getGlobalAccess($table, $rid);
82        }
83        $access->saveData($data);
84        $assignments = Assignments::getInstance();
85        $assignments->assignPageSchema($page, $table);
86    }
87
88    /**
89     * Access the plugin's English language strings
90     *
91     * @param string $key
92     * @return string
93     */
94    protected function getLang($key)
95    {
96        static $lang = null;
97        if (is_null($lang)) {
98            $lang = array();
99            include(DOKU_PLUGIN . 'struct/lang/en/lang.php');
100        }
101        return $lang[$key];
102    }
103
104    /**
105     * Removes Whitespace
106     *
107     * Makes comparing sql statements a bit simpler as it ignores formatting
108     *
109     * @param $string
110     * @return string
111     */
112    protected function cleanWS($string)
113    {
114        return preg_replace(['/\s+/s', '/\:val(\d{1,3})/'], ['', '?'], $string);
115    }
116
117    /**
118     * Create an Aggregation result set from a given flat array
119     *
120     * The result will contain simple Text columns
121     *
122     * @param array $rows
123     * @return array
124     */
125    protected function createAggregationResult($rows)
126    {
127        $result = [];
128
129        foreach ($rows as $row) {
130            $resultRow = [];
131            foreach ($row as $num => $cell) {
132                $colRef = $num + 1;
133                $resultRow[] = new Value(
134                    new Column(
135                        10,
136                        new Text(['label' => ['en' => "Label $colRef"]], "field$colRef", is_array($cell)),
137                        $colRef,
138                        true,
139                        'test'
140                    ),
141                    $cell
142                );
143            }
144            $result[] = $resultRow;
145        }
146
147        return $result;
148    }
149}
150