summaryrefslogtreecommitdiff
path: root/tests/UnitTests/SmartyMethodsTests/RegisterFunction/RegisterFunctionTest.php
blob: 7bfe1a599d868aebab464c007a7e9e78119cf66a (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
<?php
/**
 * Smarty PHPunit tests register/unregister function plugins
 *

 * @author  Uwe Tews
 */

use Smarty\Smarty;

/**
 * class for register/unregister function plugins methods tests
 *
 * 
 * @preserveGlobalState    disabled
 * 
 *
 */
class RegisterFunctionTest extends PHPUnit_Smarty
{
    public function setUp(): void
    {
        $this->setUpSmarty(__DIR__);
    }

    public function testInit()
    {
        $this->cleanDirs();
    }

    /**
     * test registerPlugin method for function
     */
    public function testRegisterFunction()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction');
        $this->assertEquals('myfunction',
                            $this->smarty->getRegisteredPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction')[0]);
        $this->assertEquals('hello world 1', $this->smarty->fetch('eval:{testfunction value=1}'));
    }

    /**
     * test registerPlugin method for function case-sensitive
     */
    public function testRegisterFunctionCaseInsensitive()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testFunction', 'myfunction');
        $this->assertEquals('myfunction',
            $this->smarty->getRegisteredPlugin(Smarty::PLUGIN_FUNCTION, 'testFunction')[0]);
        $this->assertEquals('hello world 1', $this->smarty->fetch('eval:{testFunction value=1}'));
    }

    /**
     * test registerPlugin method for function  class
     */
    public function testRegisterFunctionClass()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', array('myfunctionclass', 'execute'));
        $this->assertEquals('hello world 2', $this->smarty->fetch('eval:{testfunction value=2}'));
    }

    /**
     * test registerPlugin method for function object
     */
    public function testRegisterFunctionObject()
    {
        $myfunction_object = new myfunctionclass;
        $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', array($myfunction_object, 'execute'));
        $this->assertEquals('hello world 3', $this->smarty->fetch('eval:{testfunction value=3}'));
    }

    /**
     * test registerPlugin method for function cached
     *
     * 
     * 
     */
    public function testRegisterFunctionCaching1()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
        $this->smarty->setForceCompile(true);
        $this->smarty->assign('x', 0);
        $this->smarty->assign('y', 10);
        $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction');
        $this->assertEquals('hello world 0 10', $this->smarty->fetch('test_register_function.tpl'));
    }

    /**
     * test registerPlugin method for function cached
     *
     * 
     * 
     *
     */
    public function testRegisterFunctionCaching2()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
        $this->smarty->assign('x', 1);
        $this->smarty->assign('y', 20);
        $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction');
        $this->assertEquals('hello world 0 10', $this->smarty->fetch('test_register_function.tpl'));
    }

    /**
     * test registerPlugin method for function not cached
     *
     * 
     * 
     *
     */
    public function testRegisterFunctionCaching3()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
        $this->smarty->setForceCompile(true);
        $this->smarty->assign('x', 2);
        $this->smarty->assign('y', 30);
        $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction', false);
        $this->assertEquals('hello world 2 30', $this->smarty->fetch('test_register_function.tpl'));
    }

    /**
     * test registerPlugin method for function not cached
     *
     * 
     * 
     *
     */
    public function testRegisterFunctionCaching4()
    {
        $this->smarty->caching = 1;
        $this->smarty->cache_lifetime = 1000;
        $this->smarty->assign('x', 3);
        $this->smarty->assign('y', 40);
        $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction', false);
        $this->assertEquals('hello world 3 30', $this->smarty->fetch('test_register_function.tpl'));
    }

    /**
     * test unregisterPlugin method for function
     */
    public function testUnregisterFunction()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction', 'myfunction');
        $this->smarty->unregisterPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction');
        $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction'));
    }

    /**
     * test unregisterPlugin method for function not registered
     */
    public function testUnregisterFunctionNotRegistered()
    {
        $this->smarty->unregisterPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction');
        $this->assertNull($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction'));
    }

    /**
     * test unregisterPlugin method for function other registered
     */
    public function testUnregisterFunctionOtherRegistered()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_BLOCK, 'testfunction', 'myfunction');
        $this->smarty->unregisterPlugin(Smarty::PLUGIN_FUNCTION, 'testfunction');
        $this->assertIsArray($this->smarty->getRegisteredPlugin(Smarty::PLUGIN_BLOCK, 'testfunction'));
    }


    /**
     * Test case (in)sensitivy of plugin functions
     * @param $registerName
     * @param $templateString
     * @return void
     * @throws \Smarty\Exception
     * @group issue907
     * @dataProvider dataProviderForCaseSensitivity
     */
    public function testCaseSensitivity($registerName, $templateString) {
        $this->smarty->registerPlugin(
            Smarty::PLUGIN_FUNCTION,
            $registerName,
            function($params, $smarty) { return 'function-output'; });
        $this->assertEquals('function-output', $this->smarty->fetch('string:' . $templateString));
    }

    public function dataProviderForCaseSensitivity() {
        return [
            ['customTag', '{customTag}'],
            ['customtag', '{customtag}'],
        ];
    }

    /**
     * test registerPlugin for function name ending in 'close' #1122
     */
    public function testRegisterFunctionEndingInClose()
    {
        $this->smarty->registerPlugin(Smarty::PLUGIN_FUNCTION, 'window_close', 'myfunction');
        $this->assertEquals('hello world 1', $this->smarty->fetch('eval:{window_close value=1}'));
    }

}

function myfunction($params, $smarty)
{
    return "hello world $params[value]";
}

class myfunctionclass
{
    static function execute($params, $smarty)
    {
        return "hello world $params[value]";
    }
}