LinkTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Test\Unit\Controller\Download;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. /**
  9. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  10. */
  11. class LinkTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \Magento\Downloadable\Controller\Download\Link */
  14. protected $link;
  15. /** @var ObjectManagerHelper */
  16. protected $objectManagerHelper;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Request\Http
  19. */
  20. protected $request;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ResponseInterface
  23. */
  24. protected $response;
  25. /**
  26. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Downloadable\Model\Link\Purchased\Item
  27. */
  28. protected $linkPurchasedItem;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Downloadable\Model\Link\Purchased
  31. */
  32. protected $linkPurchased;
  33. /**
  34. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\ObjectManager\ObjectManager
  35. */
  36. protected $objectManager;
  37. /**
  38. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Message\ManagerInterface
  39. */
  40. protected $messageManager;
  41. /**
  42. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Response\RedirectInterface
  43. */
  44. protected $redirect;
  45. /**
  46. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\Session
  47. */
  48. protected $session;
  49. /**
  50. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Downloadable\Helper\Data
  51. */
  52. protected $helperData;
  53. /**
  54. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Downloadable\Helper\Download
  55. */
  56. protected $downloadHelper;
  57. /**
  58. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product
  59. */
  60. protected $product;
  61. /**
  62. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\UrlInterface
  63. */
  64. protected $urlInterface;
  65. /**
  66. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  67. */
  68. protected function setUp()
  69. {
  70. $this->objectManagerHelper = new ObjectManagerHelper($this);
  71. $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
  72. ->disableOriginalConstructor()->getMock();
  73. $this->response = $this->createPartialMock(
  74. \Magento\Framework\App\ResponseInterface::class,
  75. [
  76. 'setHttpResponseCode',
  77. 'clearBody',
  78. 'sendHeaders',
  79. 'sendResponse',
  80. 'setHeader'
  81. ]
  82. );
  83. $this->session = $this->createPartialMock(\Magento\Customer\Model\Session::class, [
  84. 'getCustomerId',
  85. 'authenticate',
  86. 'setBeforeAuthUrl'
  87. ]);
  88. $this->helperData = $this->createPartialMock(\Magento\Downloadable\Helper\Data::class, [
  89. 'getIsShareable'
  90. ]);
  91. $this->downloadHelper = $this->createPartialMock(\Magento\Downloadable\Helper\Download::class, [
  92. 'setResource',
  93. 'getFilename',
  94. 'getContentType',
  95. 'getFileSize',
  96. 'getContentDisposition',
  97. 'output'
  98. ]);
  99. $this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
  100. '_wakeup',
  101. 'load',
  102. 'getId',
  103. 'getProductUrl',
  104. 'getName'
  105. ]);
  106. $this->linkPurchasedItem = $this->createPartialMock(\Magento\Downloadable\Model\Link\Purchased\Item::class, [
  107. 'load',
  108. 'getId',
  109. 'getProductId',
  110. 'getPurchasedId',
  111. 'getNumberOfDownloadsBought',
  112. 'getNumberOfDownloadsUsed',
  113. 'getStatus',
  114. 'getLinkType',
  115. 'getLinkUrl',
  116. 'getLinkFile',
  117. 'setNumberOfDownloadsUsed',
  118. 'setStatus',
  119. 'save',
  120. ]);
  121. $this->linkPurchased = $this->createPartialMock(\Magento\Downloadable\Model\Link\Purchased::class, [
  122. 'load',
  123. 'getCustomerId'
  124. ]);
  125. $this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
  126. $this->redirect = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
  127. $this->urlInterface = $this->createMock(\Magento\Framework\UrlInterface::class);
  128. $this->objectManager = $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, [
  129. 'create',
  130. 'get'
  131. ]);
  132. $this->link = $this->objectManagerHelper->getObject(
  133. \Magento\Downloadable\Controller\Download\Link::class,
  134. [
  135. 'objectManager' => $this->objectManager,
  136. 'request' => $this->request,
  137. 'response' => $this->response,
  138. 'messageManager' => $this->messageManager,
  139. 'redirect' => $this->redirect
  140. ]
  141. );
  142. }
  143. public function testAbsentLinkId()
  144. {
  145. $this->objectManager->expects($this->once())
  146. ->method('get')
  147. ->with(\Magento\Customer\Model\Session::class)
  148. ->willReturn($this->session);
  149. $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
  150. $this->objectManager->expects($this->once())
  151. ->method('create')
  152. ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
  153. ->willReturn($this->linkPurchasedItem);
  154. $this->linkPurchasedItem->expects($this->once())
  155. ->method('load')
  156. ->with('some_id', 'link_hash')
  157. ->willReturnSelf();
  158. $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(null);
  159. $this->messageManager->expects($this->once())
  160. ->method('addNotice')
  161. ->with("We can't find the link you requested.");
  162. $this->redirect->expects($this->once())->method('redirect')->with($this->response, '*/customer/products', []);
  163. $this->assertEquals($this->response, $this->link->execute());
  164. }
  165. public function testGetLinkForGuestCustomer()
  166. {
  167. $this->objectManager->expects($this->at(0))
  168. ->method('get')
  169. ->with(\Magento\Customer\Model\Session::class)
  170. ->willReturn($this->session);
  171. $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
  172. $this->objectManager->expects($this->at(1))
  173. ->method('create')
  174. ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
  175. ->willReturn($this->linkPurchasedItem);
  176. $this->linkPurchasedItem->expects($this->once())
  177. ->method('load')
  178. ->with('some_id', 'link_hash')
  179. ->willReturnSelf();
  180. $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(5);
  181. $this->objectManager->expects($this->at(2))
  182. ->method('get')
  183. ->with(\Magento\Downloadable\Helper\Data::class)
  184. ->willReturn($this->helperData);
  185. $this->helperData->expects($this->once())
  186. ->method('getIsShareable')
  187. ->with($this->linkPurchasedItem)
  188. ->willReturn(false);
  189. $this->session->expects($this->once())->method('getCustomerId')->willReturn(null);
  190. $this->objectManager->expects($this->at(3))
  191. ->method('create')
  192. ->with(\Magento\Catalog\Model\Product::class)
  193. ->willReturn($this->product);
  194. $this->linkPurchasedItem->expects($this->once())->method('getProductId')->willReturn('product_id');
  195. $this->product->expects($this->once())->method('load')->with('product_id')->willReturnSelf();
  196. $this->product->expects($this->once())->method('getId')->willReturn('product_id');
  197. $this->product->expects($this->once())->method('getProductUrl')->willReturn('product_url');
  198. $this->product->expects($this->once())->method('getName')->willReturn('product_name');
  199. $this->messageManager->expects($this->once())
  200. ->method('addNotice')
  201. ->with('Please sign in to download your product or purchase <a href="product_url">product_name</a>.');
  202. $this->session->expects($this->once())->method('authenticate')->willReturn(true);
  203. $this->objectManager->expects($this->at(4))
  204. ->method('create')
  205. ->with(\Magento\Framework\UrlInterface::class)
  206. ->willReturn($this->urlInterface);
  207. $this->urlInterface->expects($this->once())
  208. ->method('getUrl')
  209. ->with('downloadable/customer/products/', ['_secure' => true])
  210. ->willReturn('before_auth_url');
  211. $this->session->expects($this->once())->method('setBeforeAuthUrl')->with('before_auth_url')->willReturnSelf();
  212. $this->assertNull($this->link->execute());
  213. }
  214. public function testGetLinkForWrongCustomer()
  215. {
  216. $this->objectManager->expects($this->at(0))
  217. ->method('get')
  218. ->with(\Magento\Customer\Model\Session::class)
  219. ->willReturn($this->session);
  220. $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
  221. $this->objectManager->expects($this->at(1))
  222. ->method('create')
  223. ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
  224. ->willReturn($this->linkPurchasedItem);
  225. $this->linkPurchasedItem->expects($this->once())
  226. ->method('load')
  227. ->with('some_id', 'link_hash')
  228. ->willReturnSelf();
  229. $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(5);
  230. $this->objectManager->expects($this->at(2))
  231. ->method('get')
  232. ->with(\Magento\Downloadable\Helper\Data::class)
  233. ->willReturn($this->helperData);
  234. $this->helperData->expects($this->once())
  235. ->method('getIsShareable')
  236. ->with($this->linkPurchasedItem)
  237. ->willReturn(false);
  238. $this->session->expects($this->once())->method('getCustomerId')->willReturn('customer_id');
  239. $this->objectManager->expects($this->at(3))
  240. ->method('create')
  241. ->with(\Magento\Downloadable\Model\Link\Purchased::class)
  242. ->willReturn($this->linkPurchased);
  243. $this->linkPurchasedItem->expects($this->once())->method('getPurchasedId')->willReturn('purchased_id');
  244. $this->linkPurchased->expects($this->once())->method('load')->with('purchased_id')->willReturnSelf();
  245. $this->linkPurchased->expects($this->once())->method('getCustomerId')->willReturn('other_customer_id');
  246. $this->messageManager->expects($this->once())
  247. ->method('addNotice')
  248. ->with("We can't find the link you requested.");
  249. $this->redirect->expects($this->once())->method('redirect')->with($this->response, '*/customer/products', []);
  250. $this->assertEquals($this->response, $this->link->execute());
  251. }
  252. /**
  253. * @param string $mimeType
  254. * @param string $disposition
  255. * @dataProvider downloadTypesDataProvider
  256. * @return void
  257. */
  258. public function testExceptionInUpdateLinkStatus($mimeType, $disposition)
  259. {
  260. $this->objectManager->expects($this->at(0))
  261. ->method('get')
  262. ->with(\Magento\Customer\Model\Session::class)
  263. ->willReturn($this->session);
  264. $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
  265. $this->objectManager->expects($this->at(1))
  266. ->method('create')
  267. ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
  268. ->willReturn($this->linkPurchasedItem);
  269. $this->linkPurchasedItem->expects($this->once())
  270. ->method('load')
  271. ->with('some_id', 'link_hash')
  272. ->willReturnSelf();
  273. $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(5);
  274. $this->objectManager->expects($this->at(2))
  275. ->method('get')
  276. ->with(\Magento\Downloadable\Helper\Data::class)
  277. ->willReturn($this->helperData);
  278. $this->helperData->expects($this->once())
  279. ->method('getIsShareable')
  280. ->with($this->linkPurchasedItem)
  281. ->willReturn(true);
  282. $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsBought')->willReturn(10);
  283. $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsUsed')->willReturn(9);
  284. $this->linkPurchasedItem->expects($this->once())->method('getStatus')->willReturn('available');
  285. $this->linkPurchasedItem->expects($this->once())->method('getLinkType')->willReturn('url');
  286. $this->linkPurchasedItem->expects($this->once())->method('getLinkUrl')->willReturn('link_url');
  287. $this->processDownload('link_url', 'url', $mimeType, $disposition);
  288. $this->linkPurchasedItem->expects($this->any())->method('setNumberOfDownloadsUsed')->willReturnSelf();
  289. $this->linkPurchasedItem->expects($this->any())->method('setStatus')->with('expired')->willReturnSelf();
  290. $this->linkPurchasedItem->expects($this->any())->method('save')->willThrowException(new \Exception());
  291. $this->messageManager->expects($this->once())
  292. ->method('addError')
  293. ->with('Something went wrong while getting the requested content.')
  294. ->willReturnSelf();
  295. $this->redirect->expects($this->once())->method('redirect')->with($this->response, '*/customer/products', []);
  296. $this->assertEquals($this->response, $this->link->execute());
  297. }
  298. /**
  299. * @param string $resource
  300. * @param string $resourceType
  301. * @param string $mimeType
  302. * @param string $disposition
  303. * @return void
  304. */
  305. private function processDownload($resource, $resourceType, $mimeType, $disposition)
  306. {
  307. $fileSize = 58493;
  308. $fileName = 'link.jpg';
  309. $this->objectManager->expects($this->at(3))
  310. ->method('get')
  311. ->with(\Magento\Downloadable\Helper\Download::class)
  312. ->willReturn($this->downloadHelper);
  313. $this->downloadHelper->expects($this->once())
  314. ->method('setResource')
  315. ->with($resource, $resourceType)
  316. ->willReturnSelf();
  317. $this->downloadHelper->expects($this->once())->method('getFilename')->willReturn($fileName);
  318. $this->downloadHelper->expects($this->once())->method('getContentType')->willReturn($mimeType);
  319. $this->response->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
  320. $this->response
  321. ->expects($this->any())
  322. ->method('setHeader')
  323. ->withConsecutive(
  324. ['Pragma', 'public', true],
  325. ['Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true],
  326. ['Content-type', $mimeType, true],
  327. ['Content-Length', $fileSize],
  328. ['Content-Disposition', $disposition . '; filename=' . $fileName]
  329. )
  330. ->willReturnSelf();
  331. $this->downloadHelper->expects($this->once())->method('getContentDisposition')->willReturn($disposition);
  332. $this->downloadHelper->expects($this->once())->method('getFileSize')->willReturn($fileSize);
  333. $this->response->expects($this->once())->method('clearBody')->willReturnSelf();
  334. $this->response->expects($this->once())->method('sendHeaders')->willReturnSelf();
  335. $this->downloadHelper->expects($this->once())->method('output');
  336. }
  337. /**
  338. * @param string $messageType
  339. * @param string $status
  340. * @param string $notice
  341. * @dataProvider linkNotAvailableDataProvider
  342. */
  343. public function testLinkNotAvailable($messageType, $status, $notice)
  344. {
  345. $this->objectManager->expects($this->at(0))
  346. ->method('get')
  347. ->with(\Magento\Customer\Model\Session::class)
  348. ->willReturn($this->session);
  349. $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
  350. $this->objectManager->expects($this->at(1))
  351. ->method('create')
  352. ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
  353. ->willReturn($this->linkPurchasedItem);
  354. $this->linkPurchasedItem->expects($this->once())
  355. ->method('load')
  356. ->with('some_id', 'link_hash')
  357. ->willReturnSelf();
  358. $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(5);
  359. $this->objectManager->expects($this->at(2))
  360. ->method('get')
  361. ->with(\Magento\Downloadable\Helper\Data::class)
  362. ->willReturn($this->helperData);
  363. $this->helperData->expects($this->once())
  364. ->method('getIsShareable')
  365. ->with($this->linkPurchasedItem)
  366. ->willReturn(true);
  367. $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsBought')->willReturn(10);
  368. $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsUsed')->willReturn(9);
  369. $this->linkPurchasedItem->expects($this->once())->method('getStatus')->willReturn($status);
  370. $this->messageManager->expects($this->once())->method($messageType)->with($notice)->willReturnSelf();
  371. $this->assertEquals($this->response, $this->link->execute());
  372. }
  373. /**
  374. * @param string $mimeType
  375. * @param string $disposition
  376. * @dataProvider downloadTypesDataProvider
  377. * @return void
  378. */
  379. public function testContentDisposition($mimeType, $disposition)
  380. {
  381. $this->objectManager->expects($this->any())
  382. ->method('get')
  383. ->willReturnMap([
  384. [
  385. \Magento\Customer\Model\Session::class,
  386. $this->session,
  387. ],
  388. [
  389. \Magento\Downloadable\Helper\Data::class,
  390. $this->helperData,
  391. ],
  392. [
  393. \Magento\Downloadable\Helper\Download::class,
  394. $this->downloadHelper,
  395. ],
  396. ]);
  397. $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
  398. $this->objectManager->expects($this->at(1))
  399. ->method('create')
  400. ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
  401. ->willReturn($this->linkPurchasedItem);
  402. $this->linkPurchasedItem->expects($this->once())
  403. ->method('load')
  404. ->with('some_id', 'link_hash')
  405. ->willReturnSelf();
  406. $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(5);
  407. $this->helperData->expects($this->once())
  408. ->method('getIsShareable')
  409. ->with($this->linkPurchasedItem)
  410. ->willReturn(true);
  411. $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsBought')->willReturn(10);
  412. $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsUsed')->willReturn(9);
  413. $this->linkPurchasedItem->expects($this->once())->method('getStatus')->willReturn('available');
  414. $this->linkPurchasedItem->expects($this->once())->method('getLinkType')->willReturn('url');
  415. $this->linkPurchasedItem->expects($this->once())->method('getLinkUrl')->willReturn('link_url');
  416. $fileSize = 58493;
  417. $fileName = 'link.jpg';
  418. $this->downloadHelper->expects($this->once())
  419. ->method('setResource')
  420. ->with('link_url', 'url')
  421. ->willReturnSelf();
  422. $this->downloadHelper->expects($this->once())->method('getFilename')->willReturn($fileName);
  423. $this->downloadHelper->expects($this->once())->method('getContentType')->willReturn($mimeType);
  424. $this->response->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
  425. $this->response
  426. ->expects($this->any())
  427. ->method('setHeader')
  428. ->withConsecutive(
  429. ['Pragma', 'public', true],
  430. ['Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true],
  431. ['Content-type', $mimeType, true],
  432. ['Content-Length', $fileSize],
  433. ['Content-Disposition', $disposition . '; filename=' . $fileName]
  434. )
  435. ->willReturnSelf();
  436. $this->assertEquals($this->response, $this->link->execute());
  437. }
  438. /**
  439. * @return array
  440. */
  441. public function linkNotAvailableDataProvider()
  442. {
  443. return [
  444. ['addNotice', 'expired', 'The link has expired.'],
  445. ['addNotice', 'pending', 'The link is not available.'],
  446. ['addNotice', 'payment_review', 'The link is not available.'],
  447. ['addError', 'wrong_status', 'Something went wrong while getting the requested content.']
  448. ];
  449. }
  450. /**
  451. * @return array
  452. */
  453. public function downloadTypesDataProvider()
  454. {
  455. return [
  456. ['mimeType' => 'text/html', 'disposition' => \Magento\Framework\HTTP\Mime::DISPOSITION_ATTACHMENT],
  457. ['mimeType' => 'image/jpeg', 'disposition' => \Magento\Framework\HTTP\Mime::DISPOSITION_INLINE],
  458. ];
  459. }
  460. }