Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ThemeModularTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class ThemeModularTest extends \PHPUnit\Framework\TestCase
11 {
15  private $model;
16 
20  private $themeDirectory;
21 
25  private $fileFactory;
26 
31 
35  private $readDirFactory;
36 
40  private $componentRegistrar;
41 
42  protected function setUp()
43  {
44  $this->themeDirectory = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
45  $this->themeDirectory->expects($this->any())
46  ->method('getAbsolutePath')
47  ->willReturnArgument(0);
48  $this->pathPatternHelperMock = $this->getMockBuilder(\Magento\Framework\View\Helper\PathPattern::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51 
52  $this->fileFactory = $this->createMock(\Magento\Framework\View\File\Factory::class);
53  $this->readDirFactory = $this->createMock(\Magento\Framework\Filesystem\Directory\ReadFactory::class);
54  $this->readDirFactory->expects($this->any())
55  ->method('create')
56  ->will($this->returnValue($this->themeDirectory));
57  $this->componentRegistrar = $this->getMockForAbstractClass(
58  \Magento\Framework\Component\ComponentRegistrarInterface::class
59  );
60  $this->model = new \Magento\Framework\View\File\Collector\Override\ThemeModular(
61  $this->fileFactory,
62  $this->readDirFactory,
63  $this->componentRegistrar,
64  $this->pathPatternHelperMock,
65  'override/theme'
66  );
67  }
68 
69  public function testGetFilesWrongTheme()
70  {
71  $this->componentRegistrar->expects($this->once())
72  ->method('getPath')
73  ->will($this->returnValue(''));
74  $theme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
75  $theme->expects($this->once())
76  ->method('getFullPath')
77  ->will($this->returnValue('area/Vendor/theme'));
78  $this->assertSame([], $this->model->getFiles($theme, ''));
79  }
80 
81  public function testGetFiles()
82  {
83  $themePath = 'area/theme_path';
84  $inputPath = '*.xml';
85  $grandparentTheme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
86  $grandparentTheme->expects($this->once())->method('getCode')->willReturn('vendor/grand_parent_theme');
87 
88  $parentTheme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
89  $parentTheme->expects($this->once())->method('getCode')->willReturn('vendor/parent_theme');
90  $parentTheme->expects($this->once())->method('getParentTheme')->willReturn($grandparentTheme);
91 
92  $theme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
93  $theme->expects($this->once())->method('getFullPath')->willReturn($themePath);
94  $theme->expects($this->once())->method('getParentTheme')->willReturn($parentTheme);
95 
96  $filePathOne = 'design/area/theme_path/Module_One/override/theme/vendor/parent_theme/1.xml';
97  $filePathTwo = 'design/area/theme_path/Module_Two/override/theme/vendor/grand_parent_theme/2.xml';
98  $this->themeDirectory->expects($this->once())
99  ->method('search')
100  ->with('*_*/override/theme/*/*/*.xml')
101  ->willReturn([$filePathOne, $filePathTwo]);
102  $this->pathPatternHelperMock->expects($this->any())
103  ->method('translatePatternFromGlob')
104  ->with($inputPath)
105  ->willReturn('[^/]*\\.xml');
106 
107  $fileOne = $this->createMock(\Magento\Framework\View\File::class);
108  $fileTwo = $this->createMock(\Magento\Framework\View\File::class);
109  $this->fileFactory
110  ->expects($this->exactly(2))
111  ->method('create')
112  ->willReturnMap(
113  [
114  [$filePathOne, 'Module_One', $parentTheme, false, $fileOne],
115  [$filePathTwo, 'Module_Two', $grandparentTheme, false, $fileTwo],
116  ]
117  );
118  $this->componentRegistrar->expects($this->once())
119  ->method('getPath')
120  ->with(ComponentRegistrar::THEME, $themePath)
121  ->will($this->returnValue('/full/theme/path'));
122 
123  $this->assertSame([$fileOne, $fileTwo], $this->model->getFiles($theme, $inputPath));
124  }
125 
126  public function testGetFilesWithPreset()
127  {
128  $themePath = 'area/theme_path';
129  $inputPath = 'preset/3.xml';
130  $grandparentTheme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
131  $grandparentTheme->expects($this->once())->method('getCode')->willReturn('vendor/grand_parent_theme');
132 
133  $parentTheme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
134  $parentTheme->expects($this->once())->method('getCode')->willReturn('vendor/parent_theme');
135  $parentTheme->expects($this->once())->method('getParentTheme')->willReturn($grandparentTheme);
136 
137  $theme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
138  $theme->expects($this->once())->method('getFullPath')->willReturn($themePath);
139  $theme->expects($this->once())->method('getParentTheme')->willReturn($parentTheme);
140 
141  $filePathOne = 'design/area/theme_path/Module_Two/override/theme/vendor/grand_parent_theme/preset/3.xml';
142  $this->themeDirectory->expects($this->once())
143  ->method('search')
144  ->with('*_*/override/theme/*/*/preset/3.xml')
145  ->willReturn([$filePathOne]);
146 
147  $fileOne = $this->createMock(\Magento\Framework\View\File::class);
148  $this->fileFactory
149  ->expects($this->once())
150  ->method('create')
151  ->with($filePathOne, 'Module_Two', $grandparentTheme)
152  ->willReturn($fileOne);
153  $this->pathPatternHelperMock->expects($this->any())
154  ->method('translatePatternFromGlob')
155  ->with($inputPath)
156  ->willReturn('preset/3.xml');
157  $this->componentRegistrar->expects($this->once())
158  ->method('getPath')
159  ->with(ComponentRegistrar::THEME, $themePath)
160  ->will($this->returnValue('/full/theme/path'));
161 
162  $this->assertSame([$fileOne], $this->model->getFiles($theme, $inputPath));
163  }
164 
165  public function testGetFilesWrongAncestor()
166  {
167  $themePath = 'area/theme_path';
168  $inputPath = '*.xml';
169  $filePath = 'design/area/theme_path/Module_One/override/theme/vendor/parent_theme/1.xml';
170  $expectedMessage = "Trying to override modular view file '$filePath' for theme 'vendor/parent_theme'"
171  . ", which is not ancestor of theme 'vendor/theme_path'";
172  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
173  $this->expectExceptionMessage($expectedMessage);
174 
175  $theme = $this->getMockForAbstractClass(\Magento\Framework\View\Design\ThemeInterface::class);
176  $theme->expects($this->once())->method('getFullPath')->willReturn($themePath);
177  $theme->expects($this->once())->method('getParentTheme')->willReturn(null);
178  $theme->expects($this->once())->method('getCode')->willReturn('vendor/theme_path');
179 
180  $this->themeDirectory->expects($this->once())
181  ->method('search')
182  ->with('*_*/override/theme/*/*/*.xml')
183  ->willReturn([$filePath]);
184  $this->pathPatternHelperMock->expects($this->any())
185  ->method('translatePatternFromGlob')
186  ->with($inputPath)
187  ->willReturn('[^/]*\\.xml');
188  $this->componentRegistrar->expects($this->once())
189  ->method('getPath')
190  ->with(ComponentRegistrar::THEME, $themePath)
191  ->will($this->returnValue('/full/theme/path'));
192 
193  $this->model->getFiles($theme, $inputPath);
194  }
195 }
$theme