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