-
Notifications
You must be signed in to change notification settings - Fork 676
/
TestOfInstallerMySQLDAO.php
366 lines (317 loc) · 16.4 KB
/
TestOfInstallerMySQLDAO.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
/**
*
* ThinkUp/tests/TestOfInstallerMySQLDAO.php
*
* Copyright (c) 2009-2013 Gina Trapani, Mark Wilkie
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkup.com).
*
* ThinkUp 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.
*
* ThinkUp 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 ThinkUp. If not, see
* <http://www.gnu.org/licenses/>.
*
* Test Of Installer MySQLDAO
*
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2013 Gina Trapani, Guillaume Boudreau
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*/
require_once dirname(__FILE__).'/init.tests.php';
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
require_once THINKUP_WEBAPP_PATH.'config.inc.php';
class TestOfInstallerMySQLDAO extends ThinkUpUnitTestCase {
public function setUp() {
parent::setUp();
}
public function tearDown() {
parent::tearDown();
}
public function testCreateDatabase() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO($config_array);
//test a hairy database name with dashes to assert escaping is happening correctly
$config_array['db_name'] = 'thinkup_db_creation-test_db-yes';
//Check that the database exists before trying to create it.
$before = $this->testdb_helper->databaseExists($config_array['db_name']);
$this->assertFalse($before);
//Create the database. True on success, false on fail.
//Destroy the current correct config, replace with the test db name array in the createInstallDatabase function
Config::destroyInstance();
$create_db = $dao->createInstallDatabase($config_array);
//Check the database exists after creation.
Config::destroyInstance();
$after = $this->testdb_helper->databaseExists($config_array['db_name']);
$this->assertTrue($create_db);
$this->assertTrue($after);
//Delete the database
$this->testdb_helper->deleteDatabase($config_array['db_name']);
//Assert it no longer exists
$after = $this->testdb_helper->databaseExists($config_array['db_name']);
$this->assertFalse($after);
}
public function testConstructor() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO($config_array);
$this->assertIsA($dao, 'InstallerMySQLDAO');
}
public function testGetTables() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO($config_array);
$result = $dao->getTables();
$this->assertEqual(sizeof($result), 30);
$this->assertEqual($result[0], $config_array["table_prefix"].'encoded_locations');
}
public function testCheckTable() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO($config_array);
$result = $dao->checkTable($config_array["table_prefix"].'owners');
$this->assertTrue(array_key_exists('Msg_text', $result));
}
public function testRepairTable() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO($config_array);
$result = $dao->repairTable($config_array["table_prefix"].'owners');
$this->assertTrue(array_key_exists('Msg_text', $result));
}
public function testDescribeTable() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO($config_array);
$result = $dao->describeTable($config_array["table_prefix"].'owners');
foreach ($result as $field) {
$this->assertTrue(isset($field['Field']));
$this->assertTrue(isset($field['Type']));
$this->assertTrue(isset($field['Null']));
$this->assertTrue(isset($field['Key']));
$this->assertTrue(isset($field['Extra']));
}
}
public function testShowIndex() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO($config_array);
$result = $dao->showIndex($config_array["table_prefix"].'owners');
$this->assertIdentical("id", $result[0]['Column_name']);
$this->assertIdentical("PRIMARY", $result[0]['Key_name']);
}
public function testExamineQueries() {
// test on fully installed tables
$install_queries = file_get_contents(THINKUP_ROOT_PATH."webapp/install/sql/build-db_mysql.sql");
//clean SQL: diffDataStructure requires two spaces after PRIMARY KEY, and a space between key name and (field)
$install_queries = str_replace('PRIMARY KEY (', 'PRIMARY KEY (', $install_queries);
// test on complete table set; this should return just the INSERT query into plugins table
$config = Config::getInstance();
$config_array = $config->getValuesArray();
if ($config_array['table_prefix'] != 'tu_') {
$install_queries = str_replace('tu_', $config_array['table_prefix'], $install_queries);
}
$dao = new InstallerMySQLDAO($config_array);
$output = $dao->diffDataStructure($install_queries, $dao->getTables());
$this->assertEqual(sizeof($output['for_update']), 0 );
//var_dump($output);
$expected = "/INSERT INTO ".$config_array["table_prefix"]."options/i";
$this->assertPattern( $expected, $output['queries'][0] );
$expected = "/INSERT INTO ".$config_array["table_prefix"]."plugins/i";
$this->assertPattern( $expected, $output['queries'][1] );
// test on missing tables
InstallerMySQLDAO::$PDO->exec("DROP TABLE " . $config_array["table_prefix"] . "owners");
$output = $dao->diffDataStructure($install_queries, $dao->getTables());
$expected = "/Created table {$config_array["table_prefix"]}owners/i";
$this->assertPattern($expected, $output['for_update'][$config_array["table_prefix"] . 'owners']);
$expected = "/CREATE TABLE {$config_array["table_prefix"]}owners /i";
$this->assertPattern($expected, $output['queries'][$config_array["table_prefix"] . 'owners']);
// test on missing PRIMARY KEY
InstallerMySQLDAO::$PDO->exec("ALTER TABLE " . $config_array["table_prefix"] .
"follows DROP KEY network_follower_user");
$tables = $dao->getTables();
//var_dump($tables);
$output = $dao->diffDataStructure($install_queries, $tables);
$add_pk = "ALTER TABLE " . $config_array["table_prefix"] .
"follows ADD UNIQUE KEY network_follower_user (network,follower_id,user_id)";
$this->assertTrue(in_array($add_pk, $output['queries']));
// test on missing index
InstallerMySQLDAO::$PDO->exec("ALTER TABLE ".$config_array["table_prefix"]."follows DROP INDEX active");
$output = $dao->diffDataStructure($install_queries, $dao->getTables(false));
$add_idx = "ALTER TABLE ".$config_array["table_prefix"]."follows ADD KEY active (network,active,last_seen)";
$this->assertTrue(in_array($add_idx, $output['queries']));
// test on missing column
InstallerMySQLDAO::$PDO->exec("ALTER TABLE ".$config_array["table_prefix"]."posts DROP place");
$output = $dao->diffDataStructure($install_queries, $dao->getTables(false));
$regex = "/ALTER TABLE ".$config_array["table_prefix"]."posts ADD COLUMN place varchar\(255\) DEFAULT NULL/i";
$this->assertPattern($regex, $output['queries'][2]);
}
public function testNeedsSnowflakeUpgrade() {
$dao = new InstallerMySQLDAO();
$this->assertFalse($dao->needsSnowflakeUpgrade());
$this->testdb_helper->runSQL('ALTER TABLE #prefix#posts CHANGE post_id post_id bigint(11) NOT NULL;');
$this->assertTrue($dao->needsSnowflakeUpgrade());
}
public function testRunMigration() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO();
$this->testdb_helper->runSQL('CREATE TABLE `tu_test2` (`value` int(11) NOT NULL)');
//errors
try {
$dao->runMigrationSQL('bad sql');
$this->fail('should throw exception');
} catch (Exception $e) {
$this->assertTrue(true, 'should throw exception');
}
try {
$dao->runMigrationSQL('insert into tu_test2 (valuee) VALUES ("a")');
$this->fail('should throw exception');
} catch (Exception $e) {
$this->assertTrue(true, 'should throw exception');
}
// old migration
$dao->runMigrationSQL('insert into tu_test2 (value) VALUES (1)');
$stmt = InstallerMySQLDAO::$PDO->query('select * from tu_test2');
$data = $stmt->fetchAll();
$this->assertEqual(1, $data[0]['value']);
//old migration multiple queries
$dao->runMigrationSQL("delete from tu_test2;" .
"insert into tu_test2 (value) VALUES (2);" .
"insert into tu_test2 (value) VALUES (3)");
$stmt = InstallerMySQLDAO::$PDO->query('select * from tu_test2');
$data = $stmt->fetchAll();
$this->assertEqual(2, $data[0]['value']);
$this->assertEqual(3, $data[1]['value']);
}
public function testRunNewMigrationNoTable() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO();
$this->testdb_helper->runSQL('CREATE TABLE `tu_test2` (`value` int(11) NOT NULL)');
$this->testdb_helper->runSQL('DROP TABLE IF EXISTS #prefix#completed_migrations');
$dao->runMigrationSQL("insert into tu_test2 (value) VALUES (2);", true, $filename = 'a_file');
// tu_completed_migrations table should now exists
$stmt = InstallerMySQLDAO::$PDO->query("SHOW TABLES LIKE '" . $config_array['table_prefix'] .
"completed_migrations'");
$data = $stmt->fetchAll();
$this->assertEqual(1, count($data));
$this->assertEqual($data[0][0], $config_array['table_prefix'] . "completed_migrations");
// migration should have run
$stmt = InstallerMySQLDAO::$PDO->query("select * from tu_test2");
$data = $stmt->fetchAll();
$this->assertEqual(2, $data[0]['value']);
// tu_completed_migrations table should contan a record for our latest migration
$stmt = InstallerMySQLDAO::$PDO->query("select * from " . $config_array['table_prefix'] .
"completed_migrations");
$data = $stmt->fetchAll();
$this->assertEqual($data[0]['migration'], 'a_file-0');
}
public function testRunNewMigrationsTwice() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO();
$this->testdb_helper->runSQL('CREATE TABLE `tu_test2` (`value` int(11) NOT NULL)');
$this->testdb_helper->runSQL('DROP TABLE IF EXISTS #prefix#completed_migrations');
$dao->runMigrationSQL("insert into tu_test2 (value) VALUES (2);", true, $filename = 'a_file');
// first migration should have run
$stmt = InstallerMySQLDAO::$PDO->query("select * from tu_test2");
$data = $stmt->fetchAll();
$this->assertEqual(count($data), 1);
$this->assertEqual(2, $data[0]['value']);
// tu_completed_migrations table should contain a record for our latest migration
$stmt = InstallerMySQLDAO::$PDO->query("select * from " . $config_array['table_prefix'] .
"completed_migrations");
$data = $stmt->fetchAll();
$this->assertEqual(count($data), 1);
$this->assertEqual($data[0]['migration'], 'a_file-0');
$dao->runMigrationSQL("insert into tu_test2 (value) VALUES (2);" .
"insert into tu_test2 (value) VALUES (3);" .
"insert into tu_test2 (value) VALUES (4);",
true, $filename = 'a_file');
// migration should have run
$stmt = InstallerMySQLDAO::$PDO->query("select * from tu_test2");
$data = $stmt->fetchAll();
$this->assertEqual(count($data), 3);
$this->assertEqual(2, $data[0]['value']);
$this->assertEqual(3, $data[1]['value']);
$this->assertEqual(4, $data[2]['value']);
// tu_completed_migrations table should contain a record for our latest migration
$stmt = InstallerMySQLDAO::$PDO->query("select * from " . $config_array['table_prefix'] .
"completed_migrations");
$data = $stmt->fetchAll();
$this->assertEqual(count($data), 3);
$this->assertEqual($data[0]['migration'], 'a_file-0');
$this->assertEqual($data[1]['migration'], 'a_file-1');
$this->assertEqual($data[2]['migration'], 'a_file-2');
}
public function testRunNewMigrationsSkipIfExists() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO();
$this->testdb_helper->runSQL('CREATE TABLE `tu_test2` (`value` int(11) NOT NULL)');
$this->testdb_helper->runSQL('DROP TABLE IF EXISTS #prefix#completed_migrations');
$dao->runMigrationSQL("insert into tu_test2 (value) VALUES (2);" .
"insert into tu_test2 (value) VALUES (3);" .
"DROP TABLE IF EXISTS tu_users_b16;" .
"insert into tu_test2 (value) VALUES (4);" .
"insert into tu_test2 (value) VALUES (5);",
true, $filename = 'a_file');
// migration should have run
$stmt = InstallerMySQLDAO::$PDO->query("select * from tu_test2");
$data = $stmt->fetchAll();
$this->assertEqual(count($data), 4);
$this->assertEqual(2, $data[0]['value']);
$this->assertEqual(3, $data[1]['value']);
$this->assertEqual(4, $data[2]['value']);
$this->assertEqual(5, $data[3]['value']);
// tu_completed_migrations table should contain a record for our latest migration
$stmt = InstallerMySQLDAO::$PDO->query("select * from " . $config_array['table_prefix'] .
"completed_migrations");
$data = $stmt->fetchAll();
$this->assertEqual(count($data), 4);
$this->assertEqual($data[0]['migration'], 'a_file-0');
$this->assertEqual($data[1]['migration'], 'a_file-1');
$this->assertEqual($data[2]['migration'], 'a_file-2');
$this->assertEqual($data[3]['migration'], 'a_file-3');
}
public function testRunNewMigrationStripsVersionForStorage() {
$config = Config::getInstance();
$config_array = $config->getValuesArray();
$dao = new InstallerMySQLDAO();
$this->testdb_helper->runSQL('CREATE TABLE `tu_test2` (`value` int(11) NOT NULL)');
$this->testdb_helper->runSQL('DROP TABLE IF EXISTS #prefix#completed_migrations');
$dao->runMigrationSQL("insert into tu_test2 (value) VALUES (2);" .
"insert into tu_test2 (value) VALUES (3);" .
"DROP TABLE IF EXISTS tu_users_b16;" .
"insert into tu_test2 (value) VALUES (4);" .
"insert into tu_test2 (value) VALUES (5);",
true, $filename = 'a_file_v0.13.sql');
// migration should have run
$stmt = InstallerMySQLDAO::$PDO->query("select * from tu_test2");
$data = $stmt->fetchAll();
$this->assertEqual(count($data), 4);
$this->assertEqual(2, $data[0]['value']);
$this->assertEqual(3, $data[1]['value']);
$this->assertEqual(4, $data[2]['value']);
$this->assertEqual(5, $data[3]['value']);
// tu_completed_migrations table should contain a record for our latest migration
$stmt = InstallerMySQLDAO::$PDO->query("select * from " . $config_array['table_prefix'] .
"completed_migrations");
$data = $stmt->fetchAll();
$this->assertEqual(count($data), 4);
$this->assertEqual($data[0]['migration'], 'a_file-0');
$this->assertEqual($data[1]['migration'], 'a_file-1');
$this->assertEqual($data[2]['migration'], 'a_file-2');
$this->assertEqual($data[3]['migration'], 'a_file-3');
}
}