summaryrefslogtreecommitdiff
path: root/tests/UnitTests/ConfigFileTests/file/ConfigVarTest.php
blob: 3b3662b0a5cf0d9dc92319e97fa476b436e7ec45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
 * Smarty PHPunit tests of config  variables
 *

 * @author  Uwe Tews
 */

/**
 * class for config variable tests
 *
 * 
 * 
 *
 */
class ConfigVarTest extends PHPUnit_Smarty
{
    /**
     * Sets up the fixture
     * This method is called before a test is executed.
     *
     */
    public function setUp(): void
    {
        $this->setUpSmarty(__DIR__);
    }

    /**
     * empty templat_c and cache folders
     */

    /**
     * test number config variable
     */
    public function testConfigNumber()
    {
        $this->smarty->configLoad('test.conf');
        $this->assertEquals("123.4", $this->smarty->fetch('number.tpl'));
    }

    /**
     * test string config variable
     */
    public function testConfigText()
    {
        $this->smarty->configLoad('test.conf');
        $this->assertEquals("123bvc", $this->smarty->fetch('text.tpl'));
    }

    /**
     * test line string config variable
     */
    public function testConfigLine()
    {
        $this->smarty->configLoad('test.conf');
        $this->assertEquals("123 This is a line", $this->smarty->fetch('eval:{#line#}'));
    }

    /**
     * test config variables in global sections
     */
    public function testConfigVariableGlobalSections()
    {
        $this->smarty->configLoad('test.conf');
        $this->assertEquals("Welcome to Smarty! Global Section1 Global Section2", $this->smarty->fetch('sec1sec2.tpl'));
    }

    /**
     * test config variables loading section2
     */
    public function testConfigVariableSection2()
    {
        $this->smarty->configLoad('test.conf', 'section2');
        $this->assertEquals("Welcome to Smarty! Global Section1 Hello Section2", $this->smarty->fetch('sec1sec2.tpl'));
    }

    /**
     * test config variables loading section special char
     */
    public function testConfigVariableSectionSpecialChar()
    {
        $this->smarty->configLoad('test.conf', '/');
        $this->assertEquals("Welcome to Smarty! special char", $this->smarty->fetch('sec.tpl'));
    }

    /**
     * test config variables loading section foo/bar
     */
    public function testConfigVariableSectionFooBar()
    {
        $this->smarty->configLoad('test.conf', 'foo/bar');
        $this->assertEquals("Welcome to Smarty! section foo/bar", $this->smarty->fetch('sec.tpl'));
    }

    /**
     * test config variables loaded in different scopes from different sections (Smarty and template)
     */
    public function testConfigDifferentScope()
    {
        $this->smarty->configLoad('test.conf', 'section2');
        $tpl = $this->smarty->createTemplate('sec1sec2.tpl');
        $tpl->configLoad('test.conf', 'section1');
        $this->assertEquals("Welcome to Smarty! Global Section1 Hello Section2", $this->smarty->fetch('sec1sec2.tpl'));
        $this->assertEquals("Welcome to Smarty! Hello Section1 Global Section2", $this->smarty->fetch($tpl));
    }

    /**
     * test config variables of hidden sections
     * shall display variables from hidden section
     */
    public function testConfigVariableHidden()
    {
        $this->smarty->config_read_hidden = true;
        $this->smarty->configLoad('test.conf', 'hidden');
        $this->assertEquals("Welcome to Smarty!Hidden Section", $this->smarty->fetch('hidden.tpl'));
    }

    /**
     * test config variables of disabled hidden sections
     * shall display not variables from hidden section
     */
    public function testConfigVariableHiddenDisable()
    {
        $this->smarty->setErrorReporting(error_reporting() & ~(E_NOTICE | E_USER_NOTICE));
        $this->smarty->config_read_hidden = false;
        $this->smarty->configLoad('test.conf', 'hidden');
        $this->assertEquals("Welcome to Smarty!", $this->smarty->fetch('hidden.tpl'));
    }

    /**
     * test getConfigVars
     */
    public function testConfigGetSingleConfigVar()
    {
        $this->smarty->configLoad('test.conf');
        $this->assertEquals("Welcome to Smarty!", $this->smarty->getConfigVars('title'));
    }

    /**
     * test getConfigVars return all variables
     */
    public function testConfigGetAllConfigVars()
    {
        $this->smarty->configLoad('test.conf');
        $vars = $this->smarty->getConfigVars();
        $this->assertTrue(is_array($vars));
        $this->assertEquals("Welcome to Smarty!", $vars['title']);
        $this->assertEquals("Global Section1", $vars['sec1']);
    }

    /**
     * test clearConfig for single variable
     */
    public function testConfigClearSingleConfigVar()
    {
        $this->smarty->configLoad('test.conf');
        $this->smarty->clearConfig('title');
        $this->assertEquals("", $this->smarty->getConfigVars('title'));
    }

    /**
     * test clearConfig for all variables
     */
    public function testConfigClearConfigAll()
    {
        $this->smarty->configLoad('test.conf');
        $this->smarty->clearConfig();
        $vars = $this->smarty->getConfigVars();
        $this->assertTrue(is_array($vars));
        $this->assertTrue(empty($vars));
    }

