You are here

class ScheduledTransitionsUtilityUnitTest in Scheduled Transitions 2.x

Tests scheduled transactions utility.

@coversDefaultClass \Drupal\scheduled_transitions\ScheduledTransitionsUtility @group scheduled_transitions

Hierarchy

Expanded class hierarchy of ScheduledTransitionsUtilityUnitTest

File

tests/src/Unit/ScheduledTransitionsUtilityUnitTest.php, line 27

Namespace

Drupal\Tests\scheduled_transitions\Unit
View source
class ScheduledTransitionsUtilityUnitTest extends UnitTestCase {

  /**
   * Tests revision log generator.
   *
   * @param string $transitioningRevisionId
   *   Revision ID of the transitioning revision.
   * @param string $latestRevisionId
   *   Revision ID of the latest revision of an entity..
   * @param array $options
   *   Scheduled transitions entity options.
   * @param string $expectedRevisionLog
   *   The expected revision log.
   *
   * @covers ::generateRevisionLog
   * @dataProvider providerGenerateRevisionLog
   */
  public function testGenerateRevisionLog(string $transitioningRevisionId, string $latestRevisionId, array $options, string $expectedRevisionLog) : void {
    $configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $settings = $this
      ->createMock(ImmutableConfig::class);
    $settings
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap([
      [
        'message_transition_latest',
        'template for latest revision',
      ],
      [
        'message_transition_historical',
        'template for historical revision',
      ],
    ]);
    $configFactory
      ->expects($this
      ->any())
      ->method('get')
      ->with('scheduled_transitions.settings')
      ->willReturn($settings);
    $cache = $this
      ->createMock(CacheBackendInterface::class);
    $entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entityStorage = $this
      ->createMock(RevisionableStorageInterface::class);
    $latest = $this
      ->createMock(RevisionLogInterface::class);
    $latest
      ->expects($this
      ->any())
      ->method('getRevisionId')
      ->willReturn($latestRevisionId);
    $entityStorage
      ->expects($this
      ->once())
      ->method('getLatestRevisionId')
      ->with('1337')
      ->willReturn('2000');
    $entityStorage
      ->expects($this
      ->once())
      ->method('loadRevision')
      ->with('2000')
      ->willReturn($latest);
    $entityTypeManager
      ->expects($this
      ->once())
      ->method('getStorage')
      ->with('test_entity_type')
      ->willReturn($entityStorage);
    $bundleInfo = $this
      ->createMock(EntityTypeBundleInfoInterface::class);
    $moderationInformation = $this
      ->createMock(ModerationInformationInterface::class);
    $token = $this
      ->createMock(Token::class);
    $token
      ->expects($this
      ->once())
      ->method('replace')
      ->willReturnArgument(0);
    $translation = $this
      ->createMock(TranslationInterface::class);
    $utility = new ScheduledTransitionsUtility($configFactory, $cache, $entityTypeManager, $bundleInfo, $moderationInformation, $token, $translation);
    $scheduledTransition = $this
      ->createMock(ScheduledTransitionInterface::class);
    $scheduledTransition
      ->expects($this
      ->once())
      ->method('getOptions')
      ->willReturn($options);
    $newRevision = $this
      ->createMock(RevisionLogInterface::class);
    $newRevision
      ->expects($this
      ->once())
      ->method('id')
      ->willReturn(1337);
    $newRevision
      ->expects($this
      ->once())
      ->method('getEntityTypeId')
      ->willReturn('test_entity_type');
    $newRevision
      ->expects($this
      ->any())
      ->method('getRevisionId')
      ->willReturn($transitioningRevisionId);
    $this
      ->assertEquals($expectedRevisionLog, $utility
      ->generateRevisionLog($scheduledTransition, $newRevision));
  }

  /**
   * Data provider for testGenerateRevisionLog.
   */
  public function providerGenerateRevisionLog() : array {
    $scenarios = [];
    $scenarios['historical'] = [
      // Transitioning different revisions.
      '333',
      '444',
      [],
      'template for historical revision',
    ];
    $scenarios['latest'] = [
      // Revision IDs are the same:
      '444',
      '444',
      [],
      'template for latest revision',
    ];
    $scenarios['custom'] = [
      // Revision IDs are irrelevant.
      '444',
      '444',
      [
        'revision_log_override' => TRUE,
        'revision_log' => 'custom revision log',
      ],
      'custom revision log',
    ];
    return $scenarios;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
ScheduledTransitionsUtilityUnitTest::providerGenerateRevisionLog public function Data provider for testGenerateRevisionLog.
ScheduledTransitionsUtilityUnitTest::testGenerateRevisionLog public function Tests revision log generator.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 308
UnitTestCase::setUpBeforeClass public static function