CaseCheckAddingProductReviewTest.php 3.7 KB

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