summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/batch.test
diff options
context:
space:
mode:
Diffstat (limited to 'modules/simpletest/tests/batch.test')
-rw-r--r--modules/simpletest/tests/batch.test285
1 files changed, 265 insertions, 20 deletions
diff --git a/modules/simpletest/tests/batch.test b/modules/simpletest/tests/batch.test
index 7c6ade423..54b29c8a9 100644
--- a/modules/simpletest/tests/batch.test
+++ b/modules/simpletest/tests/batch.test
@@ -3,40 +3,286 @@
/**
* @file
- * Unit tests for the Drupal Batch API.
+ * Tests for the Batch API.
*/
/**
- * Tests for the batch API progress page theme.
+ * Tests for the Batch API.
*/
-class BatchAPIThemeTestCase extends DrupalWebTestCase {
+class BatchProcessingTestCase 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.',
+ 'name' => 'Batch processing',
+ 'description' => 'Test batch processing in form and non-form workflow.',
'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');
+ parent::setUp('batch_test');
+ }
+
+ /**
+ * Test batches triggered outside of form submission.
+ */
+ function testBatchNoForm() {
+ // Displaying the page triggers batch 1.
+ $this->drupalGet('batch_test/no_form');
+ $this->assertBatchMessages($this->_resultMessages(1), t('Batch for step 2 performed successfully.'));
+ $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_1'), t('Execution order was correct.'));
+ $this->assertText('Redirection successful.', t('Redirection after batch execution is correct.'));
+ }
+
+ /**
+ * Test batches defined in a form submit handler.
+ */
+ function testBatchForm() {
+ // Batch 0: no operation.
+ $edit = array('batch' => 'batch_0');
+ $this->drupalPost('batch_test/simple', $edit, 'Submit');
+ $this->assertBatchMessages($this->_resultMessages('batch_0'), t('Batch with no operation performed successfully.'));
+ $this->assertText('Redirection successful.', t('Redirection after batch execution is correct.'));
+
+ // Batch 1: several simple operations.
+ $edit = array('batch' => 'batch_1');
+ $this->drupalPost('batch_test/simple', $edit, 'Submit');
+ $this->assertBatchMessages($this->_resultMessages('batch_1'), t('Batch with simple operations performed successfully.'));
+ $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_1'), t('Execution order was correct.'));
+ $this->assertText('Redirection successful.', t('Redirection after batch execution is correct.'));
+
+ // Batch 2: one multistep operation.
+ $edit = array('batch' => 'batch_2');
+ $this->drupalPost('batch_test/simple', $edit, 'Submit');
+ $this->assertBatchMessages($this->_resultMessages('batch_2'), t('Batch with multistep operation performed successfully.'));
+ $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_2'), t('Execution order was correct.'));
+ $this->assertText('Redirection successful.', t('Redirection after batch execution is correct.'));
+
+ // Batch 3: simple + multistep combined.
+ $edit = array('batch' => 'batch_3');
+ $this->drupalPost('batch_test/simple', $edit, 'Submit');
+ $this->assertBatchMessages($this->_resultMessages('batch_3'), t('Batch with simple and multistep operations performed successfully.'));
+ $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_3'), t('Execution order was correct.'));
+ $this->assertText('Redirection successful.', t('Redirection after batch execution is correct.'));
+
+ // Batch 4: nested batch.
+ $edit = array('batch' => 'batch_4');
+ $this->drupalPost('batch_test/simple', $edit, 'Submit');
+ $this->assertBatchMessages($this->_resultMessages('batch_4'), t('Nested batch performed successfully.'));
+ $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_4'), t('Execution order was correct.'));
+ $this->assertText('Redirection successful.', t('Redirection after batch execution is correct.'));
+ }
+
+ /**
+ * Test batches defined in a multistep form.
+ */
+ function testBatchFormMultistep() {
+ $this->drupalGet('batch_test/multistep');
+ $this->assertText('step 1', t('Form is displayed in step 1.'));
+
+ // First step triggers batch 1.
+ $this->drupalPost(NULL, array(), 'Submit');
+ $this->assertBatchMessages($this->_resultMessages('batch_1'), t('Batch for step 1 performed successfully.'));
+ $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_1'), t('Execution order was correct.'));
+ $this->assertText('step 2', t('Form is displayed in step 2.'));
+
+ // Second step triggers batch 2.
+ $this->drupalPost(NULL, array(), 'Submit');
+ $this->assertBatchMessages($this->_resultMessages('batch_2'), t('Batch for step 2 performed successfully.'));
+ $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_2'), t('Execution order was correct.'));
+ $this->assertText('Redirection successful.', t('Redirection after batch execution is correct.'));
+ }
+
+ /**
+ * Test batches defined in different submit handlers on the same form.
+ */
+ function testBatchFormMultipleBatches() {
+ // Batches 1, 2 and 3 are triggered in sequence by different submit
+ // handlers. Each submit handler modify the submitted 'value'.
+ $value = rand(0, 255);
+ $edit = array('value' => $value);
+ $this->drupalPost('batch_test/chained', $edit, 'Submit');
+ // Check that result messages are present and in the correct order.
+ $this->assertBatchMessages($this->_resultMessages('chained'), t('Batches defined in separate submit handlers performed successfully.'));
+ // The stack contains execution order of batch callbacks and submit
+ // hanlders and logging of corresponding $form_state[{values'].
+ $this->assertEqual(batch_test_stack(), $this->_resultStack('chained', $value), t('Execution order was correct, and $form_state is correctly persisted.'));
+ $this->assertText('Redirection successful.', t('Redirection after batch execution is correct.'));
+ }
+
+ /**
+ * Test batches defined in a programmatically submitted form.
+ *
+ * Same as above, but the form is submitted through drupal_form_execute().
+ */
+ function testBatchFormProgrammatic() {
+ // Batches 1, 2 and 3 are triggered in sequence by different submit
+ // handlers. Each submit handler modify the submitted 'value'.
+ $value = rand(0, 255);
+ $this->drupalGet('batch_test/programmatic/' . $value);
+ // Check that result messages are present and in the correct order.
+ $this->assertBatchMessages($this->_resultMessages('chained'), t('Batches defined in separate submit handlers performed successfully.'));
+ // The stack contains execution order of batch callbacks and submit
+ // hanlders and logging of corresponding $form_state[{values'].
+ $this->assertEqual(batch_test_stack(), $this->_resultStack('chained', $value), t('Execution order was correct, and $form_state is correctly persisted.'));
+ $this->assertText('Got out of a programmatic batched form.', t('Page execution continues normally.'));
+ }
+
+ /**
+ * Test that drupal_form_submit() can run within a batch operation.
+ */
+ function testDrupalFormSubmitInBatch() {
+ // Displaying the page triggers a batch that programmatically submits a
+ // form.
+ $value = rand(0, 255);
+ $this->drupalGet('batch_test/nested_programmatic/' . $value);
+ $this->assertEqual(batch_test_stack(), array('mock form submitted with value = ' . $value), t('drupal_form_submit() ran successfully within a batch operation.'));
+ }
+
+ /**
+ * Will trigger a pass if the texts were found in order in the raw content.
+ *
+ * @param $texts
+ * Array of raw strings to look for .
+ * @param $message
+ * Message to display.
+ * @return
+ * TRUE on pass, FALSE on fail.
+ */
+ function assertBatchMessages($texts, $message) {
+ $pattern = '|' . implode('.*', $texts) .'|s';
+ return $this->assertPattern($pattern, $message);
+ }
+
+ /**
+ * Helper function: return expected execution stacks for the test batches.
+ */
+ function _resultStack($id, $value = 0) {
+ $stack = array();
+ switch ($id) {
+ case 'batch_1':
+ for ($i = 1; $i <= 10; $i++) {
+ $stack[] = "op 1 id $i";
+ }
+ break;
+
+ case 'batch_2':
+ for ($i = 1; $i <= 10; $i++) {
+ $stack[] = "op 2 id $i";
+ }
+ break;
+
+ case 'batch_3':
+ for ($i = 1; $i <= 5; $i++) {
+ $stack[] = "op 1 id $i";
+ }
+ for ($i = 1; $i <= 5; $i++) {
+ $stack[] = "op 2 id $i";
+ }
+ for ($i = 6; $i <= 10; $i++) {
+ $stack[] = "op 1 id $i";
+ }
+ for ($i = 6; $i <= 10; $i++) {
+ $stack[] = "op 2 id $i";
+ }
+ break;
+
+ case 'batch_4':
+ for ($i = 1; $i <= 5; $i++) {
+ $stack[] = "op 1 id $i";
+ }
+ $stack[] = 'setting up batch 2';
+ for ($i = 6; $i <= 10; $i++) {
+ $stack[] = "op 1 id $i";
+ }
+ $stack = array_merge($stack, $this->_resultStack('batch_2'));
+ break;
+
+ case 'chained':
+ $stack[] = 'submit handler 1';
+ $stack[] = 'value = ' . $value;
+ $stack = array_merge($stack, $this->_resultStack('batch_1'));
+ $stack[] = 'submit handler 2';
+ $stack[] = 'value = ' . ($value + 1);
+ $stack = array_merge($stack, $this->_resultStack('batch_2'));
+ $stack[] = 'submit handler 3';
+ $stack[] = 'value = ' . ($value + 2);
+ $stack[] = 'submit handler 4';
+ $stack[] = 'value = ' . ($value + 3);
+ $stack = array_merge($stack, $this->_resultStack('batch_3'));
+ break;
+ }
+ return $stack;
+ }
+
+ /**
+ * Helper function: return expected result messages for the test batches.
+ */
+ function _resultMessages($id) {
+ $messages = array();
+
+ switch ($id) {
+ case 'batch_0':
+ $messages[] = 'results for batch 0<br />none';
+ break;
+
+ case 'batch_1':
+ $messages[] = 'results for batch 1<br />op 1: processed 10 elements';
+ break;
+
+ case 'batch_2':
+ $messages[] = 'results for batch 2<br />op 2: processed 10 elements';
+ break;
+
+ case 'batch_3':
+ $messages[] = 'results for batch 3<br />op 1: processed 10 elements<br />op 2: processed 10 elements';
+ break;
+
+ case 'batch_4':
+ $messages[] = 'results for batch 4<br />op 1: processed 10 elements';
+ $messages = array_merge($messages, $this->_resultMessages('batch_2'));
+ break;
+
+ case 'chained':
+ $messages = array_merge($messages, $this->_resultMessages('batch_1'));
+ $messages = array_merge($messages, $this->_resultMessages('batch_2'));
+ $messages = array_merge($messages, $this->_resultMessages('batch_3'));
+ break;
+ }
+ return $messages;
+ }
+}
+
+/**
+ * Tests for the Batch API Progress page.
+ */
+class BatchPageTestCase extends DrupalWebTestCase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Batch progress page',
+ 'description' => 'Test the content of the progress page.',
+ 'group' => 'Batch API',
+ );
+ }
+
+ function setUp() {
+ parent::setUp('batch_test');
}
/**
* Tests that the batch API progress page uses the correct theme.
*/
- function testBatchAPIProgressPageTheme() {
+ function testBatchProgressPageTheme() {
+ // 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');
// 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.'));
+ $this->drupalGet('admin/batch_test/test_theme');
+ // The stack should contain the name of the the used on the progress page.
+ $this->assertEqual(batch_test_stack(), array('seven'), t('A progressive batch correctly uses the theme of the page that started the batch.'));
}
}
@@ -44,13 +290,13 @@ class BatchAPIThemeTestCase extends DrupalWebTestCase {
* Tests the function _batch_api_percentage() to make sure that the rounding
* works properly in all cases.
*/
-class BatchAPIPercentagesTestCase extends DrupalWebTestCase {
+class BatchPercentagesUnitTestCase extends DrupalUnitTestCase {
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.',
+ 'name' => 'Batch percentages',
+ 'description' => 'Unit tests of progress percentage rounding.',
'group' => 'Batch API',
);
}
@@ -99,10 +345,9 @@ class BatchAPIPercentagesTestCase extends DrupalWebTestCase {
}
/**
- * Test the _batch_api_percentage() function with the data stored in the
- * testCases class variable.
+ * Test the _batch_api_percentage() function.
*/
- function testBatchAPIPercentages() {
+ function testBatchPercentages() {
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,