summaryrefslogtreecommitdiff
path: root/tests/UnitTests/SmartyMethodsTests/RegisterBlock/RegisterBlockTest.php
blob: 631ae351daeb50500c4d92f88904c75b370b5b87 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
 * Smarty PHPunit tests register->block / unregister->block methods
 *

 * @author  Uwe Tews
 */

use Smarty\Compiler\Template;
use Smarty\Smarty;

/**
 * class for register->block / unregister->block methods tests
 *
 *
 * 
 *
 */
class RegisterBlockTest extends PHPUnit_Smarty
{
    public function setUp(): void
    {
        $this->setUpSmarty(__DIR__);
        $this->smarty->disableSecurity();
    }


    /**
     * test registerPlugin method for block function
     */
    public function testRegisterBlockFunction()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock');
        $this->smarty->assign('value', 1);
        $this->assertEquals('function hello world 1 1 function hello world 1 2 function hello world 1 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
    }

    public function testRegisterBlockFunctionModifier1()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock');
        $this->smarty->assign('value', 1);
        $this->assertEquals(strtoupper('function hello world 1 1 function hello world 1 2 function hello world 1 3 '), $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock|upper}'));
    }

    public function testRegisterBlockFunctionModifier2()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock');
        $this->smarty->assign('value', 1);
        $this->assertEquals(strtoupper('function hello world 1 1 function hello world 1 2 function hello world 1 3 '), $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock|default:""|upper}'));
    }

    public function testRegisterBlockFunctionWrapper()
    {
        $this->smarty->registerPlugin('block', 'testblock', 'myblock');
        $this->smarty->assign('value', 1);
        $this->assertEquals('function hello world 1 1 function hello world 1 2 function hello world 1 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
    }

    /**
     * test registerPlugin method for block class
     */
    public function testRegisterBlockClass()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', array('myblockclass', 'static_method'));
        $this->smarty->assign('value', 2);
        $this->assertEquals('static hello world 2 1 static hello world 2 2 static hello world 2 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
    }

    public function testRegisterBlockClassWrapper()
    {
        $this->smarty->registerPlugin('block', 'testblock', array('myblockclass', 'static_method'));
        $this->smarty->assign('value', 2);
        $this->assertEquals('static hello world 2 1 static hello world 2 2 static hello world 2 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
    }

    /**
     * test registerPlugin method for block object
     */
    public function testRegisterBlockObject()
    {
        $myblock_object = new myblockclass;
        $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', array($myblock_object, 'object_method'));
        $this->smarty->assign('value', 3);
        $this->assertEquals('object hello world 3 1 object hello world 3 2 object hello world 3 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
    }

    public function testRegisterBlockObjectWrapper()
    {
        $myblock_object = new myblockclass;
        $this->smarty->registerPlugin('block', 'testblock', array($myblock_object, 'object_method'));
        $this->smarty->assign('value', 3);
        $this->assertEquals('object hello world 3 1 object hello world 3 2 object hello world 3 3 ', $this->smarty->fetch('eval:{testblock}hello world {$value}{/testblock}'));
    }

    /**
     * test registerPlugin method for block with caching
     */
    public function testRegisterBlockCaching1()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
        $this->smarty->setForceCompile(true);
        $this->smarty->assign('x', 1);
        $this->smarty->assign('y', 10);
        $this->smarty->assign('z', 100);
        $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache');
        $this->assertEquals('1 10 100', $this->smarty->fetch('test_register_block.tpl'));
    }

    /**
     * 
     * 
     *
     */
    public function testRegisterBlockCaching2()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
        $this->smarty->assign('x', 2);
        $this->smarty->assign('y', 20);
        $this->smarty->assign('z', 200);
        $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache');
        $this->assertEquals('1 10 100', $this->smarty->fetch('test_register_block.tpl'));
    }

    /**
     * 
     * 
     *
     */
    public function testRegisterBlockCaching3()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
        $this->smarty->setForceCompile(true);
        $this->smarty->assign('x', 3);
        $this->smarty->assign('y', 30);
        $this->smarty->assign('z', 300);
        $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache', false);
        $this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl'));
    }

    /**
     * 
     * 
     *
     */
    public function testRegisterBlockCaching4()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
	    $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblockcache', false);

	    $this->smarty->assign('x', 3);
	    $this->smarty->assign('y', 30);
	    $this->smarty->assign('z', 300);
	    $this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl'));

	    $this->smarty->assign('x', 4);
	    $this->smarty->assign('y', 40);
	    $this->smarty->assign('z', 400);
        $this->assertEquals('3 40 300', $this->smarty->fetch('test_register_block.tpl'));
    }

    /**
     * 
     * 
     *
     */
    public function testRegisterBlockCaching1Wrapper()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
        $this->smarty->setForceCompile(true);
        $this->smarty->assign('x', 1);
        $this->smarty->assign('y', 10);
        $this->smarty->assign('z', 100);
        $this->smarty->registerPlugin('block', 'testblock', 'myblockcache');
        $this->assertEquals('1 10 100', $this->smarty->fetch('test_register_block.tpl'));
    }

    /**
     * 
     * 
     *
     */
    public function testRegisterBlockCaching2Wrapper()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
        $this->smarty->assign('x', 2);
        $this->smarty->assign('y', 20);
        $this->smarty->assign('z', 200);
        $this->smarty->registerPlugin('block', 'testblock', 'myblockcache');
        $this->assertEquals('1 10 100', $this->smarty->fetch('test_register_block.tpl'));
    }

    /**
     * 
     * 
     *
     */
    public function testRegisterBlockCaching3Wrapper()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
        $this->smarty->setForceCompile(true);
        $this->smarty->assign('x', 3);
        $this->smarty->assign('y', 30);
        $this->smarty->assign('z', 300);
        $this->smarty->registerPlugin('block', 'testblock', 'myblockcache', false);
        $this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl'));
    }

    public function testRegisterBlockCaching4Wrapper()
    {
		$this->cleanDirs();

        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
	    $this->smarty->registerPlugin('block', 'testblock', 'myblockcache');

	    $this->smarty->assign('x', 3);
	    $this->smarty->assign('y', 30);
	    $this->smarty->assign('z', 300);
	    $this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl'));

        $this->smarty->assign('x', 4);
        $this->smarty->assign('y', 40);
        $this->smarty->assign('z', 400);
        $this->assertEquals('3 30 300', $this->smarty->fetch('test_register_block.tpl'));
    }

	/**
	 * test register block with handler that supports positional params
	 */
	public function testRegisterBlockWithPositionalParams()
	{
		$this->cleanDirs();
		$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testblock', blockparamsCompiler::class);
		$this->smarty->registerPlugin(Smarty::PLUGIN_COMPILER, 'testblockclose', blockparamsCompiler::class);
		$result = $this->smarty->fetch('string:{testblock "foo" "bar"} block 
		contents
		{/testblock}');
		$this->assertStringContainsString('first', $result);
		$this->assertStringContainsString('second', $result);
	}

    /**
     * test unregister->block method
     */
    public function testUnregisterBlock()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testblock', 'myblock');
        $this->smarty->unregisterPlugin(Smarty::PLUGIN_BLOCK, 'testblock');
        $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testblock'));
    }

    public function testUnregisterBlockWrapper()
    {
        $this->smarty->registerPlugin('block', 'testblock', 'myblock');
        $this->smarty->unregisterPlugin('block', 'testblock');
	    $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testblock'));
    }

    /**
     * test unregister->block method not registered
     */
    public function testUnregisterBlockNotRegistered()
    {
        $this->smarty->unregisterPlugin(Smarty::PLUGIN_BLOCK, 'testblock');
	    $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testblock'));
    }
}

