source: trunk/tests/VersionTest.php @ 357

Last change on this file since 357 was 357, checked in by quinn, 15 years ago

updated tests to work. updated email validation regex to include quote marks around name part. changed logmsg tmp dir name to use script_filename.

File size: 5.9 KB
Line 
1<?php
2/**
3 * PHPUnit test case for Version
4 *
5 * The method skeletons below need to be filled in with
6 * real data so that the tests will run correctly. Replace
7 * all EXPECTED_VAL and PARAM strings with real data.
8 *
9 * Created with PHPUnit_Skeleton on 2005-08-09
10 */
11require_once 'PHPUnit.php';
12class VersionTest extends PHPUnit_TestCase {
13
14    var $Version;
15    var $Auth_SQL;
16
17    function VersionTest($name)
18    {
19        $this->PHPUnit_TestCase($name);
20    }
21
22    function setUp()
23    {   
24        require dirname(__FILE__) . '/_config.inc.php';
25        require_once '../lib/Version.inc.php';
26
27        require_once '../lib/Auth_SQL.inc.php';
28        $this->Auth_SQL =& new Auth_SQL('testauth');
29        $this->Auth_SQL->setParam(array(
30            'db_table'          => 'test_user_tbl',
31            'db_primary_key'    => 'user_id',
32            'db_login_table'    => 'test_login_tbl',
33            'login_url'         => '/login.php',
34            'blocking'          => true
35        ));
36
37        // Use fresh user table.
38        $this->Auth_SQL->initDB(true);
39
40        // Insert test data.
41        $db =& DB::getInstance();
42        $db->query("
43            INSERT INTO test_user_tbl (
44                username,
45                userpass,
46                first_name,
47                last_name,
48                email
49            ) VALUES (
50                'testuser',
51                md5('testpass'),
52                'John',
53                'Doe',
54                'john@example.com'
55            )
56        ");
57
58        $this->Auth_SQL->login('testuser', 'testpass');
59
60        $this->Version =& Version::getInstance($this->Auth_SQL);
61        $this->Version->setParam(array('db_table' => 'test_version_tbl'));
62
63        // Use fresh version table.
64        $this->Version->initDB(true);
65    }
66
67    function tearDown()
68    {
69        $db =& DB::getInstance();
70   
71        unset($this->Version);
72        unset($this->Auth_SQL);
73        $db->query("DROP TABLE IF EXISTS test_user_tbl");
74        $db->query("DROP TABLE IF EXISTS test_login_tbl");
75        $db->query("DROP TABLE IF EXISTS test_version_tbl");
76    }
77
78    function test_getinstance()
79    {
80        $result = Version::getinstance($this->Auth_SQL);
81        $this->assertTrue($result && is_a($result, 'Version'));
82    }
83
84    function test_setparam()
85    {
86        $this->Version->setparam(array('var'=>'val'));
87    }
88
89    function test_getparam()
90    {
91        $this->Version->getparam('var');
92    }
93
94    function test_create()
95    {
96        $result = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user');
97        $this->assertTrue($result);
98    }
99
100    function test_restore()
101    {
102        $current = $this->Version->getCurrent('test_user_tbl', 'user_id', '1');
103        $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user');
104        $versioned = $this->Version->getData($version_id);
105        $result = $this->Version->restore($version_id);
106        $restored = $this->Version->getCurrent('test_user_tbl', 'user_id', '1');
107        $this->assertTrue(is_array($result), 'Restore did not return array.');
108        $this->assertTrue($current === $versioned && $versioned === $restored, 'Restored version does not match original.');
109    }
110
111    function test_deleteold()
112    {
113        // Creat 3 test versions.
114        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 1', '1 version of user');
115        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 2', '2 version of user');
116        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 3', '3 version of user');
117
118        $a = $this->Version->getList('test_user_tbl', 'user_id', '1');
119
120        $this->Version->deleteold('test_user_tbl', 'user_id', '1');
121        $b = $this->Version->getList('test_user_tbl', 'user_id', '1');
122
123        $this->Version->setparam(array('max_qty'=>2));
124        $this->Version->setparam(array('min_qty'=>1));
125        $this->Version->setparam(array('min_days'=>0));
126        $this->Version->deleteold('test_user_tbl', 'user_id', '1');
127        $c = $this->Version->getList('test_user_tbl', 'user_id', '1');
128
129        $this->Version->setparam(array('max_qty'=>0));
130        $this->Version->setparam(array('min_qty'=>0));
131        $this->Version->setparam(array('min_days'=>0));
132        $this->Version->deleteold('test_user_tbl', 'user_id', '1');
133        $d = $this->Version->getList('test_user_tbl', 'user_id', '1');
134
135        $this->assertTrue(sizeof($a) == 3, 'A is wrong');
136        $this->assertTrue(sizeof($b) == 3, 'B is wrong');
137        $this->assertTrue(sizeof($c) == 1, 'C is wrong');
138        $this->assertTrue(sizeof($d) == 0, 'D is wrong');
139    }
140
141    function test_getlist()
142    {
143        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 1', '1 version of user');
144        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 2', '2 version of user');
145        $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 3', '3 version of user');
146        $result = $this->Version->getList('test_user_tbl', 'user_id', '1');
147        $this->assertTrue(is_array($result) && !empty($result));
148    }
149
150    function test_getverson()
151    {
152        $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user');
153        $result = $this->Version->getverson($version_id);
154        $this->assertTrue(is_array($result) && !empty($result));
155    }
156
157    function test_getdata()
158    {
159        $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user');
160        $result = $this->Version->getdata($version_id);
161        $this->assertTrue(is_array($result) && !empty($result));
162    }
163
164    function test_getcurrent()
165    {
166        $result = $this->Version->getCurrent('test_user_tbl', 'user_id', '1');
167        $this->assertTrue(is_array($result) && !empty($result));
168    }
169
170}
171// Running the test.
172$suite = new PHPUnit_TestSuite('VersionTest');
173$result = PHPUnit::run($suite);
174echo $result->toString();
175?>
Note: See TracBrowser for help on using the repository browser.