class ViewsBulkOperationsBatchTest in Views Bulk Operations (VBO) 8
Same name and namespace in other branches
- 8.3 tests/src/Unit/ViewsBulkOperationsBatchTest.php \Drupal\Tests\views_bulk_operations\Unit\ViewsBulkOperationsBatchTest
- 8.2 tests/src/Unit/ViewsBulkOperationsBatchTest.php \Drupal\Tests\views_bulk_operations\Unit\ViewsBulkOperationsBatchTest
- 4.0.x tests/src/Unit/ViewsBulkOperationsBatchTest.php \Drupal\Tests\views_bulk_operations\Unit\ViewsBulkOperationsBatchTest
@coversDefaultClass \Drupal\views_bulk_operations\ViewsBulkOperationsBatch @group views_bulk_operations
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\views_bulk_operations\Unit\ViewsBulkOperationsBatchTest
Expanded class hierarchy of ViewsBulkOperationsBatchTest
File
- tests/
src/ Unit/ ViewsBulkOperationsBatchTest.php, line 13
Namespace
Drupal\Tests\views_bulk_operations\UnitView source
class ViewsBulkOperationsBatchTest extends UnitTestCase {
/**
* Modules to install.
*
* @var array
*/
public static $modules = [
'node',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->container = new ContainerBuilder();
\Drupal::setContainer($this->container);
}
/**
* Returns a stub ViewsBulkOperationsActionProcessor that returns dummy data.
*
* @return \Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessor
* A mocked action processor.
*/
public function getViewsBulkOperationsActionProcessorStub($entities_count) {
$actionProcessor = $this
->getMockBuilder('Drupal\\views_bulk_operations\\Service\\ViewsBulkOperationsActionProcessor')
->disableOriginalConstructor()
->getMock();
$actionProcessor
->expects($this
->any())
->method('getEntity')
->will($this
->returnValue(new \stdClass()));
$actionProcessor
->expects($this
->any())
->method('populateQueue')
->will($this
->returnValue($entities_count));
$actionProcessor
->expects($this
->any())
->method('process')
->will($this
->returnCallback(function () use ($entities_count) {
$return = [];
for ($i = 0; $i < $entities_count; $i++) {
$return[] = 'Some action';
}
return $return;
}));
return $actionProcessor;
}
/**
* Tests the getBatch() method.
*
* @covers ::getBatch
*/
public function testGetBatch() {
$data = [
'list' => [
[
0,
'en',
'node',
1,
],
],
'some_data' => [],
'action_label' => '',
];
$batch = TestViewsBulkOperationsBatch::getBatch($data);
$this
->assertArrayHasKey('title', $batch);
$this
->assertArrayHasKey('operations', $batch);
$this
->assertArrayHasKey('finished', $batch);
}
/**
* Tests the operation() method.
*
* @covers ::operation
*/
public function testOperation() {
$batch_size = 2;
$entities_count = 10;
$this->container
->set('views_bulk_operations.processor', $this
->getViewsBulkOperationsActionProcessorStub($batch_size));
$view = new View([
'id' => 'test_view',
], 'view');
$view_storage = $this
->getMockBuilder('Drupal\\Core\\Config\\Entity\\ConfigEntityStorage')
->disableOriginalConstructor()
->getMock();
$view_storage
->expects($this
->any())
->method('load')
->with('test_view')
->will($this
->returnValue($view));
$entity_manager = $this
->getMock('Drupal\\Core\\Entity\\EntityManagerInterface');
$entity_manager
->expects($this
->any())
->method('getStorage')
->with('view')
->will($this
->returnValue($view_storage));
$this->container
->set('entity.manager', $entity_manager);
$executable = $this
->getMockBuilder('Drupal\\views\\ViewExecutable')
->disableOriginalConstructor()
->getMock();
$executable->result = [];
// We set only $batch_size entities because
// $view->setItemsPerPage will not have effect.
for ($i = 0; $i < $batch_size; $i++) {
$row = new \stdClass();
$row->_entity = new \stdClass();
$executable->result[] = $row;
}
$viewExecutableFactory = $this
->getMockBuilder('Drupal\\views\\ViewExecutableFactory')
->disableOriginalConstructor()
->getMock();
$viewExecutableFactory
->expects($this
->any())
->method('get')
->will($this
->returnValue($executable));
$this->container
->set('views.executable', $viewExecutableFactory);
$data = [
'view_id' => 'test_view',
'display_id' => 'test_display',
'batch_size' => $batch_size,
'list' => [],
];
$context = [
'sandbox' => [
'processed' => 0,
'total' => $entities_count,
],
];
TestViewsBulkOperationsBatch::operation($data, $context);
$this
->assertEquals(count($context['results']['operations']), $batch_size);
$this
->assertEquals($context['finished'], $batch_size / $entities_count);
}
/**
* Tests the finished() method.
*
* @covers ::finished
*/
public function testFinished() {
$results = [
'operations' => [
'Some operation',
'Some operation',
],
];
TestViewsBulkOperationsBatch::finished(TRUE, $results, []);
$this
->assertEquals(TestViewsBulkOperationsBatch::message(), 'Action processing results: Some operation (2).');
$results = [
'operations' => [
'Some operation1',
'Some operation2',
],
];
TestViewsBulkOperationsBatch::finished(TRUE, $results, []);
$this
->assertEquals(TestViewsBulkOperationsBatch::message(), 'Action processing results: Some operation1 (1), Some operation2 (1).');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
ViewsBulkOperationsBatchTest:: |
public static | property | Modules to install. | |
ViewsBulkOperationsBatchTest:: |
public | function | Returns a stub ViewsBulkOperationsActionProcessor that returns dummy data. | |
ViewsBulkOperationsBatchTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ViewsBulkOperationsBatchTest:: |
public | function | Tests the finished() method. | |
ViewsBulkOperationsBatchTest:: |
public | function | Tests the getBatch() method. | |
ViewsBulkOperationsBatchTest:: |
public | function | Tests the operation() method. |