diff --git a/framework/Backup/lib/Horde/Backup/Writer.php b/framework/Backup/lib/Horde/Backup/Writer.php index f70f5005f1b..59f5f9e6b72 100644 --- a/framework/Backup/lib/Horde/Backup/Writer.php +++ b/framework/Backup/lib/Horde/Backup/Writer.php @@ -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; diff --git a/framework/Backup/test/Horde/Backup/BackupRestoreTest.php b/framework/Backup/test/Horde/Backup/BackupRestoreTest.php index 7b1dd889aba..754881b1182 100644 --- a/framework/Backup/test/Horde/Backup/BackupRestoreTest.php +++ b/framework/Backup/test/Horde/Backup/BackupRestoreTest.php @@ -15,6 +15,7 @@ namespace Horde\Backup; use Horde_Test_Case as TestCase; +use Horde\Backup; use Horde\Backup\Writer; use Horde\Backup\Stub; @@ -52,17 +53,14 @@ 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') + ); } /** @@ -70,24 +68,140 @@ public function 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(); @@ -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); } } diff --git a/framework/Backup/test/Horde/Backup/Stub/Application.php b/framework/Backup/test/Horde/Backup/Stub/Application.php index ce4b048455f..8cd34602f48 100644 --- a/framework/Backup/test/Horde/Backup/Stub/Application.php +++ b/framework/Backup/test/Horde/Backup/Stub/Application.php @@ -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; }