Skip to content

Commit

Permalink
Test backup/restore of individual users/applications.
Browse files Browse the repository at this point in the history
  • Loading branch information
yunosh committed May 16, 2017
1 parent 42a6c99 commit 5ee8813
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 56 deletions.
1 change: 1 addition & 0 deletions framework/Backup/lib/Horde/Backup/Writer.php
Expand Up @@ -13,6 +13,7 @@

namespace Horde\Backup;

use Horde_Compress_Tar as Tar;
use Horde_Compress_Zip as Zip;
use Horde_Pack_Driver_Json as Json;
use Horde\Backup;
Expand Down
210 changes: 158 additions & 52 deletions framework/Backup/test/Horde/Backup/BackupRestoreTest.php
Expand Up @@ -15,6 +15,7 @@
namespace Horde\Backup;

use Horde_Test_Case as TestCase;
use Horde\Backup;
use Horde\Backup\Writer;
use Horde\Backup\Stub;

Expand Down Expand Up @@ -52,42 +53,155 @@ public function tearDown()

public function testBackupMultipleUsers()
{
$this->_createBackup();
$backup = new Writer($this->_temp);
$application = new Stub\Application();
$application2 = new Stub\Application2();
$backup->backup('calendar', $application->backup());
$backup->backup('addressbook', $application2->backup());
$backup->save();
$this->assertFileExists($this->_temp . '/john.zip');
$this->assertFileExists($this->_temp . '/jane.zip');
$this->_clean = false;
return $this->_temp;
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);
$users = iterator_to_array($backup->listBackups());
sort($users);
$this->assertEquals(
array($this->_temp . '/jane.zip', $this->_temp . '/john.zip'),
$users
);
$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(2, $data);
$this->assertArrayHasKey('calendar', $data);
$this->assertArrayHasKey('addressbook', $data);
$this->assertCount(count($applications), $data);
foreach (array_keys($applications) as $application) {
$this->assertArrayHasKey($application, $data);
}
$matrix = array();
$applications = array(
'calendar' => new Stub\Application(),
'addressbook' => new Stub\Application2()
);
foreach ($data as $application => $collections) {
foreach ($collections as $collection) {
$user = $collection->getUser();
Expand All @@ -99,37 +213,29 @@ public function testRestoreMultipleUsers($temp)
}
}
ksort($matrix);
$this->assertEquals(
array(
'jane' => array(
'calendar' => array(
'events' => true
),
'addressbook' => array(
'addressbooks' => true,
'contacts' => true,
),
$expected = array(
'jane' => array(
'calendar' => array(
'events' => true
),
'john' => array(
'calendar' => array(
'events' => true,
'calendars' => true,
),
'addressbook' => array(
'addressbooks' => true,
'contacts' => true,
),
),
'john' => array(
'calendar' => array(
'events' => true,
'calendars' => true,
),
),
$matrix
);
}

public function testBackupSingleUser()
{
}

public function testBackupSingleApplication()
{
}

public function testBackupToTar()
{
if (!isset($applications['addressbook'])) {
unset($expected['jane']['addressbook']);
}
if (!in_array('john', $users)) {
unset($expected['john']);
}
$this->assertEquals($expected, $matrix);
}
}
10 changes: 6 additions & 4 deletions framework/Backup/test/Horde/Backup/Stub/Application.php
Expand Up @@ -78,10 +78,12 @@ public function backup(array $users = array())
public function getUserBackup($user)
{
$backup = new User($user);
foreach ($this->userData[$user] as $type => $data) {
$backup->collections[] = new Collection(
new ArrayIterator($data), $user, $type
);
if (isset($this->userData[$user])) {
foreach ($this->userData[$user] as $type => $data) {
$backup->collections[] = new Collection(
new ArrayIterator($data), $user, $type
);
}
}
return $backup;
}
Expand Down

0 comments on commit 5ee8813

Please sign in to comment.