]> git.mxchange.org Git - friendica.git/blob - tests/template_test.php
Merge pull request #3112 from Quix0r/rewrites/coding-convention
[friendica.git] / tests / template_test.php
1 <?php
2 /**
3  * this file contains tests for the template engine
4  *
5  * @package test.util
6  */
7
8 /** required, it is the file under test */
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() {
18         return 'clean';
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 /**
35  * TestCase for the template engine
36  *
37  * @author Alexander Kampmann
38  * @package test.util
39  */
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() {
62                 $tpl='There are $num new messages!';
63
64                 $text=replace_macros($tpl, array('$num'=>172));
65
66                 $this->assertEquals('There are 172 new messages!', $text);
67         }
68
69         public function testConditionalElse() {
70                 $tpl='There{{ if $num!=1 }} are $num new messages{{ else }} is 1 new message{{ endif }}!';
71
72                 $text1=replace_macros($tpl, array('$num'=>1));
73                 $text22=replace_macros($tpl, array('$num'=>22));
74
75                 $this->assertEquals('There is 1 new message!', $text1);
76                 $this->assertEquals('There are 22 new messages!', $text22);
77         }
78
79         public function testConditionalNoElse() {
80                 $tpl='{{ if $num!=0 }}There are $num new messages!{{ endif }}';
81
82                 $text0=replace_macros($tpl, array('$num'=>0));
83                 $text22=replace_macros($tpl, array('$num'=>22));
84
85                 $this->assertEquals('', $text0);
86                 $this->assertEquals('There are 22 new messages!', $text22);
87         }
88
89         public function testConditionalFail() {
90                 $tpl='There {{ if $num!=1 }} are $num new messages{{ else }} is 1 new message{{ endif }}!';
91
92                 $text1=replace_macros($tpl, array());
93
94                 //$this->assertEquals('There is 1 new message!', $text1);
95         }
96
97         public function testSimpleFor() {
98                 $tpl='{{ for $messages as $message }} $message {{ endfor }}';
99
100                 $text=replace_macros($tpl, array('$messages'=>array('message 1', 'message 2')));
101
102                 $this->assertEquals(' message 1  message 2 ', $text);
103         }
104
105         public function testFor() {
106                 $tpl='{{ for $messages as $message }} from: $message.from to $message.to {{ endfor }}';
107
108                 $text=replace_macros($tpl, array('$messages'=>array(array('from'=>'Mike', 'to'=>'Alex'), array('from'=>'Alex', 'to'=>'Mike'))));
109
110                 $this->assertEquals(' from: Mike to Alex  from: Alex to Mike ', $text);
111         }
112         
113         public function testKeyedFor() {
114                 $tpl='{{ for $messages as $from=>$to }} from: $from to $to {{ endfor }}';
115         
116                 $text=replace_macros($tpl, array('$messages'=>array('Mike'=>'Alex', 'Sven'=>'Mike')));
117         
118                 $this->assertEquals(' from: Mike to Alex  from: Sven to Mike ', $text);
119         }
120
121         public function testForEmpty() {
122                 $tpl='messages: {{for $messages as $message}} from: $message.from to $message.to  {{ endfor }}';
123
124                 $text=replace_macros($tpl, array('$messages'=>array()));
125
126                 $this->assertEquals('messages: ', $text);
127         }
128
129         public function testForWrongType() {
130                 $tpl='messages: {{for $messages as $message}} from: $message.from to $message.to  {{ endfor }}';
131
132                 $text=replace_macros($tpl, array('$messages'=>11));
133
134                 $this->assertEquals('messages: ', $text);
135         }
136
137         public function testForConditional() {
138                 $tpl='new messages: {{for $messages as $message}}{{ if $message.new }} $message.text{{endif}}{{ endfor }}';
139
140                 $text=replace_macros($tpl, array('$messages'=>array(
141                                 array('new'=>true, 'text'=>'new message'),
142                                 array('new'=>false, 'text'=>'old message'))));
143
144                 $this->assertEquals('new messages:  new message', $text);
145         }
146         
147         public function testConditionalFor() {
148                 $tpl='{{ if $enabled }}new messages:{{for $messages as $message}} $message.text{{ endfor }}{{endif}}';
149         
150                 $text=replace_macros($tpl, array('$enabled'=>true, 
151                                 '$messages'=>array(
152                                 array('new'=>true, 'text'=>'new message'),
153                                 array('new'=>false, 'text'=>'old message'))));
154         
155                 $this->assertEquals('new messages: new message old message', $text);
156         }
157
158         public function testFantasy() {
159                 $tpl='Fantasy: {{fantasy $messages}}';
160
161                 $text=replace_macros($tpl, array('$messages'=>'no no'));
162
163                 $this->assertEquals('Fantasy: {{fantasy no no}}', $text);
164         }
165
166         public function testInc() {
167                 $tpl='{{inc field_input.tpl with $field=$myvar}}{{ endinc }}';
168
169                 $text=replace_macros($tpl, array('$myvar'=>array('myfield', 'label', 'value', 'help')));
170
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);
177         }
178
179         public function testIncNoVar() {
180                 $tpl='{{inc field_input.tpl }}{{ endinc }}';
181
182                 $text=replace_macros($tpl, array('$field'=>array('myfield', 'label', 'value', 'help')));
183
184                 $this->assertEquals("   \n      <div class='field input'>\n             <label for='id_myfield'>label</label>\n"
185                                 ."              <input name='myfield' id='id_myfield' value=\"value\">\n"
186                                 ."              <span class='field_help'>help</span>\n"
187                                 ."      </div>\n", $text);
188         }
189         
190         public function testDoubleUse() {
191                 $tpl='Hello $name! {{ if $enabled }} I love you! {{ endif }}';
192         
193                 $text=replace_macros($tpl, array('$name'=>'Anna', '$enabled'=>false));
194         
195                 $this->assertEquals('Hello Anna! ', $text);
196                 
197                 $tpl='Hey $name! {{ if $enabled }} I hate you! {{ endif }}';
198                 
199                 $text=replace_macros($tpl, array('$name'=>'Max', '$enabled'=>true));
200                 
201                 $this->assertEquals('Hey Max!  I hate you! ', $text);
202         }
203         
204         public function testIncDouble() {
205                 $tpl='{{inc field_input.tpl with $field=$var1}}{{ endinc }}'
206                 .'{{inc field_input.tpl with $field=$var2}}{{ endinc }}';
207         
208                 $text=replace_macros($tpl, array('$var1'=>array('myfield', 'label', 'value', 'help'), 
209                                 '$var2'=>array('myfield2', 'label2', 'value2', 'help2')));
210                 
211                 $this->assertEquals("   \n"
212                                 ."      <div class='field input'>\n"
213                                 ."              <label for='id_myfield'>label</label>\n"
214                                 ."              <input name='myfield' id='id_myfield' value=\"value\">\n"
215                                 ."              <span class='field_help'>help</span>\n"
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);
223         }
224 }