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,
65                    published BOOLEAN DEFAULT NULL, col1 DEFAULT '', col2 DEFAULT '',
66                    PRIMARY KEY(pid, rid, rev)
67                )";
68
69        $res = $this->sqlite->query("SELECT * FROM types");
70        $actual_types = $this->sqlite->res2arr($res);
71        $this->sqlite->res_close($res);
72        $expected_types = array(
73            array(
74                'id' => "1",
75                'class' => 'Text',
76                'ismulti' => "0",
77                'label' => "testcolumn",
78                'config' => '{"prefix": "", "postfix": ""}'
79            ),
80            array(
81                'id' => "2",
82                'class' => 'Text',
83                'ismulti' => "1",
84                'label' => "testMulitColumn",
85                'config' => '{"prefix": "pre", "postfix": "post"}'
86            )
87        );
88
89        $res = $this->sqlite->query("SELECT * FROM schema_cols");
90        $actual_cols = $this->sqlite->res2arr($res);
91        $this->sqlite->res_close($res);
92        $expected_cols = array(
93            array(
94                'sid' => "1",
95                'colref' => "1",
96                'enabled' => "1",
97                'tid' => "1",
98                'sort' => "70"
99            ),
100            array(
101                'sid' => "1",
102                'colref' => "2",
103                'enabled' => "1",
104                'tid' => "2",
105                'sort' => "40"
106            )
107        );
108
109        $res = $this->sqlite->query("SELECT * FROM schemas");
110        $actual_schema = $this->sqlite->res2row($res);
111        $this->sqlite->res_close($res);
112
113        $this->assertSame(1, $result);
114        $this->assertEquals($expected_tableSQL, $tableSQL);
115        $this->assertEquals($expected_types, $actual_types);
116        $this->assertEquals($expected_cols, $actual_cols);
117        $this->assertEquals(1, $actual_schema['id']);
118        $this->assertEquals($testname, $actual_schema['tbl']);
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(1, $result, '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(2, $result);
176        $this->assertEquals($expected_types, $actual_types);
177
178    }
179}
180