You are here

protected function MailHandlerTest::getMockSender in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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.

1 call to MailHandlerTest::getMockSender()
MailHandlerTest::getSendMailMessages in core/modules/contact/tests/src/Unit/MailHandlerTest.php
Data provider for ::testSendMailMessages.

File

core/modules/contact/tests/src/Unit/MailHandlerTest.php, line 298
Contains \Drupal\Tests\contact\Unit\MailHandlerTest.

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
    ->getMock('\\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('getUsername')
    ->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;
}