Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CopyServiceTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class CopyServiceTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $object;
20 
24  protected $fileFactory;
25 
29  protected $filesystem;
30 
34  protected $sourceTheme;
35 
39  protected $targetTheme;
40 
44  protected $link;
45 
49  protected $linkCollection;
50 
54  protected $update;
55 
59  protected $updateCollection;
60 
64  protected $updateFactory;
65 
69  protected $customizationPath;
70 
74  protected $targetFiles = [];
75 
79  protected $sourceFiles = [];
80 
84  protected $dirWriteMock;
85 
90  protected function setUp()
91  {
92  $sourceFileOne = $this->createPartialMock(\Magento\Theme\Model\Theme\File::class, ['__wakeup', 'delete']);
93  $sourceFileOne->setData(
94  [
95  'file_path' => 'fixture_file_path_one',
96  'file_type' => 'fixture_file_type_one',
97  'content' => 'fixture_content_one',
98  'sort_order' => 10,
99  ]
100  );
101  $sourceFileTwo = $this->createPartialMock(\Magento\Theme\Model\Theme\File::class, ['__wakeup', 'delete']);
102  $sourceFileTwo->setData(
103  [
104  'file_path' => 'fixture_file_path_two',
105  'file_type' => 'fixture_file_type_two',
106  'content' => 'fixture_content_two',
107  'sort_order' => 20,
108  ]
109  );
110  $this->sourceFiles = [$sourceFileOne, $sourceFileTwo];
111  $this->sourceTheme = $this->createPartialMock(
112  \Magento\Theme\Model\Theme::class,
113  ['__wakeup', 'getCustomization']
114  );
115 
116  $this->targetFiles = [
117  $this->createPartialMock(\Magento\Theme\Model\Theme\File::class, ['__wakeup', 'delete']),
118  $this->createPartialMock(\Magento\Theme\Model\Theme\File::class, ['__wakeup', 'delete']),
119  ];
120  $this->targetTheme = $this->createPartialMock(
121  \Magento\Theme\Model\Theme::class,
122  ['__wakeup', 'getCustomization']
123  );
124  $this->targetTheme->setId(123);
125 
126  $this->customizationPath = $this->createMock(\Magento\Framework\View\Design\Theme\Customization\Path::class);
127 
128  $this->fileFactory = $this->createPartialMock(
129  \Magento\Framework\View\Design\Theme\FileFactory::class,
130  ['create']
131  );
132  $this->filesystem =
133  $this->createPartialMock(\Magento\Framework\Filesystem::class, ['getDirectoryWrite']);
134  $this->dirWriteMock = $this->createPartialMock(
135  \Magento\Framework\Filesystem\Directory\Write::class,
136  ['isDirectory', 'search', 'copy', 'delete', 'read', 'copyFile', 'isExist']
137  );
138  $this->filesystem->expects(
139  $this->any()
140  )->method(
141  'getDirectoryWrite'
142  )->with(
144  )->will(
145  $this->returnValue($this->dirWriteMock)
146  );
147 
148  /* Init \Magento\Widget\Model\ResourceModel\Layout\Update\Collection model */
149  $this->updateFactory = $this->createPartialMock(\Magento\Widget\Model\Layout\UpdateFactory::class, ['create']);
150  $this->update = $this->createPartialMock(
151  \Magento\Widget\Model\Layout\Update::class,
152  ['__wakeup', 'getCollection']
153  );
154  $this->updateFactory->expects($this->at(0))->method('create')->will($this->returnValue($this->update));
155  $this->updateCollection = $this->createPartialMock(
156  \Magento\Widget\Model\ResourceModel\Layout\Update\Collection::class,
157  ['addThemeFilter', 'delete', 'getIterator']
158  );
159  $this->update->expects(
160  $this->any()
161  )->method(
162  'getCollection'
163  )->will(
164  $this->returnValue($this->updateCollection)
165  );
166 
167  /* Init Link an Link_Collection model */
168  $this->link = $this->createPartialMock(\Magento\Widget\Model\Layout\Link::class, ['__wakeup', 'getCollection']);
169  $this->linkCollection = $this->createPartialMock(
170  \Magento\Widget\Model\ResourceModel\Layout\Link\Collection::class,
171  ['addThemeFilter', 'getIterator', 'addFieldToFilter']
172  );
173  $this->link->expects($this->any())->method('getCollection')->will($this->returnValue($this->linkCollection));
174 
175  $eventManager = $this->createPartialMock(\Magento\Framework\Event\ManagerInterface::class, ['dispatch']);
176 
177  $this->object = new \Magento\Theme\Model\CopyService(
178  $this->filesystem,
179  $this->fileFactory,
180  $this->link,
181  $this->updateFactory,
182  $eventManager,
183  $this->customizationPath
184  );
185  }
186 
187  protected function tearDown()
188  {
189  $this->object = null;
190  $this->filesystem = null;
191  $this->fileFactory = null;
192  $this->sourceTheme = null;
193  $this->targetTheme = null;
194  $this->link = null;
195  $this->linkCollection = null;
196  $this->updateCollection = null;
197  $this->updateFactory = null;
198  $this->sourceFiles = [];
199  $this->targetFiles = [];
200  }
201 
205  public function testCopyLayoutUpdates()
206  {
207  $customization = $this->createPartialMock(
208  \Magento\Framework\View\Design\Theme\Customization::class,
209  ['getFiles']
210  );
211  $customization->expects($this->atLeastOnce())->method('getFiles')->will($this->returnValue([]));
212  $this->sourceTheme->expects(
213  $this->once()
214  )->method(
215  'getCustomization'
216  )->will(
217  $this->returnValue($customization)
218  );
219  $this->targetTheme->expects(
220  $this->once()
221  )->method(
222  'getCustomization'
223  )->will(
224  $this->returnValue($customization)
225  );
226 
227  $this->updateCollection->expects($this->once())->method('delete');
228  $this->linkCollection->expects($this->once())->method('addThemeFilter');
229 
230  $targetLinkOne = $this->createPartialMock(
231  \Magento\Widget\Model\Layout\Link::class,
232  ['__wakeup', 'setId', 'setThemeId', 'save', 'setLayoutUpdateId']
233  );
234  $targetLinkOne->setData(['id' => 1, 'layout_update_id' => 1]);
235  $targetLinkTwo = $this->createPartialMock(
236  \Magento\Widget\Model\Layout\Link::class,
237  ['__wakeup', 'setId', 'setThemeId', 'save', 'setLayoutUpdateId']
238  );
239  $targetLinkTwo->setData(['id' => 2, 'layout_update_id' => 2]);
240 
241  $targetLinkOne->expects($this->at(0))->method('setThemeId')->with(123);
242  $targetLinkOne->expects($this->at(1))->method('setLayoutUpdateId')->with(1);
243  $targetLinkOne->expects($this->at(2))->method('setId')->with(null);
244  $targetLinkOne->expects($this->at(3))->method('save');
245 
246  $targetLinkTwo->expects($this->at(0))->method('setThemeId')->with(123);
247  $targetLinkTwo->expects($this->at(1))->method('setLayoutUpdateId')->with(2);
248  $targetLinkTwo->expects($this->at(2))->method('setId')->with(null);
249  $targetLinkTwo->expects($this->at(3))->method('save');
250 
251  $linkReturnValues = $this->onConsecutiveCalls(new \ArrayIterator([$targetLinkOne, $targetLinkTwo]));
252  $this->linkCollection->expects($this->any())->method('getIterator')->will($linkReturnValues);
253 
254  $targetUpdateOne = $this->createPartialMock(
255  \Magento\Widget\Model\Layout\Update::class,
256  ['__wakeup', 'setId', 'load', 'save']
257  );
258  $targetUpdateOne->setData(['id' => 1]);
259  $targetUpdateTwo = $this->createPartialMock(
260  \Magento\Widget\Model\Layout\Update::class,
261  ['__wakeup', 'setId', 'load', 'save']
262  );
263  $targetUpdateTwo->setData(['id' => 2]);
264  $updateReturnValues = $this->onConsecutiveCalls($this->update, $targetUpdateOne, $targetUpdateTwo);
265  $this->updateFactory->expects($this->any())->method('create')->will($updateReturnValues);
266 
267  $this->object->copy($this->sourceTheme, $this->targetTheme);
268  }
269 
275  {
276  $sourceCustom = $this->createPartialMock(
277  \Magento\Framework\View\Design\Theme\Customization::class,
278  ['getFiles']
279  );
280  $sourceCustom->expects(
281  $this->atLeastOnce()
282  )->method(
283  'getFiles'
284  )->will(
285  $this->returnValue($this->sourceFiles)
286  );
287  $this->sourceTheme->expects(
288  $this->once()
289  )->method(
290  'getCustomization'
291  )->will(
292  $this->returnValue($sourceCustom)
293  );
294  $targetCustom = $this->createPartialMock(
295  \Magento\Framework\View\Design\Theme\Customization::class,
296  ['getFiles']
297  );
298  $targetCustom->expects(
299  $this->atLeastOnce()
300  )->method(
301  'getFiles'
302  )->will(
303  $this->returnValue($this->targetFiles)
304  );
305  $this->targetTheme->expects(
306  $this->once()
307  )->method(
308  'getCustomization'
309  )->will(
310  $this->returnValue($targetCustom)
311  );
312 
313  $this->linkCollection->expects(
314  $this->any()
315  )->method(
316  'addFieldToFilter'
317  )->will(
318  $this->returnValue($this->linkCollection)
319  );
320  $this->linkCollection->expects(
321  $this->any()
322  )->method(
323  'getIterator'
324  )->will(
325  $this->returnValue(new \ArrayIterator([]))
326  );
327 
328  foreach ($this->targetFiles as $targetFile) {
329  $targetFile->expects($this->once())->method('delete');
330  }
331 
332  $newFileOne = $this->createPartialMock(\Magento\Theme\Model\Theme\File::class, ['__wakeup', 'setData', 'save']);
333  $newFileTwo = $this->createPartialMock(\Magento\Theme\Model\Theme\File::class, ['__wakeup', 'setData', 'save']);
334  $newFileOne->expects(
335  $this->at(0)
336  )->method(
337  'setData'
338  )->with(
339  [
340  'theme_id' => 123,
341  'file_path' => 'fixture_file_path_one',
342  'file_type' => 'fixture_file_type_one',
343  'content' => 'fixture_content_one',
344  'sort_order' => 10,
345  ]
346  );
347  $newFileOne->expects($this->at(1))->method('save');
348  $newFileTwo->expects(
349  $this->at(0)
350  )->method(
351  'setData'
352  )->with(
353  [
354  'theme_id' => 123,
355  'file_path' => 'fixture_file_path_two',
356  'file_type' => 'fixture_file_type_two',
357  'content' => 'fixture_content_two',
358  'sort_order' => 20,
359  ]
360  );
361  $newFileTwo->expects($this->at(1))->method('save');
362  $this->fileFactory->expects(
363  $this->any()
364  )->method(
365  'create'
366  )->with(
367  []
368  )->will(
369  $this->onConsecutiveCalls($newFileOne, $newFileTwo)
370  );
371 
372  $this->object->copy($this->sourceTheme, $this->targetTheme);
373  }
374 
381  {
382  $customization = $this->createPartialMock(
383  \Magento\Framework\View\Design\Theme\Customization::class,
384  ['getFiles']
385  );
386  $customization->expects($this->atLeastOnce())->method('getFiles')->will($this->returnValue([]));
387  $this->sourceTheme->expects(
388  $this->once()
389  )->method(
390  'getCustomization'
391  )->will(
392  $this->returnValue($customization)
393  );
394  $this->targetTheme->expects(
395  $this->once()
396  )->method(
397  'getCustomization'
398  )->will(
399  $this->returnValue($customization)
400  );
401 
402  $this->linkCollection->expects(
403  $this->any()
404  )->method(
405  'addFieldToFilter'
406  )->will(
407  $this->returnValue($this->linkCollection)
408  );
409  $this->linkCollection->expects(
410  $this->any()
411  )->method(
412  'getIterator'
413  )->will(
414  $this->returnValue(new \ArrayIterator([]))
415  );
416 
417  $this->customizationPath->expects(
418  $this->at(0)
419  )->method(
420  'getCustomizationPath'
421  )->will(
422  $this->returnValue('source/path')
423  );
424 
425  $this->customizationPath->expects(
426  $this->at(1)
427  )->method(
428  'getCustomizationPath'
429  )->will(
430  $this->returnValue('target/path')
431  );
432 
433  $this->dirWriteMock->expects(
434  $this->any()
435  )->method(
436  'isDirectory'
437  )->will(
438  $this->returnValueMap([['source/path', true], ['source/path/subdir', true]])
439  );
440 
441  $this->dirWriteMock->expects(
442  $this->any()
443  )->method(
444  'isExist'
445  )->will(
446  $this->returnValueMap(
447  [
448  ['target/path', true]
449  ]
450  )
451  );
452 
453  $this->dirWriteMock->expects(
454  $this->any()
455  )->method(
456  'read'
457  )->will(
458  $this->returnValueMap(
459  [
460  ['target/path', ['target/path/subdir']],
461  ['source/path', ['source/path/subdir']],
462  ['source/path/subdir', ['source/path/subdir/file_one.jpg', 'source/path/subdir/file_two.png']],
463  ]
464  )
465  );
466 
467  $expectedCopyEvents = [
468  ['source/path/subdir/file_one.jpg', 'target/path/subdir/file_one.jpg', null],
469  ['source/path/subdir/file_two.png', 'target/path/subdir/file_two.png', null],
470  ];
471  $actualCopyEvents = [];
472  $recordCopyEvent = function () use (&$actualCopyEvents) {
473  $actualCopyEvents[] = func_get_args();
474  };
475  $this->dirWriteMock->expects($this->any())->method('copyFile')->will($this->returnCallback($recordCopyEvent));
476 
477  $this->object->copy($this->sourceTheme, $this->targetTheme);
478 
479  $this->assertEquals($expectedCopyEvents, $actualCopyEvents);
480  }
481 }