|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Customer\Model\AccountManagement; |
| 9 | + |
| 10 | +use Magento\Customer\Api\AccountManagementInterface; |
| 11 | +use Magento\Customer\Api\Data\CustomerInterface; |
| 12 | +use Magento\Customer\Api\Data\CustomerInterfaceFactory; |
| 13 | +use Magento\Framework\Api\DataObjectHelper; |
| 14 | +use Magento\Framework\Exception\InputException; |
| 15 | +use Magento\Framework\Exception\LocalizedException; |
| 16 | +use Magento\Framework\Math\Random; |
| 17 | +use Magento\Framework\ObjectManagerInterface; |
| 18 | +use Magento\Framework\Validator\Exception; |
| 19 | +use Magento\TestFramework\Helper\Bootstrap; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests for customer creation via customer account management service. |
| 24 | + * |
| 25 | + * @magentoAppArea frontend |
| 26 | + * @magentoDbIsolation enabled |
| 27 | + */ |
| 28 | +class CreateAccountTest extends TestCase |
| 29 | +{ |
| 30 | + /** |
| 31 | + * @var ObjectManagerInterface |
| 32 | + */ |
| 33 | + private $objectManager; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var AccountManagementInterface |
| 37 | + */ |
| 38 | + private $accountManagement; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var CustomerInterfaceFactory |
| 42 | + */ |
| 43 | + private $customerFactory; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var DataObjectHelper |
| 47 | + */ |
| 48 | + private $dataObjectHelper; |
| 49 | + |
| 50 | + /** |
| 51 | + * @var array |
| 52 | + */ |
| 53 | + private $defaultCustomerData = [ |
| 54 | + 'email' => 'customer@example.com', |
| 55 | + 'firstname' => 'First name', |
| 56 | + 'lastname' => 'Last name', |
| 57 | + ]; |
| 58 | + |
| 59 | + /** |
| 60 | + * @inheritdoc |
| 61 | + */ |
| 62 | + protected function setUp() |
| 63 | + { |
| 64 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 65 | + $this->accountManagement = $this->objectManager->get(AccountManagementInterface::class); |
| 66 | + $this->customerFactory = $this->objectManager->get(CustomerInterfaceFactory::class); |
| 67 | + $this->dataObjectHelper = $this->objectManager->create(DataObjectHelper::class); |
| 68 | + parent::setUp(); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @dataProvider createInvalidAccountDataProvider |
| 73 | + * @param array $customerData |
| 74 | + * @param string $password |
| 75 | + * @param string $errorType |
| 76 | + * @param string $errorMessage |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + public function testCreateAccountWithInvalidFields( |
| 80 | + array $customerData, |
| 81 | + string $password, |
| 82 | + string $errorType, |
| 83 | + array $errorMessage |
| 84 | + ): void { |
| 85 | + $data = array_merge($this->defaultCustomerData, $customerData); |
| 86 | + $customerEntity = $this->customerFactory->create(); |
| 87 | + $this->dataObjectHelper->populateWithArray($customerEntity, $data, CustomerInterface::class); |
| 88 | + $this->expectException($errorType); |
| 89 | + $this->expectExceptionMessage((string)__(...$errorMessage)); |
| 90 | + $this->accountManagement->createAccount($customerEntity, $password); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @return array |
| 95 | + */ |
| 96 | + public function createInvalidAccountDataProvider(): array |
| 97 | + { |
| 98 | + return [ |
| 99 | + 'empty_firstname' => [ |
| 100 | + 'customer_data' => ['firstname' => ''], |
| 101 | + 'password' => '_aPassword1', |
| 102 | + 'error_type' => Exception::class, |
| 103 | + 'error_message' => ['"%1" is a required value.', 'First Name'], |
| 104 | + ], |
| 105 | + 'empty_lastname' => [ |
| 106 | + 'customer_data' => ['lastname' => ''], |
| 107 | + 'password' => '_aPassword1', |
| 108 | + 'error_type' => Exception::class, |
| 109 | + 'error_message' => ['"%1" is a required value.', 'Last Name'], |
| 110 | + ], |
| 111 | + 'empty_email' => [ |
| 112 | + 'customer_data' => ['email' => ''], |
| 113 | + 'password' => '_aPassword1', |
| 114 | + 'error_type' => Exception::class, |
| 115 | + 'error_message' => ['The customer email is missing. Enter and try again.'], |
| 116 | + ], |
| 117 | + 'invalid_email' => [ |
| 118 | + 'customer_data' => ['email' => 'zxczxczxc'], |
| 119 | + 'password' => '_aPassword1', |
| 120 | + 'error_type' => Exception::class, |
| 121 | + 'error_message' => ['"%1" is not a valid email address.', 'Email'], |
| 122 | + ], |
| 123 | + 'empty_password' => [ |
| 124 | + 'customer_data' => [], |
| 125 | + 'password' => '', |
| 126 | + 'error_type' => InputException::class, |
| 127 | + 'error_message' => ['The password needs at least 8 characters. Create a new password and try again.'], |
| 128 | + ], |
| 129 | + 'invalid_password_minimum_length' => [ |
| 130 | + 'customer_data' => [], |
| 131 | + 'password' => 'test', |
| 132 | + 'error_type' => InputException::class, |
| 133 | + 'error_message' => ['The password needs at least 8 characters. Create a new password and try again.'], |
| 134 | + ], |
| 135 | + 'invalid_password_maximum_length' => [ |
| 136 | + 'customer_data' => [], |
| 137 | + 'password' => $this->getRandomNumericString(257), |
| 138 | + 'error_type' => InputException::class, |
| 139 | + 'error_message' => ['Please enter a password with at most 256 characters.'], |
| 140 | + ], |
| 141 | + 'invalid_password_without_minimum_characters_classes' => [ |
| 142 | + 'customer_data' => [], |
| 143 | + 'password' => 'test_password', |
| 144 | + 'error_type' => InputException::class, |
| 145 | + 'error_message' => [ |
| 146 | + 'Minimum of different classes of characters in password is %1.' |
| 147 | + . ' Classes of characters: Lower Case, Upper Case, Digits, Special Characters.', |
| 148 | + 3, |
| 149 | + ], |
| 150 | + ], |
| 151 | + 'password_same_as_email' => [ |
| 152 | + 'customer_data' => ['email' => 'test1@test.com'], |
| 153 | + 'password' => 'test1@test.com', |
| 154 | + 'error_type' => LocalizedException::class, |
| 155 | + 'error_message' => [ |
| 156 | + 'The password can\'t be the same as the email address. Create a new password and try again.', |
| 157 | + ], |
| 158 | + ], |
| 159 | + ]; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Returns random numeric string with given length. |
| 164 | + * |
| 165 | + * @param int $length |
| 166 | + * @return string |
| 167 | + */ |
| 168 | + private function getRandomNumericString(int $length): string |
| 169 | + { |
| 170 | + $string = ''; |
| 171 | + for ($i = 0; $i <= $length; $i++) { |
| 172 | + $string .= Random::getRandomNumber(0, 9); |
| 173 | + } |
| 174 | + |
| 175 | + return $string; |
| 176 | + } |
| 177 | +} |
0 commit comments