|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Wishlist\Controller; |
| 8 | + |
| 9 | +use Magento\Customer\Helper\View; |
| 10 | +use Magento\Customer\Model\Session; |
| 11 | +use Magento\Framework\Data\Form\FormKey; |
| 12 | +use Magento\Framework\Message\ManagerInterface; |
| 13 | +use Magento\Wishlist\Model\Item; |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Zend\Http\Request; |
| 16 | + |
| 17 | +/** |
| 18 | + * Tests updating wishlist item comment. |
| 19 | + * |
| 20 | + * @magentoAppIsolation enabled |
| 21 | + * @magentoDbIsolation disabled |
| 22 | + * @magentoAppArea frontend |
| 23 | + */ |
| 24 | +class UpdateTest extends \Magento\TestFramework\TestCase\AbstractController |
| 25 | +{ |
| 26 | + /** |
| 27 | + * @var Session |
| 28 | + */ |
| 29 | + private $customerSession; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var ManagerInterface |
| 33 | + */ |
| 34 | + private $messages; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var View |
| 38 | + */ |
| 39 | + private $customerViewHelper; |
| 40 | + |
| 41 | + /** |
| 42 | + * Description field value for wishlist item. |
| 43 | + * |
| 44 | + * @var string |
| 45 | + */ |
| 46 | + private $description = 'some description'; |
| 47 | + |
| 48 | + /** |
| 49 | + * Tests updating wishlist item comment. |
| 50 | + * |
| 51 | + * @magentoDataFixture Magento/Wishlist/_files/wishlist.php |
| 52 | + * @dataProvider commentDataProvider |
| 53 | + */ |
| 54 | + public function testUpdateComment($postDescription, $postQty, $expectedResult, $presetComment) |
| 55 | + { |
| 56 | + $itemId = 1; |
| 57 | + $wishlistId = 1; |
| 58 | + |
| 59 | + if ($presetComment) { |
| 60 | + $item = $this->_objectManager->create(Item::class)->load($itemId); |
| 61 | + $item->setDescription($this->description); |
| 62 | + $item->save(); |
| 63 | + } |
| 64 | + |
| 65 | + $formKey = $this->_objectManager->get(FormKey::class); |
| 66 | + $this->getRequest()->setPostValue( |
| 67 | + [ |
| 68 | + 'description' => $postDescription, |
| 69 | + 'qty' => $postQty, |
| 70 | + 'do' => '', |
| 71 | + 'form_key' => $formKey->getFormKey() |
| 72 | + ] |
| 73 | + )->setMethod(Request::METHOD_POST); |
| 74 | + $this->dispatch('wishlist/index/update/wishlist_id/' . $wishlistId); |
| 75 | + |
| 76 | + $item = $this->_objectManager->create(Item::class)->load($itemId); |
| 77 | + |
| 78 | + self::assertEquals( |
| 79 | + $expectedResult, |
| 80 | + $item->getDescription() |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Data provider for testUpdateComment. |
| 86 | + * |
| 87 | + * @return array |
| 88 | + */ |
| 89 | + public function commentDataProvider() |
| 90 | + { |
| 91 | + return [ |
| 92 | + 'test adding comment' => [ |
| 93 | + 'postDescription' => [1 => $this->description], |
| 94 | + 'postQty' => [1 => '1'], |
| 95 | + 'expectedResult' => $this->description, |
| 96 | + 'presetComment' => false |
| 97 | + ], |
| 98 | + 'test removing comment' => [ |
| 99 | + 'postDescription' => [1 => ''], |
| 100 | + 'postQty' => [1 => '1'], |
| 101 | + 'expectedResult' => '', |
| 102 | + 'presetComment' => true |
| 103 | + ], |
| 104 | + 'test not changing comment' => [ |
| 105 | + 'postDescription' => [], |
| 106 | + 'postQty' => [1 => '1'], |
| 107 | + 'expectedResult' => $this->description, |
| 108 | + 'presetComment' => true |
| 109 | + ], |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | + protected function setUp() |
| 114 | + { |
| 115 | + parent::setUp(); |
| 116 | + $logger = $this->createMock(LoggerInterface::class); |
| 117 | + $this->customerSession = $this->_objectManager->get( |
| 118 | + Session::class, |
| 119 | + [$logger] |
| 120 | + ); |
| 121 | + /** @var \Magento\Customer\Api\AccountManagementInterface $service */ |
| 122 | + $service = $this->_objectManager->create( |
| 123 | + \Magento\Customer\Api\AccountManagementInterface::class |
| 124 | + ); |
| 125 | + $customer = $service-> authenticate( '[email protected]', 'password'); |
| 126 | + $this->customerSession->setCustomerDataAsLoggedIn($customer); |
| 127 | + |
| 128 | + $this->customerViewHelper = $this->_objectManager->create(View::class); |
| 129 | + |
| 130 | + $this->messages = $this->_objectManager->get( |
| 131 | + ManagerInterface::class |
| 132 | + ); |
| 133 | + } |
| 134 | + |
| 135 | + protected function tearDown() |
| 136 | + { |
| 137 | + $this->customerSession->logout(); |
| 138 | + $this->customerSession = null; |
| 139 | + parent::tearDown(); |
| 140 | + } |
| 141 | +} |
0 commit comments