|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Review\Controller; |
| 7 | + |
| 8 | +use Magento\Catalog\Api\Data\ProductInterface; |
| 9 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 10 | +use Magento\Framework\Data\Form\FormKey; |
| 11 | +use Magento\Framework\Message\MessageInterface; |
| 12 | +use Magento\TestFramework\Request; |
| 13 | +use Magento\TestFramework\TestCase\AbstractController; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test review product controller behavior |
| 17 | + * |
| 18 | + * @magentoAppArea frontend |
| 19 | + */ |
| 20 | +class CaseCheckAddingProductReviewTest extends AbstractController |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Test adding a review for allowed guests with incomplete data by a not logged in user |
| 24 | + * |
| 25 | + * @magentoDbIsolation enabled |
| 26 | + * @magentoAppIsolation enabled |
| 27 | + * @magentoDataFixture Magento/Review/_files/config.php |
| 28 | + * @magentoDataFixture Magento/Catalog/_files/products.php |
| 29 | + */ |
| 30 | + public function testAttemptForGuestToAddReviewsWithIncompleteData() |
| 31 | + { |
| 32 | + $product = $this->getProduct(); |
| 33 | + /** @var FormKey $formKey */ |
| 34 | + $formKey = $this->_objectManager->get(FormKey::class); |
| 35 | + $post = [ |
| 36 | + 'nickname' => 'Test nick', |
| 37 | + 'title' => 'Summary', |
| 38 | + 'form_key' => $formKey->getFormKey(), |
| 39 | + ]; |
| 40 | + $this->prepareRequestData($post); |
| 41 | + $this->dispatch('review/product/post/id/' . $product->getId()); |
| 42 | + $this->assertSessionMessages( |
| 43 | + $this->equalTo(['Please enter a review.']), |
| 44 | + MessageInterface::TYPE_ERROR |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Test adding a review for not allowed guests by a guest |
| 50 | + * |
| 51 | + * @magentoDbIsolation enabled |
| 52 | + * @magentoAppIsolation enabled |
| 53 | + * @magentoDataFixture Magento/Review/_files/disable_config.php |
| 54 | + * @magentoDataFixture Magento/Catalog/_files/products.php |
| 55 | + */ |
| 56 | + public function testAttemptForGuestToAddReview() |
| 57 | + { |
| 58 | + $product = $this->getProduct(); |
| 59 | + /** @var FormKey $formKey */ |
| 60 | + $formKey = $this->_objectManager->get(FormKey::class); |
| 61 | + $post = [ |
| 62 | + 'nickname' => 'Test nick', |
| 63 | + 'title' => 'Summary', |
| 64 | + 'detail' => 'Test Details', |
| 65 | + 'form_key' => $formKey->getFormKey(), |
| 66 | + ]; |
| 67 | + |
| 68 | + $this->prepareRequestData($post); |
| 69 | + $this->dispatch('review/product/post/id/' . $product->getId()); |
| 70 | + |
| 71 | + $this->assertRedirect($this->stringContains('customer/account/login')); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Test successfully adding a product review by a guest |
| 76 | + * |
| 77 | + * @magentoDbIsolation enabled |
| 78 | + * @magentoAppIsolation enabled |
| 79 | + * @magentoDataFixture Magento/Review/_files/config.php |
| 80 | + * @magentoDataFixture Magento/Catalog/_files/products.php |
| 81 | + */ |
| 82 | + public function testSuccessfullyAddingProductReviewForGuest() |
| 83 | + { |
| 84 | + $product = $this->getProduct(); |
| 85 | + /** @var FormKey $formKey */ |
| 86 | + $formKey = $this->_objectManager->get(FormKey::class); |
| 87 | + $post = [ |
| 88 | + 'nickname' => 'Test nick', |
| 89 | + 'title' => 'Summary', |
| 90 | + 'detail' => 'Test Details', |
| 91 | + 'form_key' => $formKey->getFormKey(), |
| 92 | + ]; |
| 93 | + |
| 94 | + $this->prepareRequestData($post); |
| 95 | + $this->dispatch('review/product/post/id/' . $product->getId()); |
| 96 | + |
| 97 | + $this->assertSessionMessages( |
| 98 | + $this->equalTo(['You submitted your review for moderation.']), |
| 99 | + MessageInterface::TYPE_SUCCESS |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @return ProductInterface |
| 105 | + */ |
| 106 | + private function getProduct() |
| 107 | + { |
| 108 | + return $this->_objectManager->get(ProductRepositoryInterface::class)->get('custom-design-simple-product'); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param array $postData |
| 113 | + * @return void |
| 114 | + */ |
| 115 | + private function prepareRequestData($postData) |
| 116 | + { |
| 117 | + $this->getRequest()->setMethod(Request::METHOD_POST); |
| 118 | + $this->getRequest()->setPostValue($postData); |
| 119 | + } |
| 120 | +} |
0 commit comments