1<?php
2
3namespace dokuwiki\plugin\struct\test\types;
4
5use dokuwiki\plugin\struct\test\mock\BaseType;
6use dokuwiki\plugin\struct\test\StructTest;
7
8/**
9 * @group plugin_struct
10 * @group plugins
11 */
12class AbstractBaseTest extends StructTest
13{
14
15    protected $preset = [
16        'label' => [
17            'de' => 'german label',
18            'zh' => 'chinese label' // always stripped
19        ],
20        'hint' => [
21            'en' => 'english hint',
22            'de' => 'german hint',
23            'zh' => 'chinese hint' // always stripped
24        ]
25    ];
26
27    /**
28     * Translation Init: empty config, no translation plugin
29     */
30    public function test_trans_empty_noplugin()
31    {
32        global $conf;
33        $conf['lang'] = 'en';
34
35        $type = new BaseType(null, 'A Label');
36        $this->assertEquals(
37            array(
38                'label' => array(
39                    'en' => ''
40                ),
41                'hint' => array(
42                    'en' => ''
43                ),
44                'visibility' => array('inpage' => true, 'ineditor' => true)
45            ),
46            $type->getConfig()
47        );
48        $this->assertEquals('A Label', $type->getTranslatedLabel());
49        $this->assertEquals('', $type->getTranslatedHint());
50    }
51
52    /**
53     * Translation Init: preset config, no translation plugin
54     */
55    public function test_trans_preset_noplugin()
56    {
57        global $conf;
58        $conf['lang'] = 'en';
59
60        $type = new BaseType($this->preset, 'A Label');
61        $this->assertEquals(
62            array(
63                'label' => array(
64                    'en' => ''
65                ),
66                'hint' => array(
67                    'en' => 'english hint'
68                ),
69                'visibility' => array('inpage' => true, 'ineditor' => true)
70            ),
71            $type->getConfig()
72        );
73        $this->assertEquals('A Label', $type->getTranslatedLabel());
74        $this->assertEquals('english hint', $type->getTranslatedHint());
75    }
76
77    /**
78     * Translation Init: empty config, translation plugin
79     */
80    public function test_trans_empty_plugin()
81    {
82        global $conf;
83        $conf['lang'] = 'en';
84        $conf['plugin']['translation']['translations'] = 'fr tr it de';
85
86        $type = new BaseType(null, 'A Label');
87        $this->assertEquals(
88            [
89                'label' => [
90                    'en' => '',
91                    'fr' => '',
92                    'tr' => '',
93                    'it' => '',
94                    'de' => '',
95                ],
96                'hint' => [
97                    'en' => '',
98                    'fr' => '',
99                    'tr' => '',
100                    'it' => '',
101                    'de' => '',
102                ],
103                'visibility' => ['inpage' => true, 'ineditor' => true]
104            ],
105            $type->getConfig()
106        );
107        $this->assertEquals('A Label', $type->getTranslatedLabel());
108        $this->assertEquals('', $type->getTranslatedHint());
109        $conf['lang'] = 'de';
110        $this->assertEquals('A Label', $type->getTranslatedLabel());
111        $this->assertEquals('', $type->getTranslatedHint());
112        $conf['lang'] = 'zh';
113        $this->assertEquals('A Label', $type->getTranslatedLabel());
114        $this->assertEquals('', $type->getTranslatedHint());
115        $conf['lang'] = 'en';
116    }
117
118    /**
119     * Translation Init: preset config, translation plugin
120     */
121    public function test_trans_preset_plugin()
122    {
123        global $conf;
124        $conf['lang'] = 'en';
125        $conf['plugin']['translation']['translations'] = 'fr tr it de';
126
127        $type = new BaseType($this->preset, 'A Label');
128        $this->assertEquals(
129            [
130                'label' => [
131                    'en' => '',
132                    'fr' => '',
133                    'tr' => '',
134                    'it' => '',
135                    'de' => 'german label',
136                ],
137                'hint' => [
138                    'en' => 'english hint',
139                    'fr' => '',
140                    'tr' => '',
141                    'it' => '',
142                    'de' => 'german hint',
143                ],
144                'visibility' => ['inpage' => true, 'ineditor' => true]
145            ],
146            $type->getConfig()
147        );
148        $this->assertEquals('A Label', $type->getTranslatedLabel());
149        $this->assertEquals('english hint', $type->getTranslatedHint());
150        $conf['lang'] = 'de';
151        $this->assertEquals('german label', $type->getTranslatedLabel());
152        $this->assertEquals('german hint', $type->getTranslatedHint());
153        $conf['lang'] = 'zh';
154        $this->assertEquals('A Label', $type->getTranslatedLabel()); # falls back to column
155        $this->assertEquals('english hint', $type->getTranslatedHint());  # falls back to english
156        $conf['lang'] = 'fr';
157        $this->assertEquals('A Label', $type->getTranslatedLabel()); # falls back to column
158        $this->assertEquals('english hint', $type->getTranslatedHint());  # falls back to english
159        $conf['lang'] = 'en';
160    }
161}
162