View source
<?php
namespace Drupal\Tests\Core\Batch;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Tests\UnitTestCase;
class BatchBuilderTest extends UnitTestCase {
public function testDefaultValues() {
$batch = (new BatchBuilder())
->toArray();
$this
->assertIsArray($batch);
$this
->assertArrayHasKey('operations', $batch);
$this
->assertIsArray($batch['operations']);
$this
->assertEmpty($batch['operations'], 'Operations array is empty.');
$this
->assertEquals(new TranslatableMarkup('Processing'), $batch['title']);
$this
->assertEquals(new TranslatableMarkup('Initializing.'), $batch['init_message']);
$this
->assertEquals(new TranslatableMarkup('Completed @current of @total.'), $batch['progress_message']);
$this
->assertEquals(new TranslatableMarkup('An error has occurred.'), $batch['error_message']);
$this
->assertNull($batch['finished']);
$this
->assertNull($batch['file']);
$this
->assertArrayHasKey('library', $batch);
$this
->assertIsArray($batch['library']);
$this
->assertEmpty($batch['library']);
$this
->assertArrayHasKey('url_options', $batch);
$this
->assertIsArray($batch['url_options']);
$this
->assertEmpty($batch['url_options']);
$this
->assertArrayHasKey('progressive', $batch);
$this
->assertTrue($batch['progressive']);
$this
->assertArrayNotHasKey('queue', $batch);
}
public function testSetTitle() {
$batch = (new BatchBuilder())
->setTitle(new TranslatableMarkup('New Title'))
->toArray();
$this
->assertEquals(new TranslatableMarkup('New Title'), $batch['title']);
}
public function testSetFinishCallback() {
$batch = (new BatchBuilder())
->setFinishCallback('\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::finishedCallback')
->toArray();
$this
->assertEquals('\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::finishedCallback', $batch['finished']);
}
public function testSetInitMessage() {
$batch = (new BatchBuilder())
->setInitMessage(new TranslatableMarkup('New initialization message.'))
->toArray();
$this
->assertEquals(new TranslatableMarkup('New initialization message.'), $batch['init_message']);
}
public function testSetProgressMessage() {
$batch = (new BatchBuilder())
->setProgressMessage(new TranslatableMarkup('Batch in progress...'))
->toArray();
$this
->assertEquals(new TranslatableMarkup('Batch in progress...'), $batch['progress_message']);
}
public function testSetErrorMessage() {
$batch = (new BatchBuilder())
->setErrorMessage(new TranslatableMarkup('Oops. An error has occurred :('))
->toArray();
$this
->assertEquals(new TranslatableMarkup('Oops. An error has occurred :('), $batch['error_message']);
}
public function testSetFile() {
$filename = dirname(__DIR__, 6) . '/core/modules/system/tests/modules/batch_test/batch_test.callbacks.inc';
$this
->assertIsNotCallable('_batch_test_callback_1');
$this
->assertIsNotCallable('_batch_test_finished_1');
$batch = (new BatchBuilder())
->setFile($filename)
->setFinishCallback('_batch_test_finished_1')
->addOperation('_batch_test_callback_1', [])
->toArray();
$this
->assertEquals($filename, $batch['file']);
$this
->assertEquals([
[
'_batch_test_callback_1',
[],
],
], $batch['operations']);
$this
->assertEquals('_batch_test_finished_1', $batch['finished']);
$this
->assertIsCallable('_batch_test_callback_1');
$this
->assertIsCallable('_batch_test_finished_1');
}
public function testAddingLibraries() {
$batch = (new BatchBuilder())
->setLibraries([
'only/library',
])
->toArray();
$this
->assertEquals([
'only/library',
], $batch['library']);
}
public function testSetProgressive() {
$batch_builder = new BatchBuilder();
$batch = $batch_builder
->setProgressive(FALSE)
->toArray();
$this
->assertFalse($batch['progressive']);
$batch = $batch_builder
->setProgressive(TRUE)
->toArray();
$this
->assertTrue($batch['progressive']);
}
public function testSetQueue() {
$batch = (new BatchBuilder())
->setQueue('BatchName', '\\Drupal\\Core\\Queue\\Batch')
->toArray();
$this
->assertEquals([
'name' => 'BatchName',
'class' => '\\Drupal\\Core\\Queue\\Batch',
], $batch['queue'], 'Batch queue has been set.');
}
public function testQueueExists() {
$batch_builder = new BatchBuilder();
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Class \\ThisIsNotAClass does not exist.');
$batch_builder
->setQueue('BatchName', '\\ThisIsNotAClass');
}
public function testQueueImplements() {
$batch_builder = new BatchBuilder();
$this
->expectException(\InvalidArgumentException::class);
$this
->expectExceptionMessage('Class Exception does not implement \\Drupal\\Core\\Queue\\QueueInterface.');
$batch_builder
->setQueue('BatchName', \Exception::class);
}
public function testSetUrlOptions() {
$options = [
'absolute' => TRUE,
'language' => 'de',
];
$batch = (new BatchBuilder())
->setUrlOptions($options)
->toArray();
$this
->assertEquals($options, $batch['url_options']);
}
public function testAddOperation() {
$batch_builder = new BatchBuilder();
$batch = $batch_builder
->addOperation('\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback')
->toArray();
$this
->assertEquals([
[
'\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback',
[],
],
], $batch['operations']);
$batch = $batch_builder
->addOperation('\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback', [
2,
])
->addOperation('\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback', [
3,
])
->toArray();
$this
->assertEquals([
[
'\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback',
[],
],
[
'\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback',
[
2,
],
],
[
'\\Drupal\\Tests\\Core\\Batch\\BatchBuilderTest::operationCallback',
[
3,
],
],
], $batch['operations']);
}
public static function finishedCallback() {
}
public static function operationCallback() {
}
}