diff --git a/core/modules/system/tests/src/Unit/Batch/BatchBuilderTest.php b/core/modules/system/tests/src/Unit/Batch/BatchBuilderTest.php new file mode 100644 index 0000000..0a6816a --- /dev/null +++ b/core/modules/system/tests/src/Unit/Batch/BatchBuilderTest.php @@ -0,0 +1,246 @@ +toArray(); + + $this->assertTrue(is_array($batch), 'Batch builder create an array.'); + $this->assertTrue(is_array($batch['operations']), 'Operations array exists.'); + $this->assertEmpty($batch['operations'], 'Operations array is empty.'); + $this->assertEquals('Processing', $batch['title'], 'Default title has been set.'); + $this->assertEquals('Initializing.', $batch['init_message'], 'Default initialization message has been set.'); + $this->assertEquals('Completed @current of @total.', $batch['progress_message'], 'Default progess message has been set.'); + $this->assertEquals('An error has occurred.', $batch['error_message'], 'Default error message has been set.'); + $this->assertNull($batch['finished'], 'Finished callback has not been set.'); + $this->assertNull($batch['file'], 'No file is to be included.'); + $this->assertTrue(is_array($batch['library']), 'Library array exists.'); + $this->assertEmpty($batch['library'], 'No libraries have been added.'); + $this->assertTrue(is_array($batch['url_options']), 'URL options array exists.'); + $this->assertEmpty($batch['url_options'], 'URL options array is empty.'); + $this->assertTrue($batch['progressive'], 'Progressive has been set.'); + $this->assertFalse(isset($batch['queue']), 'Queue has not been set.'); + } + + /** + * Tests setTitle(). + * + * @covers ::setTitle + */ + public function testSetTitle() { + $batch = (new BatchBuilder()) + ->setTitle('New Title') + ->toArray(); + + $this->assertEquals('New Title', $batch['title'], 'Title has been set.'); + } + + /** + * Tests setFinishCallback(). + * + * @covers ::setFinishCallback + */ + public function testSetFinishCallback() { + $batch = (new BatchBuilder()) + ->setFinishCallback('\Drupal\Tests\system\Unit\Batch\BatchBuilderTest::finishedCallback') + ->toArray(); + + $this->assertEquals('\Drupal\Tests\system\Unit\Batch\BatchBuilderTest::finishedCallback', $batch['finished'], 'Finish callback has been set'); + } + + /** + * Tests setInitMessage(). + * + * @covers ::setInitMessage + */ + public function testSetInitMessage() { + $batch = (new BatchBuilder()) + ->setInitMessage('New initialization message.') + ->toArray(); + + $this->assertEquals('New initialization message.', $batch['init_message'], 'Initialization message has been set/'); + } + + /** + * Tests setProgressMessage(). + * + * @covers ::setProgressMessage + */ + public function testSetProgressMessage() { + $batch = (new BatchBuilder()) + ->setProgressMessage('Batch in progress...') + ->toArray(); + + $this->assertEquals('Batch in progress...', $batch['progress_message'], 'Progress message has been set.'); + } + + /** + * Tests setErrorMessage(). + */ + public function testSetErrorMessage() { + $batch = (new BatchBuilder()) + ->setErrorMessage('Ooops. An error has occurred :(') + ->toArray(); + + $this->assertEquals('Ooops. An error has occurred :(', $batch['error_message'], 'Error message has been set.'); + } + + /** + * Tests setFile(). + * + * @covers ::setFile + */ + public function testSetFile() { + $batch = (new BatchBuilder()) + ->setFile('filename.php') + ->toArray(); + + $this->assertEquals('filename.php', $batch['file'], 'The file has been set.'); + } + + /** + * Tests setting and adding libraries. + * + * @covers ::setLibraries + * @covers ::addLibraries + */ + public function testAddingLibraries() { + $batch_builder = new BatchBuilder(); + $batch = $batch_builder + ->addLibraries('library1') + ->toArray(); + + $this->assertEquals(1, count($batch['library']), 'Library added as string.'); + + $batch = $batch_builder + ->addLibraries(['library2', 'library3']) + ->toArray(); + + $this->assertEquals(3, count($batch['library']), 'Libraries added in an array.'); + + $batch = $batch_builder + ->setLibraries(['onlylibrary']) + ->toArray(); + + $this->assertEquals(1, count($batch['library']), 'Libraries replaced by an array.'); + } + + /** + * Tests setProgressive(). + * + * @covers ::setProgressive + */ + public function testSetProgressive() { + $batch_builder = new BatchBuilder(); + $batch = $batch_builder + ->setProgressive(FALSE) + ->toArray(); + + $this->assertFalse($batch['progressive'], 'Batch has been set to not progressive.'); + + $batch = $batch_builder + ->setProgressive(TRUE) + ->toArray(); + + $this->assertTrue($batch['progressive'], 'Batch has been set to progressive.'); + } + + /** + * Tests setQueue(). + * + * @covers ::setQueue + */ + public function testSetQueue() { + $batch = (new BatchBuilder()) + ->setQueue('BatchName', '\Drupal\Core\Queue\Batch') + ->toArray(); + + $this->assertArrayEquals([ + 'name' => 'BatchName', + 'class' => '\Drupal\Core\Queue\Batch', + ], $batch['queue'], 'Batch queue has been set.'); + } + + /** + * Tests setUrlOptions(). + * + * @covers ::setUrlOptions + */ + public function testSetUrlOptions() { + $options = [ + 'absolute' => TRUE, + 'language' => 'de', + ]; + $batch = (new BatchBuilder()) + ->setUrlOptions($options) + ->toArray(); + + $this->assertEquals($options, $batch['url_options'], 'URL options have been set.'); + } + + /** + * Tests addOperation(). + * + * @covers ::addOperation + */ + public function testAddOperation() { + $batch_builder = new BatchBuilder(); + $batch = $batch_builder + ->addOperation('\Drupal\Tests\system\Unit\Batch\BatchBuilderTest::operationCallback') + ->toArray(); + + $this->assertEquals(1, count($batch['operations']), 'One operation has been added.'); + + $batch = $batch_builder + ->addOperation('\Drupal\Tests\system\Unit\Batch\BatchBuilderTest::operationCallback', [2]) + ->addOperation('\Drupal\Tests\system\Unit\Batch\BatchBuilderTest::operationCallback', [3]) + ->toArray(); + + $this->assertEquals(3, count($batch['operations']), 'Three operations have been added.'); + + $this->assertEquals('\Drupal\Tests\system\Unit\Batch\BatchBuilderTest::operationCallback', $batch['operations'][0][0], 'Operation callback has been set.'); + $this->assertEquals([], $batch['operations'][0][1], 'First operation parameters set correctly.'); + $this->assertEquals([2], $batch['operations'][1][1], 'Second operation parameters set correctly.'); + $this->assertEquals([3], $batch['operations'][2][1], 'Third operation parameters set correctly.'); + + } + + /** + * Empty callback for the tests. + * + * @internal + */ + public static function finishedCallback() { + } + + /** + * Empty callback for the tests. + * + * @internal + */ + public static function operationCallback() { + } + +}