SendmailTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\SendFriend\Controller;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. use Magento\Customer\Model\Session;
  11. use Magento\Framework\Data\Form\FormKey;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. use Magento\Framework\Message\MessageInterface;
  14. use Magento\TestFramework\Request;
  15. use Magento\TestFramework\TestCase\AbstractController;
  16. /**
  17. * Class SendmailTest
  18. */
  19. class SendmailTest extends AbstractController
  20. {
  21. /**
  22. * Share the product to friend as logged in customer
  23. *
  24. * @magentoDbIsolation enabled
  25. * @magentoAppIsolation enabled
  26. * @magentoDataFixture Magento/SendFriend/_files/disable_allow_guest_config.php
  27. * @magentoDataFixture Magento/Customer/_files/customer.php
  28. * @magentoDataFixture Magento/Catalog/_files/products.php
  29. */
  30. public function testSendActionAsLoggedIn()
  31. {
  32. $product = $this->getProduct();
  33. $this->login(1);
  34. $this->prepareRequestData();
  35. $this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
  36. $this->assertSessionMessages(
  37. $this->equalTo(['The link to a friend was sent.']),
  38. MessageInterface::TYPE_SUCCESS
  39. );
  40. }
  41. /**
  42. * Share the product to friend as guest customer
  43. *
  44. * @magentoDbIsolation enabled
  45. * @magentoAppIsolation enabled
  46. * @magentoConfigFixture default_store sendfriend/email/enabled 1
  47. * @magentoConfigFixture default_store sendfriend/email/allow_guest 1
  48. * @magentoDataFixture Magento/Catalog/_files/products.php
  49. */
  50. public function testSendActionAsGuest()
  51. {
  52. $product = $this->getProduct();
  53. $this->prepareRequestData();
  54. $this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
  55. $this->assertSessionMessages(
  56. $this->equalTo(['The link to a friend was sent.']),
  57. MessageInterface::TYPE_SUCCESS
  58. );
  59. }
  60. /**
  61. * Share the product to friend as guest customer with invalid post data
  62. *
  63. * @magentoDbIsolation enabled
  64. * @magentoAppIsolation enabled
  65. * @magentoConfigFixture default_store sendfriend/email/enabled 1
  66. * @magentoConfigFixture default_store sendfriend/email/allow_guest 1
  67. * @magentoDataFixture Magento/Catalog/_files/products.php
  68. */
  69. public function testSendActionAsGuestWithInvalidData()
  70. {
  71. $product = $this->getProduct();
  72. $this->prepareRequestData(true);
  73. $this->dispatch('sendfriend/product/sendmail/id/' . $product->getId());
  74. $this->assertSessionMessages(
  75. $this->equalTo(['Invalid Sender Email']),
  76. MessageInterface::TYPE_ERROR
  77. );
  78. }
  79. /**
  80. * @return ProductInterface
  81. */
  82. private function getProduct()
  83. {
  84. return $this->_objectManager->get(ProductRepositoryInterface::class)->get('custom-design-simple-product');
  85. }
  86. /**
  87. * Login the user
  88. *
  89. * @param string $customerId Customer to mark as logged in for the session
  90. * @return void
  91. */
  92. protected function login($customerId)
  93. {
  94. /** @var Session $session */
  95. $session = Bootstrap::getObjectManager()
  96. ->get(Session::class);
  97. $session->loginById($customerId);
  98. }
  99. /**
  100. * @param bool $invalidData
  101. * @return void
  102. */
  103. private function prepareRequestData($invalidData = false)
  104. {
  105. /** @var FormKey $formKey */
  106. $formKey = $this->_objectManager->get(FormKey::class);
  107. $post = [
  108. 'sender' => [
  109. 'name' => 'Test',
  110. 'email' => 'test@example.com',
  111. 'message' => 'Message',
  112. ],
  113. 'recipients' => [
  114. 'name' => [
  115. 'Recipient 1',
  116. 'Recipient 2'
  117. ],
  118. 'email' => [
  119. 'r1@example.com',
  120. 'r2@example.com'
  121. ]
  122. ],
  123. 'form_key' => $formKey->getFormKey(),
  124. ];
  125. if ($invalidData) {
  126. unset($post['sender']['email']);
  127. }
  128. $this->getRequest()->setMethod(Request::METHOD_POST);
  129. $this->getRequest()->setPostValue($post);
  130. }
  131. }