drupal/modules/simpletest/tests/batch.test

123 lines
5.0 KiB
Plaintext

<?php
// $Id$
/**
* @file
* Unit tests for the Drupal Batch API.
*/
/**
* Tests for the batch API progress page theme.
*/
class BatchAPIThemeTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Batch API progress page theme',
'description' => 'Tests that while a progressive batch is running, it correctly uses the theme of the page that started the batch.',
'group' => 'Batch API',
);
}
function setUp() {
parent::setUp('system_test');
// Make sure that the page which starts the batch (an administrative page)
// is using a different theme than would normally be used by the batch API.
variable_set('theme_default', 'garland');
variable_set('admin_theme', 'seven');
}
/**
* Tests that the batch API progress page uses the correct theme.
*/
function testBatchAPIProgressPageTheme() {
// Visit an administrative page that runs a test batch, and check that the
// theme that was used during batch execution (which the batch callback
// function saved as a variable) matches the theme used on the
// administrative page.
$this->drupalGet('admin/system-test/batch-theme');
$batch_theme_used = variable_get('system_test_batch_theme_used', 'garland');
$this->assertEqual($batch_theme_used, 'seven', t('A progressive batch correctly uses the theme of the page that started the batch.'));
}
}
/**
* Tests the function _batch_api_percentage() to make sure that the rounding
* works properly in all cases.
*/
class BatchAPIPercentagesTestCase extends DrupalWebTestCase {
protected $testCases = array();
public static function getInfo() {
return array(
'name' => 'Batch API percentages',
'description' => 'Tests the handling of percentage rounding in the Drupal batch API. This is critical to Drupal user experience.',
'group' => 'Batch API',
);
}
function setUp() {
// Set up an array of test cases, where the expected values are the keys,
// and the values are arrays with the keys 'total' and 'current',
// corresponding with the function parameters of _batch_api_percentage().
$this->testCases = array(
// 1/2 is 50%.
'50' => array('total' => 2, 'current' => 1),
// Though we should never encounter a case where the current set is set
// 0, if we did, we should get 0%.
'0' => array('total' => 3, 'current' => 0),
// 1/3 is closer to 33% than to 34%.
'33' => array('total' => 3, 'current' => 1),
// 2/3 is closer to 67% than to 66%.
'67' => array('total' => 3, 'current' => 2),
// A full 3/3 should equal 100%.
'100' => array('total' => 3, 'current' => 3),
// 1/199 should round up to 1%.
'1' => array('total' => 199, 'current' => 1),
// 198/199 should round down to 99%.
'99' => array('total' => 199, 'current' => 198),
// 199/200 would have rounded up to 100%, which would give the false
// impression of being finished, so we add another digit and should get
// 99.5%.
'99.5' => array('total' => 200, 'current' => 199),
// The same logic holds for 1/200: we should get 0.5%.
'0.5' => array('total' => 200, 'current' => 1),
// Numbers that come out evenly, such as 50/200, should be forced to have
// extra digits for consistancy.
'25.0' => array('total' => 200, 'current' => 50),
// Regardless of number of digits we're using, 100% should always just be
// 100%.
'100' => array('total' => 200, 'current' => 200),
// 1998/1999 should similarly round down to 99.9%.
'99.9' => array('total' => 1999, 'current' => 1998),
// 1999/2000 should add another digit and go to 99.95%.
'99.95' => array('total' => 2000, 'current' => 1999),
// 19999/20000 should add yet another digit and go to 99.995%.
'99.995' => array('total' => 20000, 'current' => 19999),
);
parent::setUp();
}
/**
* Test the _batch_api_percentage() function with the data stored in the
* testCases class variable.
*/
function testBatchAPIPercentages() {
require_once DRUPAL_ROOT . '/includes/batch.inc';
foreach ($this->testCases as $expected_result => $arguments) {
// PHP sometimes casts numeric strings that are array keys to integers,
// cast them back here.
$expected_result = (string)$expected_result;
$total = $arguments['total'];
$current = $arguments['current'];
$actual_result = _batch_api_percentage($total, $current);
if ($actual_result === $expected_result) {
$this->pass(t('Expected the batch api percentage at the state @numerator/@denominator to be @expected%, and got @actual%.', array('@numerator' => $current, '@denominator' => $total, '@expected' => $expected_result, '@actual' => $actual_result)));
}
else {
$this->fail(t('Expected the batch api percentage at the state @numerator/@denominator to be @expected%, but got @actual%.', array('@numerator' => $current, '@denominator' => $total, '@expected' => $expected_result, '@actual' => $actual_result)));
}
}
}
}