public function testGetList() { $productSku = 'downloadable_sku'; $sampleData = ['id' => 324, 'store_title' => 'rock melody sample', 'title' => 'just melody sample', 'sort_order' => 21, 'sample_type' => 'file', 'sample_url' => null, 'sample_file' => '/r/o/rock.melody.ogg']; $sampleMock = $this->getMock('\\Magento\\Downloadable\\Model\\Sample', ['getId', 'getStoreTitle', 'getTitle', 'getSampleType', 'getSampleFile', 'getSampleUrl', 'getSortOrder', 'getData', '__wakeup'], [], '', false); $sampleInterfaceMock = $this->getMock('\\Magento\\Downloadable\\Api\\Data\\SampleInterface'); $this->repositoryMock->expects($this->once())->method('get')->with($productSku)->will($this->returnValue($this->productMock)); $this->productTypeMock->expects($this->once())->method('getSamples')->with($this->productMock)->will($this->returnValue([$sampleMock])); $this->setSampleAssertions($sampleMock, $sampleData); $this->sampleFactoryMock->expects($this->once())->method('create')->willReturn($sampleInterfaceMock); $this->assertEquals([$sampleInterfaceMock], $this->service->getList($productSku)); }
public function testSave() { $data = ['sample' => ['sampleData', 'link' => ['linkData']]]; $this->product->expects($this->once())->method('getDownloadableData')->will($this->returnValue($data)); $this->typeHandler->expects($this->once())->method('save')->with($this->product, $data); $this->target->save($this->product); }
public function testGetLinkData() { $expectingFileData = ['file' => ['file' => 'file/link.gif', 'name' => '<a href="final_url">link.gif</a>', 'size' => '1.1', 'status' => 'old'], 'sample_file' => ['file' => 'file/sample.gif', 'name' => '<a href="final_url">sample.gif</a>', 'size' => '1.1', 'status' => 'old']]; $this->productModel->expects($this->any())->method('getTypeId')->will($this->returnValue('downloadable')); $this->productModel->expects($this->any())->method('getTypeInstance')->will($this->returnValue($this->downloadableProductModel)); $this->productModel->expects($this->any())->method('getStoreId')->will($this->returnValue(0)); $this->downloadableProductModel->expects($this->any())->method('getLinks')->will($this->returnValue([$this->downloadableLinkModel])); $this->coreRegistry->expects($this->any())->method('registry')->will($this->returnValue($this->productModel)); $this->downloadableLinkModel->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->downloadableLinkModel->expects($this->any())->method('getTitle')->will($this->returnValue('Link Title')); $this->downloadableLinkModel->expects($this->any())->method('getPrice')->will($this->returnValue('10')); $this->downloadableLinkModel->expects($this->any())->method('getNumberOfDownloads')->will($this->returnValue('6')); $this->downloadableLinkModel->expects($this->any())->method('getLinkUrl')->will($this->returnValue(null)); $this->downloadableLinkModel->expects($this->any())->method('getLinkType')->will($this->returnValue('file')); $this->downloadableLinkModel->expects($this->any())->method('getSampleFile')->will($this->returnValue('file/sample.gif')); $this->downloadableLinkModel->expects($this->any())->method('getSampleType')->will($this->returnValue('file')); $this->downloadableLinkModel->expects($this->any())->method('getSortOrder')->will($this->returnValue(0)); $this->downloadableLinkModel->expects($this->any())->method('getLinkFile')->will($this->returnValue('file/link.gif')); $this->downloadableLinkModel->expects($this->any())->method('getStoreTitle')->will($this->returnValue('Store Title')); $this->escaper->expects($this->any())->method('escapeHtml')->will($this->returnValue('Link Title')); $this->fileHelper->expects($this->any())->method('getFilePath')->will($this->returnValue('/file/path/link.gif')); $this->fileHelper->expects($this->any())->method('ensureFileInFilesystem')->will($this->returnValue(true)); $this->fileHelper->expects($this->any())->method('getFileSize')->will($this->returnValue('1.1')); $this->urlBuilder->expects($this->any())->method('getUrl')->will($this->returnValue('final_url')); $linkData = $this->block->getLinkData(); foreach ($linkData as $link) { $fileSave = $link->getFileSave(0); $sampleFileSave = $link->getSampleFileSave(0); $this->assertEquals($expectingFileData['file'], $fileSave); $this->assertEquals($expectingFileData['sample_file'], $sampleFileSave); } }
/** * @magentoDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php * @magentoAppArea adminhtml */ public function testSaveTypeSpecificData() { $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $product->setOrigData(); $downloadableData = []; $links = $this->_model->getLinks($product); $this->assertNotEmpty($links); $samples = $this->_model->getSamples($product); $this->assertNotEmpty($samples->getData()); $i = 0; foreach ($links as $link) { $i++; $linkData = $link->getData(); $linkData['is_delete'] = 0; $linkData['type'] = \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE; $linkData['title'] = 'Updated downloadable link #' . $i; $downloadableData['link'][] = $linkData; } $i = 0; foreach ($samples as $sample) { $i++; $sampleData = $sample->getData(); $sampleData['is_delete'] = 0; $sampleData['type'] = \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE; $sampleData['title'] = 'Updated downloadable sample #' . $i; $downloadableData['sample'][] = $sampleData; } $product->setDownloadableData($downloadableData); $this->_model->save($product); $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $expectedLink = ['default_price' => null, 'default_title' => null, 'is_shareable' => '2', 'link_file' => null, 'link_type' => 'file', 'link_url' => null, 'number_of_downloads' => '15', 'price' => '15.0000', 'product' => $product, 'product_id' => 1, 'sample_file' => null, 'sample_type' => 'file', 'sample_url' => null, 'sort_order' => '0', 'store_title' => 'Updated downloadable link #1', 'title' => 'Updated downloadable link #1', 'website_price' => '15.0000']; $links = $this->_model->getLinks($product); $this->assertNotEmpty($links); $this->assertCount(1, $links); $link = reset($links); foreach ($expectedLink as $key => $value) { $this->assertTrue($link->hasData($key), 'Key ' . $key . ' not exist!'); $this->assertArrayHasKey($key, $link); $this->assertEquals($value, $link->getData($key)); } $expectedSample = ['default_title' => null, 'product_id' => '1', 'sample_file' => null, 'sample_type' => 'file', 'sample_url' => null, 'sort_order' => '0', 'store_title' => 'Updated downloadable sample #1', 'title' => 'Updated downloadable sample #1']; $samples = $this->_model->getSamples($product); $this->assertNotEmpty($samples->getData()); $this->assertEquals(1, $samples->count()); /** @var \Magento\Downloadable\Model\Sample $sample */ $sample = $samples->getFirstItem()->getData(); foreach ($expectedSample as $key => $value) { $this->assertArrayHasKey($key, $sample); $this->assertEquals($value, $sample[$key]); } }
public function testGetSampleData() { $expectingFileData = array('sample_file' => array('file' => 'file/sample.gif', 'name' => '<a href="final_url">sample.gif</a>', 'size' => '1.1', 'status' => 'old')); $this->productModel->expects($this->any())->method('getTypeId')->will($this->returnValue('downloadable')); $this->productModel->expects($this->any())->method('getTypeInstance')->will($this->returnValue($this->downloadableProductModel)); $this->productModel->expects($this->any())->method('getStoreId')->will($this->returnValue(0)); $this->downloadableProductModel->expects($this->any())->method('getSamples')->will($this->returnValue(array($this->downloadableSampleModel))); $this->coreRegistry->expects($this->any())->method('registry')->will($this->returnValue($this->productModel)); $this->downloadableSampleModel->expects($this->any())->method('getId')->will($this->returnValue(1)); $this->downloadableSampleModel->expects($this->any())->method('getTitle')->will($this->returnValue('Sample Title')); $this->downloadableSampleModel->expects($this->any())->method('getSampleUrl')->will($this->returnValue(null)); $this->downloadableSampleModel->expects($this->any())->method('getSampleFile')->will($this->returnValue('file/sample.gif')); $this->downloadableSampleModel->expects($this->any())->method('getSampleType')->will($this->returnValue('file')); $this->downloadableSampleModel->expects($this->any())->method('getSortOrder')->will($this->returnValue(0)); $this->escaper->expects($this->any())->method('escapeHtml')->will($this->returnValue('Sample Title')); $this->fileHelper->expects($this->any())->method('getFilePath')->will($this->returnValue('/file/path/sample.gif')); $this->fileHelper->expects($this->any())->method('ensureFileInFilesystem')->will($this->returnValue(true)); $this->fileHelper->expects($this->any())->method('getFileSize')->will($this->returnValue('1.1')); $this->urlBuilder->expects($this->any())->method('getUrl')->will($this->returnValue('final_url')); $sampleData = $this->block->getSampleData(); foreach ($sampleData as $sample) { $fileSave = $sample->getFileSave(0); $this->assertEquals($expectingFileData['sample_file'], $fileSave); } }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param LinkInterface $link * @param bool $isGlobalScopeContent * @return int */ protected function saveLink(\Magento\Catalog\Api\Data\ProductInterface $product, LinkInterface $link, $isGlobalScopeContent) { $linkData = ['link_id' => $link->getid() === null ? 0 : $link->getid(), 'is_delete' => 0, 'type' => $link->getLinkType(), 'sort_order' => $link->getSortOrder(), 'title' => $link->getTitle(), 'price' => $link->getPrice(), 'number_of_downloads' => $link->getNumberOfDownloads(), 'is_shareable' => $link->getIsShareable()]; if ($link->getLinkType() == 'file' && $link->getLinkFile() === null) { $linkData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($link->getLinkFileContent(), 'link_file')]); } elseif ($link->getLinkType() === 'url') { $linkData['link_url'] = $link->getLinkUrl(); } else { //existing link file $linkData['file'] = $this->jsonEncoder->encode([['file' => $link->getLinkFile(), 'status' => 'old']]); } if ($link->getSampleType() == 'file' && $link->getSampleFile() === null) { $linkData['sample']['type'] = 'file'; $linkData['sample']['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($link->getSampleFileContent(), 'link_sample_file')]); } elseif ($link->getSampleType() == 'url') { $linkData['sample']['type'] = 'url'; $linkData['sample']['url'] = $link->getSampleUrl(); } $downloadableData = ['link' => [$linkData]]; $product->setDownloadableData($downloadableData); if ($isGlobalScopeContent) { $product->setStoreId(0); } $this->downloadableType->save($product); return $product->getLastAddedLinkId(); }
/** * List of links with associated samples * * @param ProductInterface $product * @return SampleInterface[] */ public function getSamplesByProduct(ProductInterface $product) { $sampleList = []; $samples = $this->productType->getSamples($product); /** @var \Magento\Downloadable\Model\Sample $sample */ foreach ($samples as $sample) { $sampleList[] = $this->buildSample($sample); } return $sampleList; }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @return array */ public function getLinksByProduct(\Magento\Catalog\Api\Data\ProductInterface $product) { $linkList = []; $links = $this->downloadableType->getLinks($product); /** @var \Magento\Downloadable\Model\Link $link */ foreach ($links as $link) { $linkList[] = $this->buildLink($link); } return $linkList; }
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->getMock('Magento\\Downloadable\\Model\\Link', ['getId', 'getStoreTitle', 'getTitle', 'getPrice', 'getNumberOfDownloads', 'getSortOrder', 'getIsShareable', 'getData', '__wakeup', 'setId'], [], '', false); $linkInterfaceMock = $this->getMock('\\Magento\\Downloadable\\Api\\Data\\LinkInterface'); $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->linkFactoryMock->expects($this->once())->method('create')->willReturn($linkInterfaceMock); $this->assertEquals([$linkInterfaceMock], $this->service->getList($productSku)); }
/** * {@inheritdoc} */ public function getSamples($productSku) { $sampleList = []; /** @var \Magento\Catalog\Model\Product $product */ $product = $this->productRepository->get($productSku); $samples = $this->downloadableType->getSamples($product); /** @var \Magento\Downloadable\Model\Sample $sample */ foreach ($samples as $sample) { $sampleList[] = $this->buildSample($sample); } return $sampleList; }
/** * @magentoDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php * @magentoAppArea adminhtml */ public function testDeleteTypeSpecificData() { $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $product->setOrigData(); $downloadableData = array(); $links = $this->_model->getLinks($product); $this->assertNotEmpty($links); $samples = $this->_model->getSamples($product); $this->assertNotEmpty($samples->getData()); foreach ($links as $link) { $downloadableData['link'][] = $link->getData(); } foreach ($samples as $sample) { $downloadableData['sample'][] = $sample->getData(); } $product->setDownloadableData($downloadableData); $this->_model->deleteTypeSpecificData($product); $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $links = $this->_model->getLinks($product); $this->assertEmpty($links); $samples = $this->_model->getSamples($product); $this->assertEmpty($samples->getData()); }
/** * @param \Magento\Catalog\Api\Data\ProductInterface $product * @param SampleInterface $sample * @param bool $isGlobalScopeContent * @return int */ protected function saveSample(\Magento\Catalog\Api\Data\ProductInterface $product, SampleInterface $sample, $isGlobalScopeContent) { $sampleData = ['sample_id' => $sample->getid() === null ? 0 : $sample->getid(), 'is_delete' => 0, 'type' => $sample->getSampleType(), 'sort_order' => $sample->getSortOrder(), 'title' => $sample->getTitle()]; if ($sample->getSampleType() == 'file' && $sample->getSampleFile() === null) { $sampleData['file'] = $this->jsonEncoder->encode([$this->fileContentUploader->upload($sample->getSampleFileContent(), 'sample')]); } elseif ($sample->getSampleType() === 'url') { $sampleData['sample_url'] = $sample->getSampleUrl(); } else { //existing file $sampleData['file'] = $this->jsonEncoder->encode([['file' => $sample->getSampleFile(), 'status' => 'old']]); } $downloadableData = ['sample' => [$sampleData]]; $product->setDownloadableData($downloadableData); if ($isGlobalScopeContent) { $product->setStoreId(0); } $this->downloadableType->save($product); return $product->getLastAddedSampleId(); }
/** * @magentoDataFixture Magento/Downloadable/_files/product_downloadable_with_files.php * @magentoAppArea adminhtml * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function testSaveTypeSpecificData() { $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $product->setOrigData(); $downloadableData = []; $links = $this->_model->getLinks($product); $this->assertNotEmpty($links); $samples = $this->_model->getSamples($product); $this->assertNotEmpty($samples->getData()); $i = 0; foreach ($links as $link) { $i++; $linkData = $link->getData(); $linkData['is_delete'] = 0; $linkData['type'] = \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE; $linkData['title'] = 'Updated downloadable link #' . $i; $downloadableData['link'][] = $linkData; } $i = 0; foreach ($samples as $sample) { $i++; $sampleData = $sample->getData(); $sampleData['is_delete'] = 0; $sampleData['type'] = \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE; $sampleData['title'] = 'Updated downloadable sample #' . $i; $downloadableData['sample'][] = $sampleData; } $product->setDownloadableData($downloadableData); $sampleFactory = $objectManager->create('Magento\\Downloadable\\Api\\Data\\SampleInterfaceFactory'); $linkFactory = $objectManager->create('Magento\\Downloadable\\Api\\Data\\LinkInterfaceFactory'); $extension = $product->getExtensionAttributes(); $links = []; foreach ($downloadableData['link'] as $linkData) { if (!$linkData || isset($linkData['is_delete']) && (bool) $linkData['is_delete']) { continue; } else { unset($linkData['link_id']); // TODO: need to implement setLinkFileContent() $link = $linkFactory->create(['data' => $linkData]); $link->setId(null); if (isset($linkData['sample'])) { $link->setSampleType($linkData['sample']['type']); $link->setSampleFileData($linkData['sample']['file']); $link->setSampleUrl($linkData['sample']['url']); } $link->setLinkType($linkData['type']); $link->setStoreId($product->getStoreId()); $link->setWebsiteId($product->getStore()->getWebsiteId()); $link->setProductWebsiteIds($product->getWebsiteIds()); if (!$link->getSortOrder()) { $link->setSortOrder(1); } if (null === $link->getPrice()) { $link->setPrice(0); } if ($link->getIsUnlimited()) { $link->setNumberOfDownloads(0); } $links[] = $link; } } $extension->setDownloadableProductLinks($links); if (isset($downloadableData['sample']) && is_array($downloadableData['sample'])) { $samples = []; foreach ($downloadableData['sample'] as $sampleData) { if (!$sampleData || isset($sampleData['is_delete']) && (bool) $sampleData['is_delete']) { continue; } else { unset($sampleData['sample_id']); $sample = $sampleFactory->create(['data' => $sampleData]); $sample->setId(null); $sample->setStoreId($product->getStoreId()); $sample->setSampleType($sampleData['type']); $sample->setSampleUrl($sampleData['sample_url']); if (!$sample->getSortOrder()) { $sample->setSortOrder(1); } $samples[] = $sample; } } $extension->setDownloadableProductSamples($samples); } $product->setExtensionAttributes($extension); if ($product->getLinksPurchasedSeparately()) { $product->setTypeHasRequiredOptions(true)->setRequiredOptions(true); } else { $product->setTypeHasRequiredOptions(false)->setRequiredOptions(false); } $product->save(); /** @var \Magento\Catalog\Model\Product $product */ $product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Catalog\\Model\\Product'); $product->load(1); $expectedLink = ['is_shareable' => '2', 'link_type' => 'file', 'link_file' => '/j/e/jellyfish_2_4.jpg', 'number_of_downloads' => '15', 'price' => '15.0000', 'sample_file' => '/n/d/jellyfish_1_3.jpg', 'sample_type' => 'file', 'sort_order' => '1', 'title' => 'Updated downloadable link #1']; $links = $product->getExtensionAttributes()->getDownloadableProductLinks(); $this->assertNotEmpty($links); $this->assertCount(1, $links); /** @var \Magento\Downloadable\Model\Link $link */ $link = reset($links); foreach ($expectedLink as $key => $value) { $this->assertTrue($link->hasData($key), 'Key ' . $key . ' not exist!'); $this->assertArrayHasKey($key, $link); $this->assertEquals($value, $link->getData($key)); } $expectedSample = ['sample_type' => 'file', 'sort_order' => '1', 'title' => 'Updated downloadable sample #1']; $samples = $product->getExtensionAttributes()->getDownloadableProductSamples(); $sample = reset($samples); $this->assertNotEmpty($sample->getData()); $this->assertEquals(1, count($samples)); /** @var \Magento\Downloadable\Model\Sample $sample */ $sample = $sample->getData(); /** @var \Magento\User\Api\Data\UserInterface $testAttribute */ foreach ($expectedSample as $key => $value) { $this->assertArrayHasKey($key, $sample); $this->assertEquals($value, $sample[$key]); } }
public function testBeforeSave() { $this->target->beforeSave($this->product); }
public function testBeforeSave() { $this->_model->beforeSave($this->_productMock); }
public function testHasLinks() { $this->product->expects($this->exactly(2))->method('getDownloadableLinks')->willReturn(['link1', 'link2']); $this->assertTrue($this->target->hasLinks($this->product)); }