backup_migrate_prune.test in Backup and migrate prune 7.2
Same filename and directory in other branches
Test implementation file
File
backup_migrate_prune.testView source
<?php
/**
* @file
* Test implementation file
*/
/**
* BackupMigratePruneTestCase class for unit testing
*/
class BackupMigratePruneTestCase extends DrupalUnitTestCase {
/**
* The first date for fake backups.
*/
const DATEINIT = '2010-10-01 12:00:00';
/**
* The last date for fake backups.
*/
const DATEEND = '2012-10-31 12:00:00';
/**
* The interval that separates each time increment.
*/
const TIMEINTERVAL = 'PT12H';
/**
* @return array
* Descriptive information for Simpletest page
*/
public function getInfo() {
return array(
'name' => 'Backup & Migrate Prune unit tests',
'description' => 'Tests implementation of Gardener class.',
'group' => 'Backup & Migrate Prune',
);
}
/**
* Test Gardener::arrangeFiles() method
* @see Gardener::arrangeFiles()
*/
public function testGardenerArrangeFiles() {
$gardener = new GardenerFake();
$gardener
->setDateReference(BackupMigratePruneTestCase::DATEEND);
$files = $gardener
->arrangeFiles();
$this
->assertEqual(count($files), 4, 'There are 4 time slots.');
foreach ($files as $time_slot => $fileset) {
switch ($time_slot) {
case 'thisweek_slot':
$this
->assertEqual(count($fileset), 6, 'There are 6 days (7 minus today) with backups this week.');
break;
case 'thismonth_slot':
$this
->assertTrue(count($fileset) >= 3, 'There are at least 3 weeks (4 minus current) with backups this month.');
break;
case 'thisyear_slot':
$this
->assertEqual(count($fileset), 11, 'There are 11 months (12 minus today) with backups this year.');
break;
case 'pastyears_slot':
$this
->assertTrue(count($fileset) > 0, 'There is at least one past year with backups.');
break;
default:
$this
->fail('Unexpected time slot: ' . $time_slot);
break;
}
}
}
/**
* Test number of generated files
*/
public function testNumFiles() {
$gardener = new GardenerFake();
$numfiles = count($gardener
->getDestination()
->list_files());
$this
->assertEqual($numfiles, 1522, '1522 fake files generated.');
}
/**
* Test Gardener::save() method
* @see Gardener::save()
*/
public function testPrune() {
$gardener = new GardenerFake();
$gardener
->setDateReference(BackupMigratePruneTestCase::DATEEND);
$deleted = $gardener
->prune();
// The fixture should contains 1522 files
// We will keep:
// - 1 from today
// - 6 from this week
// - 4 from this month (Oct has 5 weeks)
// - 11 from this year
// - 2 from past years
// This sums up 24 files to be kept
$this
->assertEqual($deleted, 1498, '1498 files should be deleted.');
}
}
/**
* Fake gardener for test purposes
*/
class GardenerFake extends Gardener {
/**
* Overwrites getDestination to return a fake destination
*/
public function getDestination() {
return new BackupMigrateDestinationFake();
}
/**
* Overwrites getSettings to not to consult the DB
*/
public function getSettings() {
return array(
'thisweek_slot' => array(
'active' => TRUE,
),
'thismonth_slot' => array(
'active' => TRUE,
'keep' => 4,
),
'thisyear_slot' => array(
'active' => TRUE,
'keep' => 2,
),
'pastyears_slot' => array(
'active' => TRUE,
'keep' => 10,
),
);
}
}
backup_migrate_include('files');
/**
* Fake destination for test purposes
*/
class BackupMigrateDestinationFake extends backup_migrate_destination {
/**
* Overwrites list_files to return a fake list of files
*/
public function list_files() {
$files = array();
$datetime = new \DateTime(BackupMigratePruneTestCase::DATEINIT, new \DateTimeZone('Europe/Berlin'));
$datetime_end = new \DateTime(BackupMigratePruneTestCase::DATEEND, new \DateTimeZone('Europe/Berlin'));
$interval = new \DateInterval(BackupMigratePruneTestCase::TIMEINTERVAL);
do {
$timestamp = $datetime
->getTimestamp();
$filename = "BackupMigratePruneTest-" . $datetime
->format("Y-m-d\\TH-i-s") . '.mysql.gz';
global $base_url;
$file_info = array(
'filename' => $filename,
'description' => '',
'datestamp' => $timestamp,
'generator' => 'BackupMigrateDestinationFake',
'generatorversion' => '7.x-1.x',
'sites' => array(
array(
'version' => VERSION,
'name' => 'BackupMigrateDestinationTest',
'url' => $base_url,
),
),
'filesize' => mt_rand(1024, 4096),
'filetime' => $timestamp,
);
$file = new BackupFileFake();
$file
->set_file_info($file_info);
$files[$filename] = $file;
$datetime
->add($interval);
} while ($datetime < $datetime_end);
return $files;
}
/**
* Overwrites delete_file to do nothing (fake files are not deletable)
*/
public function delete_file() {
return NULL;
}
/**
* Overwrites op to support fake deletions
*/
public function op($operation) {
if ($operation == 'delete') {
return TRUE;
}
return parent::op($operation);
}
}
/**
* Fake backup_file for test purposes.
*/
class BackupFileFake extends backup_file {
/**
* Fakes detect_filetype_from_extension function to prevent accessing the DB
*/
public function detect_filetype_from_extension() {
return array(
'extension' => 'gz',
'filemime' => 'application/x-zip',
'backup' => TRUE,
'restore' => TRUE,
'id' => 'gzip',
);
}
}
Classes
Name | Description |
---|---|
BackupFileFake | Fake backup_file for test purposes. |
BackupMigrateDestinationFake | Fake destination for test purposes |
BackupMigratePruneTestCase | BackupMigratePruneTestCase class for unit testing |
GardenerFake | Fake gardener for test purposes |