xref: /plugin/struct/_test/SchemaBuilderTest.php (revision 438a804c3e15c4f1e82daeb4ec346e2cf325d925)
18fed17f3SAndreas Gohr<?php
28fed17f3SAndreas Gohr
38fed17f3SAndreas Gohrnamespace dokuwiki\plugin\struct\test;
48fed17f3SAndreas Gohr
58fed17f3SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
68fed17f3SAndreas Gohruse dokuwiki\plugin\struct\meta\SchemaBuilder;
78fed17f3SAndreas Gohr
88fed17f3SAndreas Gohr/**
98fed17f3SAndreas Gohr * @group plugin_struct
108fed17f3SAndreas Gohr * @group plugins
118fed17f3SAndreas Gohr *
128fed17f3SAndreas Gohr */
138fed17f3SAndreas Gohrclass SchemaBuilderTest extends StructTest
148fed17f3SAndreas Gohr{
158fed17f3SAndreas Gohr
168fed17f3SAndreas Gohr    /** @var \helper_plugin_sqlite $sqlite */
178fed17f3SAndreas Gohr    protected $sqlite;
188fed17f3SAndreas Gohr
198fed17f3SAndreas Gohr    public function setUp(): void
208fed17f3SAndreas Gohr    {
218fed17f3SAndreas Gohr        parent::setUp();
228fed17f3SAndreas Gohr
238fed17f3SAndreas Gohr        /** @var \helper_plugin_struct_db $sqlite */
248fed17f3SAndreas Gohr        $sqlite = plugin_load('helper', 'struct_db');
258fed17f3SAndreas Gohr        $this->sqlite = $sqlite->getDB();
268fed17f3SAndreas Gohr    }
278fed17f3SAndreas Gohr
288fed17f3SAndreas Gohr    /**
298fed17f3SAndreas Gohr     * @noinspection SqlDialectInspection
308fed17f3SAndreas Gohr     * @noinspection SqlNoDataSourceInspection
318fed17f3SAndreas Gohr     */
328fed17f3SAndreas Gohr    public function test_build_new()
338fed17f3SAndreas Gohr    {
348fed17f3SAndreas Gohr
358fed17f3SAndreas Gohr        // arrange
368fed17f3SAndreas Gohr        $testdata = [];
378fed17f3SAndreas Gohr        $testdata['new']['new1']['sort'] = 70;
388fed17f3SAndreas Gohr        $testdata['new']['new1']['label'] = 'testcolumn';
398fed17f3SAndreas Gohr        $testdata['new']['new1']['ismulti'] = 0;
408fed17f3SAndreas Gohr        $testdata['new']['new1']['config'] = '{"prefix": "", "postfix": ""}';
418fed17f3SAndreas Gohr        $testdata['new']['new1']['class'] = 'Text';
428fed17f3SAndreas Gohr        $testdata['new']['new1']['isenabled'] = '1';
438fed17f3SAndreas Gohr        $testdata['new']['new2']['sort'] = 40;
448fed17f3SAndreas Gohr        $testdata['new']['new2']['label'] = 'testMulitColumn';
458fed17f3SAndreas Gohr        $testdata['new']['new2']['ismulti'] = 1;
468fed17f3SAndreas Gohr        $testdata['new']['new2']['config'] = '{"prefix": "pre", "postfix": "post"}';
478fed17f3SAndreas Gohr        $testdata['new']['new2']['class'] = 'Text';
488fed17f3SAndreas Gohr        $testdata['new']['new2']['isenabled'] = '1';
498fed17f3SAndreas Gohr
508fed17f3SAndreas Gohr        $testname = 'testTable';
518fed17f3SAndreas Gohr        $testname = Schema::cleanTableName($testname);
528fed17f3SAndreas Gohr
538fed17f3SAndreas Gohr        // act
548fed17f3SAndreas Gohr        $builder = new SchemaBuilder($testname, $testdata);
558fed17f3SAndreas Gohr        $result = $builder->build();
568fed17f3SAndreas Gohr
578fed17f3SAndreas Gohr        /** @noinspection SqlResolve */
58*438a804cSAnna Dabrowska        $tableSQL = $this->sqlite->queryValue("SELECT sql FROM sqlite_master WHERE type='table' AND name=?", ['data_' . $testname]);
598fed17f3SAndreas Gohr        $expected_tableSQL = "CREATE TABLE data_testtable (
608fed17f3SAndreas Gohr                    pid TEXT DEFAULT '',
618fed17f3SAndreas Gohr                    rid INTEGER,
628fed17f3SAndreas Gohr                    rev INTEGER,
638fed17f3SAndreas Gohr                    latest BOOLEAN NOT NULL DEFAULT 0,
648fed17f3SAndreas Gohr                    published BOOLEAN DEFAULT NULL, col1 DEFAULT '', col2 DEFAULT '',
658fed17f3SAndreas Gohr                    PRIMARY KEY(pid, rid, rev)
668fed17f3SAndreas Gohr                )";
678fed17f3SAndreas Gohr
6879b29326SAnna Dabrowska        $actual_types = $this->sqlite->queryAll("SELECT * FROM types");
698fed17f3SAndreas Gohr        $expected_types = [
708fed17f3SAndreas Gohr            [
718fed17f3SAndreas Gohr                'id' => "1",
728fed17f3SAndreas Gohr                'class' => 'Text',
738fed17f3SAndreas Gohr                'ismulti' => "0",
748fed17f3SAndreas Gohr                'label' => "testcolumn",
758fed17f3SAndreas Gohr                'config' => '{"prefix": "", "postfix": ""}'
768fed17f3SAndreas Gohr            ],
778fed17f3SAndreas Gohr            [
788fed17f3SAndreas Gohr                'id' => "2",
798fed17f3SAndreas Gohr                'class' => 'Text',
808fed17f3SAndreas Gohr                'ismulti' => "1",
818fed17f3SAndreas Gohr                'label' => "testMulitColumn",
828fed17f3SAndreas Gohr                'config' => '{"prefix": "pre", "postfix": "post"}'
838fed17f3SAndreas Gohr            ]
848fed17f3SAndreas Gohr        ];
858fed17f3SAndreas Gohr
8679b29326SAnna Dabrowska        $actual_cols = $this->sqlite->queryAll("SELECT * FROM schema_cols");
878fed17f3SAndreas Gohr        $expected_cols = [
888fed17f3SAndreas Gohr            [
898fed17f3SAndreas Gohr                'sid' => "1",
908fed17f3SAndreas Gohr                'colref' => "1",
918fed17f3SAndreas Gohr                'enabled' => "1",
928fed17f3SAndreas Gohr                'tid' => "1",
938fed17f3SAndreas Gohr                'sort' => "70"
948fed17f3SAndreas Gohr            ],
958fed17f3SAndreas Gohr            [
968fed17f3SAndreas Gohr                'sid' => "1",
978fed17f3SAndreas Gohr                'colref' => "2",
988fed17f3SAndreas Gohr                'enabled' => "1",
998fed17f3SAndreas Gohr                'tid' => "2",
1008fed17f3SAndreas Gohr                'sort' => "40"
1018fed17f3SAndreas Gohr            ]
1028fed17f3SAndreas Gohr        ];
1038fed17f3SAndreas Gohr
104*438a804cSAnna Dabrowska        $actual_schema = $this->sqlite->queryRecord("SELECT * FROM schemas");
1058fed17f3SAndreas Gohr
1068fed17f3SAndreas Gohr        $this->assertSame(1, $result);
1078fed17f3SAndreas Gohr        $this->assertEquals($expected_tableSQL, $tableSQL);
1088fed17f3SAndreas Gohr        $this->assertEquals($expected_types, $actual_types);
1098fed17f3SAndreas Gohr        $this->assertEquals($expected_cols, $actual_cols);
1108fed17f3SAndreas Gohr        $this->assertEquals(1, $actual_schema['id']);
1118fed17f3SAndreas Gohr        $this->assertEquals($testname, $actual_schema['tbl']);
1128fed17f3SAndreas Gohr        $this->assertTrue((int)$actual_schema['ts'] > 0, 'timestamp should be larger than 0');
1138fed17f3SAndreas Gohr    }
1148fed17f3SAndreas Gohr
1158fed17f3SAndreas Gohr    /**
1168fed17f3SAndreas Gohr     * @noinspection SqlDialectInspection
1178fed17f3SAndreas Gohr     * @noinspection SqlNoDataSourceInspection
1188fed17f3SAndreas Gohr     */
1198fed17f3SAndreas Gohr    public function test_build_update()
1208fed17f3SAndreas Gohr    {
1218fed17f3SAndreas Gohr
1228fed17f3SAndreas Gohr        // arrange
1238fed17f3SAndreas Gohr        $initialdata = [];
1248fed17f3SAndreas Gohr        $initialdata['new']['new1']['sort'] = 70;
1258fed17f3SAndreas Gohr        $initialdata['new']['new1']['label'] = 'testcolumn';
1268fed17f3SAndreas Gohr        $initialdata['new']['new1']['ismulti'] = 0;
1278fed17f3SAndreas Gohr        $initialdata['new']['new1']['config'] = '{"prefix": "", "postfix": ""}';
1288fed17f3SAndreas Gohr        $initialdata['new']['new1']['class'] = 'Text';
1298fed17f3SAndreas Gohr        $initialdata['new']['new1']['isenabled'] = '1';
1308fed17f3SAndreas Gohr
1318fed17f3SAndreas Gohr        $testname = 'testTable';
1328fed17f3SAndreas Gohr        $testname = Schema::cleanTableName($testname);
1338fed17f3SAndreas Gohr
1348fed17f3SAndreas Gohr        $builder = new SchemaBuilder($testname, $initialdata);
1358fed17f3SAndreas Gohr        $result = $builder->build();
1368fed17f3SAndreas Gohr        $this->assertSame(1, $result, 'Prerequiste setup  in order to have basis which to change during act');
1378fed17f3SAndreas Gohr
1388fed17f3SAndreas Gohr        $updatedata = [];
1398fed17f3SAndreas Gohr        $updatedata['id'] = "1";
1408fed17f3SAndreas Gohr        $updatedata['cols']['1']['sort'] = 65;
1418fed17f3SAndreas Gohr        $updatedata['cols']['1']['label'] = 'testColumn';
1428fed17f3SAndreas Gohr        $updatedata['cols']['1']['ismulti'] = 1;
1438fed17f3SAndreas Gohr        $updatedata['cols']['1']['config'] = '{"prefix": "pre", "postfix": "fix"}';
1448fed17f3SAndreas Gohr        $updatedata['cols']['1']['class'] = 'Text';
1458fed17f3SAndreas Gohr        $updatedata['cols']['1']['isenabled'] = '1';
1468fed17f3SAndreas Gohr
1478fed17f3SAndreas Gohr        // act
1488fed17f3SAndreas Gohr        $builder = new SchemaBuilder($testname, $updatedata);
1498fed17f3SAndreas Gohr        $result = $builder->build();
1508fed17f3SAndreas Gohr
15179b29326SAnna Dabrowska        $actual_types = $this->sqlite->queryAll("SELECT * FROM types");
1528fed17f3SAndreas Gohr        $expected_types = [
1538fed17f3SAndreas Gohr            [
1548fed17f3SAndreas Gohr                'id' => "1",
1558fed17f3SAndreas Gohr                'class' => 'Text',
1568fed17f3SAndreas Gohr                'ismulti' => "0",
1578fed17f3SAndreas Gohr                'label' => "testcolumn",
1588fed17f3SAndreas Gohr                'config' => '{"prefix": "", "postfix": ""}'
1598fed17f3SAndreas Gohr            ],
1608fed17f3SAndreas Gohr            [
1618fed17f3SAndreas Gohr                'id' => "2",
1628fed17f3SAndreas Gohr                'class' => 'Text',
1638fed17f3SAndreas Gohr                'ismulti' => "1",
1648fed17f3SAndreas Gohr                'label' => "testColumn",
1658fed17f3SAndreas Gohr                'config' => '{"prefix": "pre", "postfix": "fix"}'
1668fed17f3SAndreas Gohr            ]
1678fed17f3SAndreas Gohr        ];
1688fed17f3SAndreas Gohr
1698fed17f3SAndreas Gohr        // assert
1708fed17f3SAndreas Gohr        $this->assertSame(2, $result);
1718fed17f3SAndreas Gohr        $this->assertEquals($expected_types, $actual_types);
1728fed17f3SAndreas Gohr    }
1738fed17f3SAndreas Gohr}
174