function UserMailRequiredValidatorTest::providerTestValidate

Same name in other branches
  1. 9 core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/UserMailRequiredValidatorTest.php \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\UserMailRequiredValidatorTest::providerTestValidate()
  2. 10 core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/UserMailRequiredValidatorTest.php \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\UserMailRequiredValidatorTest::providerTestValidate()
  3. 11.x core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/UserMailRequiredValidatorTest.php \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\UserMailRequiredValidatorTest::providerTestValidate()

Data provider for ::testValidate().

File

core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/UserMailRequiredValidatorTest.php, line 80

Class

UserMailRequiredValidatorTest
@coversDefaultClass \Drupal\user\Plugin\Validation\Constraint\UserMailRequiredValidator @group user

Namespace

Drupal\Tests\user\Unit\Plugin\Validation\Constraint

Code

public function providerTestValidate() {
    $cases = [];
    // Case 1: Empty user should be ignored.
    $items = $this->prophesize(FieldItemListInterface::class);
    $items->getEntity()
        ->willReturn(NULL)
        ->shouldBeCalledTimes(1);
    $cases['Empty user should be ignored'] = [
        $items->reveal(),
        FALSE,
    ];
    // Case 2: New users without an email should add a violation.
    $items = $this->prophesize(FieldItemListInterface::class);
    $account = $this->prophesize(UserInterface::class);
    $account->isNew()
        ->willReturn(TRUE);
    $account->id()
        ->shouldNotBeCalled();
    $field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $field_definition->getLabel()
        ->willReturn('Email');
    $account->getFieldDefinition("mail")
        ->willReturn($field_definition->reveal())
        ->shouldBeCalledTimes(1);
    $items->getEntity()
        ->willReturn($account->reveal())
        ->shouldBeCalledTimes(1);
    $items->isEmpty()
        ->willReturn(TRUE);
    $cases['New users without an email should add a violation'] = [
        $items->reveal(),
        TRUE,
    ];
    // Case 3: Existing users without an email should add a violation.
    $items = $this->prophesize(FieldItemListInterface::class);
    $account = $this->prophesize(UserInterface::class);
    $account->isNew()
        ->willReturn(FALSE);
    $account->id()
        ->willReturn(3);
    $field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $field_definition->getLabel()
        ->willReturn('Email');
    $account->getFieldDefinition("mail")
        ->willReturn($field_definition->reveal())
        ->shouldBeCalledTimes(1);
    $items->getEntity()
        ->willReturn($account->reveal())
        ->shouldBeCalledTimes(1);
    $items->isEmpty()
        ->willReturn(TRUE);
    $cases['Existing users without an email should add a violation'] = [
        $items->reveal(),
        TRUE,
    ];
    // Case 4: New user with an e-mail is valid.
    $items = $this->prophesize(FieldItemListInterface::class);
    $account = $this->prophesize(UserInterface::class);
    $account->isNew()
        ->willReturn(TRUE);
    $account->id()
        ->shouldNotBeCalled();
    $field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $field_definition->getLabel()
        ->willReturn('Email');
    $account->getFieldDefinition("mail")
        ->willReturn($field_definition->reveal())
        ->shouldBeCalledTimes(1);
    $items->getEntity()
        ->willReturn($account->reveal())
        ->shouldBeCalledTimes(1);
    $items->isEmpty()
        ->willReturn(FALSE);
    $cases['New user with an e-mail is valid'] = [
        $items->reveal(),
        FALSE,
    ];
    // Case 5: Existing users with an email should be ignored.
    $items = $this->prophesize(FieldItemListInterface::class);
    $account = $this->prophesize(UserInterface::class);
    $account->isNew()
        ->willReturn(FALSE);
    $account->id()
        ->willReturn(3);
    $field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $field_definition->getLabel()
        ->willReturn('Email');
    $account->getFieldDefinition("mail")
        ->willReturn($field_definition->reveal())
        ->shouldBeCalledTimes(1);
    $items->getEntity()
        ->willReturn($account->reveal())
        ->shouldBeCalledTimes(1);
    $items->isEmpty()
        ->willReturn(FALSE);
    $cases['Existing users with an email should be ignored'] = [
        $items->reveal(),
        FALSE,
    ];
    // Case 6: Existing users without an email should be ignored if the current
    // user is an administrator.
    $items = $this->prophesize(FieldItemListInterface::class);
    $account = $this->prophesize(UserInterface::class);
    $account->isNew()
        ->willReturn(FALSE);
    $account->id()
        ->willReturn(3);
    $field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $field_definition->getLabel()
        ->willReturn('Email');
    $account->getFieldDefinition("mail")
        ->willReturn($field_definition->reveal())
        ->shouldBeCalledTimes(1);
    $items->getEntity()
        ->willReturn($account->reveal())
        ->shouldBeCalledTimes(1);
    $items->isEmpty()
        ->willReturn(TRUE);
    $cases['Existing users without an email should be ignored if the current user is an administrator.'] = [
        $items->reveal(),
        FALSE,
        TRUE,
    ];
    return $cases;
}

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