You are here

public function ConfigEntityRevisionsRevertFormBaseTest::setup in Config Entity Revisions 8

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/ConfigEntityRevisionsRevertFormBaseTest.php \Drupal\Tests\config_entity_revisions\Unit\ConfigEntityRevisionsRevertFormBaseTest::setup()
  2. 1.x tests/src/Unit/ConfigEntityRevisionsRevertFormBaseTest.php \Drupal\Tests\config_entity_revisions\Unit\ConfigEntityRevisionsRevertFormBaseTest::setup()

Set up for a test.

File

tests/src/Unit/ConfigEntityRevisionsRevertFormBaseTest.php, line 73

Class

ConfigEntityRevisionsRevertFormBaseTest
Test ability to revert (form) configurations.

Namespace

Drupal\Tests\config_entity_revisions\Unit

Code

public function setup() {
  $entityTypeManager = $this
    ->prophesize(EntityTypeManager::CLASS);
  $dateFormatter = $this
    ->prophesize(DateFormatter::CLASS);
  $dateFormatter
    ->format(Argument::any())
    ->will(function ($calls) {
    return $calls[count($calls) - 1];
  });
  $dateTime = $this
    ->prophesize(TimeInterface::CLASS);
  $dateTime
    ->getRequestTime()
    ->willReturn(1260);
  $this->mockOldRevision = $this
    ->prophesize(ConfigEntityRevisions::CLASS);
  $this->mockOldRevision
    ->id()
    ->willReturn(2);
  $this->mockOldRevision
    ->getRevisionId()
    ->willReturn(300);
  $this->mockOldRevision
    ->getRevisionCreationTime()
    ->willReturn(1200);
  $this->mockOldRevision
    ->isPublished()
    ->willReturn(TRUE);
  $this->mockOldRevision
    ->get(Argument::type('string'))
    ->willReturn((object) [
    'value' => 'serialisedConfiguration',
  ]);
  $this->mockDefaultRevision = $this
    ->prophesize(ConfigEntityRevisions::CLASS);
  $this->mockDefaultRevision
    ->id()
    ->willReturn(2);
  $this->mockDefaultRevision
    ->getRevisionId()
    ->willReturn(321);
  $this->mockDefaultRevision
    ->getRevisionCreationTime()
    ->willReturn(1234);
  $this->mockDefaultRevision
    ->isPublished()
    ->willReturn(TRUE);
  $this->mockNewRevision = $this
    ->prophesize(ConfigEntityRevisions::CLASS);
  $this->mockNewRevision
    ->id()
    ->willReturn(2);
  $this->mockNewRevision
    ->getRevisionId()
    ->willReturn(324);
  $this->mockNewRevision
    ->getRevisionCreationTime()
    ->willReturn(1245);
  $this->mockNewRevision
    ->isPublished()
    ->willReturn(FALSE);
  $mockContentStorage = $this
    ->prophesize(ContentEntityStorageBase::CLASS);
  $mockContentStorage
    ->loadRevision(300)
    ->willReturn($this->mockOldRevision
    ->reveal());
  $mockContentStorage
    ->loadRevision(321)
    ->willReturn($this->mockDefaultRevision
    ->reveal());
  $mockContentStorage
    ->loadRevision(324)
    ->willReturn($this->mockNewRevision
    ->reveal());
  $config_entity = $this
    ->prophesize(ConfigEntityRevisionsInterface::CLASS);
  $config_entity
    ->contentEntityStorage()
    ->willReturn($mockContentStorage);
  $config_entity
    ->getContentEntity()
    ->willReturn($this->mockDefaultRevision
    ->reveal());
  $config_entity
    ->getEntityTypeId()
    ->willReturn('my_entity_type');
  $config_entity
    ->id()
    ->willReturn('foozbar');
  $config_entity
    ->module_name()
    ->willReturn('module_name');
  $config_entity
    ->label()
    ->willReturn('config_entity_label');
  $config_entity
    ->title()
    ->willReturn('config_entity_title');
  $this->configEntity = $config_entity;
  $mock_request = new Request();
  $request_stack = $this
    ->prophesize(RequestStack::CLASS);
  $request_stack
    ->getCurrentRequest()
    ->willReturn($mock_request);
  $string_translation = $this
    ->prophesize(TranslationManager::CLASS);
  $currentUser = $this
    ->prophesize(AccountInterface::CLASS);
  $currentUser
    ->id()
    ->willReturn(1717);
  $this->serializer = new MockSerializer($this);
  $context =& $this;
  $logger = $this
    ->prophesize(LoggerInterface::CLASS);
  $logger
    ->notice(Argument::type('string'), Argument::type('array'))
    ->will(function ($args) use ($context) {
    $context
      ->assertEquals('@form: set @form to revision %revision.', $args[0]);
    $context
      ->assertEquals([
      '@form' => 'config_entity_label',
      '%revision' => 300,
    ], $args[1]);
  });
  $loggerFactory = $this
    ->prophesize(LoggerChannelFactory::CLASS);
  $loggerFactory
    ->get('content')
    ->willReturn($logger
    ->reveal());
  $messenger = $this
    ->prophesize(Messenger::CLASS);
  $messenger
    ->addMessage(Argument::type('Drupal\\Core\\StringTranslation\\TranslatableMarkup'))
    ->will(function ($calls) use ($context) {
    $message = $calls[0];
    $context
      ->assertEquals('%entity_title %title has been set to the revision from %revision-date.', $message
      ->getUntranslatedString());
    $context
      ->assertEquals([
      '%entity_title' => 'config_entity_title',
      '%title' => 'config_entity_label',
      '%revision-date' => 1200,
    ], $message
      ->getArguments());
  });
  $container = new ContainerBuilder();
  $container
    ->set('entity_type.manager', $entityTypeManager
    ->reveal());
  $container
    ->set('date.formatter', $dateFormatter
    ->reveal());
  $container
    ->set('datetime.time', $dateTime
    ->reveal());
  $container
    ->set('request_stack', $request_stack
    ->reveal());
  $container
    ->set('string_translation', $string_translation
    ->reveal());
  $container
    ->set('current_user', $currentUser
    ->reveal());
  $container
    ->set('serializer', $this->serializer);
  $container
    ->set('logger.factory', $loggerFactory
    ->reveal());
  $container
    ->set('messenger', $messenger
    ->reveal());
  $this->container = $container;
}