function myblock($params, $content, &$smarty_tpl, &$repeat)
{
    static $loop = 0;

    if ($content == null) {
        $loop = 0;

        return;
    }
    $loop ++;
    if ($loop < 3) {
        $repeat = true;
    }

    return "function $content $loop ";
}

function myblockcache($params, $content, &$smarty_tpl, &$repeat)
{
    return $content;
}

class blockparamsCompiler extends \Smarty\Compile\Base {

	protected $shorttag_order = ["first", "second"];
	protected $optional_attributes = ["first", "second"];

	public function compile($args, Template $compiler, $parameter = [], $tag = null, $function = null): string
	{
		$_attr = $this->getAttributes($compiler, $args);

		$output = '';
		if (isset($_attr['first'])) {
			$output .= 'first';
		}

		if (isset($_attr['second'])) {
			$output .= 'second';
		}

		return $output;
	}

}

class myblockclass
{
    static function static_method($params, $content, &$smarty_tpl, &$repeat)
    {
        static $loop = 0;

        if ($content == null) {
            $loop = 0;

            return;
        }
        $loop ++;
        if ($loop < 3) {
            $repeat = true;
        }

        return "static $content $loop ";
    }

    public function object_method($params, $content, &$smarty_tpl, &$repeat)
    {
        static $loop = 0;

        if ($content == null) {
            $loop = 0;

            return;
        }
        $loop ++;
        if ($loop < 3) {
            $repeat = true;
        }

        return "object $content $loop ";
    }
}