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