function MailHandlerTest::getMockSender

Same name in other branches
  1. 9 core/modules/contact/tests/src/Unit/MailHandlerTest.php \Drupal\Tests\contact\Unit\MailHandlerTest::getMockSender()
  2. 8.9.x core/modules/contact/tests/src/Unit/MailHandlerTest.php \Drupal\Tests\contact\Unit\MailHandlerTest::getMockSender()
  3. 10 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::testSendMailMessages in core/modules/contact/tests/src/Unit/MailHandlerTest.php
Tests the sendMailMessages method.

File

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

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(User::class);
    $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;
}

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