123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Downloadable\Test\Unit\Model;
- use Magento\Downloadable\Model\LinkRepository;
- /**
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- class LinkRepositoryTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $repositoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $contentValidatorMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $contentUploaderMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $jsonEncoderMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $linkFactoryMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $productMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $productTypeMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $linkDataObjectFactory;
- /**
- * @var LinkRepository
- */
- protected $service;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $metadataPoolMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $linkHandlerMock;
- /**
- * @var \PHPUnit_Framework_MockObject_MockObject
- */
- protected $entityMetadataMock;
- protected function setUp()
- {
- $this->repositoryMock = $this->createMock(\Magento\Catalog\Model\ProductRepository::class);
- $this->productTypeMock = $this->createMock(\Magento\Downloadable\Model\Product\Type::class);
- $this->linkDataObjectFactory = $this->getMockBuilder(\Magento\Downloadable\Api\Data\LinkInterfaceFactory::class)
- ->setMethods(
- [
- 'create',
- ]
- )
- ->disableOriginalConstructor()
- ->getMock();
- $this->sampleDataObjectFactory = $this->getMockBuilder(
- \Magento\Downloadable\Api\Data\SampleInterfaceFactory::class
- )->setMethods(['create'])
- ->disableOriginalConstructor()
- ->getMock();
- $this->contentValidatorMock = $this->createMock(\Magento\Downloadable\Model\Link\ContentValidator::class);
- $this->contentUploaderMock = $this->createMock(
- \Magento\Downloadable\Api\Data\File\ContentUploaderInterface::class
- );
- $this->jsonEncoderMock = $this->createMock(
- \Magento\Framework\Json\EncoderInterface::class
- );
- $this->linkFactoryMock = $this->createPartialMock(\Magento\Downloadable\Model\LinkFactory::class, ['create']);
- $this->productMock = $this->createPartialMock(\Magento\Catalog\Model\Product::class, [
- '__wakeup',
- 'getTypeId',
- 'setDownloadableData',
- 'save',
- 'getId',
- 'getStoreId',
- 'getStore',
- 'getWebsiteIds',
- 'getData'
- ]);
- $this->service = new \Magento\Downloadable\Model\LinkRepository(
- $this->repositoryMock,
- $this->productTypeMock,
- $this->linkDataObjectFactory,
- $this->linkFactoryMock,
- $this->contentValidatorMock,
- $this->jsonEncoderMock,
- $this->contentUploaderMock
- );
- $this->entityMetadataMock = $this->getMockBuilder(
- \Magento\Framework\EntityManager\EntityMetadataInterface::class
- )->getMockForAbstractClass();
- $linkRepository = new \ReflectionClass(get_class($this->service));
- $metadataPoolProperty = $linkRepository->getProperty('metadataPool');
- $this->metadataPoolMock = $this->getMockBuilder(
- \Magento\Framework\EntityManager\MetadataPool::class
- )->disableOriginalConstructor()->getMock();
- $metadataPoolProperty->setAccessible(true);
- $metadataPoolProperty->setValue(
- $this->service,
- $this->metadataPoolMock
- );
- $saveHandlerProperty = $linkRepository->getProperty('linkTypeHandler');
- $this->linkHandlerMock = $this->getMockBuilder(
- \Magento\Downloadable\Model\Product\TypeHandler\Link::class
- )->disableOriginalConstructor()->getMock();
- $saveHandlerProperty->setAccessible(true);
- $saveHandlerProperty->setValue(
- $this->service,
- $this->linkHandlerMock
- );
- $this->metadataPoolMock->expects($this->any())->method('getMetadata')->willReturn($this->entityMetadataMock);
- }
- /**
- * @param array $linkData
- * @return \PHPUnit_Framework_MockObject_MockObject
- */
- protected function getLinkMock(array $linkData)
- {
- $linkMock = $this->getMockBuilder(\Magento\Downloadable\Api\Data\LinkInterface::class)
- ->setMethods(
- [
- 'getLinkType',
- 'getId',
- 'getPrice',
- 'getTitle',
- 'getSortOrder',
- 'getNumberOfDownloads',
- 'getIsShareable',
- 'getLinkUrl',
- 'getLinkFile'
- ]
- )
- ->getMockForAbstractClass();
- if (isset($linkData['id'])) {
- $linkMock->expects($this->any())->method('getId')->willReturn($linkData['id']);
- }
- $linkMock->expects($this->any())->method('getPrice')->will(
- $this->returnValue(
- $linkData['price']
- )
- );
- $linkMock->expects($this->any())->method('getTitle')->will(
- $this->returnValue(
- $linkData['title']
- )
- );
- $linkMock->expects($this->any())->method('getSortOrder')->will(
- $this->returnValue(
- $linkData['sort_order']
- )
- );
- $linkMock->expects($this->any())->method('getNumberOfDownloads')->will(
- $this->returnValue(
- $linkData['number_of_downloads']
- )
- );
- $linkMock->expects($this->any())->method('getIsShareable')->will(
- $this->returnValue(
- $linkData['is_shareable']
- )
- );
- if (isset($linkData['link_type'])) {
- $linkMock->expects($this->any())->method('getLinkType')->will(
- $this->returnValue(
- $linkData['link_type']
- )
- );
- }
- if (isset($linkData['link_url'])) {
- $linkMock->expects($this->any())->method('getLinkUrl')->will(
- $this->returnValue(
- $linkData['link_url']
- )
- );
- }
- if (isset($linkData['link_file'])) {
- $linkMock->expects($this->any())->method('getLinkFile')->will(
- $this->returnValue(
- $linkData['link_file']
- )
- );
- }
- return $linkMock;
- }
- public function testCreate()
- {
- $productSku = 'simple';
- $linkData = [
- 'title' => 'Title',
- 'sort_order' => 1,
- 'price' => 10.1,
- 'is_shareable' => true,
- 'number_of_downloads' => 100,
- 'link_type' => 'url',
- 'link_url' => 'http://example.com/',
- ];
- $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
- ->will($this->returnValue($this->productMock));
- $this->productMock->expects($this->any())->method('getTypeId')->will($this->returnValue('downloadable'));
- $linkMock = $this->getLinkMock($linkData);
- $this->contentValidatorMock->expects($this->any())->method('isValid')->with($linkMock)
- ->will($this->returnValue(true));
- $downloadableData = [
- 'link' => [
- [
- 'link_id' => 0,
- 'is_delete' => 0,
- 'type' => $linkData['link_type'],
- 'sort_order' => $linkData['sort_order'],
- 'title' => $linkData['title'],
- 'price' => $linkData['price'],
- 'number_of_downloads' => $linkData['number_of_downloads'],
- 'is_shareable' => $linkData['is_shareable'],
- 'link_url' => $linkData['link_url'],
- ],
- ],
- ];
- $this->linkHandlerMock->expects($this->once())->method('save')
- ->with($this->productMock, $downloadableData);
- $this->service->save($productSku, $linkMock);
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage The link title is empty. Enter the link title and try again.
- */
- public function testCreateThrowsExceptionIfTitleIsEmpty()
- {
- $productSku = 'simple';
- $linkData = [
- 'title' => '',
- 'sort_order' => 1,
- 'price' => 10.1,
- 'number_of_downloads' => 100,
- 'is_shareable' => true,
- 'link_type' => 'url',
- 'link_url' => 'http://example.com/',
- ];
- $this->productMock->expects($this->any())->method('getTypeId')->will($this->returnValue('downloadable'));
- $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
- ->will($this->returnValue($this->productMock));
- $linkMock = $this->getLinkMock($linkData);
- $this->contentValidatorMock->expects($this->any())->method('isValid')->with($linkMock)
- ->will($this->returnValue(true));
- $this->productMock->expects($this->never())->method('save');
- $this->service->save($productSku, $linkMock);
- }
- public function testUpdate()
- {
- $websiteId = 1;
- $linkId = 1;
- $productSku = 'simple';
- $productId = 1;
- $linkData = [
- 'id' => $linkId,
- 'title' => 'Updated Title',
- 'sort_order' => 1,
- 'price' => 10.1,
- 'is_shareable' => true,
- 'number_of_downloads' => 100,
- 'link_type' => 'url',
- 'link_url' => 'http://example.com/',
- ];
- $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
- ->will($this->returnValue($this->productMock));
- $this->productMock->expects($this->any())->method('getData')->will($this->returnValue($productId));
- $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
- $storeMock->expects($this->any())->method('getWebsiteId')->will($this->returnValue($websiteId));
- $this->productMock->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
- $existingLinkMock = $this->createPartialMock(\Magento\Downloadable\Model\Link::class, [
- '__wakeup',
- 'getId',
- 'load',
- 'getProductId'
- ]);
- $this->linkFactoryMock->expects($this->once())->method('create')->will($this->returnValue($existingLinkMock));
- $linkMock = $this->getLinkMock($linkData);
- $this->contentValidatorMock->expects($this->any())->method('isValid')->with($linkMock)
- ->will($this->returnValue(true));
- $existingLinkMock->expects($this->any())->method('getId')->will($this->returnValue($linkId));
- $existingLinkMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
- $existingLinkMock->expects($this->once())->method('load')->with($linkId)->will($this->returnSelf());
- $this->linkHandlerMock->expects($this->once())->method('save')
- ->with(
- $this->productMock,
- [
- 'link' => [
- [
- 'link_id' => $linkId,
- 'is_delete' => 0,
- 'type' => $linkData['link_type'],
- 'sort_order' => $linkData['sort_order'],
- 'title' => $linkData['title'],
- 'price' => $linkData['price'],
- 'number_of_downloads' => $linkData['number_of_downloads'],
- 'is_shareable' => $linkData['is_shareable'],
- 'link_url' => $linkData['link_url'],
- ],
- ],
- ]
- );
- $this->assertEquals($linkId, $this->service->save($productSku, $linkMock));
- }
- public function testUpdateWithExistingFile()
- {
- $websiteId = 1;
- $linkId = 1;
- $productSku = 'simple';
- $productId = 1;
- $linkFile = '/l/i/link.jpg';
- $encodedFiles = "something";
- $linkData = [
- 'id' => $linkId,
- 'title' => 'Updated Title',
- 'sort_order' => 1,
- 'price' => 10.1,
- 'is_shareable' => true,
- 'number_of_downloads' => 100,
- 'link_type' => 'file',
- 'link_file' => $linkFile,
- ];
- $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
- ->will($this->returnValue($this->productMock));
- $this->productMock->expects($this->any())->method('getData')->will($this->returnValue($productId));
- $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
- $storeMock->expects($this->any())->method('getWebsiteId')->will($this->returnValue($websiteId));
- $this->productMock->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
- $existingLinkMock = $this->createPartialMock(\Magento\Downloadable\Model\Link::class, [
- '__wakeup',
- 'getId',
- 'load',
- 'getProductId'
- ]);
- $this->linkFactoryMock->expects($this->once())->method('create')->will($this->returnValue($existingLinkMock));
- $linkMock = $this->getLinkMock($linkData);
- $this->contentValidatorMock->expects($this->any())->method('isValid')->with($linkMock)
- ->will($this->returnValue(true));
- $existingLinkMock->expects($this->any())->method('getId')->will($this->returnValue($linkId));
- $existingLinkMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
- $existingLinkMock->expects($this->once())->method('load')->with($linkId)->will($this->returnSelf());
- $this->jsonEncoderMock->expects($this->once())
- ->method('encode')
- ->with(
- [
- [
- 'file' => $linkFile,
- 'status' => 'old'
- ]
- ]
- )->willReturn($encodedFiles);
- $this->linkHandlerMock->expects($this->once())->method('save')
- ->with(
- $this->productMock,
- [
- 'link' => [
- [
- 'link_id' => $linkId,
- 'is_delete' => 0,
- 'type' => $linkData['link_type'],
- 'sort_order' => $linkData['sort_order'],
- 'title' => $linkData['title'],
- 'price' => $linkData['price'],
- 'number_of_downloads' => $linkData['number_of_downloads'],
- 'is_shareable' => $linkData['is_shareable'],
- 'file' => $encodedFiles,
- ],
- ],
- ]
- );
- $this->assertEquals($linkId, $this->service->save($productSku, $linkMock));
- }
- /**
- * @expectedException \Magento\Framework\Exception\InputException
- * @expectedExceptionMessage The link title is empty. Enter the link title and try again.
- */
- public function testUpdateThrowsExceptionIfTitleIsEmptyAndScopeIsGlobal()
- {
- $linkId = 1;
- $productSku = 'simple';
- $productId = 1;
- $linkData = [
- 'id' => $linkId,
- 'title' => '',
- 'sort_order' => 1,
- 'price' => 10.1,
- 'number_of_downloads' => 100,
- 'is_shareable' => true,
- ];
- $this->repositoryMock->expects($this->any())->method('get')->with($productSku, true)
- ->will($this->returnValue($this->productMock));
- $this->productMock->expects($this->any())->method('getData')->will($this->returnValue($productId));
- $existingLinkMock = $this->createPartialMock(
- \Magento\Downloadable\Model\Link::class,
- ['__wakeup', 'getId', 'load', 'save', 'getProductId']
- );
- $existingLinkMock->expects($this->any())->method('getId')->will($this->returnValue($linkId));
- $existingLinkMock->expects($this->any())->method('getProductId')->will($this->returnValue($productId));
- $existingLinkMock->expects($this->once())->method('load')->with($linkId)->will($this->returnSelf());
- $this->linkFactoryMock->expects($this->once())->method('create')->will($this->returnValue($existingLinkMock));
- $linkContentMock = $this->getLinkMock($linkData);
- $this->contentValidatorMock->expects($this->any())->method('isValid')->with($linkContentMock)
- ->will($this->returnValue(true));
- $this->linkHandlerMock->expects($this->never())->method('save');
- $this->service->save($productSku, $linkContentMock, true);
- }
- public function testDelete()
- {
- $linkId = 1;
- $linkMock = $this->createMock(\Magento\Downloadable\Model\Link::class);
- $this->linkFactoryMock->expects($this->once())->method('create')->will($this->returnValue($linkMock));
- $linkMock->expects($this->once())->method('load')->with($linkId)->will($this->returnSelf());
- $linkMock->expects($this->any())->method('getId')->will($this->returnValue($linkId));
- $linkMock->expects($this->once())->method('delete');
- $this->assertTrue($this->service->delete($linkId));
- }
- /**
- * @expectedException \Magento\Framework\Exception\NoSuchEntityException
- * @expectedExceptionMessage No downloadable link with the provided ID was found. Verify the ID and try again.
- */
- public function testDeleteThrowsExceptionIfLinkIdIsNotValid()
- {
- $linkId = 1;
- $linkMock = $this->createMock(\Magento\Downloadable\Model\Link::class);
- $this->linkFactoryMock->expects($this->once())->method('create')->will($this->returnValue($linkMock));
- $linkMock->expects($this->once())->method('load')->with($linkId)->will($this->returnSelf());
- $linkMock->expects($this->once())->method('getId');
- $linkMock->expects($this->never())->method('delete');
- $this->service->delete($linkId);
- }
- public function testGetList()
- {
- $productSku = 'downloadable_sku';
- $linkData = [
- 'id' => 324,
- 'store_title' => 'rock melody',
- 'title' => 'just melody',
- 'price' => 23,
- 'number_of_downloads' => 3,
- 'sort_order' => 21,
- 'is_shareable' => 2,
- 'sample_type' => 'file',
- 'sample_url' => null,
- 'sample_file' => '/r/o/rock.melody.ogg',
- 'link_type' => 'url',
- 'link_url' => 'http://link.url',
- 'link_file' => null
- ];
- $linkMock = $this->createPartialMock(\Magento\Downloadable\Model\Link::class, [
- 'getId',
- 'getStoreTitle',
- 'getTitle',
- 'getPrice',
- 'getNumberOfDownloads',
- 'getSortOrder',
- 'getIsShareable',
- 'getData',
- '__wakeup',
- 'getSampleType',
- 'getSampleFile',
- 'getSampleUrl',
- 'getLinkType',
- 'getLinkFile',
- 'getLinkUrl'
- ]);
- $linkInterfaceMock = $this->createMock(\Magento\Downloadable\Api\Data\LinkInterface::class);
- $this->repositoryMock->expects($this->once())
- ->method('get')
- ->with($productSku)
- ->will($this->returnValue($this->productMock));
- $this->productTypeMock->expects($this->once())
- ->method('getLinks')
- ->with($this->productMock)
- ->will($this->returnValue([$linkMock]));
- $this->setLinkAssertions($linkMock, $linkData);
- $this->linkDataObjectFactory->expects($this->once())->method('create')->willReturn($linkInterfaceMock);
- $this->assertEquals([$linkInterfaceMock], $this->service->getList($productSku));
- }
- /**
- * @param $resource
- * @param $inputData
- */
- protected function setLinkAssertions($resource, $inputData)
- {
- $resource->expects($this->any())->method('getId')->will($this->returnValue($inputData['id']));
- $resource->expects($this->any())->method('getStoreTitle')
- ->will($this->returnValue($inputData['store_title']));
- $resource->expects($this->any())->method('getTitle')
- ->will($this->returnValue($inputData['title']));
- $resource->expects($this->any())->method('getSampleType')
- ->will($this->returnValue($inputData['sample_type']));
- $resource->expects($this->any())->method('getSampleFile')
- ->will($this->returnValue($inputData['sample_file']));
- $resource->expects($this->any())->method('getSampleUrl')
- ->will($this->returnValue($inputData['sample_url']));
- $resource->expects($this->any())->method('getPrice')
- ->will($this->returnValue($inputData['price']));
- $resource->expects($this->once())->method('getNumberOfDownloads')
- ->will($this->returnValue($inputData['number_of_downloads']));
- $resource->expects($this->once())->method('getSortOrder')
- ->will($this->returnValue($inputData['sort_order']));
- $resource->expects($this->once())->method('getIsShareable')
- ->will($this->returnValue($inputData['is_shareable']));
- $resource->expects($this->any())->method('getLinkType')
- ->will($this->returnValue($inputData['link_type']));
- $resource->expects($this->any())->method('getlinkFile')
- ->will($this->returnValue($inputData['link_file']));
- $resource->expects($this->any())->method('getLinkUrl')
- ->will($this->returnValue($inputData['link_url']));
- }
- }
|