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