/
BackupRestoreTest.php
241 lines (226 loc) · 6.39 KB
/
BackupRestoreTest.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
<?php
/**
* Copyright 2017 Horde LLC (http://www.horde.org/)
*
* See the enclosed file LICENSE for license information (BSD). If you
* did not receive this file, see http://www.horde.org/licenses/bsd.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/bsd BSD
* @package Backup
* @subpackage UnitTests
*/
namespace Horde\Backup;
use Horde_Test_Case as TestCase;
use Horde\Backup;
use Horde\Backup\Writer;
use Horde\Backup\Stub;
/**
* Testing backing up to and restoring from archives.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @copyright 2017 Horde LLC
* @license http://www.horde.org/licenses/bsd BSD
* @package Backup
* @subpackage UnitTests
*/
class BackupRestoreTest extends TestCase
{
protected $_temp;
protected $_clean = true;
protected function _createBackup()
{
$this->_temp = sys_get_temp_dir() . '/' . uniqid('horde_backup_');
mkdir($this->_temp);
}
public function tearDown()
{
if (!is_dir($this->_temp) || !$this->_clean) {
return;
}
foreach (glob($this->_temp . '/*') as $file) {
unlink($file);
}
rmdir($this->_temp);
}
public function testBackupMultipleUsers()
{
return $this->_backupTest(
array(
'calendar' => new Stub\Application(),
'addressbook' => new Stub\Application2()
),
null,
array('john', 'jane')
);
}
/**
* @depends testBackupMultipleUsers
*/
public function testRestoreMultipleUsers($temp)
{
$this->_restoreTest(
$temp,
array(
'calendar' => new Stub\Application(),
'addressbook' => new Stub\Application2()
),
array('jane', 'john')
);
}
public function testBackupSingleUser()
{
return $this->_backupTest(
array(
'calendar' => new Stub\Application(),
'addressbook' => new Stub\Application2()
),
array('jane'),
array('jane')
);
}
/**
* @depends testBackupSingleUser
*/
public function testRestoreSingleUser($temp)
{
$this->_restoreTest(
$temp,
array(
'calendar' => new Stub\Application(),
'addressbook' => new Stub\Application2()
),
array('jane')
);
}
public function testBackupSingleApplication()
{
return $this->_backupTest(
array(
'calendar' => new Stub\Application(),
),
null,
array('john', 'jane')
);
}
/**
* @depends testBackupSingleApplication
*/
public function testRestoreSingleApplication($temp)
{
$this->_restoreTest(
$temp,
array(
'calendar' => new Stub\Application(),
),
array('jane', 'john')
);
}
public function testBackupToTar()
{
return $this->_backupTest(
array(
'calendar' => new Stub\Application(),
'addressbook' => new Stub\Application2()
),
null,
array('john', 'jane'),
Backup::FORMAT_TAR
);
}
/**
* @depends testBackupToTar
*/
public function testRestoreFromTar($temp)
{
$this->markTestIncomplete();
$this->_restoreTest(
$temp,
array(
'calendar' => new Stub\Application(),
'addressbook' => new Stub\Application2()
),
array('jane', 'john'),
Backup::FORMAT_TAR
);
}
protected function _backupTest(
$applications, $backupUsers, $users, $format = Backup::FORMAT_ZIP
)
{
$this->_createBackup();
$backup = new Writer($this->_temp);
foreach ($applications as $application => $instance) {
$backup->backup($application, $instance->backup($users));
}
$backup->save($format);
foreach ($users as $user) {
$this->assertFileExists(
$this->_temp . '/' . $user
. ($format == Backup::FORMAT_ZIP ? '.zip' : '.tar')
);
}
$this->_clean = false;
return $this->_temp;
}
protected function _restoreTest(
$temp, $applications, $users, $format = Backup::FORMAT_ZIP
)
{
$this->_clean = true;
$this->_temp = $temp;
$backup = new Reader($this->_temp);
$backups = iterator_to_array($backup->listBackups());
foreach ($users as $user) {
$this->assertContains(
$this->_temp . '/' . $user
. ($format == Backup::FORMAT_ZIP ? '.zip' : '.tar'),
$backups
);
}
$data = $backup->restore();
$this->assertInternalType('array', $data);
$this->assertCount(count($applications), $data);
foreach (array_keys($applications) as $application) {
$this->assertArrayHasKey($application, $data);
}
$matrix = array();
foreach ($data as $application => $collections) {
foreach ($collections as $collection) {
$user = $collection->getUser();
$type = $collection->getType();
$matrix[$user][$application][$type] = true;
$this->assertTrue(
$applications[$application]->restore($collection)
);
}
}
ksort($matrix);
$expected = array(
'jane' => array(
'calendar' => array(
'events' => true
),
'addressbook' => array(
'addressbooks' => true,
'contacts' => true,
),
),
'john' => array(
'calendar' => array(
'events' => true,
'calendars' => true,
),
),
);
if (!isset($applications['addressbook'])) {
unset($expected['jane']['addressbook']);
}
if (!in_array('john', $users)) {
unset($expected['john']);
}
$this->assertEquals($expected, $matrix);
}
}