Commit f5748662 authored by Marcin Grabias's avatar Marcin Grabias
Browse files

Automated tests improvements.

parent 2aeba468
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -49,7 +49,7 @@ class DrushCommandsTest extends BrowserTestBase {
      $this->testNodes[] = $this->drupalCreateNode([
        'type' => 'page',
        'title' => 'Title ' . $i,
        'sticky' => $i % 2 ? TRUE : FALSE,
        'sticky' => $i % 2,
        'created' => $time,
        'changed' => $time,
      ]);
+0 −1
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

namespace Drupal\Tests\views_bulk_operations\Functional;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Tests\BrowserTestBase;

/**
+0 −40
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\views_bulk_operations\Unit;

use Drupal\views_bulk_operations\ViewsBulkOperationsBatch;

/**
 * Override some class methods for proper testing.
 */
class TestViewsBulkOperationsBatch extends ViewsBulkOperationsBatch {

  /**
   * Override t method.
   */
  public static function translate($string, array $args = [], array $options = []): string {
    return \strtr($string, $args);
  }

  /**
   * Override message method.
   */
  public static function message($message = NULL, $type = 'status', $repeat = TRUE): ?string {
    static $storage;
    if ($message === NULL) {
      $output = $storage;
      $storage = NULL;
      return $output;
    }
    else {
      if (isset($storage)) {
        $storage .= ' | ' . (string) $message;
      }
      else {
        $storage = (string) $message;
      }
    }
    return NULL;
  }

}
+43 −35
Original line number Diff line number Diff line
@@ -2,8 +2,11 @@

namespace Drupal\Tests\views_bulk_operations\Unit;

use Drupal\Core\Messenger\Messenger;
use Drupal\Core\StringTranslation\TranslationManager;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Tests\UnitTestCase;
use Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessor;
use Drupal\views_bulk_operations\ViewsBulkOperationsBatch;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
@@ -19,6 +22,8 @@ class ViewsBulkOperationsBatchTest extends UnitTestCase {
   */
  protected static array $modules = ['node'];

  private $messages = NULL;

  /**
   * {@inheritdoc}
   */
@@ -26,37 +31,40 @@ class ViewsBulkOperationsBatchTest extends UnitTestCase {
    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): ViewsBulkOperationsActionProcessor {
    $actionProcessor = $this->getMockBuilder('Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessor')
      ->disableOriginalConstructor()
      ->getMock();

    $actionProcessor->expects($this->any())
      ->method('populateQueue')
      ->will($this->returnValue($entities_count));

    $actionProcessor->expects($this->any())
      ->method('process')
      ->will($this->returnCallback(static function () use ($entities_count) {
        $return = [];
        for ($i = 0; $i < $entities_count; $i++) {
          $return[] = [
            'message' => 'Some action',
          ];
    // Mock translation manager.
    $this->translationManager = $this->createPartialMock(TranslationManager::class, ['translateString']);
    $this->translationManager->expects($this->any())
      ->method('translateString')
      ->willReturnCallback(function (TranslatableMarkup $translated_string) {
        return \strtr($translated_string->getUntranslatedString(), $translated_string->getOptions());
      });

    $this->container->set('string_translation', $this->translationManager);

    // Mock messanger.
    $this->messenger = $this->createMock(Messenger::class);
    $this->messenger->expects($this->any())
      ->method('addMessage')
      ->willReturnCallback(function ($message, $type, $repeat) {
        if ($this->messages === NULL) {
          $this->messages = (string) $message;
        }
        return $return;
      }));
        else {
          $this->messages .= ' | ' . (string) $message;
        }
      });
    $this->messenger->expects($this->any())
      ->method('all')
      ->willReturnCallback(function () {
        $messages = $this->messages;
        $this->messages = NULL;
        return $messages;
      });

    $this->container->set('messenger', $this->messenger);

    return $actionProcessor;
    \Drupal::setContainer($this->container);
  }

  /**
@@ -69,9 +77,9 @@ class ViewsBulkOperationsBatchTest extends UnitTestCase {
      'list' => [[0, 'en', 'node', 1]],
      'some_data' => [],
      'action_label' => '',
      'finished_callback' => [TestViewsBulkOperationsBatch::class, 'finished'],
      'finished_callback' => [ViewsBulkOperationsBatch::class, 'finished'],
    ];
    $batch = TestViewsBulkOperationsBatch::getBatch($data);
    $batch = ViewsBulkOperationsBatch::getBatch($data);
    $this->assertArrayHasKey('title', $batch);
    $this->assertArrayHasKey('operations', $batch);
    $this->assertArrayHasKey('finished', $batch);
@@ -93,8 +101,8 @@ class ViewsBulkOperationsBatchTest extends UnitTestCase {
      ],
      'api_version' => '1',
    ];
    TestViewsBulkOperationsBatch::finished(TRUE, $results, []);
    $this->assertEquals('Action processing results: Some operation (2).', TestViewsBulkOperationsBatch::message());
    ViewsBulkOperationsBatch::finished(TRUE, $results, []);
    $this->assertEquals('Action processing results: Some operation (2).', $this->messenger->all());

    $results = [
      'operations' => [
@@ -112,8 +120,8 @@ class ViewsBulkOperationsBatchTest extends UnitTestCase {
      'api_version' => '1',
    ];

    TestViewsBulkOperationsBatch::finished(TRUE, $results, []);
    $this->assertEquals('Action processing results: Some operation1 (1). | Action processing results: Some operation2 (1).', TestViewsBulkOperationsBatch::message());
    ViewsBulkOperationsBatch::finished(TRUE, $results, []);
    $this->assertEquals('Action processing results: Some operation1 (1). | Action processing results: Some operation2 (1).', $this->messenger->all());
  }

}