SubscriptionStatusTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\Integration\Api\CustomerTokenServiceInterface;
  9. use Magento\Newsletter\Model\SubscriberFactory;
  10. use Magento\TestFramework\Helper\Bootstrap;
  11. use Magento\TestFramework\TestCase\GraphQlAbstract;
  12. class SubscriptionStatusTest extends GraphQlAbstract
  13. {
  14. /**
  15. * @var CustomerTokenServiceInterface
  16. */
  17. private $customerTokenService;
  18. /**
  19. * @var SubscriberFactory
  20. */
  21. private $subscriberFactory;
  22. protected function setUp()
  23. {
  24. parent::setUp();
  25. $this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
  26. $this->subscriberFactory = Bootstrap::getObjectManager()->get(SubscriberFactory::class);
  27. }
  28. /**
  29. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  30. */
  31. public function testGetSubscriptionStatusTest()
  32. {
  33. $currentEmail = 'customer@example.com';
  34. $currentPassword = 'password';
  35. $query = <<<QUERY
  36. query {
  37. customer {
  38. is_subscribed
  39. }
  40. }
  41. QUERY;
  42. $response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  43. $this->assertFalse($response['customer']['is_subscribed']);
  44. }
  45. /**
  46. * @expectedException \Exception
  47. * @expectedExceptionMessage The current customer isn't authorized.
  48. */
  49. public function testGetSubscriptionStatusIfUserIsNotAuthorizedTest()
  50. {
  51. $query = <<<QUERY
  52. query {
  53. customer {
  54. is_subscribed
  55. }
  56. }
  57. QUERY;
  58. $this->graphQlQuery($query);
  59. }
  60. /**
  61. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  62. */
  63. public function testChangeSubscriptionStatusTest()
  64. {
  65. $currentEmail = 'customer@example.com';
  66. $currentPassword = 'password';
  67. $query = <<<QUERY
  68. mutation {
  69. updateCustomer(
  70. input: {
  71. is_subscribed: true
  72. }
  73. ) {
  74. customer {
  75. is_subscribed
  76. }
  77. }
  78. }
  79. QUERY;
  80. $response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
  81. $this->assertTrue($response['updateCustomer']['customer']['is_subscribed']);
  82. }
  83. /**
  84. * @expectedException \Exception
  85. * @expectedExceptionMessage The current customer isn't authorized.
  86. */
  87. public function testChangeSubscriptionStatuIfUserIsNotAuthorizedTest()
  88. {
  89. $query = <<<QUERY
  90. mutation {
  91. updateCustomer(
  92. input: {
  93. is_subscribed: true
  94. }
  95. ) {
  96. customer {
  97. is_subscribed
  98. }
  99. }
  100. }
  101. QUERY;
  102. $this->graphQlQuery($query);
  103. }
  104. /**
  105. * @param string $email
  106. * @param string $password
  107. * @return array
  108. */
  109. private function getCustomerAuthHeaders(string $email, string $password): array
  110. {
  111. $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
  112. return ['Authorization' => 'Bearer ' . $customerToken];
  113. }
  114. protected function tearDown()
  115. {
  116. parent::tearDown();
  117. $this->subscriberFactory->create()->loadByCustomerId(1)->delete();
  118. }
  119. }