Skip to content

Commit 497ed2d

Browse files
Merge branch 'imported-magento-magento2-page-builder-634' of github.com:magento-obsessive-owls/magento2-page-builder into imported-magento-magento2-page-builder-631
2 parents dd739fc + 8ad9bea commit 497ed2d

File tree

2 files changed

+136
-21
lines changed

2 files changed

+136
-21
lines changed

app/code/Magento/PageBuilder/Model/Stage/Preview.php

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,47 @@
77

88
namespace Magento\PageBuilder\Model\Stage;
99

10+
use Magento\Framework\App\Area;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\App\State;
13+
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
14+
use Magento\Framework\View\DesignInterface;
15+
use Magento\Store\Model\App\Emulation;
16+
use Magento\Store\Model\ScopeInterface;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
1019
/**
1120
* Handle placing Magento into Page Builder Preview mode and emulating the store front
1221
*/
1322
class Preview
1423
{
1524
/**
16-
* @var \Magento\Store\Model\App\Emulation
25+
* @var Emulation
1726
*/
1827
private $emulation;
1928

2029
/**
21-
* @var \Magento\Framework\App\State
30+
* @var State
2231
*/
2332
private $appState;
2433

2534
/**
26-
* @var \Magento\Framework\View\DesignInterface
35+
* @var DesignInterface
2736
*/
2837
private $design;
2938

3039
/**
31-
* @var \Magento\Framework\View\Design\Theme\ThemeProviderInterface
40+
* @var ThemeProviderInterface
3241
*/
3342
private $themeProvider;
3443

3544
/**
36-
* @var \Magento\Store\Model\StoreManagerInterface
45+
* @var StoreManagerInterface
3746
*/
3847
private $storeManager;
3948

4049
/**
41-
* @var \Magento\Framework\App\Config\ScopeConfigInterface
50+
* @var ScopeConfigInterface
4251
*/
4352
private $scopeConfig;
4453

@@ -49,20 +58,20 @@ class Preview
4958

5059
/**
5160
* Preview constructor.
52-
* @param \Magento\Store\Model\App\Emulation $emulation
53-
* @param \Magento\Framework\App\State $appState
54-
* @param \Magento\Framework\View\DesignInterface $design
55-
* @param \Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider
56-
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
57-
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
61+
* @param Emulation $emulation
62+
* @param State $appState
63+
* @param DesignInterface $design
64+
* @param ThemeProviderInterface $themeProvider
65+
* @param StoreManagerInterface $storeManager
66+
* @param ScopeConfigInterface $scopeConfig
5867
*/
5968
public function __construct(
60-
\Magento\Store\Model\App\Emulation $emulation,
61-
\Magento\Framework\App\State $appState,
62-
\Magento\Framework\View\DesignInterface $design,
63-
\Magento\Framework\View\Design\Theme\ThemeProviderInterface $themeProvider,
64-
\Magento\Store\Model\StoreManagerInterface $storeManager,
65-
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
69+
Emulation $emulation,
70+
State $appState,
71+
DesignInterface $design,
72+
ThemeProviderInterface $themeProvider,
73+
StoreManagerInterface $storeManager,
74+
ScopeConfigInterface $scopeConfig
6675
) {
6776
$this->emulation = $emulation;
6877
$this->appState = $appState;
@@ -79,7 +88,7 @@ public function __construct(
7988
*/
8089
public function getPreviewArea() : string
8190
{
82-
return \Magento\Framework\App\Area::AREA_FRONTEND;
91+
return Area::AREA_FRONTEND;
8392
}
8493

8594
/**
@@ -95,7 +104,7 @@ public function startPreviewMode($callback, $storeId = null)
95104
$this->isPreview = true;
96105

97106
if (!$storeId) {
98-
$storeId = $this->storeManager->getDefaultStoreView()->getId();
107+
$storeId = $this->getStoreId();
99108
}
100109
$this->emulation->startEnvironmentEmulation($storeId);
101110

@@ -104,7 +113,7 @@ public function startPreviewMode($callback, $storeId = null)
104113
function () use ($callback) {
105114
$themeId = $this->scopeConfig->getValue(
106115
'design/theme/theme_id',
107-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
116+
ScopeInterface::SCOPE_STORE
108117
);
109118
$theme = $this->themeProvider->getThemeById($themeId);
110119
$this->design->setDesignTheme($theme, $this->getPreviewArea());
@@ -131,4 +140,26 @@ public function isPreviewMode() : bool
131140
{
132141
return $this->isPreview;
133142
}
143+
144+
/**
145+
* Returns store id by default store view or store id from the available store if default store view is null
146+
*
147+
* @return int|null
148+
*/
149+
private function getStoreId(): ?int
150+
{
151+
$storeId = null;
152+
$store = $this->storeManager->getDefaultStoreView();
153+
if ($store) {
154+
$storeId = (int) $store->getId();
155+
} else {
156+
$stores = $this->storeManager->getStores();
157+
if (count($stores)) {
158+
$store = array_shift($stores);
159+
$storeId = (int) $store->getId();
160+
}
161+
}
162+
163+
return $storeId;
164+
}
134165
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\PageBuilder\Test\Unit\Model;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\State;
12+
use Magento\Framework\View\Design\Theme\ThemeProviderInterface;
13+
use Magento\Framework\View\DesignInterface;
14+
use Magento\PageBuilder\Model\Stage\Preview;
15+
use Magento\Store\Model\App\Emulation;
16+
use Magento\Store\Model\Store;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* Test for Page Builder Stage Preview Model.
23+
*/
24+
class PreviewTest extends TestCase
25+
{
26+
/**
27+
* @var Preview
28+
*/
29+
private $model;
30+
31+
/**
32+
* @var Emulation|MockObject
33+
*/
34+
private $emulation;
35+
36+
/**
37+
* @var StoreManagerInterface|MockObject
38+
*/
39+
private $storeManagerInterface;
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
protected function setUp(): void
45+
{
46+
parent::setUp();
47+
48+
$this->storeManagerInterface = $this->createMock(StoreManagerInterface::class);
49+
$this->emulation = $this->createMock(Emulation::class);
50+
$this->model = new Preview(
51+
$this->emulation,
52+
$this->createMock(State::class),
53+
$this->createMock(DesignInterface::class),
54+
$this->createMock(ThemeProviderInterface::class),
55+
$this->storeManagerInterface,
56+
$this->createMock(ScopeConfigInterface::class)
57+
);
58+
}
59+
60+
/**
61+
* Checks that method works properly even if the getDefaultStoreView returns null
62+
*
63+
* @return void
64+
* @throws \Exception
65+
*/
66+
public function testStartPreviewModeWithEmptyDefaultStoreView(): void
67+
{
68+
$callback = function () {
69+
};
70+
$storeId = 2;
71+
$store = $this->createMock(Store::class);
72+
$store->method('getId')
73+
->willReturn($storeId);
74+
$this->storeManagerInterface->method('getDefaultStoreView')
75+
->willReturn(null);
76+
$this->storeManagerInterface->expects($this->once())
77+
->method('getStores')
78+
->willReturn([$store]);
79+
$this->emulation->expects($this->once())
80+
->method('startEnvironmentEmulation')
81+
->with($storeId);
82+
$this->model->startPreviewMode($callback);
83+
}
84+
}

0 commit comments

Comments
 (0)