3 * this file contains tests for the template engine
\r
8 /** required, it is the file under test */
\r
9 require_once('include/template_processor.php');
10 require_once('include/text.php');
12 class TemplateMockApp {
13 public $theme_info=array();
16 if(!function_exists('current_theme')) {
17 function current_theme() {
\r
22 if(!function_exists('x')) {
23 function x($s,$k = NULL) {
28 if(!function_exists('get_app')) {
30 return new TemplateMockApp();
35 * TestCase for the template engine
\r
37 * @author Alexander Kampmann
\r
38 * @package test.util
\r
40 class TemplateTest extends PHPUnit_Framework_TestCase {
42 public function setUp() {
47 public function testListToShort() {
48 @list($first, $second)=array('first');
50 $this->assertTrue(is_null($second));
53 public function testSimpleVariableString() {
56 $text=replace_macros($tpl, array('$name'=>'Anna'));
58 $this->assertEquals('Hello Anna!', $text);
61 public function testSimpleVariableInt() {
\r
62 $tpl='There are $num new messages!';
\r
64 $text=replace_macros($tpl, array('$num'=>172));
\r
66 $this->assertEquals('There are 172 new messages!', $text);
\r
69 public function testConditionalElse() {
\r
70 $tpl='There{{ if $num!=1 }} are $num new messages{{ else }} is 1 new message{{ endif }}!';
\r
72 $text1=replace_macros($tpl, array('$num'=>1));
73 $text22=replace_macros($tpl, array('$num'=>22));
\r
75 $this->assertEquals('There is 1 new message!', $text1);
76 $this->assertEquals('There are 22 new messages!', $text22);
\r
79 public function testConditionalNoElse() {
\r
80 $tpl='{{ if $num!=0 }}There are $num new messages!{{ endif }}';
\r
82 $text0=replace_macros($tpl, array('$num'=>0));
\r
83 $text22=replace_macros($tpl, array('$num'=>22));
\r
85 $this->assertEquals('', $text0);
\r
86 $this->assertEquals('There are 22 new messages!', $text22);
\r
89 public function testConditionalFail() {
\r
90 $tpl='There {{ if $num!=1 }} are $num new messages{{ else }} is 1 new message{{ endif }}!';
\r
92 $text1=replace_macros($tpl, array());
\r
94 //$this->assertEquals('There is 1 new message!', $text1);
\r
97 public function testSimpleFor() {
\r
98 $tpl='{{ for $messages as $message }} $message {{ endfor }}';
\r
100 $text=replace_macros($tpl, array('$messages'=>array('message 1', 'message 2')));
\r
102 $this->assertEquals(' message 1 message 2 ', $text);
\r
105 public function testFor() {
\r
106 $tpl='{{ for $messages as $message }} from: $message.from to $message.to {{ endfor }}';
\r
108 $text=replace_macros($tpl, array('$messages'=>array(array('from'=>'Mike', 'to'=>'Alex'), array('from'=>'Alex', 'to'=>'Mike'))));
\r
110 $this->assertEquals(' from: Mike to Alex from: Alex to Mike ', $text);
\r
113 public function testKeyedFor() {
\r
114 $tpl='{{ for $messages as $from=>$to }} from: $from to $to {{ endfor }}';
\r
116 $text=replace_macros($tpl, array('$messages'=>array('Mike'=>'Alex', 'Sven'=>'Mike')));
\r
118 $this->assertEquals(' from: Mike to Alex from: Sven to Mike ', $text);
\r
121 public function testForEmpty() {
\r
122 $tpl='messages: {{for $messages as $message}} from: $message.from to $message.to {{ endfor }}';
\r
124 $text=replace_macros($tpl, array('$messages'=>array()));
\r
126 $this->assertEquals('messages: ', $text);
\r
129 public function testForWrongType() {
\r
130 $tpl='messages: {{for $messages as $message}} from: $message.from to $message.to {{ endfor }}';
\r
132 $text=replace_macros($tpl, array('$messages'=>11));
\r
134 $this->assertEquals('messages: ', $text);
\r
137 public function testForConditional() {
\r
138 $tpl='new messages: {{for $messages as $message}}{{ if $message.new }} $message.text{{endif}}{{ endfor }}';
\r
140 $text=replace_macros($tpl, array('$messages'=>array(
141 array('new'=>true, 'text'=>'new message'),
142 array('new'=>false, 'text'=>'old message'))));
\r
144 $this->assertEquals('new messages: new message', $text);
\r
147 public function testConditionalFor() {
\r
148 $tpl='{{ if $enabled }}new messages:{{for $messages as $message}} $message.text{{ endfor }}{{endif}}';
\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
155 $this->assertEquals('new messages: new message old message', $text);
\r
158 public function testFantasy() {
\r
159 $tpl='Fantasy: {{fantasy $messages}}';
\r
161 $text=replace_macros($tpl, array('$messages'=>'no no'));
\r
163 $this->assertEquals('Fantasy: {{fantasy no no}}', $text);
\r
166 public function testInc() {
\r
167 $tpl='{{inc field_input.tpl with $field=$myvar}}{{ endinc }}';
\r
169 $text=replace_macros($tpl, array('$myvar'=>array('myfield', 'label', 'value', 'help')));
\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
179 public function testIncNoVar() {
\r
180 $tpl='{{inc field_input.tpl }}{{ endinc }}';
\r
182 $text=replace_macros($tpl, array('$field'=>array('myfield', 'label', 'value', 'help')));
\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
190 public function testDoubleUse() {
\r
191 $tpl='Hello $name! {{ if $enabled }} I love you! {{ endif }}';
\r
193 $text=replace_macros($tpl, array('$name'=>'Anna', '$enabled'=>false));
\r
195 $this->assertEquals('Hello Anna! ', $text);
197 $tpl='Hey $name! {{ if $enabled }} I hate you! {{ endif }}';
\r
199 $text=replace_macros($tpl, array('$name'=>'Max', '$enabled'=>true));
\r
201 $this->assertEquals('Hey Max! I hate you! ', $text);
\r
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
208 $text=replace_macros($tpl, array('$var1'=>array('myfield', 'label', 'value', 'help'),
209 '$var2'=>array('myfield2', 'label2', 'value2', 'help2')));
\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
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