/** * Check and process robots file * * @return $this */ public function afterSave() { if ($this->getValue()) { $this->_directory->writeFile($this->_file, $this->getValue()); } return parent::afterSave(); }
/** * @param \Magento\Framework\Event\Observer $observer */ public function execute(Observer $observer) { $value = $this->_helper->getRobotsConfig('content'); $this->_directory->writeFile($this->_fileRobot, $value); $value = $this->_helper->getHtaccessConfig('content'); $this->_directory->writeFile($this->_fileHtaccess, $value); }
/** * Check var/generation read and write access * * @return bool */ public function check() { $initParams = $this->serviceManager->get(InitParamListener::BOOTSTRAP_PARAM); $filesystemDirPaths = isset($initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS]) ? $initParams[Bootstrap::INIT_PARAM_FILESYSTEM_DIR_PATHS] : []; $directoryList = new DirectoryList(BP, $filesystemDirPaths); $generationDirectoryPath = $directoryList->getPath(DirectoryList::GENERATION); $driverPool = new DriverPool(); $fileWriteFactory = new WriteFactory($driverPool); /** @var \Magento\Framework\Filesystem\DriverInterface $driver */ $driver = $driverPool->getDriver(DriverPool::FILE); $directoryWrite = new Write($fileWriteFactory, $driver, $generationDirectoryPath); if ($directoryWrite->isExist()) { if ($directoryWrite->isDirectory() || $directoryWrite->isReadable()) { try { $probeFilePath = $generationDirectoryPath . DIRECTORY_SEPARATOR . uniqid(mt_rand()) . 'tmp'; $fileWriteFactory->create($probeFilePath, DriverPool::FILE, 'w'); $driver->deleteFile($probeFilePath); } catch (\Exception $e) { return false; } } else { return false; } } else { try { $directoryWrite->create(); } catch (\Exception $e) { return false; } } return true; }
public function testReplaceTmpEncryptKey() { $keyPlaceholder = \Magento\Install\Model\Installer\Config::TMP_ENCRYPT_KEY_VALUE; $fixtureConfigData = "<key>{$keyPlaceholder}</key>"; $expectedConfigData = '<key>3c7cf2e909fd5e2268a6e1539ae3c835</key>'; $this->_directoryMock->expects($this->once())->method('readFile')->with($this->equalTo($this->_tmpConfigFile))->will($this->returnValue($fixtureConfigData)); $this->_directoryMock->expects($this->once())->method('writeFile')->with($this->equalTo($this->_tmpConfigFile), $this->equalTo($expectedConfigData))->will($this->returnValue($fixtureConfigData)); $this->_model->replaceTmpEncryptKey('3c7cf2e909fd5e2268a6e1539ae3c835'); }
public function testGenerateSwatchVariations() { $this->mediaDirectoryMock->expects($this->atLeastOnce())->method('getAbsolutePath')->willReturn('attribute/swatch/e/a/earth.png'); $image = $this->getMock('\\Magento\\Framework\\Image', ['resize', 'save', 'keepTransparency', 'constrainOnly', 'keepFrame', 'keepAspectRatio', 'backgroundColor', 'quality'], [], '', false); $this->imageFactoryMock->expects($this->any())->method('create')->willReturn($image); $this->generateImageConfig(); $image->expects($this->any())->method('resize')->will($this->returnSelf()); $this->mediaHelperObject->generateSwatchVariations('/e/a/earth.png'); }
/** * @param \Magento\Framework\Filesystem\Directory\Write $flagDir * @param OutputInterface $output * @param null $onOption */ protected function handleEnable(\Magento\Framework\Filesystem\Directory\Write $flagDir, OutputInterface $output, $onOption = null) { $flagDir->touch(MaintenanceMode::FLAG_FILENAME); $output->writeln(self::ENABLED_MESSAGE); if (!is_null($onOption)) { // Write IPs to exclusion file $flagDir->writeFile(MaintenanceMode::IP_FILENAME, $onOption); $output->writeln(self::WROTE_IP_MESSAGE); } }
/** * Open file and detect column names * * There must be column names in the first line * * @param string $file * @param \Magento\Framework\Filesystem\Directory\Write $directory * @param string $delimiter * @param string $enclosure * @throws \LogicException */ public function __construct($file, \Magento\Framework\Filesystem\Directory\Write $directory, $delimiter = ',', $enclosure = '"') { try { $this->_file = $directory->openFile($directory->getRelativePath($file), 'r'); } catch (\Magento\Framework\Filesystem\FilesystemException $e) { throw new \LogicException("Unable to open file: '{$file}'"); } $this->_delimiter = $delimiter; $this->_enclosure = $enclosure; parent::__construct($this->_getNextRow()); }
/** * Set up */ public function setUp() { $this->context = $this->getMock('Magento\\Framework\\App\\Helper\\Context', [], [], '', false); $this->timezone = $this->getMock('Magento\\Framework\\Stdlib\\DateTime\\Timezone', ['date', 'getConfigTimezone', 'diff', 'format'], [], '', false); $this->varDirectory = $this->getMock('Magento\\Framework\\Filesystem\\Directory\\Write', ['getRelativePath', 'readFile', 'isFile', 'stat'], [], '', false); $this->filesystem = $this->getMock('Magento\\Framework\\Filesystem', ['getDirectoryWrite'], [], '', false); $this->varDirectory->expects($this->any())->method('getRelativePath')->willReturn('path'); $this->varDirectory->expects($this->any())->method('readFile')->willReturn('contents'); $this->varDirectory->expects($this->any())->method('isFile')->willReturn(true); $this->varDirectory->expects($this->any())->method('stat')->willReturn(100); $this->filesystem->expects($this->any())->method('getDirectoryWrite')->willReturn($this->varDirectory); $this->objectManagerHelper = new ObjectManagerHelper($this); $this->report = $this->objectManagerHelper->getObject('Magento\\ImportExport\\Helper\\Report', ['context' => $this->context, 'timeZone' => $this->timezone, 'filesystem' => $this->filesystem]); }
/** * Clear temporary directories * * @param \Magento\Install\Controller\Index\Index $subject * @param \Magento\Framework\App\RequestInterface $request * * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeDispatch(\Magento\Install\Controller\Index\Index $subject, \Magento\Framework\App\RequestInterface $request) { if (!$this->appState->isInstalled()) { foreach ($this->varDirectory->read() as $dir) { if ($this->varDirectory->isDirectory($dir)) { try { $this->varDirectory->delete($dir); } catch (FilesystemException $exception) { $this->logger->log($exception->getMessage()); } } } } }
public function testCreateSymlinkTargetDirectoryExists() { $targetDir = $this->getMockBuilder('Magento\\Framework\\Filesystem\\Directory\\WriteInterface')->getMock(); $targetDir->driver = $this->driver; $sourcePath = 'source/path/file'; $destinationDirectory = 'destination/path'; $destinationFile = $destinationDirectory . '/' . 'file'; $this->assertIsFileExpectation($sourcePath); $this->driver->expects($this->once())->method('getParentDirectory')->with($destinationFile)->willReturn($destinationDirectory); $targetDir->expects($this->once())->method('isExist')->with($destinationDirectory)->willReturn(true); $targetDir->expects($this->once())->method('getAbsolutePath')->with($destinationFile)->willReturn($this->getAbsolutePath($destinationFile)); $this->driver->expects($this->once())->method('symlink')->with($this->getAbsolutePath($sourcePath), $this->getAbsolutePath($destinationFile), $targetDir->driver)->willReturn(true); $this->assertTrue($this->write->createSymlink($sourcePath, $destinationFile, $targetDir)); }
/** * @param string $key * @return $this */ public function replaceTmpEncryptKey($key) { $localXml = $this->_configDirectory->readFile($this->_localConfigFile); $localXml = str_replace(self::TMP_ENCRYPT_KEY_VALUE, $key, $localXml); $this->_configDirectory->writeFile($this->_localConfigFile, $localXml); return $this; }
/** * @expectedException \InvalidArgumentException * @expectedExceptionMessage wrongColumnsNumber */ public function testRewind() { $this->_directoryMock->expects($this->any())->method('openFile')->will($this->returnValue(new \Magento\Framework\Filesystem\File\Read(__DIR__ . '/_files/test.csv', new \Magento\Framework\Filesystem\Driver\File()))); $model = new \Magento\ImportExport\Model\Import\Source\Csv(__DIR__ . '/_files/test.csv', $this->_directoryMock); $this->assertSame(-1, $model->key()); $model->next(); $this->assertSame(0, $model->key()); $model->next(); $this->assertSame(1, $model->key()); $model->rewind(); $this->assertSame(0, $model->key()); $model->next(); $model->next(); $this->assertSame(2, $model->key()); $model->current(); }
/** * Copy file to tmp theme customization path * * @param string $sourceFile * @return string */ protected function _copyFileToTmpCustomizationPath($sourceFile) { $targetFile = $this->_helperStorage->getStorageRoot() . '/' . basename($sourceFile); $this->directoryTmp->create(pathinfo($targetFile, PATHINFO_DIRNAME)); $this->directoryVar->copyFile($this->directoryVar->getRelativePath($sourceFile), $this->directoryTmp->getRelativePath($targetFile), $this->directoryTmp); return $targetFile; }
/** * Check and process robots file * * @return $this */ protected function _afterSave() { if ($this->getValue()) { $this->_directory->writeFile($this->_file, $this->getValue()); } return parent::_afterSave(); }
/** * Log information about fatal error. * * @param string $reportData * @return string */ protected function _saveFatalErrorReport($reportData) { $this->directoryWrite->create('report/api'); $reportId = abs(intval(microtime(true) * rand(100, 1000))); $this->directoryWrite->writeFile('report/api/' . $reportId, serialize($reportData)); return $reportId; }
/** * cover \Magento\Theme\Model\Wysiwyg\Storage::deleteDirectory */ public function testDeleteDirectory() { $directoryPath = $this->_storageRoot . '/../root'; $this->_helperStorage->expects($this->atLeastOnce())->method('getStorageRoot')->will($this->returnValue($this->_storageRoot)); $this->directoryWrite->expects($this->once())->method('delete')->with($directoryPath); $this->_storageModel->deleteDirectory($directoryPath); }
public function testGetThumbnailPath() { $image = 'image_name.jpg'; $thumbnailPath = '/' . implode('/', array(\Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE, \Magento\Theme\Model\Wysiwyg\Storage::THUMBNAIL_DIRECTORY, $image)); $this->customization->expects($this->any())->method('getCustomizationPath')->will($this->returnValue($this->customizationPath)); $this->directoryWrite->expects($this->any())->method('isExist')->will($this->returnValue(true)); $this->assertEquals($thumbnailPath, $this->helper->getThumbnailPath($image)); }
/** * Delete all files in a directory recursively * * @param string $targetDir * @return void */ protected function _deleteFilesRecursively($targetDir) { if ($this->_directory->isExist($targetDir)) { foreach ($this->_directory->read($targetDir) as $path) { $this->_directory->delete($path); } } }
/** * Get thumbnail path in current directory by image name * * @param string $imageName * @return string * @throws \InvalidArgumentException */ public function getThumbnailPath($imageName) { $imagePath = $this->getCurrentPath() . '/' . $imageName; if (!$this->mediaDirectoryWrite->isExist($imagePath) || 0 !== strpos($imagePath, $this->getStorageRoot())) { throw new \InvalidArgumentException('The image not found.'); } return $this->getThumbnailDirectory($imagePath) . '/' . pathinfo($imageName, PATHINFO_BASENAME); }
/** * Delete directory * * @param string $path * @return bool * @throws \Magento\Framework\Exception\LocalizedException */ public function deleteDirectory($path) { $rootCmp = rtrim($this->_helper->getStorageRoot(), '/'); $pathCmp = rtrim($path, '/'); if ($rootCmp == $pathCmp) { throw new \Magento\Framework\Exception\LocalizedException(__('We can\'t delete root directory %1 right now.', $path)); } return $this->mediaWriteDirectory->delete($path); }
/** * Return URL based on current selected directory or root directory for startup * * @return string */ public function getCurrentUrl() { if (!$this->_currentUrl) { $path = $this->getCurrentPath(); $mediaUrl = $this->_storeManager->getStore($this->_storeId)->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA); $this->_currentUrl = $mediaUrl . $this->_directory->getRelativePath($path) . '/'; } return $this->_currentUrl; }
public function testGetDirsCollectionCreateSubDirectories() { $directoryName = 'test1'; $this->coreFileStorageMock->expects($this->once())->method('checkDbUsage')->willReturn(true); $this->directoryCollectionMock->expects($this->once())->method('getSubdirectories')->with(self::STORAGE_ROOT_DIR)->willReturn([['name' => $directoryName]]); $this->directoryDatabaseFactoryMock->expects($this->once())->method('create')->willReturn($this->directoryCollectionMock); $this->directoryMock->expects($this->once())->method('create')->with(rtrim(self::STORAGE_ROOT_DIR, '/') . '/' . $directoryName); $this->generalTestGetDirsCollection(self::STORAGE_ROOT_DIR); }
public function testRunReadinessCheckLastTimestamp() { $this->dbValidator->expects($this->once())->method('checkDatabaseConnection')->willReturn(true); $this->write->expects($this->once())->method('isExist')->willReturn(true); $this->write->expects($this->once())->method('readFile')->willReturn('{"current_timestamp": 50}'); $expected = [ReadinessCheck::KEY_READINESS_CHECKS => [ReadinessCheck::KEY_DB_WRITE_PERMISSION_VERIFIED => true], ReadinessCheck::KEY_PHP_CHECKS => $this->expected, ReadinessCheck::KEY_LAST_TIMESTAMP => 50, ReadinessCheck::KEY_CURRENT_TIMESTAMP => 100]; $expectedJson = json_encode($expected, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); $this->write->expects($this->once())->method('writeFile')->with(ReadinessCheck::SETUP_CRON_JOB_STATUS_FILE, $expectedJson); $this->readinessCheck->runReadinessCheck(); }
public function setUpDirectoryWriteInstallation() { // CONFIG $this->directoryWriteMock ->expects($this->at(0)) ->method('isExist') ->will($this->returnValue(true)); $this->directoryWriteMock ->expects($this->at(1)) ->method('isDirectory') ->will($this->returnValue(true)); $this->directoryWriteMock ->expects($this->at(2)) ->method('isReadable') ->will($this->returnValue(true)); $this->directoryWriteMock ->expects($this->at(3)) ->method('isWritable') ->will($this->returnValue(true)); // VAR $this->directoryWriteMock ->expects($this->at(4)) ->method('isExist') ->will($this->returnValue(false)); // MEDIA $this->directoryWriteMock ->expects($this->at(5)) ->method('isExist') ->will($this->returnValue(true)); $this->directoryWriteMock ->expects($this->at(6)) ->method('isDirectory') ->will($this->returnValue(false)); // STATIC_VIEW $this->directoryWriteMock ->expects($this->at(7)) ->method('isExist') ->will($this->returnValue(true)); $this->directoryWriteMock ->expects($this->at(8)) ->method('isDirectory') ->will($this->returnValue(true)); $this->directoryWriteMock ->expects($this->at(9)) ->method('isReadable') ->will($this->returnValue(true)); $this->directoryWriteMock ->expects($this->at(10)) ->method('isWritable') ->will($this->returnValue(false)); }
/** * Load composer packages available for update from cache * * @return bool|string */ private function loadPackagesForUpdateFromCache() { if ($this->directory->isExist($this->pathToCacheFile) && $this->directory->isReadable($this->pathToCacheFile)) { try { $data = $this->directory->readFile($this->pathToCacheFile); return json_decode($data, true); } catch (\Magento\Framework\Exception\FileSystemException $e) { } } return false; }
public function testGetCurrentUrl() { $storeId = 1; $baseUrl = 'http://localhost'; $relativePath = '/../wysiwyg'; $this->imagesHelper->setStoreId($storeId); $this->storeMock->expects($this->once())->method('getBaseUrl')->with(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)->willReturn($baseUrl); $this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock); $this->directoryWriteMock->expects($this->any())->method('getRelativePath')->willReturn($relativePath); $this->assertEquals($baseUrl . $relativePath . '/', $this->imagesHelper->getCurrentUrl()); }
/** * @covers \Magento\Framework\View\Design\Theme\Image::uploadPreviewImage */ public function testUploadPreviewImage() { $scope = 'test_scope'; $tmpFilePath = '/media_path/tmp/temporary.png'; $this->_themeMock->setData($this->_getThemeSampleData()); $this->_themeMock->setData('preview_image', 'test.png'); $this->_uploaderMock->expects($this->once())->method('uploadPreviewImage')->with($scope, '/media_path/tmp')->will($this->returnValue($tmpFilePath)); $this->_mediaDirectoryMock->expects($this->at(0))->method('getRelativePath')->will($this->returnArgument(0)); $this->_mediaDirectoryMock->expects($this->at(1))->method('delete')->with($this->stringContains('test.png')); $this->_mediaDirectoryMock->expects($this->at(2))->method('delete')->with($tmpFilePath); $this->_model->uploadPreviewImage($scope); }
/** * Get all folders as options array * * @return array */ public function collectFolders() { $collectFiles = $this->_collectFiles; $collectDirs = $this->_collectDirs; $this->setCollectFiles(false)->setCollectDirs(true); $this->_collectRecursive($this->connectDirectory->getAbsolutePath('connect')); $result = array('/' => '/'); foreach ($this->_collectedDirs as $dir) { $dir = substr($this->connectDirectory->getRelativePath($dir), strlen('connect/')) . '/'; $result[$dir] = $dir; } $this->setCollectFiles($collectFiles)->setCollectDirs($collectDirs); return $result; }
/** * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ protected function setUp() { $this->_filesystemMock = $this->getMock('Magento\\Framework\\Filesystem', [], [], '', false); $this->_driverMock = $this->getMockForAbstractClass('Magento\\Framework\\Filesystem\\DriverInterface', [], '', false, false, true, ['getRealPath']); $this->_driverMock->expects($this->any())->method('getRealPath')->will($this->returnArgument(0)); $this->_directoryMock = $this->getMock('Magento\\Framework\\Filesystem\\Directory\\Write', ['delete', 'getDriver'], [], '', false); $this->_directoryMock->expects($this->any())->method('getDriver')->will($this->returnValue($this->_driverMock)); $this->_filesystemMock = $this->getMock('Magento\\Framework\\Filesystem', ['getDirectoryWrite'], [], '', false); $this->_filesystemMock->expects($this->any())->method('getDirectoryWrite')->with(DirectoryList::MEDIA)->will($this->returnValue($this->_directoryMock)); $this->_adapterFactoryMock = $this->getMock('Magento\\Framework\\Image\\AdapterFactory', [], [], '', false); $this->_imageHelperMock = $this->getMock('Magento\\Cms\\Helper\\Wysiwyg\\Images', ['getStorageRoot'], [], '', false); $this->_imageHelperMock->expects($this->any())->method('getStorageRoot')->will($this->returnValue(self::STORAGE_ROOT_DIR)); $this->_resizeParameters = ['width' => 100, 'height' => 50]; $this->_storageCollectionFactoryMock = $this->getMock('Magento\\Cms\\Model\\Wysiwyg\\Images\\Storage\\CollectionFactory', [], [], '', false); $this->_storageFileFactoryMock = $this->getMock('Magento\\MediaStorage\\Model\\File\\Storage\\FileFactory', [], [], '', false); $this->_storageDatabaseFactoryMock = $this->getMock('Magento\\MediaStorage\\Model\\File\\Storage\\DatabaseFactory', [], [], '', false); $this->_directoryDatabaseFactoryMock = $this->getMock('Magento\\MediaStorage\\Model\\File\\Storage\\Directory\\DatabaseFactory', [], [], '', false); $this->_uploaderFactoryMock = $this->getMockBuilder('Magento\\MediaStorage\\Model\\File\\UploaderFactory')->disableOriginalConstructor()->getMock(); $this->_sessionMock = $this->getMock('Magento\\Backend\\Model\\Session', [], [], '', false); $this->_backendUrlMock = $this->getMock('Magento\\Backend\\Model\\Url', [], [], '', false); $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->_model = $objectManagerHelper->getObject('Magento\\Cms\\Model\\Wysiwyg\\Images\\Storage', ['session' => $this->_sessionMock, 'backendUrl' => $this->_backendUrlMock, 'cmsWysiwygImages' => $this->_imageHelperMock, 'coreFileStorageDb' => $this->getMock('Magento\\MediaStorage\\Helper\\File\\Storage\\Database', [], [], '', false), 'filesystem' => $this->_filesystemMock, 'imageFactory' => $this->_adapterFactoryMock, 'assetRepo' => $this->getMock('Magento\\Framework\\View\\Asset\\Repository', [], [], '', false), 'storageCollectionFactory' => $this->_storageCollectionFactoryMock, 'storageFileFactory' => $this->_storageFileFactoryMock, 'storageDatabaseFactory' => $this->_storageDatabaseFactoryMock, 'directoryDatabaseFactory' => $this->_directoryDatabaseFactoryMock, 'uploaderFactory' => $this->_uploaderFactoryMock, 'resizeParameters' => $this->_resizeParameters]); }
/** * @test * @return void * @expectedException \InvalidArgumentException * @expectedExceptionMessage The image not found */ public function testGetThumbnailPathNotFound() { $image = 'notFoundImage.png'; $root = '/image'; $sourceNode = '/not/a/root'; $node = base64_encode($sourceNode); $this->request->expects($this->at(0))->method('getParam')->willReturnMap([[\Magento\Theme\Helper\Storage::PARAM_THEME_ID, null, 6], [\Magento\Theme\Helper\Storage::PARAM_CONTENT_TYPE, null, \Magento\Theme\Model\Wysiwyg\Storage::TYPE_IMAGE], [\Magento\Theme\Helper\Storage::PARAM_NODE, null, $node]]); $this->urlDecoder->expects($this->once())->method('decode')->with($node)->willReturnCallback(function ($path) { return base64_decode($path); }); $this->directoryWrite->expects($this->once())->method('isDirectory')->with($root . $sourceNode)->willReturn(true); $this->directoryWrite->expects($this->once())->method('getRelativePath')->with($root . $sourceNode)->willReturn($sourceNode); $this->directoryWrite->expects($this->once())->method('isExist')->with($sourceNode . '/' . $image); $this->helper->getThumbnailPath($image); }