* Copyright 2001-2012 Strangecode, LLC * * This file is part of The Strangecode Codebase. * * The Strangecode Codebase 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 3 of the License, or (at your option) * any later version. * * The Strangecode Codebase 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 * The Strangecode Codebase. If not, see . */ /** * PHPUnit test case for codebase/lib/Version.inc.php * * The method skeletons below need to be filled in with * real data so that the tests will run correctly. Replace * all EXPECTED_VAL and PARAM strings with real data. * * Created with PHPUnit_Skeleton on 2005-08-09 */ class VersionTest extends PHPUnit_Framework_TestCase { var $Version; var $Auth_SQL; function setUp() { require dirname(__FILE__) . '/_config.inc.php'; require_once '../lib/Version.inc.php'; require_once '../lib/Auth_SQL.inc.php'; $this->Auth_SQL = new Auth_SQL('testauth'); $this->Auth_SQL->setParam(array( 'db_table' => 'test_user_tbl', 'db_primary_key' => 'user_id', 'db_login_table' => 'test_login_tbl', 'login_url' => '/login.php', 'blocking' => true )); // Use fresh user table. $this->Auth_SQL->initDB(true); // Insert test data. $db =& DB::getInstance(); $db->query(" INSERT INTO test_user_tbl ( username, userpass, first_name, last_name, email ) VALUES ( 'testuser', md5('testpass'), 'John', 'Doe', 'john@example.com' ) "); $this->Auth_SQL->login('testuser', 'testpass'); $this->Version =& Version::getInstance($this->Auth_SQL); $this->Version->setParam(array('db_table' => 'test_version_tbl')); // Use fresh version table. $this->Version->initDB(true); } function tearDown() { $db =& DB::getInstance(); unset($this->Version); unset($this->Auth_SQL); $db->query("DROP TABLE IF EXISTS test_user_tbl"); $db->query("DROP TABLE IF EXISTS test_login_tbl"); $db->query("DROP TABLE IF EXISTS test_version_tbl"); } function test_getinstance() { $result = Version::getinstance($this->Auth_SQL); $this->assertTrue($result && $result instanceof Version); } function test_setparam() { $this->Version->setparam(array('var'=>'val')); } function test_getparam() { $this->Version->getparam('var'); } function test_create() { $result = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); $this->assertSame('1', $result); } function test_restore() { $current = $this->Version->getCurrent('test_user_tbl', 'user_id', '1'); $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); $versioned = $this->Version->getData($version_id); $result = $this->Version->restore($version_id); $restored = $this->Version->getCurrent('test_user_tbl', 'user_id', '1'); $this->assertTrue(is_array($result), 'Restore did not return array.'); $this->assertTrue($current === $versioned && $versioned === $restored, 'Restored version does not match original.'); } function test_deleteold() { // Creat 3 test versions. $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 1', '1 version of user'); $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 2', '2 version of user'); $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 3', '3 version of user'); $a = $this->Version->getList('test_user_tbl', 'user_id', '1'); $this->Version->deleteold('test_user_tbl', 'user_id', '1'); $b = $this->Version->getList('test_user_tbl', 'user_id', '1'); $this->Version->setparam(array('max_qty'=>2)); $this->Version->setparam(array('min_qty'=>1)); $this->Version->setparam(array('min_days'=>0)); $this->Version->deleteold('test_user_tbl', 'user_id', '1'); $c = $this->Version->getList('test_user_tbl', 'user_id', '1'); $this->Version->setparam(array('max_qty'=>0)); $this->Version->setparam(array('min_qty'=>0)); $this->Version->setparam(array('min_days'=>0)); $this->Version->deleteold('test_user_tbl', 'user_id', '1'); $d = $this->Version->getList('test_user_tbl', 'user_id', '1'); $this->assertTrue(sizeof($a) == 3, 'A is wrong'); $this->assertTrue(sizeof($b) == 3, 'B is wrong'); $this->assertTrue(sizeof($c) == 1, 'C is wrong'); $this->assertTrue(sizeof($d) == 0, 'D is wrong'); } function test_getlist() { $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 1', '1 version of user'); $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 2', '2 version of user'); $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 3', '3 version of user'); $result = $this->Version->getList('test_user_tbl', 'user_id', '1'); $this->assertTrue(is_array($result) && !empty($result)); } function test_getverson() { $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); $result = $this->Version->getverson($version_id); $this->assertTrue(is_array($result) && !empty($result)); } function test_getdata() { $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); $result = $this->Version->getdata($version_id); $this->assertTrue(is_array($result) && !empty($result)); } function test_getcurrent() { $result = $this->Version->getCurrent('test_user_tbl', 'user_id', '1'); $this->assertTrue(is_array($result) && !empty($result)); } }