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