ea-Geiertest
[ class tree: ea-Geier ] [ index: ea-Geier ] [ all elements ]

Source for file smarty.class.php

Documentation is available at smarty.class.php

  1. <?php
  2. /**
  3.  * test case for smarty wrapper
  4.  *
  5.  * LICENSE:
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
  13.  * GNU General Public License for more details.
  14.  * You should have received a copy of the GNU General Public License along
  15.  * with this program; if not, write to the Free Software Foundation, Inc.,
  16.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17.  *
  18.  * @package       ea-Geier
  19.  * @subpackage test
  20.  * @author       m2mtech <tech@m2m.at>
  21.  * @copyright  2007 m2m server software gmbh
  22.  * @license       http://www.gnu.org/licenses/gpl.html GNU General Public License Version 2
  23.  * @version       $Id: smarty.class.php 103 2007-07-31 19:27:19Z m2mtech $
  24.  * @link       http://www.ea-geier.at/
  25.  */
  26.  
  27. /**
  28.  * class to be tested
  29.  */
  30. require_once('code/base/smarty.class.php');
  31.  
  32. /**
  33.  * work around for quick template testing
  34.  */
  35. require_once(eaSMARTY_DIR 'plugins/function.eval.php');
  36.  
  37. /**
  38.  * tast case for smarty wrapper
  39.  *
  40.  * @package       ea-Geier
  41.  */
  42. class TestSmartyWrapper extends UnitTestCase {
  43.     
  44.     /**
  45.      * constructor
  46.      */
  47.     function TestSmartyWrapper({
  48.         $this->UnitTestCase('Test Smarty Wrapper');
  49.     }
  50.  
  51.     /**
  52.      * tests the constructor
  53.      *
  54.      * - checks if german is the native language
  55.      * - checks folder availibilty
  56.      */
  57.     function testConstructor({
  58.         $smarty new eaSmarty();
  59.         $this->assertEqual($smarty->language'de''german is not native language');
  60.         $this->assertTrue(is_dir($smarty->config_dir)'ini folder not available');
  61.         $this->assertTrue(is_dir($smarty->compile_dir)'compile folder not available');
  62.         $this->assertTrue(is_dir($smarty->cache_dir)'cache folder not available');
  63.     }
  64.   
  65.     /**
  66.      * tests the changeLanguage methode
  67.      *
  68.      * - checks unavailable language
  69.      * - checks available language
  70.      * - checks effective change
  71.      * - checks changed values
  72.      * - checks folder availibility
  73.      */
  74.     function testChangeLanguage({
  75.         $smarty new eaSmarty();
  76.         $vars array('config_dir''compile_dir''cache_dir');
  77.         // save old values
  78.         foreach ($vars as $var$old[$var$smarty->$var;
  79.         // start testing
  80.         $this->assertFalse($smarty->changeLanguage('xx')'invalid change possible');
  81.         $this->assertTrue($smarty->changeLanguage('en')'valid change not possible');
  82.         $this->assertEqual($smarty->language'en''change effective');
  83.         foreach ($vars as $var
  84.             $this->assertNotEqual($old[$var]$smarty->$var'value ' $var ' did not change');
  85.         foreach ($vars as $var
  86.             $this->assertTrue(is_dir($smarty->$var)'folder ' $var ' not available');
  87.     }
  88.  
  89.     /**
  90.      * tests the {eaInput} function plugin
  91.      */
  92.     function testPluginEAInput({
  93.         $smarty new eaSmarty();
  94.         
  95.         error_log('the following error message is part of unit testing');
  96.         $template array('var' => '{eaInput}');
  97.         $this->expectError("Smarty error: eaInput: 'name' missing"'no error message');
  98.         smarty_function_eval($template$smarty);
  99.         
  100.         $template array('var' => '{eaInput name=test}');
  101.         $result smarty_function_eval($template$smarty);
  102.         $this->assertEqual('<input type="text" name="test" id="test" />'$result'wrong default type');
  103.         
  104.         $template array('var' => '{eaInput name=test type=nothing}');        
  105.         $result smarty_function_eval($template$smarty);
  106.         $this->assertEqual('<input type="text" name="test" id="test" />'$result'wrong type accepted');
  107.  
  108.         $template array('var' => '{eaInput name=test type=text}');
  109.         $result smarty_function_eval($template$smarty);
  110.         $this->assertEqual('<input type="text" name="test" id="test" />'$result'type text not working');
  111.  
  112.         $template array('var' => '{eaInput name=test type=password}');
  113.         $result smarty_function_eval($template$smarty);
  114.         $this->assertEqual('<input type="password" name="test" id="test" />'$result'type password not working');
  115.  
  116.         $smarty->assign('data'array('test' => 'testvalue'));
  117.         $template array('var' => '{eaInput name=test type=text}');
  118.         $result smarty_function_eval($template$smarty);
  119.         $this->assertEqual('<input type="text" name="test" id="test" value="testvalue" />'$result'value not working');
  120.         $smarty->clear_assign('data');
  121.  
  122.         $smarty->assign('error'array('test' => 'testvalue'));
  123.         $template array('var' => '{eaInput name=test type=text}');
  124.         $result smarty_function_eval($template$smarty);
  125.         $this->assertEqual('<input type="text" name="test" id="test" class="error" />'$result'error class not working');
  126.         $smarty->clear_assign('error');
  127.  
  128.         $smarty->_config[0]['vars']['helptest''helpcontent';
  129.         $template array('var' => '{eaInput name=test type=text}');
  130.         $result smarty_function_eval($template$smarty);
  131.         $this->assertEqual('<input type="text" name="test" id="test" title="helpcontent" />'$result'title not working');
  132.         $smarty->clear_config();
  133.  
  134.         $smarty->_config[0]['vars']['labeltest''testlabel';
  135.         $template array('var' => '{eaInput name=test type=text}');
  136.         $result smarty_function_eval($template$smarty);
  137.         $this->assertEqual('<label for="test">testlabel</label><input type="text" name="test" id="test" />'$result'label not working');
  138.         $smarty->clear_config();
  139.         
  140.         // align 
  141.         $template array('var' => '{eaInput name=test align=right}');
  142.         $result smarty_function_eval($template$smarty);
  143.         $this->assertEqual('<input type="text" name="test" id="test" style="text-align: right" />'$result);
  144.  
  145.         // onchange
  146.         $template array('var' => '{eaInput name=test onchange=something()}');
  147.         $result smarty_function_eval($template$smarty);
  148.         $this->assertEqual('<input type="text" name="test" id="test" onchange="something()" />'$result);
  149.     }
  150.  
  151.     /**
  152.      * tests the {eaSelect} block plugin
  153.      *
  154.      * checks missing end tag, missing name, error, title label
  155.      */
  156.     function testPluginEASelect({
  157.         $smarty new eaSmarty();
  158.  
  159.         // missing end tag
  160.         error_log('the following error message is part of unit testing');
  161.         $template array('var' => '{eaSelect}');
  162.         $this->expectError();
  163.         $this->assertFalse(smarty_function_eval($template$smarty));
  164.  
  165.         // missing name
  166.         error_log('the following error message is part of unit testing');
  167.         $template array('var' => '{eaSelect}test{/eaSelect}');
  168.         $this->expectError();
  169.         $this->assertFalse(smarty_function_eval($template$smarty));
  170.  
  171.         // select
  172.         $template array('var' => '{eaSelect name=test}test{/eaSelect}');
  173.         $this->assertTrue($result smarty_function_eval($template$smarty));
  174.         $this->assertEqual('<select name="test" id="test" >test</select>'$result);
  175.         $smarty->clear_config();
  176.  
  177.         // error
  178.         $smarty->assign('error'array('test' => 'testvalue'));
  179.         $template array('var' => '{eaSelect name=test}test{/eaSelect}');
  180.         $this->assertTrue($result smarty_function_eval($template$smarty));
  181.         $this->assertEqual('<select name="test" id="test" class="error" >test</select>'$result);
  182.         $smarty->clear_assign('error');
  183.  
  184.         // title
  185.         $smarty->_config[0]['vars']['helptest''helpcontent';
  186.         $template array('var' => '{eaSelect name=test}test{/eaSelect}');
  187.         $this->assertTrue($result smarty_function_eval($template$smarty));
  188.         $this->assertEqual('<select name="test" id="test" title="helpcontent" >test</select>'$result);
  189.         $smarty->clear_config();
  190.  
  191.         // label
  192.         $smarty->_config[0]['vars']['labeltest''testlabel';
  193.         $template array('var' => '{eaSelect name=test}test{/eaSelect}');
  194.         $this->assertTrue($result smarty_function_eval($template$smarty));
  195.         $this->assertEqual('<label for="test">testlabel</label><select name="test" id="test" >test</select>'$result);
  196.         $smarty->clear_config();
  197.     }
  198.  
  199.     /**
  200.      * tests the {eaSubmitButton} function plugin
  201.      */
  202.     function testPluginEASubmitButton({
  203.         $smarty new eaSmarty();
  204.         
  205.         error_log('the following error message is part of unit testing');
  206.         $template array('var' => '{eaSubmitButton}');
  207.         $this->expectError('Smarty error: trigger error for testing:');
  208.         $smarty->trigger_error('trigger error for testing:'E_USER_NOTICE);
  209.         $this->expectError("Smarty error: eaSubmitButton: 'name' missing"'no error message');
  210.         smarty_function_eval($template$smarty);
  211.         
  212.         // wront default type
  213.         $template array('var' => '{eaSubmitButton name=test}');
  214.         $result smarty_function_eval($template$smarty);
  215.         $this->assertEqual('<label>&nbsp;</label><input type="submit" name="test" value="submit" class="submit" />'$result);
  216.         
  217.         // wront value
  218.         $smarty->_config[0]['vars']['test''testsubmit';
  219.         $template array('var' => '{eaSubmitButton name=test}');
  220.         $result smarty_function_eval($template$smarty);
  221.         $this->assertEqual('<label>&nbsp;</label><input type="submit" name="test" value="testsubmit" class="submit" />'$result);
  222.         $smarty->clear_config();
  223.     }
  224.  
  225.     /**
  226.      * tests the {eaURL} function plugin
  227.      */
  228.     function testPluginEAURL({
  229.         $smarty new eaSmarty();
  230.         
  231.         $template array('var' => '{eaURL state=test}');
  232.         $result smarty_function_eval($template$smarty);
  233.         $this->assertTrue('test'$result'missing types');
  234.         
  235.         $template array('var' => '{eaURL state=testdummy type=short}');
  236.         $result smarty_function_eval($template$smarty);
  237.         $this->assertPattern('/\/testdummy/'$result'short url');
  238.  
  239.         $template array('var' => '{eaURL state=testdummy type=long}');
  240.         $result smarty_function_eval($template$smarty);
  241.         $this->assertPattern('/\?state=testdummy/'$result'long url');
  242.     }
  243.  
  244.     /**
  245.      * tests the {eaCheckBox} function plugin
  246.      */
  247.     function testPluginEACheckBox({
  248.         $smarty new eaSmarty();
  249.         
  250.         // error no name given
  251.         error_log('the following error message is part of unit testing');
  252.         $template array('var' => '{eaCheckBox}');
  253.         $this->expectError("Smarty error: eaCheckBox: 'name' missing");
  254.         smarty_function_eval($template$smarty);
  255.         
  256.         // no options
  257.         $template array('var' => '{eaCheckBox name=test}');
  258.         $result smarty_function_eval($template$smarty);
  259.         $this->assertEqual(''$result);
  260.         
  261.         // options
  262.         $smarty->assign('opt'array('test' => array(=> true=> true)));
  263.         $template array('var' => '{eaCheckBox name=test}');
  264.         $result smarty_function_eval($template$smarty);
  265.         $this->assertEqual('<label><input type="checkbox" name="test[]" value="1" /></label>' "\n" '<label><input type="checkbox" name="test[]" value="2" /></label>'$result);
  266.  
  267.         // labels from config
  268.         $smarty->_config[0]['vars']['opttest1''abc';
  269.         $smarty->_config[0]['vars']['opttest2''def';
  270.         $template array('var' => '{eaCheckBox name=test}');
  271.         $result smarty_function_eval($template$smarty);
  272.         $this->assertEqual('<label><input type="checkbox" name="test[]" value="1" />abc</label>' "\n" '<label><input type="checkbox" name="test[]" value="2" />def</label>'$result);
  273.  
  274.         // single checked
  275.         $smarty->assign('data'array('test' => '1'));
  276.         $template array('var' => '{eaCheckBox name=test}');
  277.         $result smarty_function_eval($template$smarty);
  278.         $this->assertEqual('<label><input type="checkbox" name="test[]" value="1" checked="checked" />abc</label>' "\n" '<label><input type="checkbox" name="test[]" value="2" />def</label>'$result);
  279.  
  280.         // multiple checked
  281.         $smarty->assign('data'array('test' => array(12)));
  282.         $template array('var' => '{eaCheckBox name=test}');
  283.         $result smarty_function_eval($template$smarty);
  284.         $this->assertEqual('<label><input type="checkbox" name="test[]" value="1" checked="checked" />abc</label>' "\n" '<label><input type="checkbox" name="test[]" value="2" checked="checked" />def</label>'$result);
  285.  
  286.         $smarty->clear_config();
  287.         
  288.         // check error class
  289.         $smarty->assign('error'array('test' => 'testvalue'));
  290.         $smarty->assign('opt'array('test' => array(=> true)));
  291.         $template array('var' => '{eaCheckBox name=test}');
  292.         $result smarty_function_eval($template$smarty);
  293.         $this->assertEqual('<label><input type="checkbox" name="test[]" value="1" checked="checked" class="error" /></label>'$result);
  294.  
  295.         $smarty->clear_assign('error');
  296.  
  297.         // check help text
  298.         $smarty->_config[0]['vars']['helptest''helpcontent';
  299.         $template array('var' => '{eaCheckBox name=test}');
  300.         $result smarty_function_eval($template$smarty);
  301.         $this->assertEqual('<label><input type="checkbox" name="test[]" value="1" checked="checked" title="helpcontent" /></label>'$result);
  302.  
  303.         $smarty->clear_config();
  304.  
  305.         // label        
  306.         $smarty->_config[0]['vars']['labeltest''testlabel';
  307.         $template array('var' => '{eaCheckBox name=test}');
  308.         $result smarty_function_eval($template$smarty);
  309.         $this->assertEqual('<label for="test">testlabel</label><label><input type="checkbox" name="test[]" value="1" checked="checked" /></label>'$result);
  310.     }
  311.  
  312.     /**
  313.      * tests the {eaPage} function plugin
  314.      */
  315.     function testPluginEAPage({
  316.         $smarty new eaSmarty();
  317.  
  318.         // wrong input        
  319.         $template array('var' => '{eaPage}');
  320.         $result smarty_function_eval($template$smarty);
  321.         $this->assertEqual('1'$result);
  322.         
  323.         // one page
  324.         $template array('var' => '{eaPage pages=1 cur=1}');
  325.         $result smarty_function_eval($template$smarty);
  326.         $this->assertEqual('1'$result);
  327.         
  328.         // two pages first selected
  329.         $template array('var' => '{eaPage pages=2 cur=1}');
  330.         $result smarty_function_eval($template$smarty);
  331.         $this->assertEqual('1 <a href="page2">2</a><a href="page2">&gt;</a>'$result);
  332.         
  333.         // two pages second selected
  334.         $template array('var' => '{eaPage pages=2 cur=2}');
  335.         $result smarty_function_eval($template$smarty);
  336.         $this->assertEqual('<a href="page1">&lt;</a><a href="page1">1</a> 2'$result);
  337.         
  338.         // multiple pages first selected
  339.         $template array('var' => '{eaPage pages=20 cur=1}');
  340.         $result smarty_function_eval($template$smarty);
  341.         $this->assertEqual('1 <a href="page2">2</a> <a href="page3">3</a> <a href="page4">4</a><a href="page2">&gt;</a>'$result);
  342.         
  343.         // multiple pages second selected
  344.         $template array('var' => '{eaPage pages=20 cur=2}');
  345.         $result smarty_function_eval($template$smarty);
  346.         $this->assertEqual('<a href="page1">&lt;</a><a href="page1">1</a> 2 <a href="page3">3</a> <a href="page4">4</a> <a href="page5">5</a><a href="page3">&gt;</a>'$result);
  347.         
  348.         // multiple pages third selected
  349.         $template array('var' => '{eaPage pages=20 cur=2}');
  350.         $result smarty_function_eval($template$smarty);
  351.         $this->assertEqual('<a href="page1">&lt;</a><a href="page1">1</a> 2 <a href="page3">3</a> <a href="page4">4</a> <a href="page5">5</a><a href="page3">&gt;</a>'$result);
  352.         
  353.         // multiple pages last selected
  354.         $template array('var' => '{eaPage pages=20 cur=20}');
  355.         $result smarty_function_eval($template$smarty);
  356.         $this->assertEqual('<a href="page19">&lt;</a><a href="page17">17</a> <a href="page18">18</a> <a href="page19">19</a> 20'$result);
  357.         
  358.         // multiple pages second last selected
  359.         $template array('var' => '{eaPage pages=20 cur=19}');
  360.         $result smarty_function_eval($template$smarty);
  361.         $this->assertEqual('<a href="page18">&lt;</a><a href="page16">16</a> <a href="page17">17</a> <a href="page18">18</a> 19 <a href="page20">20</a><a href="page20">&gt;</a>'$result);
  362.         
  363.         // multiple pages third selected
  364.         $template array('var' => '{eaPage pages=20 cur=18}');
  365.         $result smarty_function_eval($template$smarty);
  366.         $this->assertEqual('<a href="page17">&lt;</a><a href="page15">15</a> <a href="page16">16</a> <a href="page17">17</a> 18 <a href="page19">19</a> <a href="page20">20</a><a href="page19">&gt;</a>'$result);
  367.                         
  368.         // multiple pages middle selected
  369.         $template array('var' => '{eaPage pages=20 cur=10}');
  370.         $result smarty_function_eval($template$smarty);
  371.         $this->assertEqual('<a href="page9">&lt;</a><a href="page7">7</a> <a href="page8">8</a> <a href="page9">9</a> 10 <a href="page11">11</a> <a href="page12">12</a> <a href="page13">13</a><a href="page11">&gt;</a>'$result);
  372.  
  373.     }
  374.  
  375. }
  376. ?>

Documentation generated on Sun, 09 Sep 2007 17:09:32 +0200 by phpDocumentor 1.3.1