Example #1
0
 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);
     }
 }
Example #2
0
 /**
  * Upload file controller action
  *
  * @return \Magento\Framework\Controller\ResultInterface
  */
 public function executeInternal()
 {
     $type = $this->getRequest()->getParam('type');
     $tmpPath = '';
     if ($type == 'samples') {
         $tmpPath = $this->_sample->getBaseTmpPath();
     } elseif ($type == 'links') {
         $tmpPath = $this->_link->getBaseTmpPath();
     } elseif ($type == 'link_samples') {
         $tmpPath = $this->_link->getBaseSampleTmpPath();
     }
     try {
         $uploader = $this->uploaderFactory->create(['fileId' => $type]);
         $result = $this->_fileHelper->uploadFromTmp($tmpPath, $uploader);
         if (!$result) {
             throw new \Exception('File can not be moved from temporary folder to the destination folder.');
         }
         /**
          * Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS
          */
         $result['tmp_name'] = str_replace('\\', '/', $result['tmp_name']);
         $result['path'] = str_replace('\\', '/', $result['path']);
         if (isset($result['file'])) {
             $relativePath = rtrim($tmpPath, '/') . '/' . ltrim($result['file'], '/');
             $this->storageDatabase->saveFile($relativePath);
         }
         $result['cookie'] = ['name' => $this->_getSession()->getName(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain()];
     } catch (\Exception $e) {
         $result = ['error' => $e->getMessage(), 'errorcode' => $e->getCode()];
     }
     return $this->resultFactory->create(ResultFactory::TYPE_JSON)->setData($result);
 }
 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);
     }
 }
Example #4
0
 /**
  * @param SampleInterface $sample
  * @return SampleInterface
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function build(SampleInterface $sample)
 {
     $downloadableData = $this->objectCopyService->getDataFromFieldset('downloadable_data', 'to_sample', $this->data);
     $this->dataObjectHelper->populateWithArray($sample, array_merge($this->data, $downloadableData), SampleInterface::class);
     if ($sample->getSampleType() === \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
         if (!isset($this->data['file'])) {
             throw new \Magento\Framework\Exception\LocalizedException(__('Sample file not provided'));
         }
         $fileName = $this->downloadableFile->moveFileFromTmp($this->getComponent()->getBaseTmpPath(), $this->getComponent()->getBasePath(), $this->data['file']);
         $sample->setSampleFile($fileName);
     }
     if (!$sample->getSortOrder()) {
         $sample->setSortOrder(1);
     }
     $this->resetData();
     return $sample;
 }
Example #5
0
 protected function _setupFileMocks($doesExist = true, $size = self::FILE_SIZE, $path = self::FILE_PATH)
 {
     $this->_handleMock->expects($this->any())->method('stat')->will($this->returnValue(['size' => $size]));
     $this->_downloadableFileMock->expects($this->any())->method('ensureFileInFilesystem')->with($path)->will($this->returnValue($doesExist));
     $this->_workingDirectoryMock->expects($doesExist ? $this->once() : $this->never())->method('openFile')->will($this->returnValue($this->_handleMock));
     $this->_filesystemMock->expects($this->any())->method('getDirectoryRead')->with(DirectoryList::MEDIA)->will($this->returnValue($this->_workingDirectoryMock));
     $this->_helper->setResource($path, DownloadHelper::LINK_TYPE_FILE);
 }
Example #6
0
 /**
  * Get all allowed extensions
  *
  * @return array
  */
 protected function getAllowedExtensions()
 {
     $result = [];
     foreach (array_keys($this->fileHelper->getAllMineTypes()) as $option) {
         $result[] = substr($option, 1);
     }
     return $result;
 }
 /**
  * Load file and set path to sample
  *
  * @param SampleInterface $sample
  * @return void
  */
 protected function setFiles(SampleInterface $sample)
 {
     if ($sample->getSampleType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE && $sample->getFile()) {
         $sampleFileName = $this->downloadableFile->moveFileFromTmp($sample->getBaseTmpPath(), $sample->getBasePath(), $this->jsonHelper->jsonDecode($sample->getFile()));
         $sample->setSampleFile($sampleFileName);
         $sample->setSampleUrl(null);
     }
 }
