123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?php
- class BatchProcessingTestCase extends DrupalWebTestCase {
- public static function getInfo() {
- return array(
- 'name' => 'Batch processing',
- 'description' => 'Test batch processing in form and non-form workflow.',
- 'group' => 'Batch API',
- );
- }
- function setUp() {
- parent::setUp('batch_test');
- }
-
- function testBatchNoForm() {
-
- $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.'));
- }
-
- function testBatchForm() {
-
- $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.'));
-
- $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.'));
-
- $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.'));
-
- $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.'));
-
- $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.'));
- }
-
- function testBatchFormMultistep() {
- $this->drupalGet('batch-test/multistep');
- $this->assertText('step 1', t('Form is displayed in step 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.'));
-
- $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.'));
- }
-
- function testBatchFormMultipleBatches() {
-
-
- $value = rand(0, 255);
- $edit = array('value' => $value);
- $this->drupalPost('batch-test/chained', $edit, 'Submit');
-
- $this->assertBatchMessages($this->_resultMessages('chained'), t('Batches defined in separate submit handlers performed successfully.'));
-
-
- $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.'));
- }
-
- function testBatchFormProgrammatic() {
-
-
- $value = rand(0, 255);
- $this->drupalGet('batch-test/programmatic/' . $value);
-
- $this->assertBatchMessages($this->_resultMessages('chained'), t('Batches defined in separate submit handlers performed successfully.'));
-
-
- $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.'));
- }
-
- function testDrupalFormSubmitInBatch() {
-
-
- $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.'));
- }
-
- function testBatchLargePercentage() {
-
- $this->drupalGet('batch-test/large-percentage');
- $this->assertBatchMessages($this->_resultMessages(1), t('Batch for step 2 performed successfully.'));
- $this->assertEqual(batch_test_stack(), $this->_resultStack('batch_5'), t('Execution order was correct.'));
- $this->assertText('Redirection successful.', t('Redirection after batch execution is correct.'));
- }
-
- function assertBatchMessages($texts, $message) {
- $pattern = '|' . implode('.*', $texts) .'|s';
- return $this->assertPattern($pattern, $message);
- }
-
- 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 'batch_5':
- for ($i = 1; $i <= 10; $i++) {
- $stack[] = "op 5 id $i";
- }
- 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;
- }
-
- 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 'batch_5':
- $messages[] = 'results for batch 5<br />op 1: processed 10 elements. $context[\'finished\'] > 1 returned from batch process, with success.';
- 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;
- }
- }
- 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');
- }
-
- function testBatchProgressPageTheme() {
-
-
- variable_set('theme_default', 'bartik');
- variable_set('admin_theme', 'seven');
-
- $admin_user = $this->drupalCreateUser(array('view the administration theme'));
- $this->drupalLogin($admin_user);
-
-
-
-
- $this->drupalGet('admin/batch-test/test-theme');
-
-
- $this->assertEqual(batch_test_stack(), array('seven'), t('A progressive batch correctly uses the theme of the page that started the batch.'));
- }
- }
- class BatchPercentagesUnitTestCase extends DrupalUnitTestCase {
- protected $testCases = array();
- public static function getInfo() {
- return array(
- 'name' => 'Batch percentages',
- 'description' => 'Unit tests of progress percentage rounding.',
- 'group' => 'Batch API',
- );
- }
- function setUp() {
-
-
-
- $this->testCases = array(
-
- '50' => array('total' => 2, 'current' => 1),
-
-
- '0' => array('total' => 3, 'current' => 0),
-
- '33' => array('total' => 3, 'current' => 1),
-
- '67' => array('total' => 3, 'current' => 2),
-
- '1' => array('total' => 199, 'current' => 1),
-
- '99' => array('total' => 199, 'current' => 198),
-
-
-
- '99.5' => array('total' => 200, 'current' => 199),
-
- '0.5' => array('total' => 200, 'current' => 1),
-
-
- '25.0' => array('total' => 200, 'current' => 50),
-
-
- '100' => array('total' => 200, 'current' => 200),
-
- '99.9' => array('total' => 1999, 'current' => 1998),
-
- '99.95' => array('total' => 2000, 'current' => 1999),
-
- '99.995' => array('total' => 20000, 'current' => 19999),
-
-
-
-
-
-
-
-
- '20' => array('total' => 1, 'current' => 100/501),
- '40' => array('total' => 1, 'current' => 200/501),
- '60' => array('total' => 1, 'current' => 300/501),
- '80' => array('total' => 1, 'current' => 400/501),
- '99.8' => array('total' => 1, 'current' => 500/501),
- );
- require_once DRUPAL_ROOT . '/includes/batch.inc';
- parent::setUp();
- }
-
- function testBatchPercentages() {
- foreach ($this->testCases as $expected_result => $arguments) {
-
-
- $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)));
- }
- }
- }
- }
|