/
TestOfBackupMySQLDAO.php
197 lines (176 loc) · 6.44 KB
/
TestOfBackupMySQLDAO.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
<?php
/**
*
* ThinkUp/tests/TestOfBackupMySQLDAO.php
*
* Copyright (c) 2009-2012 Mark Wilkie
*
* LICENSE:
*
* This file is part of ThinkUp (http://thinkupapp.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/>.
*
*
* @author Mark Wilkie <mark[at]bitterpill[dot]org>
* @license http://www.gnu.org/licenses/gpl.html
* @copyright 2009-2012 Mark Wilkie
*/
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 TestOfBackupMySQLDAO extends ThinkUpUnitTestCase {
public function setUp() {
parent::setUp();
new BackupMySQLDAO();
$this->pdo = BackupMySQLDAO::$PDO;
}
public function tearDown() {
parent::tearDown();
$zipfile = FileDataManager::getBackupPath('.htthinkup_db_backup.zip');
$backup_dir = FileDataManager::getBackupPath();
if (file_exists($zipfile)) {
unlink($zipfile);
}
if (file_exists($backup_dir)) {
$this->recursiveDelete($backup_dir);
}
}
/**
* Test constructor
*/
public function testConstructor() {
$dao = new BackupMySQLDAO();
$this->assertTrue(isset($dao));
}
/**
* test export data
*/
public function testExportData() {
$dao = new BackupMySQLDAO();
$export_file = $dao->export();
$this->assertTrue( file_exists($export_file) );
$zip_stats = stat($export_file);
$this->assertTrue($zip_stats['size'] > 0);
$za = new ZipArchive();
$za->open($export_file);
$zip_files = array();
for ($i=0; $i<$za->numFiles;$i++) {
$zfile = $za->statIndex($i);
$zip_files[$zfile['name']] = $zfile['name'];
}
//verify we have create table file
$this->assertTrue($zip_files["create_tables.sql"]);
$za->close();
$q = "show tables";
$q2 = "show create table ";
$stmt = $this->pdo->query($q);
// verify we have all table files
while($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
foreach($data as $key => $value) {
$zfile = '/' . $value .'.txt';
$this->assertTrue($zip_files[$zfile]);
}
}
}
/**
* test import data, no such file
*/
public function testImportDataNoFile() {
$dao = new BackupMySQLDAO();
$export_file = $dao->export();
$this->expectException('Exception', 'Unable to open import file: jshshsgsgs-badfile.nofile');
$dao->import('jshshsgsgs-badfile.nofile');
}
/**
* test import data, bad zip file
*/
public function testImportDataBadFile() {
$dao = new BackupMySQLDAO();
$zipfile = 'tests/data/backup/bad-zip-archive.zip';
$this->expectException('Exception', 'Unable to open import file, corrupted zip file?: ' . $zipfile);
$dao->import($zipfile);
}
/**
* test import data, good zip file, but data missing
*/
public function testImportDataBadFile2() {
$dao = new BackupMySQLDAO();
$zipfile = 'tests/data/backup/bad-zip-archive2.zip';
$this->expectException('Exception', 'Unable to open import file, corrupted zip file?: ' . $zipfile);
$dao->import($zipfile);
}
/**
* test import data
*/
public function testImportData() {
$dao = new BackupMySQLDAO();
$stmt = $this->pdo->query("show tables");
$data = $stmt->fetchAll();
$pre_import = count($data);
$export_file = $dao->export();
$this->pdo->query("drop table " . $this->table_prefix . "plugins");
$this->assertTrue( $dao->import($export_file) );
$stmt = $this->pdo->query("show create table " . $this->table_prefix . "plugins");
$data = $stmt->fetch();
$stmt->closeCursor();
$this->assertEqual($data['Table'], $this->table_prefix . 'plugins');
$stmt = $this->pdo->query("select * from " . $this->table_prefix . "plugins");
$data = $stmt->fetch();
$this->assertEqual($data['id'], 1);
$this->assertEqual($data['name'], 'Twitter');
$stmt = $this->pdo->query("show tables");
$data = $stmt->fetchAll();
$post_import = count($data);
$this->assertEqual($pre_import, $post_import);
}
/**
* test import data, drop new tables not in backup
*/
public function testImportDataDropNewTables() {
$dao = new BackupMySQLDAO();
$stmt = $this->pdo->query("show tables");
$data = $stmt->fetchAll();
$pre_import = count($data);
$export_file = $dao->export();
$this->pdo->query("drop table " . $this->table_prefix . "plugins");
$this->pdo->query("create table tu_dropme (`value` int(11) NOT NULL)");
$this->assertTrue( $dao->import($export_file) );
$stmt = $this->pdo->query("show create table " . $this->table_prefix . "plugins");
$data = $stmt->fetch();
$stmt->closeCursor();
$this->assertEqual($data['Table'], $this->table_prefix . 'plugins');
$stmt = $this->pdo->query("show tables like '%dropme'");
$data = $stmt->fetch();
$this->assertFalse($data); // table should be dropped
$stmt = $this->pdo->query("show tables");
$data = $stmt->fetchAll();
$post_import = count($data);
$this->assertEqual($pre_import, $post_import);
}
public function recursiveDelete($str){
if (is_file($str)){
if (! preg_match("MAKETHISDIRWRITABLE", $str)) {
return @unlink($str);
} else {
return true;
}
}
elseif (is_dir($str)){
$scan = glob(rtrim($str,'/').'/*');
foreach($scan as $index=>$path){
$this->recursiveDelete($path);
}
return @rmdir($str);
}
}
}