Example #8
0
 public function testExecute()
 {
     $data = ['tmp_name' => 'tmp_name', 'path' => 'path', 'file' => 'file'];
     $uploader = $this->getMockBuilder('Magento\\MediaStorage\\Model\\File\\Uploader')->disableOriginalConstructor()->getMock();
     $resultJson = $this->getMockBuilder('Magento\\Framework\\Controller\\Result\\Json')->disableOriginalConstructor()->setMethods(['setData'])->getMock();
     $this->request->expects($this->once())->method('getParam')->with('type')->willReturn('samples');
     $this->sample->expects($this->once())->method('getBaseTmpPath')->willReturn('base_tmp_path');
     $this->uploaderFactory->expects($this->once())->method('create')->willReturn($uploader);
     $this->fileHelper->expects($this->once())->method('uploadFromTmp')->willReturn($data);
     $this->storageDatabase->expects($this->once())->method('saveFile');
     $this->session->expects($this->once())->method('getName')->willReturn('Name');
     $this->session->expects($this->once())->method('getSessionId')->willReturn('SessionId');
     $this->session->expects($this->once())->method('getCookieLifetime')->willReturn('CookieLifetime');
     $this->session->expects($this->once())->method('getCookiePath')->willReturn('CookiePath');
     $this->session->expects($this->once())->method('getCookieDomain')->willReturn('CookieDomain');
     $this->resultFactory->expects($this->once())->method('create')->willReturn($resultJson);
     $resultJson->expects($this->once())->method('setData')->willReturnSelf();
     $this->assertEquals($resultJson, $this->upload->execute());
 }
Example #9
0
 /**
  * Add Link File info into $linkData
  *
  * @param array $linkData
  * @param LinkInterface $link
  * @return array
  */
 protected function addLinkFile(array $linkData, LinkInterface $link)
 {
     $linkFile = $link->getLinkFile();
     if ($linkFile) {
         $file = $this->downloadableFile->getFilePath($this->linkModel->getBasePath(), $linkFile);
         if ($this->downloadableFile->ensureFileInFilesystem($file)) {
             $linkData['file'][0] = ['file' => $linkFile, 'name' => $this->downloadableFile->getFileFromPathFile($linkFile), 'size' => $this->downloadableFile->getFileSize($file), 'status' => 'old', 'url' => $this->urlBuilder->addSessionParam()->getUrl('adminhtml/downloadable_product_edit/link', ['id' => $link->getId(), 'type' => 'link', '_secure' => true])];
         }
     }
     return $linkData;
 }
Example #10
0
 /**
  * Add Sample File info into $sampleData
  *
  * @param array $sampleData
  * @param SampleInterface $sample
  * @return array
  */
 protected function addSampleFile(array $sampleData, SampleInterface $sample)
 {
     $sampleFile = $sample->getSampleFile();
     if ($sampleFile) {
         $file = $this->downloadableFile->getFilePath($this->sampleModel->getBasePath(), $sampleFile);
         if ($this->downloadableFile->ensureFileInFilesystem($file)) {
             $sampleData['file'][0] = ['file' => $sampleFile, 'name' => $this->downloadableFile->getFileFromPathFile($sampleFile), 'size' => $this->downloadableFile->getFileSize($file), 'status' => 'old', 'url' => $this->urlBuilder->addSessionParam()->getUrl('adminhtml/downloadable_product_edit/sample', ['id' => $sample->getId(), '_secure' => true])];
         }
     }
     return $sampleData;
 }
Example #11
0
 /**
  * @param \Magento\Downloadable\Api\Data\LinkInterface $link
  * @param array $sample
  * @return \Magento\Downloadable\Api\Data\LinkInterface
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 private function buildSample(\Magento\Downloadable\Api\Data\LinkInterface $link, array $sample)
 {
     if (!empty($sample['url']) || !empty($sample['file'])) {
         $downloadableLinkSampleData = $this->objectCopyService->getDataFromFieldset('downloadable_link_sample_data', 'to_link_sample', $this->data['sample']);
         $this->dataObjectHelper->populateWithArray($link, array_merge($this->data, $downloadableLinkSampleData), \Magento\Downloadable\Api\Data\LinkInterface::class);
         if ($link->getSampleType() === \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
             $linkSampleFileName = $this->downloadableFile->moveFileFromTmp($this->getComponent()->getBaseSampleTmpPath(), $this->getComponent()->getBaseSamplePath(), $sample['file']);
             $link->setSampleFile($linkSampleFileName);
         }
     }
     return $link;
 }
 /**
  * Load files and set paths to link and sample of link
  *
  * @param LinkInterface $link
  * @return void
  */
 protected function setFiles(LinkInterface $link)
 {
     if ($link->getSampleType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE && $link->getSampleFileData()) {
         $linkSampleFileName = $this->downloadableFile->moveFileFromTmp($link->getBaseSampleTmpPath(), $link->getBaseSamplePath(), $this->jsonHelper->jsonDecode($link->getSampleFileData()));
         $link->setSampleFile($linkSampleFileName);
         $link->setSampleUrl(null);
     }
     if ($link->getLinkType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE && $link->getFile()) {
         $linkFileName = $this->downloadableFile->moveFileFromTmp($link->getBaseTmpPath(), $link->getBasePath(), $this->jsonHelper->jsonDecode($link->getFile()));
         $link->setLinkFile($linkFileName);
         $link->setLinkUrl(null);
     }
 }
