Source for file input.class.php
Documentation is available at input.class.php
* test case for input wrapper
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author m2mtech <tech@m2m.at>
* @copyright 2007 m2m server software gmbh
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License Version 2
* @version $Id: input.class.php 139 2007-08-29 20:49:58Z m2mtech $
* @link http://www.ea-geier.at/
require_once('code/base/input.class.php');
* helper class - database
require_once('code/base/db.class.php');
* tast case for input wrapper
$this->UnitTestCase('Test Input Wrapper');
* does not test anything at the moment
* checks false input, and valid input, and the result
$this->assertFalse($in->createTestPattern($dsn, $pattern, $test), 'empty dsn');
$dsn = ' a:abc:abc,abc ';
$this->assertFalse($in->createTestPattern($dsn, $pattern, $test), 'wrong method');
$dsn = ' gp:wrong:abc,abc ';
$this->assertFalse($in->createTestPattern($dsn, $pattern, $test), 'wrong function');
$dsn = ' gp:abc:ab-c,abc ';
$this->assertFalse($in->createTestPattern($dsn, $pattern, $test), 'wrong prefix');
$dsn = ' gp:abc:abc,txt ';
$this->assertTrue($in->createTestPattern($dsn, $pattern, $test), 'format');
$validTest = array ('g' => array ('abc' => 'abc', 'txt' => 'abc'), 'p' => array ('abc' => 'abc', 'txt' => 'abc'));
$validPattern = array ('g' => 'abc|txt', 'p' => 'abc|txt' );
$this->assertEqual($test, $validTest, 'test array');
$this->assertEqual($pattern, $validPattern, 'test pattern');
* checks non existing, false, valid, and corrected input
$this->assertFalse($in->check($dsn), 'not existing get');
$this->assertTrue($in->check($dsn), 'integer :: alpha');
$this->assertEqual($in->error['abcTest'], 'badformat', 'error set');
$_GET['txtTest'] = 'test';
$dsn = ' g:abc:abc,txt ';
$this->assertTrue($in->check($dsn), 'alpha');
$this->assertFalse(isset ($in->error['txtTest']), 'no error');
$this->assertEqual($in->get['txtTest'], 'test', 'correct value');
$_GET['abcTest'] = 'test123';
$dsn = ' g:abc:abc,txt ';
$this->assertTrue($in->check($dsn), 'alpha');
$this->assertTrue(isset ($in->error['abcTest']), 'error set');
$this->assertEqual($in->get['abcTest'], 'test', 'correct value');
$this->assertNotEqual($in->get['abcTest'], $_GET['abcTest'], 'correct value');
$_GET['abcTest'] = array(1 => 'test123', 2 => 'test456');
$this->assertTrue($in->check($dsn));
$this->assertEqual($in->get['abcTest'], $_GET['abcTest']);
$_GET['abcTest'] = array(1 => 't<b>est123', 2 => 'test456');
$this->assertTrue($in->check($dsn));
$this->assertNotEqual($in->get['abcTest'], $_GET['abcTest']);
$this->assertTrue(isset ($in->error['abcTest'][1]));
* checks non existing, false, and valid input
$_GET['abcTest1'] = 'ok';
$_GET['abcTest2'] = 'wrong123';
$this->assertEqual($in->get('abcTest1'), 'ok', 'valid input');
$this->assertFalse($in->get('abcTest2'), 'wrong input');
$this->assertFalse($in->get('abcTest3'), 'not existing input');
unset ($_GET['abcTest1']);
unset ($_GET['abcTest2']);
* checks non existing, false, and valid input
* checks non existing, false, valid, and corrected input
$_POST['abcTest1'] = 'ok';
$_POST['abcTest2'] = 'wrong123';
$this->assertEqual($in->post('abcTest1'), 'ok', 'valid input');
$this->assertFalse($in->post('abcTest2'), 'wrong input');
$this->assertFalse($in->post('abcTest3'), 'not existing input');
unset ($_POST['abcTest1']);
unset ($_POST['abcTest2']);
* tests getClient function
* checks input from short & long urls or sessions
$oldUrl = $_SERVER['REQUEST_URI'];
$db->setReturnValue('loadClientConf', 1, array(1));
$db->setReturnValue('loadClientConf', 23, array(23));
$db->setReturnValue('loadClientConf', 123, array(123));
$opt['clients'][1] = 'dummy';
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
// no state -> force first client
$opt['clients'][2] = 'dummy';
unset ($_SESSION['client']);
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
// wrong state -> force first client
$in->get['state'] = 'teststate';
$_SERVER['REQUEST_URI'] = '/teststatewrong/23';
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
// no client set -> force first client
$_SERVER['REQUEST_URI'] = '/teststate';
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
// no client set -> force first client
$_SERVER['REQUEST_URI'] = '/teststate/';
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
// wrong client set -> force first client
$_SERVER['REQUEST_URI'] = '/teststate/23';
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
$opt['clients'][23] = 'correctclient';
$_SERVER['REQUEST_URI'] = '/teststate/23';
$this->assertEqual($in->getClient($db), 23);
$this->assertEqual($opt['client'], 23);
// client stored in session
$_SERVER['REQUEST_URI'] = '/teststate';
$this->assertEqual($in->getClient($db), 23);
$this->assertEqual($opt['client'], 23);
// client stored in session
$_SERVER['REQUEST_URI'] = '/teststate/';
$this->assertEqual($in->getClient($db), 23);
$this->assertEqual($opt['client'], 23);
$opt['clients'][123] = 'newclient';
$_SERVER['REQUEST_URI'] = '/teststate/123';
$this->assertEqual($in->getClient($db), 123);
$this->assertEqual($opt['client'], 123);
// wrong client in session -> force first client
unset ($opt['clients'][23]);
unset ($opt['clients'][123]);
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
// wrong state -> force first client
$in->get['state'] = 'teststate';
$_SERVER['REQUEST_URI'] = '/teststatewrong/23';
$_SERVER['REQUEST_URI'] = '/index.php?state=teststatewrong&23';
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
// no client set -> force first client
$_SERVER['REQUEST_URI'] = '/index.php?state=teststate';
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
// no client set -> force first client
$_SERVER['REQUEST_URI'] = '/index.php?state=teststate&';
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
// wrong client set -> force first client
$_SERVER['REQUEST_URI'] = '/index.php?state=teststate&23';
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
$opt['clients'][23] = 'correctclient';
$this->assertEqual($in->getClient($db), 23);
$this->assertEqual($opt['client'], 23);
// client stored in session
$_SERVER['REQUEST_URI'] = '/index.php?state=teststate';
$this->assertEqual($in->getClient($db), 23);
$this->assertEqual($opt['client'], 23);
// client stored in session
$_SERVER['REQUEST_URI'] = '/index.php?state=teststate&';
$this->assertEqual($in->getClient($db), 23);
$this->assertEqual($opt['client'], 23);
$opt['clients'][123] = 'newclient';
$_SERVER['REQUEST_URI'] = '/index.php?state=teststate&123';
$this->assertEqual($in->getClient($db), 123);
$this->assertEqual($opt['client'], 123);
// wrong client in session -> force first client
unset ($opt['clients'][23]);
unset ($opt['clients'][123]);
$this->assertEqual($in->getClient($db), 1);
$this->assertEqual($opt['client'], 1);
unset ($_SESSION['client']);
$_SERVER['REQUEST_URI'] = $oldUrl;
* tests setClient function
$this->assertFalse($in->setClient());
$this->assertTrue($in->setClient(123));
$this->assertEqual($opt['client'], 123);
$this->assertEqual($_SESSION['client'], 123);
unset ($_SESSION['cleint']);
$oldUrl = $_SERVER['REQUEST_URI'];
unset ($_SERVER['REQUEST_URI']);
$this->assertFalse($in->getGet('test'));
$_SERVER['REQUEST_URI'] = '';
$this->assertFalse($in->getGet('test'));
$_SERVER['REQUEST_URI'] = 'something/123/somethingelse';
$this->assertFalse($in->getGet('test'));
$_SERVER['REQUEST_URI'] = 'something.html?123&somethingelse';
$this->assertFalse($in->getGet('test'));
$_SERVER['REQUEST_URI'] = 'something/123/test1false';
$this->assertFalse($in->getGet('test'));
$_SERVER['REQUEST_URI'] = 'something.html?123&test1false';
$this->assertFalse($in->getGet('test'));
$_SERVER['REQUEST_URI'] = 'something/123/test123';
$this->assertEqual($in->getGet('test'), 123);
$_SERVER['REQUEST_URI'] = 'something.html?123&test123';
$this->assertEqual($in->getGet('test'), 123);
$_SERVER['REQUEST_URI'] = 'something/123/testabc';
$this->assertEqual($in->getGet('test', 'abc'), 'abc');
$_SERVER['REQUEST_URI'] = 'something.html?123&testabc';
$this->assertEqual($in->getGet('test', 'abc'), 'abc');
$_SERVER['REQUEST_URI'] = $oldUrl;
$oldUrl = $_SERVER['REQUEST_URI'];
unset ($_SERVER['REQUEST_URI']);
$this->assertFalse($in->getSort());
// no url & standard sort
unset ($_SESSION['sortwhat']);
unset ($_SESSION['sortdir']);
$this->assertEqual($in->getSort(array('id')), 'id desc');
$_SESSION['sortwhat'] = 'id';
$_SESSION['sortdir'] = 'desc';
$this->assertEqual($in->getSort(array('id')), 'id desc');
$_SERVER['REQUEST_URI'] = 'something/123/sortwrong';
$this->assertFalse($in->getSort(array('id')));
$_SERVER['REQUEST_URI'] = 'something.html?123&sortwrong';
$this->assertFalse($in->getSort(array('id')));
$_SERVER['REQUEST_URI'] = 'something/123/sortid';
$this->assertEqual($in->getSort(array('id')), 'id asc');
$_SERVER['REQUEST_URI'] = 'something.html?123&sortid';
$this->assertEqual($in->getSort(array('id')), 'id desc');
$_SERVER['REQUEST_URI'] = 'something/123/sorttxtSomething';
$this->assertEqual($in->getSort(array('id', 'txtSomething')), 'txtSomething asc');
$_SERVER['REQUEST_URI'] = 'something.html?123&sorttxtSomething';
$this->assertEqual($in->getSort(array('id', 'txtSomething')), 'txtSomething desc');
$_SERVER['REQUEST_URI'] = $oldUrl;
$oldUrl = $_SERVER['REQUEST_URI'];
unset ($_SERVER['REQUEST_URI']);
$this->assertFalse($in->getPage(''));
$this->assertFalse($in->getPage('test'));
$_SERVER['REQUEST_URI'] = 'something/123/page';
$this->assertFalse($in->getPage('test'));
$_SERVER['REQUEST_URI'] = 'something.html?123&page';
$this->assertFalse($in->getPage('test'));
$_SERVER['REQUEST_URI'] = 'something/123/page1704';
$this->assertEqual($in->getPage('test'), 1704);
$_SERVER['REQUEST_URI'] = 'something.html?123&page1705';
$this->assertEqual($in->getPage('test'), 1705);
$_SERVER['REQUEST_URI'] = 'something/123/page';
$this->assertEqual($in->getPage('test'), 1705);
$_SERVER['REQUEST_URI'] = 'something.html?123&page';
$this->assertEqual($in->getPage('test'), 1705);
$_SERVER['REQUEST_URI'] = $oldUrl;
|