]> git.mxchange.org Git - friendica.git/blob - tests/template_test.php
Merge pull request #2190 from annando/1512-getload
[friendica.git] / tests / template_test.php
1 <?php
2 /**\r
3  * this file contains tests for the template engine\r
4  *\r
5  * @package test.util\r
6  */\r
7 \r
8 /** required, it is the file under test */\r
9 require_once('include/template_processor.php');
10 require_once('include/text.php');
11
12 class TemplateMockApp {
13         public $theme_info=array();
14 }
15
16 if(!function_exists('current_theme')) {
17 function current_theme() {\r
18         return 'clean';\r
19 }
20 }
21
22 if(!function_exists('x')) {
23 function x($s,$k = NULL) {
24         return false;
25 }
26 }
27
28 if(!function_exists('get_app')) {
29 function get_app() {
30         return new TemplateMockApp();
31 }
32 }
33
34 /**\r
35  * TestCase for the template engine\r
36  *\r
37  * @author Alexander Kampmann\r
38  * @package test.util\r
39  */\r
40 class TemplateTest extends PHPUnit_Framework_TestCase {
41
42         public function setUp() {
43                 global $t;
44                 $t=new Template;
45         }
46
47         public function testListToShort() {
48                 @list($first, $second)=array('first');
49
50                 $this->assertTrue(is_null($second));
51         }
52
53         public function testSimpleVariableString() {
54                 $tpl='Hello $name!';
55
56                 $text=replace_macros($tpl, array('$name'=>'Anna'));
57
58                 $this->assertEquals('Hello Anna!', $text);
59         }
60
61         public function testSimpleVariableInt() {\r
62                 $tpl='There are $num new messages!';\r
63 \r
64                 $text=replace_macros($tpl, array('$num'=>172));\r
65 \r
66                 $this->assertEquals('There are 172 new messages!', $text);\r
67         }
68
69         public function testConditionalElse() {\r
70                 $tpl='There{{ if $num!=1 }} are $num new messages{{ else }} is 1 new message{{ endif }}!';\r
71 \r
72                 $text1=replace_macros($tpl, array('$num'=>1));
73                 $text22=replace_macros($tpl, array('$num'=>22));\r
74 \r
75                 $this->assertEquals('There is 1 new message!', $text1);
76                 $this->assertEquals('There are 22 new messages!', $text22);\r
77         }
78
79         public function testConditionalNoElse() {\r
80                 $tpl='{{ if $num!=0 }}There are $num new messages!{{ endif }}';\r
81 \r
82                 $text0=replace_macros($tpl, array('$num'=>0));\r
83                 $text22=replace_macros($tpl, array('$num'=>22));\r
84 \r
85                 $this->assertEquals('', $text0);\r
86                 $this->assertEquals('There are 22 new messages!', $text22);\r
87         }
88
89         public function testConditionalFail() {\r
90                 $tpl='There {{ if $num!=1 }} are $num new messages{{ else }} is 1 new message{{ endif }}!';\r
91 \r
92                 $text1=replace_macros($tpl, array());\r
93 \r
94                 //$this->assertEquals('There is 1 new message!', $text1);\r
95         }
96
97         public function testSimpleFor() {\r
98                 $tpl='{{ for $messages as $message }} $message {{ endfor }}';\r
99 \r
100                 $text=replace_macros($tpl, array('$messages'=>array('message 1', 'message 2')));\r
101 \r
102                 $this->assertEquals(' message 1  message 2 ', $text);\r
103         }
104
105         public function testFor() {\r
106                 $tpl='{{ for $messages as $message }} from: $message.from to $message.to {{ endfor }}';\r
107 \r
108                 $text=replace_macros($tpl, array('$messages'=>array(array('from'=>'Mike', 'to'=>'Alex'), array('from'=>'Alex', 'to'=>'Mike'))));\r
109 \r
110                 $this->assertEquals(' from: Mike to Alex  from: Alex to Mike ', $text);\r
111         }
112         
113         public function testKeyedFor() {\r
114                 $tpl='{{ for $messages as $from=>$to }} from: $from to $to {{ endfor }}';\r
115         \r
116                 $text=replace_macros($tpl, array('$messages'=>array('Mike'=>'Alex', 'Sven'=>'Mike')));\r
117         \r
118                 $this->assertEquals(' from: Mike to Alex  from: Sven to Mike ', $text);\r
119         }
120
121         public function testForEmpty() {\r
122                 $tpl='messages: {{for $messages as $message}} from: $message.from to $message.to  {{ endfor }}';\r
123 \r
124                 $text=replace_macros($tpl, array('$messages'=>array()));\r
125 \r
126                 $this->assertEquals('messages: ', $text);\r
127         }
128
129         public function testForWrongType() {\r
130                 $tpl='messages: {{for $messages as $message}} from: $message.from to $message.to  {{ endfor }}';\r
131 \r
132                 $text=replace_macros($tpl, array('$messages'=>11));\r
133 \r
134                 $this->assertEquals('messages: ', $text);\r
135         }
136
137         public function testForConditional() {\r
138                 $tpl='new messages: {{for $messages as $message}}{{ if $message.new }} $message.text{{endif}}{{ endfor }}';\r
139 \r
140                 $text=replace_macros($tpl, array('$messages'=>array(
141                                 array('new'=>true, 'text'=>'new message'),
142                                 array('new'=>false, 'text'=>'old message'))));\r
143 \r
144                 $this->assertEquals('new messages:  new message', $text);\r
145         }
146         
147         public function testConditionalFor() {\r
148                 $tpl='{{ if $enabled }}new messages:{{for $messages as $message}} $message.text{{ endfor }}{{endif}}';\r
149         \r
150                 $text=replace_macros($tpl, array('$enabled'=>true, 
151                                 '$messages'=>array(\r
152                                 array('new'=>true, 'text'=>'new message'),\r
153                                 array('new'=>false, 'text'=>'old message'))));\r
154         \r
155                 $this->assertEquals('new messages: new message old message', $text);\r
156         }
157
158         public function testFantasy() {\r
159                 $tpl='Fantasy: {{fantasy $messages}}';\r
160 \r
161                 $text=replace_macros($tpl, array('$messages'=>'no no'));\r
162 \r
163                 $this->assertEquals('Fantasy: {{fantasy no no}}', $text);\r
164         }
165
166         public function testInc() {\r
167                 $tpl='{{inc field_input.tpl with $field=$myvar}}{{ endinc }}';\r
168 \r
169                 $text=replace_macros($tpl, array('$myvar'=>array('myfield', 'label', 'value', 'help')));\r
170 \r
171                 $this->assertEquals("   \n"
172                                 ."      <div class='field input'>\n"
173                                 ."              <label for='id_myfield'>label</label>\n"
174                                 ."              <input name='myfield' id='id_myfield' value=\"value\">\n"
175                                 ."              <span class='field_help'>help</span>\n"
176                                 ."      </div>\n", $text);\r
177         }
178
179         public function testIncNoVar() {\r
180                 $tpl='{{inc field_input.tpl }}{{ endinc }}';\r
181 \r
182                 $text=replace_macros($tpl, array('$field'=>array('myfield', 'label', 'value', 'help')));\r
183 \r
184                 $this->assertEquals("   \n      <div class='field input'>\n             <label for='id_myfield'>label</label>\n"\r
185                                 ."              <input name='myfield' id='id_myfield' value=\"value\">\n"\r
186                                 ."              <span class='field_help'>help</span>\n"\r
187                                 ."      </div>\n", $text);\r
188         }
189         
190         public function testDoubleUse() {\r
191                 $tpl='Hello $name! {{ if $enabled }} I love you! {{ endif }}';\r
192         \r
193                 $text=replace_macros($tpl, array('$name'=>'Anna', '$enabled'=>false));\r
194         \r
195                 $this->assertEquals('Hello Anna! ', $text);
196                 
197                 $tpl='Hey $name! {{ if $enabled }} I hate you! {{ endif }}';\r
198                 \r
199                 $text=replace_macros($tpl, array('$name'=>'Max', '$enabled'=>true));\r
200                 \r
201                 $this->assertEquals('Hey Max!  I hate you! ', $text);\r
202         }
203         
204         public function testIncDouble() {\r
205                 $tpl='{{inc field_input.tpl with $field=$var1}}{{ endinc }}'
206                 .'{{inc field_input.tpl with $field=$var2}}{{ endinc }}';\r
207         \r
208                 $text=replace_macros($tpl, array('$var1'=>array('myfield', 'label', 'value', 'help'), 
209                                 '$var2'=>array('myfield2', 'label2', 'value2', 'help2')));\r
210                 \r
211                 $this->assertEquals("   \n"\r
212                                 ."      <div class='field input'>\n"\r
213                                 ."              <label for='id_myfield'>label</label>\n"\r
214                                 ."              <input name='myfield' id='id_myfield' value=\"value\">\n"\r
215                                 ."              <span class='field_help'>help</span>\n"\r
216                                 ."      </div>\n"
217                                 ."      \n"
218                                 ."      <div class='field input'>\n"
219                                 ."              <label for='id_myfield2'>label2</label>\n"
220                                 ."              <input name='myfield2' id='id_myfield2' value=\"value2\">\n"
221                                 ."              <span class='field_help'>help2</span>\n"
222                                 ."      </div>\n", $text);\r
223         }
224 }