You are here

public function ViewsBulkOperationsBatchTest::testOperation in Views Bulk Operations (VBO) 8

Same name and namespace in other branches
  1. 8.3 tests/src/Unit/ViewsBulkOperationsBatchTest.php \Drupal\Tests\views_bulk_operations\Unit\ViewsBulkOperationsBatchTest::testOperation()
  2. 8.2 tests/src/Unit/ViewsBulkOperationsBatchTest.php \Drupal\Tests\views_bulk_operations\Unit\ViewsBulkOperationsBatchTest::testOperation()
  3. 4.0.x tests/src/Unit/ViewsBulkOperationsBatchTest.php \Drupal\Tests\views_bulk_operations\Unit\ViewsBulkOperationsBatchTest::testOperation()

Tests the operation() method.

@covers ::operation

File

tests/src/Unit/ViewsBulkOperationsBatchTest.php, line 85

Class

ViewsBulkOperationsBatchTest
@coversDefaultClass \Drupal\views_bulk_operations\ViewsBulkOperationsBatch @group views_bulk_operations

Namespace

Drupal\Tests\views_bulk_operations\Unit

Code

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);
}