GetCustomerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Customer;
  8. use Magento\Customer\Model\CustomerAuthUpdate;
  9. use Magento\Customer\Model\CustomerRegistry;
  10. use Magento\Integration\Api\CustomerTokenServiceInterface;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. use Magento\TestFramework\TestCase\GraphQlAbstract;
  13. class GetCustomerTest extends GraphQlAbstract
  14. {
  15. /**
  16. * @var CustomerTokenServiceInterface
  17. */
  18. private $customerTokenService;
  19. /**
  20. * @var CustomerRegistry
  21. */
  22. private $customerRegistry;
  23. /**
  24. * @var CustomerAuthUpdate
  25. */
  26. private $customerAuthUpdate;
  27. protected function setUp()
  28. {
  29. parent::setUp();
  30. $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
  31. $this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
  32. $this->customerAuthUpdate = Bootstrap::getObjectManager()->get(CustomerAuthUpdate::class);
  33. }
  34. /**
  35. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  36. */
  37. public function testGetCustomer()
  38. {
  39. $currentEmail = 'customer@example.com';
  40. $currentPassword = 'password';
  41. $query = <<<QUERY
  42. query {
  43. customer {
  44. firstname
  45. lastname
  46. email
  47. }
  48. }
  49. QUERY;
  50. $response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  51. $this->assertEquals('John', $response['customer']['firstname']);
  52. $this->assertEquals('Smith', $response['customer']['lastname']);
  53. $this->assertEquals($currentEmail, $response['customer']['email']);
  54. }
  55. /**
  56. * @expectedException \Exception
  57. * @expectedExceptionMessage The current customer isn't authorized.
  58. */
  59. public function testGetCustomerIfUserIsNotAuthorized()
  60. {
  61. $query = <<<QUERY
  62. query {
  63. customer {
  64. firstname
  65. lastname
  66. email
  67. }
  68. }
  69. QUERY;
  70. $this->graphQlQuery($query);
  71. }
  72. /**
  73. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  74. * @expectedException \Exception
  75. * @expectedExceptionMessage The account is locked.
  76. */
  77. public function testGetCustomerIfAccountIsLocked()
  78. {
  79. $this->lockCustomer(1);
  80. $currentEmail = 'customer@example.com';
  81. $currentPassword = 'password';
  82. $query = <<<QUERY
  83. query {
  84. customer {
  85. firstname
  86. lastname
  87. email
  88. }
  89. }
  90. QUERY;
  91. $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  92. }
  93. /**
  94. * @param string $email
  95. * @param string $password
  96. * @return array
  97. */
  98. private function getCustomerAuthHeaders(string $email, string $password): array
  99. {
  100. $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
  101. return ['Authorization' => 'Bearer ' . $customerToken];
  102. }
  103. /**
  104. * @param int $customerId
  105. * @return void
  106. */
  107. private function lockCustomer(int $customerId): void
  108. {
  109. $customerSecure = $this->customerRegistry->retrieveSecureData($customerId);
  110. $customerSecure->setLockExpires('2030-12-31 00:00:00');
  111. $this->customerAuthUpdate->saveAuth($customerId);
  112. }
  113. }