Example #13
0
 /**
  * Execute download sample file action
  */
 public function testExecuteFile()
 {
     $this->request->expects($this->at(0))->method('getParam')->with('id', 0)->will($this->returnValue(1));
     $this->response->expects($this->once())->method('setHttpResponseCode')->will($this->returnSelf());
     $this->response->expects($this->once())->method('clearBody')->will($this->returnSelf());
     $this->response->expects($this->any())->method('setHeader')->will($this->returnSelf());
     $this->response->expects($this->once())->method('sendHeaders')->will($this->returnSelf());
     $this->objectManager->expects($this->at(1))->method('get')->with('Magento\\Downloadable\\Helper\\File')->will($this->returnValue($this->fileHelper));
     $this->objectManager->expects($this->at(2))->method('get')->with('Magento\\Downloadable\\Model\\Sample')->will($this->returnValue($this->sampleModel));
     $this->objectManager->expects($this->at(3))->method('get')->with('Magento\\Downloadable\\Helper\\Download')->will($this->returnValue($this->downloadHelper));
     $this->fileHelper->expects($this->once())->method('getFilePath')->will($this->returnValue('filepath/sample.jpg'));
     $this->downloadHelper->expects($this->once())->method('setResource')->will($this->returnSelf());
     $this->downloadHelper->expects($this->once())->method('getFilename')->will($this->returnValue('sample.jpg'));
     $this->downloadHelper->expects($this->once())->method('getContentType')->will($this->returnSelf('file'));
     $this->downloadHelper->expects($this->once())->method('getFileSize')->will($this->returnValue(null));
     $this->downloadHelper->expects($this->once())->method('getContentDisposition')->will($this->returnValue(null));
     $this->downloadHelper->expects($this->once())->method('output')->will($this->returnSelf());
     $this->sampleModel->expects($this->once())->method('load')->will($this->returnSelf());
     $this->sampleModel->expects($this->once())->method('getId')->will($this->returnValue('1'));
     $this->sampleModel->expects($this->any())->method('getSampleType')->will($this->returnValue('file'));
     $this->objectManager->expects($this->once())->method('create')->will($this->returnValue($this->sampleModel));
     $this->sample->execute();
 }
Example #14
0
 /**
  * Return MIME type of a file.
  *
  * @return string
  */
 public function getContentType()
 {
     $this->_getHandle();
     if ($this->_linkType == self::LINK_TYPE_FILE) {
         if (function_exists('mime_content_type') && ($contentType = mime_content_type($this->_workingDirectory->getAbsolutePath($this->_resourceFile)))) {
             return $contentType;
         } else {
             return $this->_downloadableFile->getFileType($this->_resourceFile);
         }
     } elseif ($this->_linkType == self::LINK_TYPE_URL) {
         return $this->_handle->stat($this->_resourceFile)['type'];
     }
     return $this->_contentType;
 }
 public function testSaveWithFile()
 {
     $sampleId = 1;
     $productId = 1;
     $productSku = 'simple';
     $sampleFile = '/s/a/sample.jpg';
     $sampleData = ['id' => $sampleId, 'title' => 'Updated Title', 'sort_order' => 1, 'sample_type' => 'file', 'file' => $sampleFile];
     $this->repositoryMock->expects($this->once())->method('get')->with($productSku, true)->willReturn($this->productMock);
     $this->productMock->expects($this->once())->method('getTypeId')->willReturn('downloadable');
     $this->productMock->expects($this->once())->method('getData')->with('id')->willReturn($productId);
     $sampleMock = $this->getSampleMock($sampleData);
     $this->contentValidatorMock->expects($this->any())->method('isValid')->with($sampleMock)->willReturn(true);
     $sampleMock->expects($this->once())->method('setProductId')->with($productId);
     $sampleMock->expects($this->exactly(2))->method('getFile')->willReturn($sampleFile);
     $this->jsonEncoderMock->expects($this->once())->method('jsonDecode')->willReturn($sampleFile);
     $this->contentUploaderMock->expects($this->once())->method('moveFileFromTmp')->with($sampleMock->getBaseTmpPath(), $sampleMock->getBasePath(), $sampleFile)->willReturn($sampleFile);
     $sampleMock->expects($this->once())->method('setSampleFile')->with($sampleFile);
     $sampleMock->expects($this->once())->method('setSampleUrl')->with(null);
     $this->sampleResourceMock->expects($this->once())->method('save')->with($sampleMock)->willReturn($sampleId);
     $this->assertEquals($sampleId, $this->service->save($productSku, $sampleMock));
 }
