source: trunk/tests/RecordVersionTest.php @ 1

Last change on this file since 1 was 1, checked in by scdev, 19 years ago

Initial import.

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