function WorkspacePublishFormTest::testSubmitFormWithException

Same name in other branches
  1. 10 core/modules/workspaces/tests/src/Kernel/WorkspacePublishFormTest.php \Drupal\Tests\workspaces\Kernel\WorkspacePublishFormTest::testSubmitFormWithException()

@covers ::submitForm

File

core/modules/workspaces/tests/src/Kernel/WorkspacePublishFormTest.php, line 27

Class

WorkspacePublishFormTest
@coversDefaultClass \Drupal\workspaces\Form\WorkspacePublishForm @group workspaces

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testSubmitFormWithException() : void {
    
    /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */
    $messenger = \Drupal::service('messenger');
    $workspaceOperationFactory = $this->createMock(WorkspaceOperationFactory::class);
    $entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
    $logger = $this->createMock(LoggerInterface::class);
    
    /** @var \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory */
    $loggerFactory = \Drupal::service('logger.factory');
    $loggerFactory->addLogger($logger);
    $workspace = $this->createMock(Workspace::class);
    $workspacePublisher = $this->createMock(WorkspacePublisherInterface::class);
    $workspace->expects($this->any())
        ->method('label');
    $workspace->expects($this->once())
        ->method('publish')
        ->willThrowException(new \Exception('Unexpected error'));
    $workspaceOperationFactory->expects($this->once())
        ->method('getPublisher')
        ->willReturn($workspacePublisher);
    $workspacePublisher->expects($this->once())
        ->method('getTargetLabel');
    $publishForm = new WorkspacePublishForm($workspaceOperationFactory, $entityTypeManager);
    $form = [];
    $formState = new FormState();
    $publishForm->buildForm($form, $formState, $workspace);
    $logger->expects($this->once())
        ->method('log')
        ->with(RfcLogLevel::ERROR, 'Unexpected error');
    $publishForm->submitForm($form, $formState);
    $messages = $messenger->messagesByType(MessengerInterface::TYPE_ERROR);
    $this->assertCount(1, $messages);
    $this->assertEquals('Publication failed. All errors have been logged.', $messages[0]);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.