|
| 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\CustomerGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Customer\Api\CustomerRepositoryInterface; |
| 11 | +use Magento\Framework\ObjectManagerInterface; |
| 12 | +use Magento\Framework\Serialize\SerializerInterface; |
| 13 | +use Magento\GraphQl\Service\GraphQlRequest; |
| 14 | +use Magento\Store\Api\StoreRepositoryInterface; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\TestFramework\Mail\Template\TransportBuilderMock; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test creating a customer through GraphQL |
| 21 | + * |
| 22 | + * @magentoAppArea graphql |
| 23 | + */ |
| 24 | +class CreateCustomerTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var ObjectManagerInterface |
| 28 | + */ |
| 29 | + private $objectManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var GraphQlRequest |
| 33 | + */ |
| 34 | + private $graphQlRequest; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var SerializerInterface |
| 38 | + */ |
| 39 | + private $json; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var CustomerRepositoryInterface |
| 43 | + */ |
| 44 | + private $customerRepository; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var StoreRepositoryInterface |
| 48 | + */ |
| 49 | + private $storeRepository; |
| 50 | + |
| 51 | + public function setUp(): void |
| 52 | + { |
| 53 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 54 | + $this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class); |
| 55 | + $this->json = $this->objectManager->get(SerializerInterface::class); |
| 56 | + |
| 57 | + $this->customerRepository = $this->objectManager->create(CustomerRepositoryInterface::class); |
| 58 | + $this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Test that creating a customer sends an email |
| 63 | + */ |
| 64 | + public function testCreateCustomerSendsEmail() |
| 65 | + { |
| 66 | + $query |
| 67 | + = <<<QUERY |
| 68 | +mutation createAccount { |
| 69 | + createCustomer( |
| 70 | + input: { |
| 71 | + |
| 72 | + firstname: "Test" |
| 73 | + lastname: "Magento" |
| 74 | + password: "T3stP4assw0rd" |
| 75 | + is_subscribed: false |
| 76 | + } |
| 77 | + ) { |
| 78 | + customer { |
| 79 | + id |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +QUERY; |
| 84 | + |
| 85 | + $response = $this->graphQlRequest->send($query); |
| 86 | + $responseData = $this->json->unserialize($response->getContent()); |
| 87 | + |
| 88 | + // Assert the response of the GraphQL request |
| 89 | + $this->assertNull($responseData['data']['createCustomer']['customer']['id']); |
| 90 | + |
| 91 | + // Verify the customer was created and has the correct data |
| 92 | + $customer = $this-> customerRepository-> get( '[email protected]'); |
| 93 | + $this->assertEquals('Test', $customer->getFirstname()); |
| 94 | + $this->assertEquals('Magento', $customer->getLastname()); |
| 95 | + |
| 96 | + /** @var TransportBuilderMock $transportBuilderMock */ |
| 97 | + $transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class); |
| 98 | + $sentMessage = $transportBuilderMock->getSentMessage(); |
| 99 | + |
| 100 | + // Verify an email was dispatched to the correct user |
| 101 | + $this->assertNotNull($sentMessage); |
| 102 | + $this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName()); |
| 103 | + $this-> assertEquals( '[email protected]', $sentMessage-> getTo()[ 0]-> getEmail()); |
| 104 | + |
| 105 | + // Assert the email contains the expected content |
| 106 | + $this->assertEquals('Welcome to Main Website Store', $sentMessage->getSubject()); |
| 107 | + $messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent(); |
| 108 | + $this->assertStringContainsString('Welcome to Main Website Store.', $messageRaw); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test that creating a customer on an alternative store sends an email |
| 113 | + * |
| 114 | + * @magentoDataFixture Magento/CustomerGraphQl/_files/website_store_with_store_view.php |
| 115 | + */ |
| 116 | + public function testCreateCustomerForStoreSendsEmail() |
| 117 | + { |
| 118 | + $query |
| 119 | + = <<<QUERY |
| 120 | +mutation createAccount { |
| 121 | + createCustomer( |
| 122 | + input: { |
| 123 | + |
| 124 | + firstname: "Test" |
| 125 | + lastname: "Magento" |
| 126 | + password: "T3stP4assw0rd" |
| 127 | + is_subscribed: false |
| 128 | + } |
| 129 | + ) { |
| 130 | + customer { |
| 131 | + id |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | +QUERY; |
| 136 | + |
| 137 | + $response = $this->graphQlRequest->send( |
| 138 | + $query, |
| 139 | + [], |
| 140 | + '', |
| 141 | + [ |
| 142 | + 'Store' => 'test_store_view' |
| 143 | + ] |
| 144 | + ); |
| 145 | + $responseData = $this->json->unserialize($response->getContent()); |
| 146 | + |
| 147 | + // Assert the response of the GraphQL request |
| 148 | + $this->assertNull($responseData['data']['createCustomer']['customer']['id']); |
| 149 | + |
| 150 | + // Verify the customer was created and has the correct data |
| 151 | + $customer = $this-> customerRepository-> get( '[email protected]'); |
| 152 | + $this->assertEquals('Test', $customer->getFirstname()); |
| 153 | + $this->assertEquals('Magento', $customer->getLastname()); |
| 154 | + $this->assertEquals('Test Store View', $customer->getCreatedIn()); |
| 155 | + |
| 156 | + $store = $this->storeRepository->getById($customer->getStoreId()); |
| 157 | + $this->assertEquals('test_store_view', $store->getCode()); |
| 158 | + |
| 159 | + /** @var TransportBuilderMock $transportBuilderMock */ |
| 160 | + $transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class); |
| 161 | + $sentMessage = $transportBuilderMock->getSentMessage(); |
| 162 | + |
| 163 | + // Verify an email was dispatched to the correct user |
| 164 | + $this->assertNotNull($sentMessage); |
| 165 | + $this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName()); |
| 166 | + $this-> assertEquals( '[email protected]', $sentMessage-> getTo()[ 0]-> getEmail()); |
| 167 | + |
| 168 | + // Assert the email contains the expected content |
| 169 | + $this->assertEquals('Welcome to Test Group', $sentMessage->getSubject()); |
| 170 | + $messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent(); |
| 171 | + $this->assertStringContainsString('Welcome to Test Group.', $messageRaw); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Test that creating a customer on an alternative store sends an email in the translated language |
| 176 | + * |
| 177 | + * @magentoDataFixture Magento/CustomerGraphQl/_files/website_store_with_store_view.php |
| 178 | + * @magentoConfigFixture test_store_view_store general/locale/code fr_FR |
| 179 | + * @magentoComponentsDir Magento/CustomerGraphQl/_files |
| 180 | + */ |
| 181 | + public function testCreateCustomerForStoreSendsTranslatedEmail() |
| 182 | + { |
| 183 | + $query |
| 184 | + = <<<QUERY |
| 185 | +mutation createAccount { |
| 186 | + createCustomer( |
| 187 | + input: { |
| 188 | + |
| 189 | + firstname: "Test" |
| 190 | + lastname: "Magento" |
| 191 | + password: "T3stP4assw0rd" |
| 192 | + is_subscribed: false |
| 193 | + } |
| 194 | + ) { |
| 195 | + customer { |
| 196 | + id |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | +QUERY; |
| 201 | + |
| 202 | + $response = $this->graphQlRequest->send( |
| 203 | + $query, |
| 204 | + [], |
| 205 | + '', |
| 206 | + [ |
| 207 | + 'Store' => 'test_store_view' |
| 208 | + ] |
| 209 | + ); |
| 210 | + $responseData = $this->json->unserialize($response->getContent()); |
| 211 | + |
| 212 | + // Assert the response of the GraphQL request |
| 213 | + $this->assertNull($responseData['data']['createCustomer']['customer']['id']); |
| 214 | + |
| 215 | + // Verify the customer was created and has the correct data |
| 216 | + $customer = $this-> customerRepository-> get( '[email protected]'); |
| 217 | + $this->assertEquals('Test', $customer->getFirstname()); |
| 218 | + $this->assertEquals('Magento', $customer->getLastname()); |
| 219 | + $this->assertEquals('Test Store View', $customer->getCreatedIn()); |
| 220 | + |
| 221 | + $store = $this->storeRepository->getById($customer->getStoreId()); |
| 222 | + $this->assertEquals('test_store_view', $store->getCode()); |
| 223 | + |
| 224 | + /** @var TransportBuilderMock $transportBuilderMock */ |
| 225 | + $transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class); |
| 226 | + $sentMessage = $transportBuilderMock->getSentMessage(); |
| 227 | + |
| 228 | + // Verify an email was dispatched to the correct user |
| 229 | + $this->assertNotNull($sentMessage); |
| 230 | + $this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName()); |
| 231 | + $this-> assertEquals( '[email protected]', $sentMessage-> getTo()[ 0]-> getEmail()); |
| 232 | + |
| 233 | + // Assert the email contains the expected content |
| 234 | + $this->assertEquals('Bienvenue sur Test Group', $sentMessage->getSubject()); |
| 235 | + $messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent(); |
| 236 | + $this->assertStringContainsString('Bienvenue sur Test Group.', $messageRaw); |
| 237 | + } |
| 238 | +} |
0 commit comments