1<?php
2
3namespace dokuwiki\plugin\struct\test;
4
5use dokuwiki\plugin\struct\meta;
6
7/**
8 * Tests for parsing the aggregation config for the struct plugin
9 *
10 * @group plugin_struct
11 * @group plugins
12 *
13 */
14class ConfigParserTest extends StructTest
15{
16
17    public function test_simple()
18    {
19        $lines = [
20            "schema    : testtable, another, foo bar",
21            "cols      : %pageid%, count",
22            "sort      : ^count",
23            "sort      : %pageid%, ^bam",
24            "align     : r,l,center,foo",
25            "class     : foo, bar",
26        ];
27
28        $configParser = new meta\ConfigParser($lines);
29        $actual_config = $configParser->getConfig();
30
31        $expected_config = [
32            'limit' => 0,
33            'dynfilters' => false,
34            'summarize' => false,
35            'rownumbers' => false,
36            'sepbyheaders' => false,
37            'headers' =>
38                [
39                    0 => NULL,
40                    1 => NULL,
41                ],
42            'widths' =>
43                [],
44            'filter' =>
45                [],
46            'schemas' =>
47                [
48                    0 =>
49                        [
50                            0 => 'testtable',
51                            1 => '',
52                        ],
53                    1 =>
54                        [
55                            0 => 'another',
56                            1 => '',
57                        ],
58                    2 =>
59                        [
60                            0 => 'foo',
61                            1 => 'bar',
62                        ],
63                ],
64            'cols' =>
65                [
66                    0 => '%pageid%',
67                    1 => 'count',
68                ],
69            'sort' =>
70                [
71                    [
72                        0 => 'count',
73                        1 => false,
74                    ],
75                    [
76                        0 => '%pageid%',
77                        1 => true,
78                    ],
79                    [
80                        0 => 'bam',
81                        1 => false,
82                    ]
83                ],
84            'csv' => true,
85            'target' => '',
86            'align' => ['right', 'left', 'center', null],
87            'nesting' => 0,
88            'index' => 0,
89            'classes' => ['struct-custom-foo', 'struct-custom-bar'],
90            'actcol' => -1,
91        ];
92
93        $this->assertEquals($expected_config, $actual_config);
94    }
95
96    public function test_width()
97    {
98        $lines = ['width: 5, 15px, 23.4em, meh, 10em'];
99
100        $configParser = new meta\ConfigParser($lines);
101
102        $config = $configParser->getConfig();
103
104        $this->assertEquals(
105            ['5px', '15px', '23.4em', '', '10em'],
106            $config['widths']
107        );
108    }
109
110    /**
111     * @see test_splitLine
112     */
113    public function provide_splitLine()
114    {
115        return [
116            ['', ['', '']],
117            ['   ', ['', '']],
118            ['foo', ['foo', '']],
119            ['foo:bar', ['foo', 'bar']],
120            ['foo: bar', ['foo', 'bar']],
121            ['fOO: bar', ['foo', 'bar']],
122            ['  fOO: bar  ', ['foo', 'bar']],
123        ];
124    }
125
126    /**
127     * @dataProvider provide_splitLine
128     */
129    public function test_splitLine($line, $expected)
130    {
131        $configParser = new meta\ConfigParser(array());
132        $actual = $this->callInaccessibleMethod($configParser, 'splitLine', [$line]);
133        $this->assertEquals($expected, $actual);
134    }
135}
136