1<?php 2 3namespace dokuwiki\plugin\struct\test; 4 5use dokuwiki\plugin\struct\meta; 6use dokuwiki\plugin\struct\meta\Column; 7 8/** 9 * @group plugin_struct 10 * @group plugins 11 * 12 */ 13class SchemaImporterTest extends StructTest 14{ 15 16 public function test_export() 17 { 18 $sb = new meta\SchemaBuilder( 19 'schema1', 20 [ 21 'new' => [ 22 'new1' => ['label' => 'first', 'class' => 'Text', 'sort' => 10, 'ismulti' => 0, 'isenabled' => 1], 23 'new2' => ['label' => 'second', 'class' => 'Text', 'sort' => 20, 'ismulti' => 1, 'isenabled' => 1], 24 'new3' => ['label' => 'third', 'class' => 'Text', 'sort' => 30, 'ismulti' => 0, 'isenabled' => 1], 25 'new4' => ['label' => 'fourth', 'class' => 'Text', 'sort' => 40, 'ismulti' => 0, 'isenabled' => 1], 26 ] 27 ] 28 ); 29 $sb->build(); 30 31 $schema = new meta\Schema('schema1'); 32 $expect = json_decode(file_get_contents(__DIR__ . '/json/schema1.struct.json'), true); 33 $actual = json_decode($schema->toJSON(), true); 34 // we don't expect this to match 35 unset($expect['structversion']); 36 unset($actual['structversion']); 37 $this->assertEquals($expect, $actual); 38 } 39 40 public function test_import_one() 41 { 42 $sb = new meta\SchemaImporter('tag', file_get_contents(__DIR__ . '/json/tag.struct.json')); 43 $this->assertTrue((bool)$sb->build()); 44 45 $schema = new meta\Schema('tag'); 46 $columns = $schema->getColumns(); 47 48 $this->assertCount(2, $columns); 49 $this->assertTrue(is_a($columns[0], Column::class)); 50 $this->assertTrue(is_a($columns[1], Column::class)); 51 $this->assertEquals('tag', $columns[0]->getLabel()); 52 $this->assertEquals('tags', $columns[1]->getLabel()); 53 } 54 55 public function test_import_export() 56 { 57 $sb = new meta\SchemaImporter('foobar', file_get_contents(__DIR__ . '/json/schema1.struct.json')); 58 $this->assertTrue((bool)$sb->build()); 59 60 $schema = new meta\Schema('foobar'); 61 $expect = json_decode(file_get_contents(__DIR__ . '/json/schema1.struct.json'), true); 62 $actual = json_decode($schema->toJSON(), true); 63 // we don't expect this to match 64 unset($expect['structversion']); 65 unset($actual['structversion']); 66 $expect['schema'] = 'foobar'; // we exported the new schema 67 $this->assertEquals($expect, $actual); 68 } 69 70} 71