|
| 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\Test\Fixture\Customer; |
| 11 | +use Magento\Framework\Exception\AuthenticationException; |
| 12 | +use Magento\Framework\ObjectManagerInterface; |
| 13 | +use Magento\Framework\Serialize\SerializerInterface; |
| 14 | +use Magento\GraphQl\Service\GraphQlRequest; |
| 15 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 16 | +use Magento\TestFramework\Fixture\DataFixture; |
| 17 | +use Magento\TestFramework\Fixture\DataFixtureStorage; |
| 18 | +use Magento\TestFramework\Fixture\DataFixtureStorageManager; |
| 19 | +use Magento\TestFramework\Fixture\DbIsolation; |
| 20 | +use Magento\TestFramework\Helper\Bootstrap; |
| 21 | +use Magento\TestFramework\Mail\Template\TransportBuilderMock; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +class ChangePasswordTest extends TestCase |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var ObjectManagerInterface |
| 28 | + */ |
| 29 | + private $objectManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var DataFixtureStorage |
| 33 | + */ |
| 34 | + private $fixtures; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var GraphQlRequest |
| 38 | + */ |
| 39 | + private $graphQlRequest; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var SerializerInterface |
| 43 | + */ |
| 44 | + private $json; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var CustomerTokenServiceInterface |
| 48 | + */ |
| 49 | + private $customerTokenService; |
| 50 | + |
| 51 | + public function setUp(): void |
| 52 | + { |
| 53 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 54 | + $this->fixtures = DataFixtureStorageManager::getStorage(); |
| 55 | + $this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class); |
| 56 | + $this->json = $this->objectManager->get(SerializerInterface::class); |
| 57 | + $this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Test that change password sends an email |
| 62 | + * |
| 63 | + * @magentoAppArea graphql |
| 64 | + * @throws AuthenticationException |
| 65 | + */ |
| 66 | + #[ |
| 67 | + DbIsolation(false), |
| 68 | + DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'), |
| 69 | + ] |
| 70 | + public function testChangePasswordSendsEmail(): void |
| 71 | + { |
| 72 | + $currentPassword = 'password'; |
| 73 | + $query |
| 74 | + = <<<QUERY |
| 75 | +mutation { |
| 76 | + changeCustomerPassword( |
| 77 | + currentPassword: "$currentPassword" |
| 78 | + newPassword: "T3stP4assw0rd" |
| 79 | + ) { |
| 80 | + id |
| 81 | + email |
| 82 | + } |
| 83 | +} |
| 84 | +QUERY; |
| 85 | + $customer = $this->fixtures->get('customer'); |
| 86 | + $response = $this->graphQlRequest->send( |
| 87 | + $query, |
| 88 | + [], |
| 89 | + '', |
| 90 | + $this->getCustomerAuthHeaders($customer->getEmail(), $currentPassword) |
| 91 | + ); |
| 92 | + $responseData = $this->json->unserialize($response->getContent()); |
| 93 | + |
| 94 | + // Assert the response of the GraphQL request |
| 95 | + $this->assertNull($responseData['data']['changeCustomerPassword']['id']); |
| 96 | + $this->assertEquals($customer->getEmail(), $responseData['data']['changeCustomerPassword']['email']); |
| 97 | + |
| 98 | + /** @var TransportBuilderMock $transportBuilderMock */ |
| 99 | + $transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class); |
| 100 | + $sentMessage = $transportBuilderMock->getSentMessage(); |
| 101 | + |
| 102 | + // Verify an email was dispatched to the correct user |
| 103 | + $this->assertNotNull($sentMessage); |
| 104 | + $this->assertEquals($customer->getName(), $sentMessage->getTo()[0]->getName()); |
| 105 | + $this->assertEquals($customer->getEmail(), $sentMessage->getTo()[0]->getEmail()); |
| 106 | + |
| 107 | + // Assert the email contains the expected content |
| 108 | + $this->assertEquals('Your Main Website Store password has been changed', $sentMessage->getSubject()); |
| 109 | + $messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent(); |
| 110 | + $this->assertStringContainsString( |
| 111 | + 'We have received a request to change the following information associated with your account', |
| 112 | + $messageRaw |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param string $email |
| 118 | + * @param string $password |
| 119 | + * @return array |
| 120 | + * @throws AuthenticationException |
| 121 | + */ |
| 122 | + private function getCustomerAuthHeaders(string $email, string $password): array |
| 123 | + { |
| 124 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password); |
| 125 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 126 | + } |
| 127 | +} |
0 commit comments