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

Source for file input.class.php

Documentation is available at input.class.php

  1. <?php
  2. /**
  3.  * test case for input 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: input.class.php 139 2007-08-29 20:49:58Z m2mtech $
  24.  * @link       http://www.ea-geier.at/
  25.  */
  26.  
  27. /**
  28.  * class to be tested
  29.  */
  30. require_once('code/base/input.class.php');
  31.  
  32. /**
  33.  * helper class - database
  34.  */
  35. require_once('code/base/db.class.php');
  36. Mock::generate('eaDB');
  37.  
  38. /**
  39.  * tast case for input wrapper
  40.  *
  41.  * @package       ea-Geier
  42.  */
  43. class TestInputWrapper extends UnitTestCase {
  44.     
  45.     /**
  46.      * constructor
  47.      */
  48.     function TestInputWrapper({
  49.         $this->UnitTestCase('Test Input Wrapper');
  50.     }
  51.  
  52.     /**
  53.      * tests the constructor
  54.      *
  55.      * does not test anything at the moment
  56.      */
  57.     function testConstructor({
  58.     }
  59.  
  60.     /**
  61.      * tests pattern creator
  62.      *
  63.      * checks false input, and valid input, and the result
  64.      */
  65.     function testPatternCreator({
  66.         $in new eaInput();
  67.         $pattern array();
  68.         $test array();
  69.         
  70.         $dsn ' ';
  71.         $this->assertFalse($in->createTestPattern($dsn$pattern$test)'empty dsn');
  72.         
  73.         $dsn ' a:abc:abc,abc ';
  74.         $this->assertFalse($in->createTestPattern($dsn$pattern$test)'wrong method');
  75.         
  76.         $dsn ' gp:wrong:abc,abc ';
  77.         $this->assertFalse($in->createTestPattern($dsn$pattern$test)'wrong function');
  78.         
  79.         $dsn ' gp:abc:ab-c,abc ';
  80.         $this->assertFalse($in->createTestPattern($dsn$pattern$test)'wrong prefix');
  81.         
  82.         $dsn ' gp:abc:abc,txt ';
  83.         $this->assertTrue($in->createTestPattern($dsn$pattern$test)'format');
  84.         $validTest array ('g' => array ('abc' => 'abc''txt' => 'abc')'p' => array ('abc' => 'abc''txt' => 'abc'));
  85.         $validPattern array ('g' => 'abc|txt''p' => 'abc|txt' );
  86.         $this->assertEqual($test$validTest'test array');
  87.         $this->assertEqual($pattern$validPattern'test pattern');
  88.     }
  89.  
  90.     /**
  91.      * tests input validator
  92.      *
  93.      * checks non existing, false, valid, and corrected input
  94.      */
  95.     function testCheck({
  96.         $in new eaInput();
  97.  
  98.         $dsn ' g:abc:abc ';
  99.         $this->assertFalse($in->check($dsn)'not existing get');
  100.  
  101.         $_GET['abcTest'10;
  102.         $dsn ' g:abc:abc ';
  103.         $this->assertTrue($in->check($dsn)'integer :: alpha');
  104.         $this->assertEqual($in->error['abcTest']'badformat''error set');
  105.         unset($_GET['abcTest']);
  106.  
  107.         $_GET['txtTest''test';
  108.         $dsn ' g:abc:abc,txt ';
  109.         $this->assertTrue($in->check($dsn)'alpha');
  110.         $this->assertFalse(isset($in->error['txtTest'])'no error');
  111.         $this->assertEqual($in->get['txtTest']'test''correct value');
  112.         unset($_GET['txtTest']);
  113.  
  114.         $_GET['abcTest''test123';
  115.         $dsn ' g:abc:abc,txt ';
  116.         $this->assertTrue($in->check($dsn)'alpha');
  117.         $this->assertTrue(isset($in->error['abcTest'])'error set');
  118.         $this->assertEqual($in->get['abcTest']'test''correct value');
  119.         $this->assertNotEqual($in->get['abcTest']$_GET['abcTest']'correct value');
  120.         unset($_GET['abcTest']);
  121.  
  122.         // check array
  123.         $in->get array();
  124.         $_GET['abcTest'array(=> 'test123'=> 'test456');
  125.         $dsn ' g:txt:abc ';
  126.         $this->assertTrue($in->check($dsn));
  127.         $this->assertEqual($in->get['abcTest']$_GET['abcTest']);
  128.         unset($_GET['abcTest']);
  129.  
  130.         // check array error
  131.         $in->get array();
  132.         $_GET['abcTest'array(=> 't<b>est123'=> 'test456');
  133.         $dsn ' g:txt:abc ';
  134.         $this->assertTrue($in->check($dsn));
  135.         $this->assertNotEqual($in->get['abcTest']$_GET['abcTest']);
  136.         $this->assertTrue(isset($in->error['abcTest'][1]));
  137.         unset($_GET['abcTest']);
  138.     }
  139.  
  140.     /**
  141.      * tests get()
  142.      *
  143.      * checks non existing, false, and valid input
  144.      */
  145.     function testGet({
  146.         $in new eaInput();
  147.  
  148.         $_GET['abcTest1''ok';
  149.         $_GET['abcTest2''wrong123';
  150.         $dsn ' g:abc:abc ';
  151.         $in->check($dsn);
  152.         $this->assertEqual($in->get('abcTest1')'ok''valid input');
  153.         $this->assertFalse($in->get('abcTest2')'wrong input');
  154.         $this->assertFalse($in->get('abcTest3')'not existing input');
  155.  
  156.         unset($_GET['abcTest1']);
  157.         unset($_GET['abcTest2']);
  158.     }
  159.  
  160.     /**
  161.      * checks non existing, false, and valid input
  162.      *
  163.      * checks non existing, false, valid, and corrected input
  164.      */
  165.     function testPost({
  166.         $in new eaInput();
  167.  
  168.         $_POST['abcTest1''ok';
  169.         $_POST['abcTest2''wrong123';
  170.         $dsn ' p:abc:abc ';
  171.         $in->check($dsn);
  172.         $this->assertEqual($in->post('abcTest1')'ok''valid input');
  173.         $this->assertFalse($in->post('abcTest2')'wrong input');
  174.         $this->assertFalse($in->post('abcTest3')'not existing input');
  175.     
  176.         unset($_POST['abcTest1']);
  177.         unset($_POST['abcTest2']);
  178.     }
  179.  
  180.     /**
  181.      * tests getClient function
  182.      *
  183.      * checks input from short & long urls or sessions
  184.      */
  185.     function testGetClient({
  186.         $in new eaInput();
  187.         $opt =$in->options;
  188.         $oldUrl $_SERVER['REQUEST_URI'];
  189.  
  190.         $db new MockeaDB();
  191.         $db->setReturnValue('loadClientConf'1array(1));
  192.         $db->setReturnValue('loadClientConf'23array(23));
  193.         $db->setReturnValue('loadClientConf'123array(123));
  194.  
  195.         // single client
  196.         $opt['clients'][1'dummy';
  197.         $this->assertEqual($in->getClient($db)1);
  198.         $this->assertEqual($opt['client']1);
  199.         unset($_SESSION['client']);        
  200.  
  201.         // no state -> force first client
  202.         $opt['clients'][2'dummy';
  203.         unset($_SESSION['client']);        
  204.         $this->assertEqual($in->getClient($db)1);
  205.         $this->assertEqual($opt['client']1);
  206.         unset($_SESSION['client']);        
  207.  
  208.         // wrong state -> force first client
  209.         $in->get['state''teststate';
  210.         $_SERVER['REQUEST_URI''/teststatewrong/23';
  211.         $this->assertEqual($in->getClient($db)1);
  212.         $this->assertEqual($opt['client']1);
  213.         unset($_SESSION['client']);        
  214.         
  215.         // no client set -> force first client
  216.         $_SERVER['REQUEST_URI''/teststate';
  217.         $this->assertEqual($in->getClient($db)1);
  218.         $this->assertEqual($opt['client']1);
  219.         unset($_SESSION['client']);        
  220.  
  221.         // no client set -> force first client
  222.         $_SERVER['REQUEST_URI''/teststate/';
  223.         $this->assertEqual($in->getClient($db)1);
  224.         $this->assertEqual($opt['client']1);
  225.         unset($_SESSION['client']);        
  226.         
  227.         // wrong client set -> force first client
  228.         $_SERVER['REQUEST_URI''/teststate/23';
  229.         $this->assertEqual($in->getClient($db)1);
  230.         $this->assertEqual($opt['client']1);
  231.         unset($_SESSION['client']);        
  232.  
  233.         // correct client
  234.         $opt['clients'][23'correctclient';
  235.         $_SERVER['REQUEST_URI''/teststate/23';
  236.         $this->assertEqual($in->getClient($db)23);
  237.         $this->assertEqual($opt['client']23);
  238.  
  239.         // client stored in session
  240.         $_SERVER['REQUEST_URI''/teststate';
  241.         $this->assertEqual($in->getClient($db)23);
  242.         $this->assertEqual($opt['client']23);
  243.  
  244.         // client stored in session
  245.         $_SERVER['REQUEST_URI''/teststate/';
  246.         $this->assertEqual($in->getClient($db)23);
  247.         $this->assertEqual($opt['client']23);
  248.         
  249.         // new client
  250.         $opt['clients'][123'newclient';
  251.         $_SERVER['REQUEST_URI''/teststate/123';
  252.         $this->assertEqual($in->getClient($db)123);
  253.         $this->assertEqual($opt['client']123);
  254.  
  255.         // wrong client in session -> force first client
  256.         unset($opt['clients'][23]);
  257.         unset($opt['clients'][123]);
  258.         $this->assertEqual($in->getClient($db)1);
  259.         $this->assertEqual($opt['client']1);
  260.         unset($_SESSION['client']);        
  261.  
  262.         // wrong state -> force first client
  263.         $in->get['state''teststate';
  264.         $_SERVER['REQUEST_URI''/teststatewrong/23';
  265.         $_SERVER['REQUEST_URI''/index.php?state=teststatewrong&23';
  266.         $this->assertEqual($in->getClient($db)1);
  267.         $this->assertEqual($opt['client']1);
  268.         unset($_SESSION['client']);        
  269.         
  270.         // no client set -> force first client
  271.         $_SERVER['REQUEST_URI''/index.php?state=teststate';
  272.         $this->assertEqual($in->getClient($db)1);
  273.         $this->assertEqual($opt['client']1);
  274.         unset($_SESSION['client']);        
  275.  
  276.         // no client set -> force first client
  277.         $_SERVER['REQUEST_URI''/index.php?state=teststate&';
  278.         $this->assertEqual($in->getClient($db)1);
  279.         $this->assertEqual($opt['client']1);
  280.         unset($_SESSION['client']);        
  281.         
  282.         // wrong client set -> force first client
  283.         $_SERVER['REQUEST_URI''/index.php?state=teststate&23';
  284.         $this->assertEqual($in->getClient($db)1);
  285.         $this->assertEqual($opt['client']1);
  286.         unset($_SESSION['client']);        
  287.  
  288.         // correct client
  289.         $opt['clients'][23'correctclient';
  290.         $this->assertEqual($in->getClient($db)23);
  291.         $this->assertEqual($opt['client']23);
  292.  
  293.         // client stored in session
  294.         $_SERVER['REQUEST_URI''/index.php?state=teststate';
  295.         $this->assertEqual($in->getClient($db)23);
  296.         $this->assertEqual($opt['client']23);
  297.  
  298.         // client stored in session
  299.         $_SERVER['REQUEST_URI''/index.php?state=teststate&';
  300.         $this->assertEqual($in->getClient($db)23);
  301.         $this->assertEqual($opt['client']23);
  302.         
  303.         // new client
  304.         $opt['clients'][123'newclient';
  305.         $_SERVER['REQUEST_URI''/index.php?state=teststate&123';
  306.         $this->assertEqual($in->getClient($db)123);
  307.         $this->assertEqual($opt['client']123);
  308.  
  309.         // wrong client in session -> force first client
  310.         unset($opt['clients'][23]);
  311.         unset($opt['clients'][123]);
  312.         $this->assertEqual($in->getClient($db)1);
  313.         $this->assertEqual($opt['client']1);
  314.         unset($_SESSION['client']);        
  315.  
  316.         $_SERVER['REQUEST_URI'$oldUrl;
  317.     }
  318.  
  319.     /**
  320.      * tests setClient function
  321.      */
  322.     function testSetClient({
  323.         $in new eaInput();
  324.         $opt =$in->options;
  325.         
  326.         // error empty input
  327.         $this->assertFalse($in->setClient());
  328.  
  329.         // valid input
  330.         $this->assertTrue($in->setClient(123));
  331.         $this->assertEqual($opt['client']123);
  332.         $this->assertEqual($_SESSION['client']123);
  333.         
  334.         unset($_SESSION['cleint']);
  335.     }
  336.  
  337.     /**
  338.      * tests getGet function
  339.      */
  340.     function testGetGet({
  341.         $in new eaInput();
  342.         $opt =$in->options;
  343.         
  344.         $opt =$this->options;
  345.  
  346.         $oldUrl $_SERVER['REQUEST_URI'];
  347.         
  348.         // no url
  349.         unset ($_SERVER['REQUEST_URI']);
  350.         $this->assertFalse($in->getGet('test'));
  351.         
  352.         // empty url
  353.         $_SERVER['REQUEST_URI''';
  354.         $this->assertFalse($in->getGet('test'));
  355.         
  356.         // variable not found
  357.         $_SERVER['REQUEST_URI''something/123/somethingelse';
  358.         $this->assertFalse($in->getGet('test'));
  359.         
  360.         // variable not found
  361.         $_SERVER['REQUEST_URI''something.html?123&somethingelse';
  362.         $this->assertFalse($in->getGet('test'));
  363.         
  364.         // variable not valid
  365.         $_SERVER['REQUEST_URI''something/123/test1false';
  366.         $this->assertFalse($in->getGet('test'));
  367.         
  368.         // variable not valid
  369.         $_SERVER['REQUEST_URI''something.html?123&test1false';
  370.         $this->assertFalse($in->getGet('test'));
  371.         
  372.         // variable valid
  373.         $_SERVER['REQUEST_URI''something/123/test123';
  374.         $this->assertEqual($in->getGet('test')123);
  375.         
  376.         // variable valid
  377.         $_SERVER['REQUEST_URI''something.html?123&test123';
  378.         $this->assertEqual($in->getGet('test')123);
  379.         
  380.         // variable valid
  381.         $_SERVER['REQUEST_URI''something/123/testabc';
  382.         $this->assertEqual($in->getGet('test''abc')'abc');
  383.  
  384.         // variable valid
  385.         $_SERVER['REQUEST_URI''something.html?123&testabc';
  386.         $this->assertEqual($in->getGet('test''abc')'abc');
  387.  
  388.         $_SERVER['REQUEST_URI'$oldUrl;
  389.     }
  390.  
  391.     /**
  392.      * tests getSort function
  393.      */
  394.     function testGetSort({
  395.         $in new eaInput();
  396.         $opt =$in->options;
  397.         
  398.         $opt =$this->options;
  399.  
  400.         $oldUrl $_SERVER['REQUEST_URI'];
  401.         
  402.         // no url
  403.         unset ($_SERVER['REQUEST_URI']);
  404.         $this->assertFalse($in->getSort());
  405.         
  406.         // no url & standard sort
  407.         unset ($_SESSION['sortwhat']);
  408.         unset ($_SESSION['sortdir']);
  409.         $this->assertEqual($in->getSort(array('id'))'id desc');
  410.  
  411.         // no url but session
  412.         $_SESSION['sortwhat''id';
  413.         $_SESSION['sortdir''desc';
  414.         $this->assertEqual($in->getSort(array('id'))'id desc');
  415.         
  416.         // url but not valid
  417.         $_SERVER['REQUEST_URI''something/123/sortwrong';
  418.         $this->assertFalse($in->getSort(array('id')));
  419.  
  420.         // url but not valid
  421.         $_SERVER['REQUEST_URI''something.html?123&sortwrong';
  422.         $this->assertFalse($in->getSort(array('id')));
  423.  
  424.         // valid url
  425.         $_SERVER['REQUEST_URI''something/123/sortid';
  426.         $this->assertEqual($in->getSort(array('id'))'id asc');
  427.  
  428.         // valid url
  429.         $_SERVER['REQUEST_URI''something.html?123&sortid';
  430.         $this->assertEqual($in->getSort(array('id'))'id desc');
  431.  
  432.         // valid url
  433.         $_SERVER['REQUEST_URI''something/123/sorttxtSomething';
  434.         $this->assertEqual($in->getSort(array('id''txtSomething'))'txtSomething asc');
  435.  
  436.         // valid url
  437.         $_SERVER['REQUEST_URI''something.html?123&sorttxtSomething';
  438.         $this->assertEqual($in->getSort(array('id''txtSomething'))'txtSomething desc');
  439.  
  440.         $_SERVER['REQUEST_URI'$oldUrl;
  441.     }
  442.  
  443.     /**
  444.      * tests getPage function
  445.      */
  446.     function testGetPage({
  447.         $in new eaInput();
  448.         $opt =$in->options;
  449.         
  450.         $opt =$this->options;
  451.  
  452.         $oldUrl $_SERVER['REQUEST_URI'];
  453.         
  454.         // no prefix
  455.         unset ($_SERVER['REQUEST_URI']);
  456.         $this->assertFalse($in->getPage(''));
  457.         
  458.         // no url
  459.         $this->assertFalse($in->getPage('test'));
  460.         
  461.         // invalid url
  462.         $_SERVER['REQUEST_URI''something/123/page';
  463.         $this->assertFalse($in->getPage('test'));
  464.         
  465.         // invalid url
  466.         $_SERVER['REQUEST_URI''something.html?123&page';
  467.         $this->assertFalse($in->getPage('test'));
  468.         
  469.         // valid url
  470.         $_SERVER['REQUEST_URI''something/123/page1704';
  471.         $this->assertEqual($in->getPage('test')1704);
  472.         
  473.         // valid url
  474.         $_SERVER['REQUEST_URI''something.html?123&page1705';
  475.         $this->assertEqual($in->getPage('test')1705);
  476.         
  477.         // stored in session
  478.         $_SERVER['REQUEST_URI''something/123/page';
  479.         $this->assertEqual($in->getPage('test')1705);
  480.  
  481.         // valid url
  482.         $_SERVER['REQUEST_URI''something.html?123&page';
  483.         $this->assertEqual($in->getPage('test')1705);
  484.  
  485.         $_SERVER['REQUEST_URI'$oldUrl;
  486.     }
  487. }
  488.  
  489. ?>

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