You are here

protected function MailHandlerTest::getMockSender in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/contact/tests/src/Unit/MailHandlerTest.php \Drupal\Tests\contact\Unit\MailHandlerTest::getMockSender()
  2. 9 core/modules/contact/tests/src/Unit/MailHandlerTest.php \Drupal\Tests\contact\Unit\MailHandlerTest::getMockSender()

Builds a mock sender on given scenario.

Parameters

bool $anonymous: TRUE if the sender is anonymous.

string $mail_address: The mail address of the user.

Return value

\Drupal\Core\Session\AccountInterface|\PHPUnit\Framework\MockObject\MockObject Mock sender for testing.

File

core/modules/contact/tests/src/Unit/MailHandlerTest.php, line 294

Class

MailHandlerTest
@coversDefaultClass \Drupal\contact\MailHandler @group contact

Namespace

Drupal\Tests\contact\Unit

Code

protected function getMockSender($anonymous = TRUE, $mail_address = 'anonymous@drupal.org') {
  $sender = $this
    ->createMock('\\Drupal\\Core\\Session\\AccountInterface');
  $sender
    ->expects($this
    ->once())
    ->method('isAnonymous')
    ->willReturn($anonymous);
  $sender
    ->expects($this
    ->any())
    ->method('getEmail')
    ->willReturn($mail_address);
  $sender
    ->expects($this
    ->any())
    ->method('getDisplayName')
    ->willReturn('user');

  // User ID 1 has special implications, use 3 instead.
  $sender
    ->expects($this
    ->any())
    ->method('id')
    ->willReturn($anonymous ? 0 : 3);
  if ($anonymous) {

    // Anonymous user values set in params include updated values for name and
    // mail.
    $sender->name = 'Anonymous (not verified)';
    $sender->mail = 'anonymous@drupal.org';
  }
  return $sender;
}