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. 8.9.x core/modules/user/tests/src/Unit/Plugin/Validation/Constraint/UserMailRequiredValidatorTest.php \Drupal\Tests\user\Unit\Plugin\Validation\Constraint\UserMailRequiredValidatorTest::providerTestValidate()
  3. 10 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 83

Class

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

Namespace

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

Code

public static function providerTestValidate() {
    $prophet = new Prophet();
    $cases = [];
    // Case 1: Empty user should be ignored.
    $items = $prophet->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 = $prophet->prophesize(FieldItemListInterface::class);
    $account = $prophet->prophesize(UserInterface::class);
    $account->isNew()
        ->willReturn(TRUE);
    $account->id()
        ->shouldNotBeCalled();
    $field_definition = $prophet->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 = $prophet->prophesize(FieldItemListInterface::class);
    $account = $prophet->prophesize(UserInterface::class);
    $account->isNew()
        ->willReturn(FALSE);
    $account->id()
        ->willReturn(3);
    $field_definition = $prophet->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 email is valid.
    $items = $prophet->prophesize(FieldItemListInterface::class);
    $account = $prophet->prophesize(UserInterface::class);
    $account->isNew()
        ->willReturn(TRUE);
    $account->id()
        ->shouldNotBeCalled();
    $field_definition = $prophet->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 email is valid'] = [
        $items->reveal(),
        FALSE,
    ];
    // Case 5: Existing users with an email should be ignored.
    $items = $prophet->prophesize(FieldItemListInterface::class);
    $account = $prophet->prophesize(UserInterface::class);
    $account->isNew()
        ->willReturn(FALSE);
    $account->id()
        ->willReturn(3);
    $field_definition = $prophet->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 = $prophet->prophesize(FieldItemListInterface::class);
    $account = $prophet->prophesize(UserInterface::class);
    $account->isNew()
        ->willReturn(FALSE);
    $account->id()
        ->willReturn(3);
    $field_definition = $prophet->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.