ReviewTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Review\Test\Unit\Model;
  7. use Magento\Catalog\Model\Product;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. use Magento\Review\Model\Review;
  10. /**
  11. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  12. */
  13. class ReviewTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /** @var \Magento\Review\Model\Review */
  16. protected $review;
  17. /** @var ObjectManagerHelper */
  18. protected $objectManagerHelper;
  19. /** @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $contextMock;
  21. /** @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $registryMock;
  23. /** @var \PHPUnit_Framework_MockObject_MockObject */
  24. protected $productFactoryMock;
  25. /** @var \PHPUnit_Framework_MockObject_MockObject */
  26. protected $statusFactoryMock;
  27. /** @var \PHPUnit_Framework_MockObject_MockObject */
  28. protected $reviewSummaryMock;
  29. /** @var \PHPUnit_Framework_MockObject_MockObject */
  30. protected $summaryModMock;
  31. /** @var \Magento\Review\Model\Review\Summary|\PHPUnit_Framework_MockObject_MockObject */
  32. protected $summaryMock;
  33. /** @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  34. protected $storeManagerMock;
  35. /** @var \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
  36. protected $urlInterfaceMock;
  37. /** @var \Magento\Review\Model\ResourceModel\Review|\PHPUnit_Framework_MockObject_MockObject */
  38. protected $resource;
  39. /** @var int */
  40. protected $reviewId = 8;
  41. protected function setUp()
  42. {
  43. $this->contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
  44. $this->registryMock = $this->createMock(\Magento\Framework\Registry::class);
  45. $this->productFactoryMock = $this->createPartialMock(
  46. \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory::class,
  47. ['create']
  48. );
  49. $this->statusFactoryMock = $this->createPartialMock(
  50. \Magento\Review\Model\ResourceModel\Review\Status\CollectionFactory::class,
  51. ['create']
  52. );
  53. $this->reviewSummaryMock = $this->createMock(
  54. \Magento\Review\Model\ResourceModel\Review\Summary\CollectionFactory::class
  55. );
  56. $this->summaryModMock = $this->createPartialMock(
  57. \Magento\Review\Model\Review\SummaryFactory::class,
  58. ['create']
  59. );
  60. $this->summaryMock = $this->createMock(\Magento\Review\Model\Review\Summary::class);
  61. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  62. $this->urlInterfaceMock = $this->createMock(\Magento\Framework\UrlInterface::class);
  63. $this->resource = $this->createMock(\Magento\Review\Model\ResourceModel\Review::class);
  64. $this->objectManagerHelper = new ObjectManagerHelper($this);
  65. $this->review = $this->objectManagerHelper->getObject(
  66. \Magento\Review\Model\Review::class,
  67. [
  68. 'context' => $this->contextMock,
  69. 'registry' => $this->registryMock,
  70. 'productFactory' => $this->productFactoryMock,
  71. 'statusFactory' => $this->statusFactoryMock,
  72. 'summaryFactory' => $this->reviewSummaryMock,
  73. 'summaryModFactory' => $this->summaryModMock,
  74. 'reviewSummary' => $this->summaryMock,
  75. 'storeManager' => $this->storeManagerMock,
  76. 'urlModel' => $this->urlInterfaceMock,
  77. 'resource' => $this->resource,
  78. 'data' => ['review_id' => $this->reviewId, 'status_id' => 1, 'stores' => [2, 3, 4]]
  79. ]
  80. );
  81. }
  82. public function testGetProductCollection()
  83. {
  84. $collection = $this->createMock(\Magento\Review\Model\ResourceModel\Review\Product\Collection::class);
  85. $this->productFactoryMock->expects($this->once())
  86. ->method('create')
  87. ->will($this->returnValue($collection));
  88. $this->assertSame($collection, $this->review->getProductCollection());
  89. }
  90. public function testGetStatusCollection()
  91. {
  92. $collection = $this->createMock(\Magento\Review\Model\ResourceModel\Review\Status\Collection::class);
  93. $this->statusFactoryMock->expects($this->once())
  94. ->method('create')
  95. ->will($this->returnValue($collection));
  96. $this->assertSame($collection, $this->review->getStatusCollection());
  97. }
  98. public function testGetTotalReviews()
  99. {
  100. $primaryKey = 'review_id';
  101. $approvedOnly = false;
  102. $storeId = 0;
  103. $result = 5;
  104. $this->resource->expects($this->once())->method('getTotalReviews')
  105. ->with($this->equalTo($primaryKey), $this->equalTo($approvedOnly), $this->equalTo($storeId))
  106. ->will($this->returnValue($result));
  107. $this->assertSame($result, $this->review->getTotalReviews($primaryKey, $approvedOnly, $storeId));
  108. }
  109. public function testAggregate()
  110. {
  111. $this->resource->expects($this->once())->method('aggregate')
  112. ->with($this->equalTo($this->review))
  113. ->will($this->returnValue($this->review));
  114. $this->assertSame($this->review, $this->review->aggregate());
  115. }
  116. public function testGetEntitySummary()
  117. {
  118. $productId = 6;
  119. $storeId = 4;
  120. $testSummaryData = ['test' => 'value'];
  121. $summary = new \Magento\Framework\DataObject();
  122. $summary->setData($testSummaryData);
  123. $product = $this->createPartialMock(
  124. \Magento\Catalog\Model\Product::class,
  125. ['getId', 'setRatingSummary', '__wakeup']
  126. );
  127. $product->expects($this->once())->method('getId')->will($this->returnValue($productId));
  128. $product->expects($this->once())->method('setRatingSummary')->with($summary)->will($this->returnSelf());
  129. $summaryData = $this->createPartialMock(
  130. \Magento\Review\Model\Review\Summary::class,
  131. ['load', 'getData', 'setStoreId', '__wakeup']
  132. );
  133. $summaryData->expects($this->once())->method('setStoreId')
  134. ->with($this->equalTo($storeId))
  135. ->will($this->returnSelf());
  136. $summaryData->expects($this->once())->method('load')
  137. ->with($this->equalTo($productId))
  138. ->will($this->returnSelf());
  139. $summaryData->expects($this->once())->method('getData')->will($this->returnValue($testSummaryData));
  140. $this->summaryModMock->expects($this->once())->method('create')->will($this->returnValue($summaryData));
  141. $this->assertNull($this->review->getEntitySummary($product, $storeId));
  142. }
  143. public function testGetPendingStatus()
  144. {
  145. $this->assertSame(Review::STATUS_PENDING, $this->review->getPendingStatus());
  146. }
  147. public function testGetReviewUrl()
  148. {
  149. $result = 'http://some.url';
  150. $this->urlInterfaceMock->expects($this->once())->method('getUrl')
  151. ->with($this->equalTo('review/product/view'), $this->equalTo(['id' => $this->reviewId]))
  152. ->will($this->returnValue($result));
  153. $this->assertSame($result, $this->review->getReviewUrl());
  154. }
  155. /**
  156. * @param int $productId
  157. * @param int $storeId
  158. * @param string $result
  159. * @dataProvider getProductUrlDataProvider
  160. */
  161. public function testGetProductUrl($productId, $storeId, $result)
  162. {
  163. if ($storeId) {
  164. $this->urlInterfaceMock->expects($this->once())->method('setScope')
  165. ->with($this->equalTo($storeId))
  166. ->will($this->returnSelf());
  167. }
  168. $this->urlInterfaceMock->expects($this->once())->method('getUrl')
  169. ->with($this->equalTo('catalog/product/view'), $this->equalTo(['id' => $productId]))
  170. ->will($this->returnValue($result));
  171. $this->assertSame($result, $this->review->getProductUrl($productId, $storeId));
  172. }
  173. /**
  174. * @return array
  175. */
  176. public function getProductUrlDataProvider()
  177. {
  178. return [
  179. 'store id specified' => [3, 5, 'http://some.url'],
  180. 'store id is not specified' => [3, null, 'http://some.url/2/'],
  181. ];
  182. }
  183. public function testIsApproved()
  184. {
  185. $this->assertTrue($this->review->isApproved());
  186. }
  187. /**
  188. * @param int|null $storeId
  189. * @param bool $result
  190. * @dataProvider isAvailableOnStoreDataProvider
  191. */
  192. public function testIsAvailableOnStore($storeId, $result)
  193. {
  194. $store = $this->createMock(\Magento\Store\Model\Store::class);
  195. if ($storeId) {
  196. $store->expects($this->once())->method('getId')->will($this->returnValue($storeId));
  197. $this->storeManagerMock->expects($this->once())
  198. ->method('getStore')
  199. ->with($this->equalTo($store))
  200. ->will($this->returnValue($store));
  201. }
  202. $this->assertSame($result, $this->review->isAvailableOnStore($store));
  203. }
  204. /**
  205. * @return array
  206. */
  207. public function isAvailableOnStoreDataProvider()
  208. {
  209. return [
  210. 'store id is set and not in list' => [1, false],
  211. 'store id is set' => [3, true],
  212. 'store id is not set' => [null, false],
  213. ];
  214. }
  215. public function testGetEntityIdByCode()
  216. {
  217. $entityCode = 'test';
  218. $result = 22;
  219. $this->resource->expects($this->once())->method('getEntityIdByCode')
  220. ->with($this->equalTo($entityCode))
  221. ->will($this->returnValue($result));
  222. $this->assertSame($result, $this->review->getEntityIdByCode($entityCode));
  223. }
  224. public function testGetIdentities()
  225. {
  226. $this->review->setStatusId(Review::STATUS_PENDING);
  227. $this->assertEmpty($this->review->getIdentities());
  228. $productId = 1;
  229. $this->review->setEntityPkValue($productId);
  230. $this->review->setStatusId(Review::STATUS_PENDING);
  231. $this->assertEquals([Product::CACHE_TAG . '_' . $productId], $this->review->getIdentities());
  232. $this->review->setEntityPkValue($productId);
  233. $this->review->setStatusId(Review::STATUS_APPROVED);
  234. $this->assertEquals([Product::CACHE_TAG . '_' . $productId], $this->review->getIdentities());
  235. $this->review->setEntityPkValue($productId);
  236. $this->review->setStatusId(Review::STATUS_NOT_APPROVED);
  237. $this->assertEquals([Product::CACHE_TAG . '_' . $productId], $this->review->getIdentities());
  238. }
  239. }