ContentValidatorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Test\Unit\Model\Link;
  7. use Magento\Downloadable\Model\Link\ContentValidator;
  8. class ContentValidatorTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var ContentValidator
  12. */
  13. protected $validator;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $fileValidatorMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $urlValidatorMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $linkFileMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $sampleFileMock;
  30. protected function setUp()
  31. {
  32. $this->fileValidatorMock = $this->createMock(\Magento\Downloadable\Model\File\ContentValidator::class);
  33. $this->urlValidatorMock = $this->createMock(\Magento\Framework\Url\Validator::class);
  34. $this->linkFileMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
  35. $this->sampleFileMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
  36. $this->validator = new ContentValidator($this->fileValidatorMock, $this->urlValidatorMock);
  37. }
  38. public function testIsValid()
  39. {
  40. $linkFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
  41. $sampleFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
  42. $linkData = [
  43. 'title' => 'Title',
  44. 'sort_order' => 1,
  45. 'price' => 10.1,
  46. 'shareable' => true,
  47. 'number_of_downloads' => 100,
  48. 'link_type' => 'file',
  49. 'sample_type' => 'file',
  50. 'link_file_content' => $linkFileContentMock,
  51. 'sample_file_content' => $sampleFileContentMock,
  52. ];
  53. $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  54. $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  55. $linkMock = $this->getLinkMock($linkData);
  56. $this->assertTrue($this->validator->isValid($linkMock));
  57. }
  58. public function testIsValidSkipLinkContent()
  59. {
  60. $sampleFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
  61. $linkData = [
  62. 'title' => 'Title',
  63. 'sort_order' => 1,
  64. 'price' => 10.1,
  65. 'shareable' => true,
  66. 'number_of_downloads' => 100,
  67. 'link_type' => 'url',
  68. 'link_url' => 'http://example.com',
  69. 'sample_type' => 'file',
  70. 'sample_file_content' => $sampleFileContentMock,
  71. ];
  72. $this->fileValidatorMock->expects($this->once())->method('isValid')->will($this->returnValue(true));
  73. $this->urlValidatorMock->expects($this->never())->method('isValid')->will($this->returnValue(true));
  74. $linkMock = $this->getLinkMock($linkData);
  75. $this->assertTrue($this->validator->isValid($linkMock, false));
  76. }
  77. public function testIsValidSkipSampleContent()
  78. {
  79. $sampleFileContentMock = $this->createMock(\Magento\Downloadable\Api\Data\File\ContentInterface::class);
  80. $linkData = [
  81. 'title' => 'Title',
  82. 'sort_order' => 1,
  83. 'price' => 10.1,
  84. 'shareable' => true,
  85. 'number_of_downloads' => 100,
  86. 'link_type' => 'url',
  87. 'link_url' => 'http://example.com',
  88. 'sample_type' => 'file',
  89. 'sample_file_content' => $sampleFileContentMock,
  90. ];
  91. $this->fileValidatorMock->expects($this->never())->method('isValid')->will($this->returnValue(true));
  92. $this->urlValidatorMock->expects($this->once())->method('isValid')->will($this->returnValue(true));
  93. $linkMock = $this->getLinkMock($linkData);
  94. $this->assertTrue($this->validator->isValid($linkMock, true, false));
  95. }
  96. /**
  97. * @param string|int|float $sortOrder
  98. * @dataProvider getInvalidSortOrder
  99. * @expectedException \Magento\Framework\Exception\InputException
  100. * @expectedExceptionMessage Sort order must be a positive integer.
  101. */
  102. public function testIsValidThrowsExceptionIfSortOrderIsInvalid($sortOrder)
  103. {
  104. $linkContentData = [
  105. 'title' => 'Title',
  106. 'sort_order' => $sortOrder,
  107. 'price' => 10.1,
  108. 'shareable' => true,
  109. 'number_of_downloads' => 100,
  110. 'link_type' => 'file',
  111. 'sample_type' => 'file',
  112. ];
  113. $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  114. $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  115. $contentMock = $this->getLinkMock($linkContentData);
  116. $this->validator->isValid($contentMock);
  117. }
  118. /**
  119. * @return array
  120. */
  121. public function getInvalidSortOrder()
  122. {
  123. return [
  124. [-1],
  125. ['string'],
  126. [1.1],
  127. ];
  128. }
  129. /**
  130. * @param string|int|float $price
  131. * @dataProvider getInvalidPrice
  132. * @expectedException \Magento\Framework\Exception\InputException
  133. * @expectedExceptionMessage Link price must have numeric positive value.
  134. */
  135. public function testIsValidThrowsExceptionIfPriceIsInvalid($price)
  136. {
  137. $linkContentData = [
  138. 'title' => 'Title',
  139. 'sort_order' => 1,
  140. 'price' => $price,
  141. 'shareable' => true,
  142. 'number_of_downloads' => 100,
  143. 'link_type' => 'file',
  144. 'sample_type' => 'file',
  145. ];
  146. $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  147. $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  148. $contentMock = $this->getLinkMock($linkContentData);
  149. $this->validator->isValid($contentMock);
  150. }
  151. /**
  152. * @return array
  153. */
  154. public function getInvalidPrice()
  155. {
  156. return [
  157. [-1],
  158. ['string'],
  159. ];
  160. }
  161. /**
  162. * @param string|int|float $numberOfDownloads
  163. * @dataProvider getInvalidNumberOfDownloads
  164. * @expectedException \Magento\Framework\Exception\InputException
  165. * @expectedExceptionMessage Number of downloads must be a positive integer.
  166. */
  167. public function testIsValidThrowsExceptionIfNumberOfDownloadsIsInvalid($numberOfDownloads)
  168. {
  169. $linkContentData = [
  170. 'title' => 'Title',
  171. 'sort_order' => 1,
  172. 'price' => 10.5,
  173. 'shareable' => true,
  174. 'number_of_downloads' => $numberOfDownloads,
  175. 'link_type' => 'file',
  176. 'sample_type' => 'file',
  177. ];
  178. $this->urlValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  179. $this->fileValidatorMock->expects($this->any())->method('isValid')->will($this->returnValue(true));
  180. $contentMock = $this->getLinkMock($linkContentData);
  181. $this->validator->isValid($contentMock);
  182. }
  183. /**
  184. * @return array
  185. */
  186. public function getInvalidNumberOfDownloads()
  187. {
  188. return [
  189. [-1],
  190. [2.71828],
  191. ['string'],
  192. ];
  193. }
  194. /**
  195. * @param array $linkData
  196. * @return \PHPUnit_Framework_MockObject_MockObject
  197. */
  198. protected function getLinkMock(array $linkData)
  199. {
  200. $linkMock = $this->getMockBuilder(\Magento\Downloadable\Api\Data\LinkInterface::class)
  201. ->setMethods(
  202. [
  203. 'getTitle',
  204. 'getPrice',
  205. 'getSortOrder',
  206. 'isShareable',
  207. 'getNumberOfDownloads',
  208. 'getLinkType',
  209. 'getLinkFile'
  210. ]
  211. )
  212. ->getMockForAbstractClass();
  213. $linkMock->expects($this->any())->method('getTitle')->will($this->returnValue(
  214. $linkData['title']
  215. ));
  216. $linkMock->expects($this->any())->method('getPrice')->will($this->returnValue(
  217. $linkData['price']
  218. ));
  219. $linkMock->expects($this->any())->method('getSortOrder')->will($this->returnValue(
  220. $linkData['sort_order']
  221. ));
  222. $linkMock->expects($this->any())->method('isShareable')->will($this->returnValue(
  223. $linkData['shareable']
  224. ));
  225. $linkMock->expects($this->any())->method('getNumberOfDownloads')->will($this->returnValue(
  226. $linkData['number_of_downloads']
  227. ));
  228. $linkMock->expects($this->any())->method('getLinkType')->will($this->returnValue(
  229. $linkData['link_type']
  230. ));
  231. $linkMock->expects($this->any())->method('getLinkFile')->will($this->returnValue(
  232. $this->linkFileMock
  233. ));
  234. if (isset($linkData['link_url'])) {
  235. $linkMock->expects($this->any())->method('getLinkUrl')->will($this->returnValue(
  236. $linkData['link_url']
  237. ));
  238. }
  239. if (isset($linkData['sample_url'])) {
  240. $linkMock->expects($this->any())->method('getSampleUrl')->will($this->returnValue(
  241. $linkData['sample_url']
  242. ));
  243. }
  244. if (isset($linkData['sample_type'])) {
  245. $linkMock->expects($this->any())->method('getSampleType')->will($this->returnValue(
  246. $linkData['sample_type']
  247. ));
  248. }
  249. if (isset($linkData['link_file_content'])) {
  250. $linkMock->expects($this->any())->method('getLinkFileContent')->willReturn($linkData['link_file_content']);
  251. }
  252. if (isset($linkData['sample_file_content'])) {
  253. $linkMock->expects($this->any())->method('getSampleFileContent')
  254. ->willReturn($linkData['sample_file_content']);
  255. }
  256. $linkMock->expects($this->any())->method('getSampleFile')->will($this->returnValue(
  257. $this->sampleFileMock
  258. ));
  259. return $linkMock;
  260. }
  261. }