protected function setUp() { $this->closure = function () { return true; }; $this->configMock = $this->getMock('Magento\\MediaStorage\\Model\\File\\Storage\\Config', [], [], '', false); $this->sync = $this->getMock('Magento\\MediaStorage\\Model\\File\\Storage\\Synchronization', [], [], '', false); $this->configFactoryMock = $this->getMock('Magento\\MediaStorage\\Model\\File\\Storage\\ConfigFactory', ['create'], [], '', false); $this->configFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->configMock)); $this->syncFactoryMock = $this->getMock('Magento\\MediaStorage\\Model\\File\\Storage\\SynchronizationFactory', ['create'], [], '', false); $this->syncFactoryMock->expects($this->any())->method('create')->will($this->returnValue($this->sync)); $this->filesystemMock = $this->getMock('Magento\\Framework\\Filesystem', [], [], '', false); $this->directoryMock = $this->getMockForAbstractClass('Magento\\Framework\\Filesystem\\Directory\\WriteInterface'); $this->filesystemMock->expects($this->any())->method('getDirectoryWrite')->with(DirectoryList::MEDIA)->will($this->returnValue($this->directoryMock)); $this->responseMock = $this->getMock('Magento\\MediaStorage\\Model\\File\\Storage\\Response', [], [], '', false); $this->model = new \Magento\MediaStorage\App\Media($this->configFactoryMock, $this->syncFactoryMock, $this->responseMock, $this->closure, self::MEDIA_DIRECTORY, self::CACHE_FILE_PATH, self::RELATIVE_FILE_PATH, $this->filesystemMock); }
/** * Run application * * @return Response * @throws \LogicException */ public function launch() { if ($this->mediaDirectoryPath !== $this->directory->getAbsolutePath()) { // Path to media directory changed or absent - update the config /** @var \Magento\MediaStorage\Model\File\Storage\Config $config */ $config = $this->configFactory->create(['cacheFile' => $this->configCacheFile]); $config->save(); $this->mediaDirectoryPath = $config->getMediaDirectory(); $allowedResources = $config->getAllowedResources(); $isAllowed = $this->isAllowed; if (!$isAllowed($this->relativeFileName, $allowedResources)) { throw new \LogicException('The specified path is not allowed.'); } } /** @var \Magento\MediaStorage\Model\File\Storage\Synchronization $sync */ $sync = $this->syncFactory->create(['directory' => $this->directory]); $sync->synchronize($this->relativeFileName); if ($this->directory->isReadable($this->relativeFileName)) { $this->response->setFilePath($this->directory->getAbsolutePath($this->relativeFileName)); } else { $this->response->setHttpResponseCode(404); } return $this->response; }