Example #16
0
 /**
  * Save Product downloadable information (links and samples)
  *
  * @param \Magento\Catalog\Model\Product $product
  * @return $this
  */
 public function save($product)
 {
     parent::save($product);
     if ($data = $product->getDownloadableData()) {
         if (isset($data['sample'])) {
             $_deleteItems = array();
             foreach ($data['sample'] as $sampleItem) {
                 if ($sampleItem['is_delete'] == '1') {
                     if ($sampleItem['sample_id']) {
                         $_deleteItems[] = $sampleItem['sample_id'];
                     }
                 } else {
                     unset($sampleItem['is_delete']);
                     if (!$sampleItem['sample_id']) {
                         unset($sampleItem['sample_id']);
                     }
                     $sampleModel = $this->_createSample();
                     $files = array();
                     if (isset($sampleItem['file'])) {
                         $files = $this->_coreData->jsonDecode($sampleItem['file']);
                         unset($sampleItem['file']);
                     }
                     $sampleModel->setData($sampleItem)->setSampleType($sampleItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId());
                     if ($sampleModel->getSampleType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
                         $sampleFileName = $this->_downloadableFile->moveFileFromTmp($sampleModel->getBaseTmpPath(), $sampleModel->getBasePath(), $files);
                         $sampleModel->setSampleFile($sampleFileName);
                     }
                     $sampleModel->save();
                     $product->setLastAddedSampleId($sampleModel->getId());
                 }
             }
             if ($_deleteItems) {
                 $this->_sampleResFactory->create()->deleteItems($_deleteItems);
             }
         }
         if (isset($data['link'])) {
             $_deleteItems = array();
             foreach ($data['link'] as $linkItem) {
                 if (isset($linkItem['is_delete']) && $linkItem['is_delete'] == '1') {
                     if ($linkItem['link_id']) {
                         $_deleteItems[] = $linkItem['link_id'];
                     }
                 } else {
                     unset($linkItem['is_delete']);
                     if (isset($linkItem['link_id']) && !$linkItem['link_id']) {
                         unset($linkItem['link_id']);
                     }
                     $files = array();
                     if (isset($linkItem['file'])) {
                         $files = $this->_coreData->jsonDecode($linkItem['file']);
                         unset($linkItem['file']);
                     }
                     $sample = array();
                     if (isset($linkItem['sample'])) {
                         $sample = $linkItem['sample'];
                         unset($linkItem['sample']);
                     }
                     $linkModel = $this->_createLink()->setData($linkItem)->setLinkType($linkItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId())->setWebsiteId($product->getStore()->getWebsiteId())->setProductWebsiteIds($product->getWebsiteIds());
                     if (null === $linkModel->getPrice()) {
                         $linkModel->setPrice(0);
                     }
                     if ($linkModel->getIsUnlimited()) {
                         $linkModel->setNumberOfDownloads(0);
                     }
                     $sampleFile = array();
                     if ($sample && isset($sample['type'])) {
                         if ($sample['type'] == 'url' && $sample['url'] != '') {
                             $linkModel->setSampleUrl($sample['url']);
                         }
                         $linkModel->setSampleType($sample['type']);
                         if (isset($sample['file'])) {
                             $sampleFile = $this->_coreData->jsonDecode($sample['file']);
                         }
                     }
                     if ($linkModel->getLinkType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
                         $linkFileName = $this->_downloadableFile->moveFileFromTmp($this->_createLink()->getBaseTmpPath(), $this->_createLink()->getBasePath(), $files);
                         $linkModel->setLinkFile($linkFileName);
                     }
                     if ($linkModel->getSampleType() == \Magento\Downloadable\Helper\Download::LINK_TYPE_FILE) {
                         $linkSampleFileName = $this->_downloadableFile->moveFileFromTmp($this->_createLink()->getBaseSampleTmpPath(), $this->_createLink()->getBaseSamplePath(), $sampleFile);
                         $linkModel->setSampleFile($linkSampleFileName);
                     }
                     $linkModel->save();
                     $product->setLastAddedLinkId($linkModel->getId());
                 }
             }
             if ($_deleteItems) {
                 $this->_linkResource->deleteItems($_deleteItems);
             }
             if ($product->getLinksPurchasedSeparately()) {
                 $product->setIsCustomOptionChanged();
             }
         }
     }
     return $this;
 }