]> git.mxchange.org Git - friendica.git/commitdiff
Merge pull request #157 from campino/master
authorfriendica <info@friendica.com>
Thu, 22 Mar 2012 12:49:08 +0000 (05:49 -0700)
committerfriendica <info@friendica.com>
Thu, 22 Mar 2012 12:49:08 +0000 (05:49 -0700)
Tests for the template engine

.htaccess
tests/template_test.php [new file with mode: 0755]
tests/xss_filter_test.php

index 28ac3dd8021c59df634ef7da5f14a042df823957..6cb3a074943c33d61c076d427ae4e5c154c4861c 100755 (executable)
--- a/.htaccess
+++ b/.htaccess
@@ -5,9 +5,6 @@ AddType audio/ogg .oga
 <FilesMatch "\.(out|log)$">
 Deny from all
 </FilesMatch>
-<Files "(include|library)">
-Deny from all
-</Files>
 
 <IfModule mod_rewrite.c>
   RewriteEngine on
diff --git a/tests/template_test.php b/tests/template_test.php
new file mode 100755 (executable)
index 0000000..1f9f805
--- /dev/null
@@ -0,0 +1,224 @@
+<?php
+/**\r
+ * this file contains tests for the template engine\r
+ *\r
+ * @package test.util\r
+ */\r
+\r
+/** required, it is the file under test */\r
+require_once('include/template_processor.php');
+require_once('include/text.php');
+
+class TemplateMockApp {
+       public $theme_info=array();
+}
+
+if(!function_exists('current_theme')) {
+function current_theme() {\r
+       return 'clean';\r
+}
+}
+
+if(!function_exists('x')) {
+function x($s,$k = NULL) {
+       return false;
+}
+}
+
+if(!function_exists('get_app')) {
+function get_app() {
+       return new TemplateMockApp();
+}
+}
+
+/**\r
+ * TestCase for the template engine\r
+ *\r
+ * @author Alexander Kampmann\r
+ * @package test.util\r
+ */\r
+class TemplateTest extends PHPUnit_Framework_TestCase {
+
+       public function setUp() {
+               global $t;
+               $t=new Template;
+       }
+
+       public function testListToShort() {
+               @list($first, $second)=array('first');
+
+               $this->assertTrue(is_null($second));
+       }
+
+       public function testSimpleVariableString() {
+               $tpl='Hello $name!';
+
+               $text=replace_macros($tpl, array('$name'=>'Anna'));
+
+               $this->assertEquals('Hello Anna!', $text);
+       }
+
+       public function testSimpleVariableInt() {\r
+               $tpl='There are $num new messages!';\r
+\r
+               $text=replace_macros($tpl, array('$num'=>172));\r
+\r
+               $this->assertEquals('There are 172 new messages!', $text);\r
+       }
+
+       public function testConditionalElse() {\r
+               $tpl='There{{ if $num!=1 }} are $num new messages{{ else }} is 1 new message{{ endif }}!';\r
+\r
+               $text1=replace_macros($tpl, array('$num'=>1));
+               $text22=replace_macros($tpl, array('$num'=>22));\r
+\r
+               $this->assertEquals('There is 1 new message!', $text1);
+               $this->assertEquals('There are 22 new messages!', $text22);\r
+       }
+
+       public function testConditionalNoElse() {\r
+               $tpl='{{ if $num!=0 }}There are $num new messages!{{ endif }}';\r
+\r
+               $text0=replace_macros($tpl, array('$num'=>0));\r
+               $text22=replace_macros($tpl, array('$num'=>22));\r
+\r
+               $this->assertEquals('', $text0);\r
+               $this->assertEquals('There are 22 new messages!', $text22);\r
+       }
+
+       public function testConditionalFail() {\r
+               $tpl='There {{ if $num!=1 }} are $num new messages{{ else }} is 1 new message{{ endif }}!';\r
+\r
+               $text1=replace_macros($tpl, array());\r
+\r
+               //$this->assertEquals('There is 1 new message!', $text1);\r
+       }
+
+       public function testSimpleFor() {\r
+               $tpl='{{ for $messages as $message }} $message {{ endfor }}';\r
+\r
+               $text=replace_macros($tpl, array('$messages'=>array('message 1', 'message 2')));\r
+\r
+               $this->assertEquals(' message 1  message 2 ', $text);\r
+       }
+
+       public function testFor() {\r
+               $tpl='{{ for $messages as $message }} from: $message.from to $message.to {{ endfor }}';\r
+\r
+               $text=replace_macros($tpl, array('$messages'=>array(array('from'=>'Mike', 'to'=>'Alex'), array('from'=>'Alex', 'to'=>'Mike'))));\r
+\r
+               $this->assertEquals(' from: Mike to Alex  from: Alex to Mike ', $text);\r
+       }
+       
+       public function testKeyedFor() {\r
+               $tpl='{{ for $messages as $from=>$to }} from: $from to $to {{ endfor }}';\r
+       \r
+               $text=replace_macros($tpl, array('$messages'=>array('Mike'=>'Alex', 'Sven'=>'Mike')));\r
+       \r
+               $this->assertEquals(' from: Mike to Alex  from: Sven to Mike ', $text);\r
+       }
+
+       public function testForEmpty() {\r
+               $tpl='messages: {{for $messages as $message}} from: $message.from to $message.to  {{ endfor }}';\r
+\r
+               $text=replace_macros($tpl, array('$messages'=>array()));\r
+\r
+               $this->assertEquals('messages: ', $text);\r
+       }
+
+       public function testForWrongType() {\r
+               $tpl='messages: {{for $messages as $message}} from: $message.from to $message.to  {{ endfor }}';\r
+\r
+               $text=replace_macros($tpl, array('$messages'=>11));\r
+\r
+               $this->assertEquals('messages: ', $text);\r
+       }
+
+       public function testForConditional() {\r
+               $tpl='new messages: {{for $messages as $message}}{{ if $message.new }} $message.text{{endif}}{{ endfor }}';\r
+\r
+               $text=replace_macros($tpl, array('$messages'=>array(
+                               array('new'=>true, 'text'=>'new message'),
+                               array('new'=>false, 'text'=>'old message'))));\r
+\r
+               $this->assertEquals('new messages:  new message', $text);\r
+       }
+       
+       public function testConditionalFor() {\r
+               $tpl='{{ if $enabled }}new messages:{{for $messages as $message}} $message.text{{ endfor }}{{endif}}';\r
+       \r
+               $text=replace_macros($tpl, array('$enabled'=>true, 
+                               '$messages'=>array(\r
+                               array('new'=>true, 'text'=>'new message'),\r
+                               array('new'=>false, 'text'=>'old message'))));\r
+       \r
+               $this->assertEquals('new messages: new message old message', $text);\r
+       }
+
+       public function testFantasy() {\r
+               $tpl='Fantasy: {{fantasy $messages}}';\r
+\r
+               $text=replace_macros($tpl, array('$messages'=>'no no'));\r
+\r
+               $this->assertEquals('Fantasy: {{fantasy no no}}', $text);\r
+       }
+
+       public function testInc() {\r
+               $tpl='{{inc field_input.tpl with $field=$myvar}}{{ endinc }}';\r
+\r
+               $text=replace_macros($tpl, array('$myvar'=>array('myfield', 'label', 'value', 'help')));\r
+\r
+               $this->assertEquals("   \n"
+                               ."      <div class='field input'>\n"
+                               ."              <label for='id_myfield'>label</label>\n"
+                               ."              <input name='myfield' id='id_myfield' value=\"value\">\n"
+                               ."              <span class='field_help'>help</span>\n"
+                               ."      </div>\n", $text);\r
+       }
+
+       public function testIncNoVar() {\r
+               $tpl='{{inc field_input.tpl }}{{ endinc }}';\r
+\r
+               $text=replace_macros($tpl, array('$field'=>array('myfield', 'label', 'value', 'help')));\r
+\r
+               $this->assertEquals("   \n      <div class='field input'>\n             <label for='id_myfield'>label</label>\n"\r
+                               ."              <input name='myfield' id='id_myfield' value=\"value\">\n"\r
+                               ."              <span class='field_help'>help</span>\n"\r
+                               ."      </div>\n", $text);\r
+       }
+       
+       public function testDoubleUse() {\r
+               $tpl='Hello $name! {{ if $enabled }} I love you! {{ endif }}';\r
+       \r
+               $text=replace_macros($tpl, array('$name'=>'Anna', '$enabled'=>false));\r
+       \r
+               $this->assertEquals('Hello Anna! ', $text);
+               
+               $tpl='Hey $name! {{ if $enabled }} I hate you! {{ endif }}';\r
+               \r
+               $text=replace_macros($tpl, array('$name'=>'Max', '$enabled'=>true));\r
+               \r
+               $this->assertEquals('Hey Max!  I hate you! ', $text);\r
+       }
+       
+       public function testIncDouble() {\r
+               $tpl='{{inc field_input.tpl with $field=$var1}}{{ endinc }}'
+               .'{{inc field_input.tpl with $field=$var2}}{{ endinc }}';\r
+       \r
+               $text=replace_macros($tpl, array('$var1'=>array('myfield', 'label', 'value', 'help'), 
+                               '$var2'=>array('myfield2', 'label2', 'value2', 'help2')));\r
+               \r
+               $this->assertEquals("   \n"\r
+                               ."      <div class='field input'>\n"\r
+                               ."              <label for='id_myfield'>label</label>\n"\r
+                               ."              <input name='myfield' id='id_myfield' value=\"value\">\n"\r
+                               ."              <span class='field_help'>help</span>\n"\r
+                               ."      </div>\n"
+                               ."      \n"
+                               ."      <div class='field input'>\n"
+                               ."              <label for='id_myfield2'>label2</label>\n"
+                               ."              <input name='myfield2' id='id_myfield2' value=\"value2\">\n"
+                               ."              <span class='field_help'>help2</span>\n"
+                               ."      </div>\n", $text);\r
+       }
+}
\ No newline at end of file
index d7dcf0472bd698c84df6487d7d1144b2d0e7b1d3..3fb6ac3109daf150c071d7d767b4ecd72a8be5a8 100644 (file)
@@ -27,11 +27,32 @@ class AntiXSSTest extends PHPUnit_Framework_TestCase {
         */
        public function testXmlify() {
                $text="<tag>I want to break\n this!11!<?hard?></tag>";
-               $xml=xmlify($text); //test whether it actually may be part of a xml document
+               $xml=xmlify($text); 
                $retext=unxmlify($text);
 
                $this->assertEquals($text, $retext);
        }
+       
+       /**\r
+        * xmlify and put in a document\r
+        */\r
+       public function testXmlifyDocument() {\r
+               $tag="<tag>I want to break</tag>";\r
+               $xml=xmlify($tag);
+               $text='<text>'.$xml.'</text>'; \r
+               
+               $xml_parser=xml_parser_create(); 
+               //should be possible to parse it
+               $values=array(); $index=array(); 
+               $this->assertEquals(1, xml_parse_into_struct($xml_parser, $text, $values, $index)); 
+               
+               $this->assertEquals(array('TEXT'=>array(0)), 
+                               $index); 
+               $this->assertEquals(array(array('tag'=>'TEXT', 'type'=>'complete', 'level'=>1, 'value'=>$tag)),\r
+                               $values);
+               
+               xml_parser_free($xml_parser); \r
+       }
 
        /**
         * test hex2bin and reverse