123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Downloadable\Test\Unit\Controller\Download;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class LinkTest extends \PHPUnit\Framework\TestCase
- {
- /** @var \Magento\Downloadable\Controller\Download\Link */
- protected $link;
- /** @var ObjectManagerHelper */
- protected $objectManagerHelper;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Request\Http
- */
- protected $request;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\ResponseInterface
- */
- protected $response;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Downloadable\Model\Link\Purchased\Item
- */
- protected $linkPurchasedItem;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Downloadable\Model\Link\Purchased
- */
- protected $linkPurchased;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\ObjectManager\ObjectManager
- */
- protected $objectManager;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Message\ManagerInterface
- */
- protected $messageManager;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\App\Response\RedirectInterface
- */
- protected $redirect;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\Session
- */
- protected $session;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Downloadable\Helper\Data
- */
- protected $helperData;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Downloadable\Helper\Download
- */
- protected $downloadHelper;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Catalog\Model\Product
- */
- protected $product;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\UrlInterface
- */
- protected $urlInterface;
- /**
- * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
- */
- protected function setUp()
- {
- $this->objectManagerHelper = new ObjectManagerHelper($this);
- $this->request = $this->getMockBuilder(\Magento\Framework\App\Request\Http::class)
- ->disableOriginalConstructor()->getMock();
- $this->response = $this->createPartialMock(
- \Magento\Framework\App\ResponseInterface::class,
- [
- 'setHttpResponseCode',
- 'clearBody',
- 'sendHeaders',
- 'sendResponse',
- 'setHeader'
- ]
- );
- $this->session = $this->createPartialMock(\Magento\Customer\Model\Session::class, [
- 'getCustomerId',
- 'authenticate',
- 'setBeforeAuthUrl'
- ]);
- $this->helperData = $this->createPartialMock(\Magento\Downloadable\Helper\Data::class, [
- 'getIsShareable'
- ]);
- $this->downloadHelper = $this->createPartialMock(\Magento\Downloadable\Helper\Download::class, [
- 'setResource',
- 'getFilename',
- 'getContentType',
- 'getFileSize',
- 'getContentDisposition',
- 'output'
- ]);
- $this->product = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
- '_wakeup',
- 'load',
- 'getId',
- 'getProductUrl',
- 'getName'
- ]);
- $this->linkPurchasedItem = $this->createPartialMock(\Magento\Downloadable\Model\Link\Purchased\Item::class, [
- 'load',
- 'getId',
- 'getProductId',
- 'getPurchasedId',
- 'getNumberOfDownloadsBought',
- 'getNumberOfDownloadsUsed',
- 'getStatus',
- 'getLinkType',
- 'getLinkUrl',
- 'getLinkFile',
- 'setNumberOfDownloadsUsed',
- 'setStatus',
- 'save',
- ]);
- $this->linkPurchased = $this->createPartialMock(\Magento\Downloadable\Model\Link\Purchased::class, [
- 'load',
- 'getCustomerId'
- ]);
- $this->messageManager = $this->createMock(\Magento\Framework\Message\ManagerInterface::class);
- $this->redirect = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
- $this->urlInterface = $this->createMock(\Magento\Framework\UrlInterface::class);
- $this->objectManager = $this->createPartialMock(\Magento\Framework\ObjectManager\ObjectManager::class, [
- 'create',
- 'get'
- ]);
- $this->link = $this->objectManagerHelper->getObject(
- \Magento\Downloadable\Controller\Download\Link::class,
- [
- 'objectManager' => $this->objectManager,
- 'request' => $this->request,
- 'response' => $this->response,
- 'messageManager' => $this->messageManager,
- 'redirect' => $this->redirect
- ]
- );
- }
- public function testAbsentLinkId()
- {
- $this->objectManager->expects($this->once())
- ->method('get')
- ->with(\Magento\Customer\Model\Session::class)
- ->willReturn($this->session);
- $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
- $this->objectManager->expects($this->once())
- ->method('create')
- ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
- ->willReturn($this->linkPurchasedItem);
- $this->linkPurchasedItem->expects($this->once())
- ->method('load')
- ->with('some_id', 'link_hash')
- ->willReturnSelf();
- $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(null);
- $this->messageManager->expects($this->once())
- ->method('addNotice')
- ->with("We can't find the link you requested.");
- $this->redirect->expects($this->once())->method('redirect')->with($this->response, '*/customer/products', []);
- $this->assertEquals($this->response, $this->link->execute());
- }
- public function testGetLinkForGuestCustomer()
- {
- $this->objectManager->expects($this->at(0))
- ->method('get')
- ->with(\Magento\Customer\Model\Session::class)
- ->willReturn($this->session);
- $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
- $this->objectManager->expects($this->at(1))
- ->method('create')
- ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
- ->willReturn($this->linkPurchasedItem);
- $this->linkPurchasedItem->expects($this->once())
- ->method('load')
- ->with('some_id', 'link_hash')
- ->willReturnSelf();
- $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(5);
- $this->objectManager->expects($this->at(2))
- ->method('get')
- ->with(\Magento\Downloadable\Helper\Data::class)
- ->willReturn($this->helperData);
- $this->helperData->expects($this->once())
- ->method('getIsShareable')
- ->with($this->linkPurchasedItem)
- ->willReturn(false);
- $this->session->expects($this->once())->method('getCustomerId')->willReturn(null);
- $this->objectManager->expects($this->at(3))
- ->method('create')
- ->with(\Magento\Catalog\Model\Product::class)
- ->willReturn($this->product);
- $this->linkPurchasedItem->expects($this->once())->method('getProductId')->willReturn('product_id');
- $this->product->expects($this->once())->method('load')->with('product_id')->willReturnSelf();
- $this->product->expects($this->once())->method('getId')->willReturn('product_id');
- $this->product->expects($this->once())->method('getProductUrl')->willReturn('product_url');
- $this->product->expects($this->once())->method('getName')->willReturn('product_name');
- $this->messageManager->expects($this->once())
- ->method('addNotice')
- ->with('Please sign in to download your product or purchase <a href="product_url">product_name</a>.');
- $this->session->expects($this->once())->method('authenticate')->willReturn(true);
- $this->objectManager->expects($this->at(4))
- ->method('create')
- ->with(\Magento\Framework\UrlInterface::class)
- ->willReturn($this->urlInterface);
- $this->urlInterface->expects($this->once())
- ->method('getUrl')
- ->with('downloadable/customer/products/', ['_secure' => true])
- ->willReturn('before_auth_url');
- $this->session->expects($this->once())->method('setBeforeAuthUrl')->with('before_auth_url')->willReturnSelf();
- $this->assertNull($this->link->execute());
- }
- public function testGetLinkForWrongCustomer()
- {
- $this->objectManager->expects($this->at(0))
- ->method('get')
- ->with(\Magento\Customer\Model\Session::class)
- ->willReturn($this->session);
- $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
- $this->objectManager->expects($this->at(1))
- ->method('create')
- ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
- ->willReturn($this->linkPurchasedItem);
- $this->linkPurchasedItem->expects($this->once())
- ->method('load')
- ->with('some_id', 'link_hash')
- ->willReturnSelf();
- $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(5);
- $this->objectManager->expects($this->at(2))
- ->method('get')
- ->with(\Magento\Downloadable\Helper\Data::class)
- ->willReturn($this->helperData);
- $this->helperData->expects($this->once())
- ->method('getIsShareable')
- ->with($this->linkPurchasedItem)
- ->willReturn(false);
- $this->session->expects($this->once())->method('getCustomerId')->willReturn('customer_id');
- $this->objectManager->expects($this->at(3))
- ->method('create')
- ->with(\Magento\Downloadable\Model\Link\Purchased::class)
- ->willReturn($this->linkPurchased);
- $this->linkPurchasedItem->expects($this->once())->method('getPurchasedId')->willReturn('purchased_id');
- $this->linkPurchased->expects($this->once())->method('load')->with('purchased_id')->willReturnSelf();
- $this->linkPurchased->expects($this->once())->method('getCustomerId')->willReturn('other_customer_id');
- $this->messageManager->expects($this->once())
- ->method('addNotice')
- ->with("We can't find the link you requested.");
- $this->redirect->expects($this->once())->method('redirect')->with($this->response, '*/customer/products', []);
- $this->assertEquals($this->response, $this->link->execute());
- }
- /**
- * @param string $mimeType
- * @param string $disposition
- * @dataProvider downloadTypesDataProvider
- * @return void
- */
- public function testExceptionInUpdateLinkStatus($mimeType, $disposition)
- {
- $this->objectManager->expects($this->at(0))
- ->method('get')
- ->with(\Magento\Customer\Model\Session::class)
- ->willReturn($this->session);
- $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
- $this->objectManager->expects($this->at(1))
- ->method('create')
- ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
- ->willReturn($this->linkPurchasedItem);
- $this->linkPurchasedItem->expects($this->once())
- ->method('load')
- ->with('some_id', 'link_hash')
- ->willReturnSelf();
- $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(5);
- $this->objectManager->expects($this->at(2))
- ->method('get')
- ->with(\Magento\Downloadable\Helper\Data::class)
- ->willReturn($this->helperData);
- $this->helperData->expects($this->once())
- ->method('getIsShareable')
- ->with($this->linkPurchasedItem)
- ->willReturn(true);
- $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsBought')->willReturn(10);
- $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsUsed')->willReturn(9);
- $this->linkPurchasedItem->expects($this->once())->method('getStatus')->willReturn('available');
- $this->linkPurchasedItem->expects($this->once())->method('getLinkType')->willReturn('url');
- $this->linkPurchasedItem->expects($this->once())->method('getLinkUrl')->willReturn('link_url');
- $this->processDownload('link_url', 'url', $mimeType, $disposition);
- $this->linkPurchasedItem->expects($this->any())->method('setNumberOfDownloadsUsed')->willReturnSelf();
- $this->linkPurchasedItem->expects($this->any())->method('setStatus')->with('expired')->willReturnSelf();
- $this->linkPurchasedItem->expects($this->any())->method('save')->willThrowException(new \Exception());
- $this->messageManager->expects($this->once())
- ->method('addError')
- ->with('Something went wrong while getting the requested content.')
- ->willReturnSelf();
- $this->redirect->expects($this->once())->method('redirect')->with($this->response, '*/customer/products', []);
- $this->assertEquals($this->response, $this->link->execute());
- }
- /**
- * @param string $resource
- * @param string $resourceType
- * @param string $mimeType
- * @param string $disposition
- * @return void
- */
- private function processDownload($resource, $resourceType, $mimeType, $disposition)
- {
- $fileSize = 58493;
- $fileName = 'link.jpg';
- $this->objectManager->expects($this->at(3))
- ->method('get')
- ->with(\Magento\Downloadable\Helper\Download::class)
- ->willReturn($this->downloadHelper);
- $this->downloadHelper->expects($this->once())
- ->method('setResource')
- ->with($resource, $resourceType)
- ->willReturnSelf();
- $this->downloadHelper->expects($this->once())->method('getFilename')->willReturn($fileName);
- $this->downloadHelper->expects($this->once())->method('getContentType')->willReturn($mimeType);
- $this->response->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
- $this->response
- ->expects($this->any())
- ->method('setHeader')
- ->withConsecutive(
- ['Pragma', 'public', true],
- ['Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true],
- ['Content-type', $mimeType, true],
- ['Content-Length', $fileSize],
- ['Content-Disposition', $disposition . '; filename=' . $fileName]
- )
- ->willReturnSelf();
- $this->downloadHelper->expects($this->once())->method('getContentDisposition')->willReturn($disposition);
- $this->downloadHelper->expects($this->once())->method('getFileSize')->willReturn($fileSize);
- $this->response->expects($this->once())->method('clearBody')->willReturnSelf();
- $this->response->expects($this->once())->method('sendHeaders')->willReturnSelf();
- $this->downloadHelper->expects($this->once())->method('output');
- }
- /**
- * @param string $messageType
- * @param string $status
- * @param string $notice
- * @dataProvider linkNotAvailableDataProvider
- */
- public function testLinkNotAvailable($messageType, $status, $notice)
- {
- $this->objectManager->expects($this->at(0))
- ->method('get')
- ->with(\Magento\Customer\Model\Session::class)
- ->willReturn($this->session);
- $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
- $this->objectManager->expects($this->at(1))
- ->method('create')
- ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
- ->willReturn($this->linkPurchasedItem);
- $this->linkPurchasedItem->expects($this->once())
- ->method('load')
- ->with('some_id', 'link_hash')
- ->willReturnSelf();
- $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(5);
- $this->objectManager->expects($this->at(2))
- ->method('get')
- ->with(\Magento\Downloadable\Helper\Data::class)
- ->willReturn($this->helperData);
- $this->helperData->expects($this->once())
- ->method('getIsShareable')
- ->with($this->linkPurchasedItem)
- ->willReturn(true);
- $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsBought')->willReturn(10);
- $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsUsed')->willReturn(9);
- $this->linkPurchasedItem->expects($this->once())->method('getStatus')->willReturn($status);
- $this->messageManager->expects($this->once())->method($messageType)->with($notice)->willReturnSelf();
- $this->assertEquals($this->response, $this->link->execute());
- }
- /**
- * @param string $mimeType
- * @param string $disposition
- * @dataProvider downloadTypesDataProvider
- * @return void
- */
- public function testContentDisposition($mimeType, $disposition)
- {
- $this->objectManager->expects($this->any())
- ->method('get')
- ->willReturnMap([
- [
- \Magento\Customer\Model\Session::class,
- $this->session,
- ],
- [
- \Magento\Downloadable\Helper\Data::class,
- $this->helperData,
- ],
- [
- \Magento\Downloadable\Helper\Download::class,
- $this->downloadHelper,
- ],
- ]);
-
- $this->request->expects($this->once())->method('getParam')->with('id', 0)->willReturn('some_id');
- $this->objectManager->expects($this->at(1))
- ->method('create')
- ->with(\Magento\Downloadable\Model\Link\Purchased\Item::class)
- ->willReturn($this->linkPurchasedItem);
- $this->linkPurchasedItem->expects($this->once())
- ->method('load')
- ->with('some_id', 'link_hash')
- ->willReturnSelf();
- $this->linkPurchasedItem->expects($this->once())->method('getId')->willReturn(5);
- $this->helperData->expects($this->once())
- ->method('getIsShareable')
- ->with($this->linkPurchasedItem)
- ->willReturn(true);
- $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsBought')->willReturn(10);
- $this->linkPurchasedItem->expects($this->any())->method('getNumberOfDownloadsUsed')->willReturn(9);
- $this->linkPurchasedItem->expects($this->once())->method('getStatus')->willReturn('available');
- $this->linkPurchasedItem->expects($this->once())->method('getLinkType')->willReturn('url');
- $this->linkPurchasedItem->expects($this->once())->method('getLinkUrl')->willReturn('link_url');
- $fileSize = 58493;
- $fileName = 'link.jpg';
- $this->downloadHelper->expects($this->once())
- ->method('setResource')
- ->with('link_url', 'url')
- ->willReturnSelf();
- $this->downloadHelper->expects($this->once())->method('getFilename')->willReturn($fileName);
- $this->downloadHelper->expects($this->once())->method('getContentType')->willReturn($mimeType);
- $this->response->expects($this->once())->method('setHttpResponseCode')->with(200)->willReturnSelf();
- $this->response
- ->expects($this->any())
- ->method('setHeader')
- ->withConsecutive(
- ['Pragma', 'public', true],
- ['Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true],
- ['Content-type', $mimeType, true],
- ['Content-Length', $fileSize],
- ['Content-Disposition', $disposition . '; filename=' . $fileName]
- )
- ->willReturnSelf();
- $this->assertEquals($this->response, $this->link->execute());
- }
- /**
- * @return array
- */
- public function linkNotAvailableDataProvider()
- {
- return [
- ['addNotice', 'expired', 'The link has expired.'],
- ['addNotice', 'pending', 'The link is not available.'],
- ['addNotice', 'payment_review', 'The link is not available.'],
- ['addError', 'wrong_status', 'Something went wrong while getting the requested content.']
- ];
- }
- /**
- * @return array
- */
- public function downloadTypesDataProvider()
- {
- return [
- ['mimeType' => 'text/html', 'disposition' => \Magento\Framework\HTTP\Mime::DISPOSITION_ATTACHMENT],
- ['mimeType' => 'image/jpeg', 'disposition' => \Magento\Framework\HTTP\Mime::DISPOSITION_INLINE],
- ];
- }
- }
|