public function testCreateWithDriverCode() { $driverPool = $this->getMock('Magento\\Framework\\Filesystem\\DriverPool', ['getDriver']); $driverMock = $this->getMockForAbstractClass('Magento\\Framework\\Filesystem\\DriverInterface'); $driverMock->expects($this->any())->method('isExists')->willReturn(true); $driverPool->expects($this->once())->method('getDriver')->willReturn($driverMock); $factory = new ReadFactory($driverPool); $result = $factory->create('path', 'driverCode'); $this->assertInstanceOf('Magento\\Framework\\Filesystem\\File\\Read', $result); }
public function testToArray() { $contents = ['content1', 'content2']; $expectedArray = []; $index = 0; foreach ($this->filePaths as $filePath) { $expectedArray[$filePath] = $contents[$index]; $this->fileReadFactory->expects($this->at($index))->method('create')->with($filePath)->willReturn($this->fileRead); $this->fileRead->expects($this->at($index))->method('readAll')->will($this->returnValue($contents[$index++])); } $this->assertEquals($expectedArray, $this->fileIterator->toArray()); }
/** * Retrieve Resource file handle (socket, file pointer etc) * * @return \Magento\Framework\Filesystem\File\ReadInterface * @throws CoreException|\Exception */ protected function _getHandle() { if (!$this->_resourceFile) { throw new CoreException(__('Please set resource file and link type.')); } if (is_null($this->_handle)) { if ($this->_linkType == self::LINK_TYPE_URL) { $path = $this->_resourceFile; $protocol = strtolower(parse_url($path, PHP_URL_SCHEME)); if ($protocol) { // Strip down protocol from path $path = preg_replace('#.+://#', '', $path); } $this->_handle = $this->fileReadFactory->create($path, $protocol); } elseif ($this->_linkType == self::LINK_TYPE_FILE) { $this->_workingDirectory = $this->_filesystem->getDirectoryRead(DirectoryList::MEDIA); $fileExists = $this->_downloadableFile->ensureFileInFilesystem($this->_resourceFile); if ($fileExists) { $this->_handle = $this->_workingDirectory->openFile($this->_resourceFile); } else { throw new CoreException(__('Invalid download link type.')); } } else { throw new CoreException(__('Invalid download link type.')); } } return $this->_handle; }
/** * Retrieve file contents from given path * * @param string $path * @param string|null $flag * @param resource|null $context * @param string|null $protocol * @return string * @throws FilesystemException */ public function readFile($path, $flag = null, $context = null, $protocol = null) { $absolutePath = $this->driver->getAbsolutePath($this->path, $path, $protocol); /** @var \Magento\Framework\Filesystem\File\Read $fileReader */ $fileReader = $this->fileFactory->create($absolutePath, $protocol, $this->driver); return $fileReader->readAll($flag, $context); }
/** * Get translation data * * @param string $themePath * @return array * @throws \Exception * @throws \Magento\Framework\Exception\LocalizedException */ public function getData($themePath) { $areaCode = $this->appState->getAreaCode(); $files = array_merge($this->filesUtility->getJsFiles('base', $themePath), $this->filesUtility->getJsFiles($areaCode, $themePath), $this->filesUtility->getStaticHtmlFiles('base', $themePath), $this->filesUtility->getStaticHtmlFiles($areaCode, $themePath)); $dictionary = []; foreach ($files as $filePath) { /** @var \Magento\Framework\Filesystem\File\Read $read */ $read = $this->fileReadFactory->create($filePath[0], \Magento\Framework\Filesystem\DriverPool::FILE); $content = $read->readAll(); foreach ($this->getPhrases($content) as $phrase) { $translatedPhrase = $this->translate->render([$phrase], []); if ($phrase != $translatedPhrase) { $dictionary[$phrase] = $translatedPhrase; } } } return $dictionary; }
/** * @dataProvider moveFileUrlDataProvider */ public function testMoveFileUrl($fileUrl, $expectedHost, $expectedFileName) { $expectedRelativeFilePath = $this->uploader->getTmpDir() . '/' . $expectedFileName; $this->directoryMock->expects($this->any())->method('getRelativePath')->with($expectedRelativeFilePath); // Check writeFile() method invoking. $this->directoryMock->expects($this->any())->method('writeFile')->will($this->returnValue(null)); // Create adjusted reader which does not validate path. $readMock = $this->getMockBuilder('Magento\\Framework\\Filesystem\\File\\Read')->disableOriginalConstructor()->setMethods(['readAll'])->getMock(); // Check readAll() method invoking. $readMock->expects($this->once())->method('readAll')->will($this->returnValue(null)); $this->readFactory = $this->getMockBuilder('\\Magento\\Framework\\Filesystem\\File\\ReadFactory')->disableOriginalConstructor()->setMethods(['create'])->getMock(); // Check create() method invoking with expected argument. $this->readFactory->expects($this->once())->method('create')->will($this->returnValue($readMock))->with($expectedHost); $uploaderMock = $this->getMockBuilder('\\Magento\\CatalogImportExport\\Model\\Import\\Uploader')->setConstructorArgs([$this->coreFileStorageDb, $this->coreFileStorage, $this->imageFactory, $this->validator, $this->filesystem, $this->readFactory])->setMethods(['_setUploadFile', 'save', 'getTmpDir'])->getMock(); //Check invoking of getTmpDir(), _setUploadFile(), save() methods. $uploaderMock->expects($this->any())->method('getTmpDir')->will($this->returnValue('')); $uploaderMock->expects($this->once())->method('_setUploadFile')->will($this->returnSelf()); $uploaderMock->expects($this->once())->method('save')->will($this->returnValue(['name' => null])); $uploaderMock->move($fileUrl); }
/** * Proceed moving a file from TMP to destination folder * * @param string $fileName * @return array */ public function move($fileName) { if (preg_match('/\\bhttps?:\\/\\//i', $fileName, $matches)) { $url = str_replace($matches[0], '', $fileName); $read = $this->_readFactory->create($url, DriverPool::HTTP); $fileName = preg_replace('/[^a-z0-9\\._-]+/i', '', $fileName); $this->_directory->writeFile($this->_directory->getRelativePath($this->getTmpDir() . '/' . $fileName), $read->readAll()); } $filePath = $this->_directory->getRelativePath($this->getTmpDir() . '/' . $fileName); $this->_setUploadFile($filePath); $result = $this->save($this->getDestDir()); $result['name'] = self::getCorrectFileName($result['name']); return $result; }
/** * Get aggregated distributed configuration * * @return string */ public function getConfig() { $distributedConfig = ''; $customConfigFiles = $this->fileSource->getFiles($this->design->getDesignTheme(), self::CONFIG_FILE_NAME); foreach ($customConfigFiles as $file) { /** @var $fileReader \Magento\Framework\Filesystem\File\Read */ $fileReader = $this->readFactory->create($file->getFileName(), DriverPool::FILE); $config = $fileReader->readAll($file->getName()); $distributedConfig .= str_replace(['%config%', '%context%'], [$config, $file->getModule()], self::PARTIAL_CONFIG_TEMPLATE); } $fullConfig = str_replace(['%function%', '%usages%'], [$distributedConfig], self::FULL_CONFIG_TEMPLATE); if ($this->minification->isEnabled('js')) { $fullConfig = $this->minifyAdapter->minify($fullConfig); } return $fullConfig; }
/** * @param string $path * @param string|null $protocol * @return ReadInterface */ public function getRemoteResource($path, $protocol = null) { if (!$this->fileReadFactory) { // case when a temporary Filesystem object is used for loading primary configuration return null; } if (empty($protocol)) { $protocol = strtolower(parse_url($path, PHP_URL_SCHEME)); if ($protocol) { // Strip down protocol from path $path = preg_replace('#.+://#', '', $path); } } if (!array_key_exists($protocol, $this->remoteResourceInstances)) { $this->remoteResourceInstances[$protocol] = $this->fileReadFactory->create($path, $protocol); } return $this->remoteResourceInstances[$protocol]; }
protected function _setupUrlMocks($size = self::FILE_SIZE, $url = self::URL, $additionalStatData = []) { $this->_handleMock->expects($this->any())->method('stat')->will($this->returnValue(array_merge(['size' => $size, 'type' => self::MIME_TYPE], $additionalStatData))); $this->fileReadFactory->expects($this->once())->method('create')->will($this->returnValue($this->_handleMock)); $this->_helper->setResource($url, DownloadHelper::LINK_TYPE_URL); }
public function testGetRemoteResource() { $fileReadMock = $this->getMock('Magento\\Framework\\Filesystem\\File\\ReadInterface', array(), array(), '', false); $this->_fileReadFactoryMock->expects($this->once())->method('create')->with('example.com', 'http')->will($this->returnValue($fileReadMock)); $this->assertEquals($fileReadMock, $this->_filesystem->getRemoteResource('http://example.com')); }
/** * Open file in read mode * * @param string $path * * @return \Magento\Framework\Filesystem\File\ReadInterface */ public function openFile($path) { return $this->fileFactory->create($this->driver->getAbsolutePath($this->path, $path), $this->driver); }
/** * Collect and merge layout updates from files * * @return \Magento\Framework\View\Layout\Element * @throws \Magento\Framework\Exception\LocalizedException */ protected function _loadFileLayoutUpdatesXml() { $layoutStr = ''; $theme = $this->_getPhysicalTheme($this->theme); $updateFiles = $this->fileSource->getFiles($theme, '*.xml'); $updateFiles = array_merge($updateFiles, $this->pageLayoutFileSource->getFiles($theme, '*.xml')); $useErrors = libxml_use_internal_errors(true); foreach ($updateFiles as $file) { /** @var $fileReader \Magento\Framework\Filesystem\File\Read */ $fileReader = $this->readFactory->create($file->getFilename(), DriverPool::FILE); $fileStr = $fileReader->readAll($file->getName()); $fileStr = $this->_substitutePlaceholders($fileStr); /** @var $fileXml \Magento\Framework\View\Layout\Element */ $fileXml = $this->_loadXmlString($fileStr); if (!$fileXml instanceof \Magento\Framework\View\Layout\Element) { $this->_logXmlErrors($file->getFilename(), libxml_get_errors()); libxml_clear_errors(); continue; } if (!$file->isBase() && $fileXml->xpath(self::XPATH_HANDLE_DECLARATION)) { throw new \Magento\Framework\Exception\LocalizedException(new \Magento\Framework\Phrase('Theme layout update file \'%1\' must not declare page types.', [$file->getFileName()])); } $handleName = basename($file->getFilename(), '.xml'); $tagName = $fileXml->getName() === 'layout' ? 'layout' : 'handle'; $handleAttributes = ' id="' . $handleName . '"' . $this->_renderXmlAttributes($fileXml); $handleStr = '<' . $tagName . $handleAttributes . '>' . $fileXml->innerXml() . '</' . $tagName . '>'; $layoutStr .= $handleStr; } libxml_use_internal_errors($useErrors); $layoutStr = '<layouts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">' . $layoutStr . '</layouts>'; $layoutXml = $this->_loadXmlString($layoutStr); return $layoutXml; }
/** * Current * * @return string */ public function current() { /** @var \Magento\Framework\Filesystem\File\Read $fileRead */ $fileRead = $this->fileReadFactory->create($this->key(), DriverPool::FILE); return $fileRead->readAll(); }