This commit is contained in:
2026-09-15 10:26:34 +08:00
commit ecf36afa56
10493 changed files with 1404888 additions and 0 deletions
@@ -0,0 +1,55 @@
<?php
/**
* Copyright 2023 Adobe
* All Rights Reserved.
*/
declare(strict_types=1);
namespace Magento\CustomerGraphQl\Model\Context;
use Magento\Authorization\Model\UserContextInterface;
use Magento\Authorization\Model\UserContextInterfaceFactory;
use Magento\Customer\Model\Session;
use Magento\Customer\Test\Fixture\Customer;
use Magento\GraphQl\Model\Query\ContextParametersInterface;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
class AddUserInfoToContextTest extends TestCase
{
#[
DataFixture(Customer::class, as: 'customer'),
]
public function testExecute()
{
$objectManager = Bootstrap::getObjectManager();
$service = $objectManager->get(AddUserInfoToContext::class);
$parameters = $objectManager->get(ContextParametersInterface::class);
/** @var \Magento\Customer\Model\Customer $customer */
$customer = DataFixtureStorageManager::getStorage()->get('customer');
$userId = $customer->getId();
$userType = UserContextInterface::USER_TYPE_CUSTOMER;
$context = $this->createMock(UserContextInterface::class);
$context->method('getUserId')->willReturn($userId);
$context->method('getUserType')->willReturn($userType);
$service->setUserContext($context);
$returnedParameters = $service->execute($parameters);
$this->assertEquals($userId, $returnedParameters->getUserId());
$this->assertEquals($userType, $returnedParameters->getUserType());
$extensionAttributes = $returnedParameters->getExtensionAttributesData();
$this->assertArrayHasKey('is_customer', $extensionAttributes);
$this->assertTrue($extensionAttributes['is_customer']);
$session = $objectManager->get(Session::class);
$this->assertEquals($session->getCustomer()->getData(), $customer->getData());
$this->assertEquals($session->getCustomerGroupId(), $customer->getGroupId());
}
}
@@ -0,0 +1,121 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CustomerGraphQl\Model\Resolver\Cache;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Data\Address;
use Magento\Customer\Model\Data\Customer;
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
use Magento\CustomerGraphQl\Model\Resolver\Cache\Customer\ModelDehydrator;
use Magento\CustomerGraphQl\Model\Resolver\Cache\Customer\ModelHydrator;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
class CustomerModelHydratorDehydratorTest extends TestCase
{
/**
* @var \Magento\TestFramework\ObjectManager
*/
private $objectManager;
/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;
/**
* @var ExtractCustomerData
*/
private $resolverDataExtractor;
/**
* @var SerializerInterface
*/
private $serializer;
public function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
$this->resolverDataExtractor = $this->objectManager->get(ExtractCustomerData::class);
$this->serializer = $this->objectManager->get(SerializerInterface::class);
}
/**
* @magentoDataFixture Magento/Customer/_files/customer_with_addresses.php
*/
public function testModelHydration(): void
{
$customerModel = $this->customerRepository->get('customer_with_addresses@test.com');
$resolverData = $this->resolverDataExtractor->execute($customerModel);
/** @var ModelDehydrator $dehydrator */
$dehydrator = $this->objectManager->get(ModelDehydrator::class);
$dehydrator->dehydrate($resolverData);
$serializedData = $this->serializer->serialize($resolverData);
$resolverData = $this->serializer->unserialize($serializedData);
/** @var ModelHydrator $hydrator */
$hydrator = $this->objectManager->get(ModelHydrator::class);
$hydrator->hydrate($resolverData);
$this->assertInstanceOf(Customer::class, $resolverData['model']);
$assertionMap = [
'model_id' => 'id',
'firstname' => 'firstname',
'lastname' => 'lastname'
];
foreach ($assertionMap as $resolverDataField => $modelDataField) {
$this->assertEquals(
$resolverData[$resolverDataField],
$resolverData['model']->{'get' . $this->camelize($modelDataField)}()
);
}
$this->assertEquals(
$customerModel->getExtensionAttributes(),
$resolverData['model']->getExtensionAttributes()
);
$assertionMap = [
'id' => 'id',
'customer_id' => 'customer_id',
'region_id' => 'region_id',
'country_id' => 'country_id',
'street' => 'street',
'postcode' => 'postcode',
'city' => 'city',
'firstname' => 'firstname',
'lastname' => 'lastname',
];
$addresses = $resolverData['model']->getAddresses();
foreach ($addresses as $key => $address) {
$this->assertInstanceOf(Address::class, $address);
foreach ($assertionMap as $resolverDataField => $modelDataField) {
$this->assertEquals(
$resolverData['addresses'][$key][$resolverDataField],
$address->{'get' . $this->camelize($modelDataField)}()
);
}
}
}
/**
* Transform snake case to camel case
*
* @param $string
* @param $separator
* @return string
*/
private function camelize($string, $separator = '_')
{
return str_replace($separator, '', ucwords($string, $separator));
}
}
@@ -0,0 +1,127 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CustomerGraphQl\Model\Resolver;
use Magento\Customer\Test\Fixture\Customer;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\GraphQl\Service\GraphQlRequest;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Fixture\DbIsolation;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
use PHPUnit\Framework\TestCase;
class ChangePasswordTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var DataFixtureStorage
*/
private $fixtures;
/**
* @var GraphQlRequest
*/
private $graphQlRequest;
/**
* @var SerializerInterface
*/
private $json;
/**
* @var CustomerTokenServiceInterface
*/
private $customerTokenService;
public function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->fixtures = DataFixtureStorageManager::getStorage();
$this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class);
$this->json = $this->objectManager->get(SerializerInterface::class);
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
}
/**
* Test that change password sends an email
*
* @magentoAppArea graphql
* @throws AuthenticationException
*/
#[
DbIsolation(false),
DataFixture(Customer::class, ['email' => 'customer@example.com'], as: 'customer'),
]
public function testChangePasswordSendsEmail(): void
{
$currentPassword = 'password';
$query
= <<<QUERY
mutation {
changeCustomerPassword(
currentPassword: "$currentPassword"
newPassword: "T3stP4assw0rd"
) {
id
email
}
}
QUERY;
$customer = $this->fixtures->get('customer');
$response = $this->graphQlRequest->send(
$query,
[],
'',
$this->getCustomerAuthHeaders($customer->getEmail(), $currentPassword)
);
$responseData = $this->json->unserialize($response->getContent());
// Assert the response of the GraphQL request
$this->assertNull($responseData['data']['changeCustomerPassword']['id']);
$this->assertEquals($customer->getEmail(), $responseData['data']['changeCustomerPassword']['email']);
/** @var TransportBuilderMock $transportBuilderMock */
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
$sentMessage = $transportBuilderMock->getSentMessage();
// Verify an email was dispatched to the correct user
$this->assertNotNull($sentMessage);
$this->assertEquals($customer->getName(), $sentMessage->getTo()[0]->getName());
$this->assertEquals($customer->getEmail(), $sentMessage->getTo()[0]->getEmail());
// Assert the email contains the expected content
$this->assertEquals('Your Main Website Store password has been changed', $sentMessage->getSubject());
$messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent();
$this->assertStringContainsString(
'We have received a request to change the following information associated with your account',
$messageRaw
);
}
/**
* @param string $email
* @param string $password
* @return array
* @throws AuthenticationException
*/
private function getCustomerAuthHeaders(string $email, string $password): array
{
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
return ['Authorization' => 'Bearer ' . $customerToken];
}
}
@@ -0,0 +1,254 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\CustomerGraphQl\Model\Resolver;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\GraphQl\Service\GraphQlRequest;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Test\Fixture\Group as StoreGroupFixture;
use Magento\Store\Test\Fixture\Store as StoreFixture;
use Magento\Store\Test\Fixture\Website as WebsiteFixture;
use Magento\TestFramework\Fixture\ComponentsDir;
use Magento\TestFramework\Fixture\Config;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
use PHPUnit\Framework\TestCase;
/**
* Test creating a customer through GraphQL
*
* @magentoAppArea graphql
*/
class CreateCustomerTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var GraphQlRequest
*/
private $graphQlRequest;
/**
* @var SerializerInterface
*/
private $json;
/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;
/**
* @var StoreRepositoryInterface
*/
private $storeRepository;
public function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class);
$this->json = $this->objectManager->get(SerializerInterface::class);
$this->customerRepository = $this->objectManager->create(CustomerRepositoryInterface::class);
$this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class);
}
/**
* Test that creating a customer sends an email
*/
public function testCreateCustomerSendsEmail()
{
$query
= <<<QUERY
mutation createAccount {
createCustomer(
input: {
email: "test@magento.com"
firstname: "Test"
lastname: "Magento"
password: "T3stP4assw0rd"
is_subscribed: false
}
) {
customer {
id
}
}
}
QUERY;
$response = $this->graphQlRequest->send($query);
$responseData = $this->json->unserialize($response->getContent());
// Assert the response of the GraphQL request
$this->assertNull($responseData['data']['createCustomer']['customer']['id']);
// Verify the customer was created and has the correct data
$customer = $this->customerRepository->get('test@magento.com');
$this->assertEquals('Test', $customer->getFirstname());
$this->assertEquals('Magento', $customer->getLastname());
/** @var TransportBuilderMock $transportBuilderMock */
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
$sentMessage = $transportBuilderMock->getSentMessage();
// Verify an email was dispatched to the correct user
$this->assertNotNull($sentMessage);
$this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName());
$this->assertEquals('test@magento.com', $sentMessage->getTo()[0]->getEmail());
// Assert the email contains the expected content
$this->assertEquals('Welcome to Main Website Store', $sentMessage->getSubject());
$messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent();
$this->assertStringContainsString('Welcome to Main Website Store.', $messageRaw);
}
/**
* Test that creating a customer on an alternative store sends an email
*
* @magentoDataFixture Magento/CustomerGraphQl/_files/website_store_with_store_view.php
*/
public function testCreateCustomerForStoreSendsEmail()
{
$query
= <<<QUERY
mutation createAccount {
createCustomer(
input: {
email: "test@magento.com"
firstname: "Test"
lastname: "Magento"
password: "T3stP4assw0rd"
is_subscribed: false
}
) {
customer {
id
}
}
}
QUERY;
$response = $this->graphQlRequest->send(
$query,
[],
'',
[
'Store' => 'test_store_view'
]
);
$responseData = $this->json->unserialize($response->getContent());
// Assert the response of the GraphQL request
$this->assertNull($responseData['data']['createCustomer']['customer']['id']);
// Verify the customer was created and has the correct data
$customer = $this->customerRepository->get('test@magento.com');
$this->assertEquals('Test', $customer->getFirstname());
$this->assertEquals('Magento', $customer->getLastname());
$this->assertEquals('Test Store View', $customer->getCreatedIn());
$store = $this->storeRepository->getById($customer->getStoreId());
$this->assertEquals('test_store_view', $store->getCode());
/** @var TransportBuilderMock $transportBuilderMock */
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
$sentMessage = $transportBuilderMock->getSentMessage();
// Verify an email was dispatched to the correct user
$this->assertNotNull($sentMessage);
$this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName());
$this->assertEquals('test@magento.com', $sentMessage->getTo()[0]->getEmail());
// Assert the email contains the expected content
$this->assertEquals('Welcome to Test Group', $sentMessage->getSubject());
$messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent();
$this->assertStringContainsString('Welcome to Test Group.', $messageRaw);
}
/**
* Test that creating a customer on an alternative store sends an email in the translated language
*/
#[
DataFixture(WebsiteFixture::class, as: 'website2'),
DataFixture(
StoreGroupFixture::class,
['name' => 'Test Group', 'website_id' => '$website2.id$'],
'store_group2'
),
DataFixture(
StoreFixture::class,
['code' => 'test_store_view', 'name' => 'Test Store View', 'store_group_id' => '$store_group2.id$']
),
Config('general/locale/code', 'fr_FR', 'store', 'test_store_view'),
ComponentsDir('Magento/CustomerGraphQl/_files')
]
public function testCreateCustomerForStoreSendsTranslatedEmail()
{
$query
= <<<QUERY
mutation createAccount {
createCustomer(
input: {
email: "test@magento.com"
firstname: "Test"
lastname: "Magento"
password: "T3stP4assw0rd"
is_subscribed: false
}
) {
customer {
id
}
}
}
QUERY;
$response = $this->graphQlRequest->send(
$query,
[],
'',
[
'Store' => 'test_store_view'
]
);
$responseData = $this->json->unserialize($response->getContent());
// Assert the response of the GraphQL request
$this->assertNull($responseData['data']['createCustomer']['customer']['id']);
// Verify the customer was created and has the correct data
$customer = $this->customerRepository->get('test@magento.com');
$this->assertEquals('Test', $customer->getFirstname());
$this->assertEquals('Magento', $customer->getLastname());
$this->assertEquals('Test Store View', $customer->getCreatedIn());
$store = $this->storeRepository->getById($customer->getStoreId());
$this->assertEquals('test_store_view', $store->getCode());
/** @var TransportBuilderMock $transportBuilderMock */
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
$sentMessage = $transportBuilderMock->getSentMessage();
// Verify an email was dispatched to the correct user
$this->assertNotNull($sentMessage);
$this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName());
$this->assertEquals('test@magento.com', $sentMessage->getTo()[0]->getEmail());
// Assert the email contains the expected content
$this->assertEquals('Bienvenue sur Test Group', $sentMessage->getSubject());
$messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent();
$this->assertStringContainsString('Bienvenue sur Test Group.', $messageRaw);
}
}