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