/** * @param array $files * @param string $filePath * @param string $pathPattern * * @dataProvider getFilesDataProvider */ public function testGetFiles($files, $filePath, $pathPattern) { $theme = $this->getMockForAbstractClass('Magento\Framework\View\Design\ThemeInterface'); $theme->expects($this->once())->method('getFullPath')->willReturn('area/theme/path'); $handlePath = 'design/area/theme/path/%s/subdir/%s'; $returnKeys = []; foreach ($files as $file) { $returnKeys[] = sprintf($handlePath, $file['module'], $file['handle']); } $this->pathPatternHelperMock->expects($this->any()) ->method('translatePatternFromGlob') ->with($filePath) ->willReturn($pathPattern); $this->directory->expects($this->once()) ->method('search') ->willReturn($returnKeys); $this->directory->expects($this->any()) ->method('getAbsolutePath') ->willReturnArgument(0); $checkResult = []; foreach ($files as $key => $file) { $checkResult[$key] = new \Magento\Framework\View\File($file['handle'], $file['module'], $theme); $this->fileFactory ->expects($this->at($key)) ->method('create') ->with(sprintf($handlePath, $file['module'], $file['handle']), $file['module'], $theme) ->willReturn($checkResult[$key]); } $this->assertSame($checkResult, $this->model->getFiles($theme, $filePath)); }
/** * Retrieve files * * @param ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] */ public function getFiles(ThemeInterface $theme, $filePath) { $namespace = $module = '*'; $themePath = $theme->getFullPath(); if (empty($themePath)) { return []; } $themeAbsolutePath = $this->componentRegistrar->getPath(ComponentRegistrar::THEME, $themePath); if (!$themeAbsolutePath) { return []; } $themeDir = $this->readDirFactory->create($themeAbsolutePath); $searchPattern = "{$namespace}_{$module}/{$this->subDir}{$filePath}"; $files = $themeDir->search($searchPattern); $result = []; $pattern = "#(?<moduleName>[^/]+)/{$this->subDir}" . $this->pathPatternHelper->translatePatternFromGlob($filePath) . "\$#i"; foreach ($files as $file) { $filename = $themeDir->getAbsolutePath($file); if (!preg_match($pattern, $filename, $matches)) { continue; } $result[] = $this->fileFactory->create($filename, $matches['moduleName']); } return $result; }
public function testGetFilesWrongAncestor() { $themePath = 'area/theme_path'; $inputPath = '*.xml'; $filePath = 'design/area/theme_path/Module_One/override/theme/vendor/parent_theme/1.xml'; $expectedMessage = "Trying to override modular view file '{$filePath}' for theme 'vendor/parent_theme'" . ", which is not ancestor of theme 'vendor/theme_path'"; $this->setExpectedException('Magento\\Framework\\Exception\\LocalizedException', $expectedMessage); $theme = $this->getMockForAbstractClass('Magento\\Framework\\View\\Design\\ThemeInterface'); $theme->expects($this->once())->method('getFullPath')->willReturn($themePath); $theme->expects($this->once())->method('getParentTheme')->willReturn(null); $theme->expects($this->once())->method('getCode')->willReturn('vendor/theme_path'); $this->themeDirectory->expects($this->once())->method('search')->with('*_*/override/theme/*/*/*.xml')->willReturn([$filePath]); $this->pathPatternHelperMock->expects($this->any())->method('translatePatternFromGlob')->with($inputPath)->willReturn('[^/]*\\.xml'); $this->componentRegistrar->expects($this->once())->method('getPath')->with(ComponentRegistrar::THEME, $themePath)->will($this->returnValue('/full/theme/path')); $this->model->getFiles($theme, $inputPath); }
public function testGetFiles() { $sharedFiles = [ 'Namespace/One/view/base/layout/one.xml', 'Namespace/Two/view/base/layout/two.xml' ]; $themeFiles = [ 'Namespace/Two/view/frontend/layout/four.txt', 'Namespace/Two/view/frontend/layout/three.xml' ]; $this->directoryMock->expects($this->any()) ->method('search') ->willReturnMap( [ ['*/*/view/base/layout/*.xml', null, $sharedFiles], ['*/*/view/frontend/layout/*.xml', null, $themeFiles] ] ); $this->pathPatternHelperMock->expects($this->once()) ->method('translatePatternFromGlob') ->with('*.xml') ->willReturn('[^/]*\\.xml'); $this->directoryMock->expects($this->atLeastOnce()) ->method('getAbsolutePath') ->willReturnArgument(0); $this->fileFactoryMock->expects($this->atLeastOnce()) ->method('create') ->willReturn($this->createFileMock()); $this->themeMock->expects($this->once()) ->method('getData') ->with('area') ->willReturn('frontend'); $result = $this->fileCollector->getFiles($this->themeMock, '*.xml'); $this->assertCount(3, $result); $this->assertInstanceOf('Magento\Framework\View\File', $result[0]); $this->assertInstanceOf('Magento\Framework\View\File', $result[1]); $this->assertInstanceOf('Magento\Framework\View\File', $result[2]); }
/** * Retrieve files * * @param ThemeInterface $theme * @param string $filePath * @return \Magento\Framework\View\File[] * @throws \Magento\Framework\Exception\LocalizedException */ public function getFiles(ThemeInterface $theme, $filePath) { $namespace = $module = '*'; $themePath = $theme->getFullPath(); if (empty($themePath)) { return []; } $themeAbsolutePath = $this->componentRegistrar->getPath(ComponentRegistrar::THEME, $themePath); if (!$themeAbsolutePath) { return []; } $themeDir = $this->readDirFactory->create($themeAbsolutePath); $files = $themeDir->search("{$namespace}_{$module}/{$this->subDir}*/*/{$filePath}"); if (empty($files)) { return []; } $themes = []; $currentTheme = $theme; while ($currentTheme = $currentTheme->getParentTheme()) { $themes[$currentTheme->getCode()] = $currentTheme; } $result = []; $pattern = "#/(?<module>[^/]+)/{$this->subDir}(?<themeVendor>[^/]+)/(?<themeName>[^/]+)/" . $this->pathPatternHelper->translatePatternFromGlob($filePath) . "\$#i"; foreach ($files as $file) { $filename = $themeDir->getAbsolutePath($file); if (!preg_match($pattern, $filename, $matches)) { continue; } $moduleFull = $matches['module']; $ancestorThemeCode = $matches['themeVendor'] . '/' . $matches['themeName']; if (!isset($themes[$ancestorThemeCode])) { throw new LocalizedException(new \Magento\Framework\Phrase("Trying to override modular view file '%1' for theme '%2', which is not ancestor of theme '%3'", [$filename, $ancestorThemeCode, $theme->getCode()])); } $result[] = $this->fileFactory->create($filename, $moduleFull, $themes[$ancestorThemeCode]); } return $result; }
/** * @param string $path * @param string $expectedPattern * * @dataProvider translatePatternFromGlobDataProvider */ public function testTranslatePatternFromGlob($path, $expectedPattern) { $this->assertEquals($expectedPattern, $this->pathPatternHelper->translatePatternFromGlob($path)); }