xref: /plugin/struct/_test/StructTest.php (revision f1812f0b7ac4d9c715e38beb78ffe9711588b0c0)
128318177SAndreas Gohr<?php
228318177SAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\test;
428318177SAndreas Gohr
5ea9843dcSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
6*f1812f0bSAndreas Gohruse dokuwiki\plugin\struct\meta\Column;
7ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaImporter;
8*f1812f0bSAndreas Gohruse dokuwiki\plugin\struct\meta\Value;
90549dcc5SAndreas Gohruse dokuwiki\plugin\struct\test\mock\Assignments;
10*f1812f0bSAndreas Gohruse dokuwiki\plugin\struct\types\Text;
1128318177SAndreas Gohr
1228318177SAndreas Gohr/**
1328318177SAndreas Gohr * Base class for all struct tests
1428318177SAndreas Gohr *
1528318177SAndreas Gohr * It cleans up the database in teardown and provides some useful helper methods
1628318177SAndreas Gohr *
17ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\test
1828318177SAndreas Gohr */
190549dcc5SAndreas Gohrabstract class StructTest extends \DokuWikiTest
200549dcc5SAndreas Gohr{
2128318177SAndreas Gohr
2228318177SAndreas Gohr    /** @var array alway enable the needed plugins */
2328318177SAndreas Gohr    protected $pluginsEnabled = array('struct', 'sqlite');
2428318177SAndreas Gohr
2528318177SAndreas Gohr    /**
2628318177SAndreas Gohr     * Default teardown
2728318177SAndreas Gohr     *
2828318177SAndreas Gohr     * we always make sure the database is clear
2928318177SAndreas Gohr     */
300549dcc5SAndreas Gohr    protected function tearDown(): void
310549dcc5SAndreas Gohr    {
3228318177SAndreas Gohr        parent::tearDown();
339d580426SAndreas Gohr        /** @var \helper_plugin_struct_db $db */
349d580426SAndreas Gohr        $db = plugin_load('helper', 'struct_db');
359d580426SAndreas Gohr        $db->resetDB();
36025cb9daSAndreas Gohr        Assignments::reset();
3728318177SAndreas Gohr    }
3828318177SAndreas Gohr
3928318177SAndreas Gohr    /**
4028318177SAndreas Gohr     * Creates a schema from one of the available schema files
4128318177SAndreas Gohr     *
4228318177SAndreas Gohr     * @param string $schema
4328318177SAndreas Gohr     * @param string $json base name of the JSON file optional, defaults to $schema
449d580426SAndreas Gohr     * @param int $rev allows to create schemas back in time
4528318177SAndreas Gohr     */
460549dcc5SAndreas Gohr    protected function loadSchemaJSON($schema, $json = '', $rev = 0)
470549dcc5SAndreas Gohr    {
4828318177SAndreas Gohr        if (!$json) $json = $schema;
4928318177SAndreas Gohr        $file = __DIR__ . "/json/$json.struct.json";
5028318177SAndreas Gohr        if (!file_exists($file)) {
5128318177SAndreas Gohr            throw new \RuntimeException("$file does not exist");
5228318177SAndreas Gohr        }
5328318177SAndreas Gohr
547f803aa8SAnna Dabrowska        $importer = new SchemaImporter($schema, file_get_contents($file));
5528318177SAndreas Gohr
569d580426SAndreas Gohr        if (!$importer->build($rev)) {
5728318177SAndreas Gohr            throw new \RuntimeException("build of $schema from $file failed");
5828318177SAndreas Gohr        }
5928318177SAndreas Gohr    }
6028318177SAndreas Gohr
6128318177SAndreas Gohr    /**
6228318177SAndreas Gohr     * Saves struct data for given page and schema
6328318177SAndreas Gohr     *
649d580426SAndreas Gohr     * Please note that setting the $rev only influences the struct data timestamp,
659d580426SAndreas Gohr     * not the page and changelog entries.
669d580426SAndreas Gohr     *
6728318177SAndreas Gohr     * @param string $page
684cd5cc28SAnna Dabrowska     * @param string $table
6928318177SAndreas Gohr     * @param array $data
709d580426SAndreas Gohr     * @param int $rev allows to override the revision timestamp
71777be84fSAnna Dabrowska     * @param int $rid
7228318177SAndreas Gohr     */
730549dcc5SAndreas Gohr    protected function saveData($page, $table, $data, $rev = 0, $rid = 0)
740549dcc5SAndreas Gohr    {
7528318177SAndreas Gohr        saveWikiText($page, "test for $page", "saved for testing");
764cd5cc28SAnna Dabrowska        if (AccessTable::isTypePage($page, $rev)) {
774cd5cc28SAnna Dabrowska            $access = AccessTable::getPageAccess($table, $page, $rev);
784cd5cc28SAnna Dabrowska        } elseif (AccessTable::isTypeSerial($page, $rev)) {
794cd5cc28SAnna Dabrowska            $access = AccessTable::getSerialAccess($table, $page);
804cd5cc28SAnna Dabrowska        } else {
81777be84fSAnna Dabrowska            $access = AccessTable::getGlobalAccess($table, $rid);
824cd5cc28SAnna Dabrowska        }
834cd5cc28SAnna Dabrowska        $access->saveData($data);
84025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
854cd5cc28SAnna Dabrowska        $assignments->assignPageSchema($page, $table);
8628318177SAndreas Gohr    }
879d580426SAndreas Gohr
889d580426SAndreas Gohr    /**
894cd5cc28SAnna Dabrowska     * Access the plugin's English language strings
909d580426SAndreas Gohr     *
919d580426SAndreas Gohr     * @param string $key
929d580426SAndreas Gohr     * @return string
939d580426SAndreas Gohr     */
940549dcc5SAndreas Gohr    protected function getLang($key)
950549dcc5SAndreas Gohr    {
969d580426SAndreas Gohr        static $lang = null;
979d580426SAndreas Gohr        if (is_null($lang)) {
989d580426SAndreas Gohr            $lang = array();
999d580426SAndreas Gohr            include(DOKU_PLUGIN . 'struct/lang/en/lang.php');
1009d580426SAndreas Gohr        }
1019d580426SAndreas Gohr        return $lang[$key];
1029d580426SAndreas Gohr    }
103df30dbf7SAndreas Gohr
104df30dbf7SAndreas Gohr    /**
105df30dbf7SAndreas Gohr     * Removes Whitespace
106df30dbf7SAndreas Gohr     *
107df30dbf7SAndreas Gohr     * Makes comparing sql statements a bit simpler as it ignores formatting
108df30dbf7SAndreas Gohr     *
109df30dbf7SAndreas Gohr     * @param $string
110df30dbf7SAndreas Gohr     * @return string
111df30dbf7SAndreas Gohr     */
1120549dcc5SAndreas Gohr    protected function cleanWS($string)
1130549dcc5SAndreas Gohr    {
114df30dbf7SAndreas Gohr        return preg_replace('/\s+/s', '', $string);
115df30dbf7SAndreas Gohr    }
116*f1812f0bSAndreas Gohr
117*f1812f0bSAndreas Gohr    /**
118*f1812f0bSAndreas Gohr     * Create an Aggregation result set from a given flat array
119*f1812f0bSAndreas Gohr     *
120*f1812f0bSAndreas Gohr     * The result will contain simple Text columns
121*f1812f0bSAndreas Gohr     *
122*f1812f0bSAndreas Gohr     * @param array $rows
123*f1812f0bSAndreas Gohr     * @return array
124*f1812f0bSAndreas Gohr     */
125*f1812f0bSAndreas Gohr    protected function createAggregationResult($rows)
126*f1812f0bSAndreas Gohr    {
127*f1812f0bSAndreas Gohr        $result = [];
128*f1812f0bSAndreas Gohr
129*f1812f0bSAndreas Gohr        foreach ($rows as $row) {
130*f1812f0bSAndreas Gohr            $resultRow = [];
131*f1812f0bSAndreas Gohr            foreach ($row as $num => $cell) {
132*f1812f0bSAndreas Gohr                $colRef = $num + 1;
133*f1812f0bSAndreas Gohr                $resultRow[] = new Value(
134*f1812f0bSAndreas Gohr                    new Column(
135*f1812f0bSAndreas Gohr                        10,
136*f1812f0bSAndreas Gohr                        new Text(['label' => ['en' => "Label $colRef"]], "field$colRef", is_array($cell)),
137*f1812f0bSAndreas Gohr                        $colRef,
138*f1812f0bSAndreas Gohr                        true,
139*f1812f0bSAndreas Gohr                        'test'
140*f1812f0bSAndreas Gohr                    ),
141*f1812f0bSAndreas Gohr                    $cell
142*f1812f0bSAndreas Gohr                );
143*f1812f0bSAndreas Gohr            }
144*f1812f0bSAndreas Gohr            $result[] = $resultRow;
145*f1812f0bSAndreas Gohr        }
146*f1812f0bSAndreas Gohr
147*f1812f0bSAndreas Gohr        return $result;
148*f1812f0bSAndreas Gohr    }
14928318177SAndreas Gohr}
150