1 | <?php |
---|
2 | /** |
---|
3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
5 | * Copyright 2001-2012 Strangecode, LLC |
---|
6 | * |
---|
7 | * This file is part of The Strangecode Codebase. |
---|
8 | * |
---|
9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License as published by the |
---|
11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
12 | * any later version. |
---|
13 | * |
---|
14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
17 | * details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License along with |
---|
20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | /** |
---|
24 | * PHPUnit test case for codebase/lib/Version.inc.php |
---|
25 | * |
---|
26 | * The method skeletons below need to be filled in with |
---|
27 | * real data so that the tests will run correctly. Replace |
---|
28 | * all EXPECTED_VAL and PARAM strings with real data. |
---|
29 | * |
---|
30 | * Created with PHPUnit_Skeleton on 2005-08-09 |
---|
31 | */ |
---|
32 | |
---|
33 | class VersionTest extends PHPUnit_Framework_TestCase { |
---|
34 | |
---|
35 | var $Version; |
---|
36 | var $Auth_SQL; |
---|
37 | |
---|
38 | function setUp() |
---|
39 | { |
---|
40 | require dirname(__FILE__) . '/_config.inc.php'; |
---|
41 | require_once '../lib/Version.inc.php'; |
---|
42 | |
---|
43 | require_once '../lib/Auth_SQL.inc.php'; |
---|
44 | $this->Auth_SQL = new Auth_SQL('testauth'); |
---|
45 | $this->Auth_SQL->setParam(array( |
---|
46 | 'db_table' => 'test_user_tbl', |
---|
47 | 'db_primary_key' => 'user_id', |
---|
48 | 'db_login_table' => 'test_login_tbl', |
---|
49 | 'login_url' => '/login.php', |
---|
50 | 'blocking' => true |
---|
51 | )); |
---|
52 | |
---|
53 | // Use fresh user table. |
---|
54 | $this->Auth_SQL->initDB(true); |
---|
55 | |
---|
56 | // Insert test data. |
---|
57 | $db =& DB::getInstance(); |
---|
58 | $db->query(" |
---|
59 | INSERT INTO test_user_tbl ( |
---|
60 | username, |
---|
61 | userpass, |
---|
62 | first_name, |
---|
63 | last_name, |
---|
64 | email |
---|
65 | ) VALUES ( |
---|
66 | 'testuser', |
---|
67 | md5('testpass'), |
---|
68 | 'John', |
---|
69 | 'Doe', |
---|
70 | 'john@example.com' |
---|
71 | ) |
---|
72 | "); |
---|
73 | |
---|
74 | $this->Auth_SQL->login('testuser', 'testpass'); |
---|
75 | |
---|
76 | $this->Version =& Version::getInstance($this->Auth_SQL); |
---|
77 | $this->Version->setParam(array('db_table' => 'test_version_tbl')); |
---|
78 | |
---|
79 | // Use fresh version table. |
---|
80 | $this->Version->initDB(true); |
---|
81 | } |
---|
82 | |
---|
83 | function tearDown() |
---|
84 | { |
---|
85 | $db =& DB::getInstance(); |
---|
86 | |
---|
87 | unset($this->Version); |
---|
88 | unset($this->Auth_SQL); |
---|
89 | $db->query("DROP TABLE IF EXISTS test_user_tbl"); |
---|
90 | $db->query("DROP TABLE IF EXISTS test_login_tbl"); |
---|
91 | $db->query("DROP TABLE IF EXISTS test_version_tbl"); |
---|
92 | } |
---|
93 | |
---|
94 | function test_getinstance() |
---|
95 | { |
---|
96 | $result = Version::getinstance($this->Auth_SQL); |
---|
97 | $this->assertTrue($result && is_a($result, 'Version')); |
---|
98 | } |
---|
99 | |
---|
100 | function test_setparam() |
---|
101 | { |
---|
102 | $this->Version->setparam(array('var'=>'val')); |
---|
103 | } |
---|
104 | |
---|
105 | function test_getparam() |
---|
106 | { |
---|
107 | $this->Version->getparam('var'); |
---|
108 | } |
---|
109 | |
---|
110 | function test_create() |
---|
111 | { |
---|
112 | $result = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); |
---|
113 | $this->assertSame(1, $result); |
---|
114 | } |
---|
115 | |
---|
116 | function test_restore() |
---|
117 | { |
---|
118 | $current = $this->Version->getCurrent('test_user_tbl', 'user_id', '1'); |
---|
119 | $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); |
---|
120 | $versioned = $this->Version->getData($version_id); |
---|
121 | $result = $this->Version->restore($version_id); |
---|
122 | $restored = $this->Version->getCurrent('test_user_tbl', 'user_id', '1'); |
---|
123 | $this->assertTrue(is_array($result), 'Restore did not return array.'); |
---|
124 | $this->assertTrue($current === $versioned && $versioned === $restored, 'Restored version does not match original.'); |
---|
125 | } |
---|
126 | |
---|
127 | function test_deleteold() |
---|
128 | { |
---|
129 | // Creat 3 test versions. |
---|
130 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 1', '1 version of user'); |
---|
131 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 2', '2 version of user'); |
---|
132 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 3', '3 version of user'); |
---|
133 | |
---|
134 | $a = $this->Version->getList('test_user_tbl', 'user_id', '1'); |
---|
135 | |
---|
136 | $this->Version->deleteold('test_user_tbl', 'user_id', '1'); |
---|
137 | $b = $this->Version->getList('test_user_tbl', 'user_id', '1'); |
---|
138 | |
---|
139 | $this->Version->setparam(array('max_qty'=>2)); |
---|
140 | $this->Version->setparam(array('min_qty'=>1)); |
---|
141 | $this->Version->setparam(array('min_days'=>0)); |
---|
142 | $this->Version->deleteold('test_user_tbl', 'user_id', '1'); |
---|
143 | $c = $this->Version->getList('test_user_tbl', 'user_id', '1'); |
---|
144 | |
---|
145 | $this->Version->setparam(array('max_qty'=>0)); |
---|
146 | $this->Version->setparam(array('min_qty'=>0)); |
---|
147 | $this->Version->setparam(array('min_days'=>0)); |
---|
148 | $this->Version->deleteold('test_user_tbl', 'user_id', '1'); |
---|
149 | $d = $this->Version->getList('test_user_tbl', 'user_id', '1'); |
---|
150 | |
---|
151 | $this->assertTrue(sizeof($a) == 3, 'A is wrong'); |
---|
152 | $this->assertTrue(sizeof($b) == 3, 'B is wrong'); |
---|
153 | $this->assertTrue(sizeof($c) == 1, 'C is wrong'); |
---|
154 | $this->assertTrue(sizeof($d) == 0, 'D is wrong'); |
---|
155 | } |
---|
156 | |
---|
157 | function test_getlist() |
---|
158 | { |
---|
159 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 1', '1 version of user'); |
---|
160 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 2', '2 version of user'); |
---|
161 | $this->Version->create('test_user_tbl', 'user_id', '1', 'Test 3', '3 version of user'); |
---|
162 | $result = $this->Version->getList('test_user_tbl', 'user_id', '1'); |
---|
163 | $this->assertTrue(is_array($result) && !empty($result)); |
---|
164 | } |
---|
165 | |
---|
166 | function test_getverson() |
---|
167 | { |
---|
168 | $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); |
---|
169 | $result = $this->Version->getverson($version_id); |
---|
170 | $this->assertTrue(is_array($result) && !empty($result)); |
---|
171 | } |
---|
172 | |
---|
173 | function test_getdata() |
---|
174 | { |
---|
175 | $version_id = $this->Version->create('test_user_tbl', 'user_id', '1', 'Test User', 'First version of user'); |
---|
176 | $result = $this->Version->getdata($version_id); |
---|
177 | $this->assertTrue(is_array($result) && !empty($result)); |
---|
178 | } |
---|
179 | |
---|
180 | function test_getcurrent() |
---|
181 | { |
---|
182 | $result = $this->Version->getCurrent('test_user_tbl', 'user_id', '1'); |
---|
183 | $this->assertTrue(is_array($result) && !empty($result)); |
---|
184 | } |
---|
185 | |
---|
186 | } |
---|