    /**
     * test config vars on data object
     */
    public function testConfigTextData()
    {
        $data = $this->smarty->createData();
        $data->configLoad('test.conf');
        $tpl = $this->smarty->createTemplate('text.tpl', $data);
        $this->assertEquals("123bvc", $this->smarty->fetch($tpl));
    }

    /**
     * test getConfigVars on data object
     */
    public function testConfigGetSingleConfigVarData()
    {
        $data = $this->smarty->createData();
        $data->configLoad('test.conf');
        $this->assertEquals("Welcome to Smarty!", $data->getConfigVars('title'));
    }

    /**
     * test getConfigVars return all variables on data object
     */
    public function testConfigGetAllConfigVarsData()
    {
        $data = $this->smarty->createData();
        $data->configLoad('test.conf');
        $vars = $data->getConfigVars();
        $this->assertTrue(is_array($vars));
        $this->assertEquals("Welcome to Smarty!", $vars['title']);
        $this->assertEquals("Global Section1", $vars['sec1']);
    }

    /**
     * test clearConfig for single variable on data object
     */
    public function testConfigClearSingleConfigVarData()
    {
        $data = $this->smarty->createData();
        $data->configLoad('test.conf');
        $data->clearConfig('title');
        $this->assertEquals("", $data->getConfigVars('title'));
        $this->assertEquals("Global Section1", $data->getConfigVars('sec1'));
    }

    /**
     * test clearConfig for all variables on data object
     */
    public function testConfigClearConfigAllData()
    {
        $data = $this->smarty->createData();
        $data->configLoad('test.conf');
        $data->clearConfig();
        $vars = $data->getConfigVars();
        $this->assertTrue(is_array($vars));
        $this->assertTrue(empty($vars));
    }

    /**
     * test config vars on template object
     */
    public function testConfigTextTemplate()
    {
        $tpl = $this->smarty->createTemplate('text.tpl');
        $tpl->configLoad('test.conf');
        $this->assertEquals("123bvc", $this->smarty->fetch($tpl));
    }

    /**
     * test getConfigVars on template object
     */
    public function testConfigGetSingleConfigVarTemplate()
    {
        $tpl = $this->smarty->createTemplate('text.tpl');
        $tpl->configLoad('test.conf');
        $this->assertEquals("Welcome to Smarty!", $tpl->getConfigVars('title'));
    }

    /**
     * test getConfigVariable on template object
     */
    public function testConfigGetSingleConfigVarTemplate2()
    {
        $tpl = $this->smarty->createTemplate('text.tpl');
        $tpl->configLoad('test.conf');
        $this->assertEquals("Welcome to Smarty!", $tpl->getConfigVariable('title'));
    }

    /**
     * test getConfigVars return all variables on template object
     */
    public function testConfigGetAllConfigVarsTemplate()
    {
        $tpl = $this->smarty->createTemplate('text.tpl');
        $tpl->configLoad('test.conf');
        $vars = $tpl->getConfigVars();
        $this->assertTrue(is_array($vars));
        $this->assertEquals("Welcome to Smarty!", $vars['title']);
        $this->assertEquals("Global Section1", $vars['sec1']);
    }

    /**
     * test clearConfig for single variable on template object
     */
    public function testConfigClearSingleConfigVarTemplate()
    {
        $tpl = $this->smarty->createTemplate('text.tpl');
        $tpl->configLoad('test.conf');
        $tpl->clearConfig('title');
        $this->assertEquals("", $tpl->getConfigVars('title'));
        $this->assertEquals("Global Section1", $tpl->getConfigVars('sec1'));
    }

    /**
     * test clearConfig for all variables on template object
     */
    public function testConfigClearConfigAllTemplate()
    {
        $tpl = $this->smarty->createTemplate('text.tpl');
        $tpl->configLoad('test.conf');
        $tpl->clearConfig();
        $vars = $tpl->getConfigVars();
        $this->assertTrue(is_array($vars));
        $this->assertTrue(empty($vars));
    }

    /**
     * test config variables loading from absolute file path
     */
    public function testConfigAbsolutePath()
    {
        $file = realpath($this->smarty->getConfigDir(0) . 'test.conf');
        $this->smarty->configLoad($file);
        $this->assertEquals("123.4", $this->smarty->fetch('number.tpl'));
    }

    public function testConfigResourceDb4()
    {
        $this->smarty->addPluginsDir(__DIR__ . "/../../ResourceTests/ResourcePlugins/PHPunitplugins/");
        $this->smarty->configLoad('db4:foo.conf');
        $this->assertEquals("bar", $this->smarty->fetch('foo.tpl'));
    }
    public function testConfigUndefinedSilent()
    {
        $this->assertEquals("", $this->smarty->fetch('foo.tpl'));
    }

    public function testConfigUndefinedNotice()
    {
        $this->smarty->error_unassigned = true;
        try {
            $this->assertEquals("", $this->smarty->fetch('foo.tpl'));
        }
        catch (Exception $e) {
            $this->assertStringStartsWith('Undefined variable', $e->getMessage());
        }
    }
}