1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta\Schema;
6use dokuwiki\plugin\struct\meta\SchemaBuilder;
7
8/**
9 * @group plugin_struct
10 * @group plugins
11 *
12 */
13class schemaBuilder_struct_test extends StructTest
14{
15
16    /** @var \helper_plugin_sqlite $sqlite */
17    protected $sqlite;
18
19    public function setUp(): void
20    {
21        parent::setUp();
22
23        /** @var \helper_plugin_struct_db $sqlite */
24        $sqlite = plugin_load('helper', 'struct_db');
25        $this->sqlite = $sqlite->getDB();
26    }
27
28    /**
29     *
30     */
31    public function test_build_new()
32    {
33
34        // arrange
35        $testdata = array();
36        $testdata['new']['new1']['sort'] = 70;
37        $testdata['new']['new1']['label'] = 'testcolumn';
38        $testdata['new']['new1']['ismulti'] = 0;
39        $testdata['new']['new1']['config'] = '{"prefix": "", "postfix": ""}';
40        $testdata['new']['new1']['class'] = 'Text';
41        $testdata['new']['new1']['isenabled'] = '1';
42        $testdata['new']['new2']['sort'] = 40;
43        $testdata['new']['new2']['label'] = 'testMulitColumn';
44        $testdata['new']['new2']['ismulti'] = 1;
45        $testdata['new']['new2']['config'] = '{"prefix": "pre", "postfix": "post"}';
46        $testdata['new']['new2']['class'] = 'Text';
47        $testdata['new']['new2']['isenabled'] = '1';
48
49        $testname = 'testTable';
50        $testname = Schema::cleanTableName($testname);
51
52        // act
53        $builder = new SchemaBuilder($testname, $testdata);
54        $result = $builder->build();
55
56        /** @noinspection SqlResolve */
57        $res = $this->sqlite->query("SELECT sql FROM sqlite_master WHERE type='table' AND name=?", 'data_' . $testname);
58        $tableSQL = $this->sqlite->res2single($res);
59        $this->sqlite->res_close($res);
60        $expected_tableSQL = "CREATE TABLE data_testtable (
61                    pid TEXT DEFAULT '',
62                    rid INTEGER,
63                    rev INTEGER,
64                    latest BOOLEAN NOT NULL DEFAULT 0, col1 DEFAULT '', col2 DEFAULT '',
65                    PRIMARY KEY(pid, rid, rev)
66                )";
67
68        $res = $this->sqlite->query("SELECT * FROM types");
69        $actual_types = $this->sqlite->res2arr($res);
70        $this->sqlite->res_close($res);
71        $expected_types = array(
72            array(
73                'id' => "1",
74                'class' => 'Text',
75                'ismulti' => "0",
76                'label' => "testcolumn",
77                'config' => '{"prefix": "", "postfix": ""}'
78            ),
79            array(
80                'id' => "2",
81                'class' => 'Text',
82                'ismulti' => "1",
83                'label' => "testMulitColumn",
84                'config' => '{"prefix": "pre", "postfix": "post"}'
85            )
86        );
87
88        $res = $this->sqlite->query("SELECT * FROM schema_cols");
89        $actual_cols = $this->sqlite->res2arr($res);
90        $this->sqlite->res_close($res);
91        $expected_cols = array(
92            array(
93                'sid' => "1",
94                'colref' => "1",
95                'enabled' => "1",
96                'tid' => "1",
97                'sort' => "70"
98            ),
99            array(
100                'sid' => "1",
101                'colref' => "2",
102                'enabled' => "1",
103                'tid' => "2",
104                'sort' => "40"
105            )
106        );
107
108        $res = $this->sqlite->query("SELECT * FROM schemas");
109        $actual_schema = $this->sqlite->res2row($res);
110        $this->sqlite->res_close($res);
111
112        $this->assertSame('1', $result);
113        $this->assertEquals($expected_tableSQL, $tableSQL);
114        $this->assertEquals($expected_types, $actual_types);
115        $this->assertEquals($expected_cols, $actual_cols);
116        $this->assertEquals('1', $actual_schema['id']);
117        $this->assertEquals($testname, $actual_schema['tbl']);
118        $this->assertEquals('', $actual_schema['chksum']);
119        $this->assertTrue((int)$actual_schema['ts'] > 0, 'timestamp should be larger than 0');
120    }
121
122    public function test_build_update()
123    {
124
125        // arrange
126        $initialdata = array();
127        $initialdata['new']['new1']['sort'] = 70;
128        $initialdata['new']['new1']['label'] = 'testcolumn';
129        $initialdata['new']['new1']['ismulti'] = 0;
130        $initialdata['new']['new1']['config'] = '{"prefix": "", "postfix": ""}';
131        $initialdata['new']['new1']['class'] = 'Text';
132        $initialdata['new']['new1']['isenabled'] = '1';
133
134        $testname = 'testTable';
135        $testname = Schema::cleanTableName($testname);
136
137        $builder = new SchemaBuilder($testname, $initialdata);
138        $result = $builder->build();
139        $this->assertSame($result, '1', 'Prerequiste setup  in order to have basis which to change during act');
140
141        $updatedata = array();
142        $updatedata['id'] = "1";
143        $updatedata['cols']['1']['sort'] = 65;
144        $updatedata['cols']['1']['label'] = 'testColumn';
145        $updatedata['cols']['1']['ismulti'] = 1;
146        $updatedata['cols']['1']['config'] = '{"prefix": "pre", "postfix": "fix"}';
147        $updatedata['cols']['1']['class'] = 'Text';
148        $updatedata['cols']['1']['isenabled'] = '1';
149
150        // act
151        $builder = new SchemaBuilder($testname, $updatedata);
152        $result = $builder->build();
153
154        $res = $this->sqlite->query("SELECT * FROM types");
155        $actual_types = $this->sqlite->res2arr($res);
156        $this->sqlite->res_close($res);
157        $expected_types = array(
158            array(
159                'id' => "1",
160                'class' => 'Text',
161                'ismulti' => "0",
162                'label' => "testcolumn",
163                'config' => '{"prefix": "", "postfix": ""}'
164            ),
165            array(
166                'id' => "2",
167                'class' => 'Text',
168                'ismulti' => "1",
169                'label' => "testColumn",
170                'config' => '{"prefix": "pre", "postfix": "fix"}'
171            )
172        );
173
174        // assert
175        $this->assertSame($result, '2');
176        $this->assertEquals($actual_types, $expected_types);
177
178    }
179}
180