Skip to content

Commit 5a3d628

Browse files
author
Bohdan Korablov
committed
MAGETWO-89315: Magento application compatible with cloud
1 parent cec4bf4 commit 5a3d628

File tree

2 files changed

+114
-5
lines changed

2 files changed

+114
-5
lines changed

lib/internal/Magento/Framework/Setup/FilePermissions.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Magento\Framework\Backup\Filesystem\Iterator\Filter;
1010
use Magento\Framework\Filesystem;
1111
use Magento\Framework\Filesystem\Filter\ExcludeFilter;
12+
use Magento\Framework\App\State;
13+
use Magento\Framework\App\ObjectManager;
1214

1315
/**
1416
* Checks permissions to files and folders.
@@ -25,6 +27,11 @@ class FilePermissions
2527
*/
2628
protected $directoryList;
2729

30+
/**
31+
* @var State
32+
*/
33+
private $state;
34+
2835
/**
2936
* List of required writable directories for installation
3037
*
@@ -66,10 +73,12 @@ class FilePermissions
6673
*/
6774
public function __construct(
6875
Filesystem $filesystem,
69-
DirectoryList $directoryList
76+
DirectoryList $directoryList,
77+
State $state = null
7078
) {
7179
$this->filesystem = $filesystem;
7280
$this->directoryList = $directoryList;
81+
$this->state = $state ?:ObjectManager::getInstance()->get(State::class);
7382
}
7483

7584
/**
@@ -85,8 +94,10 @@ public function getInstallationWritableDirectories()
8594
DirectoryList::VAR_DIR,
8695
DirectoryList::MEDIA,
8796
DirectoryList::STATIC_VIEW,
88-
DirectoryList::GENERATED,
8997
];
98+
if ($this->state->getMode() !== State::MODE_PRODUCTION) {
99+
$data[] = DirectoryList::GENERATED;
100+
}
90101
foreach ($data as $code) {
91102
$this->installationWritableDirectories[$code] = $this->directoryList->getPath($code);
92103
}

lib/internal/Magento/Framework/Setup/Test/Unit/FilePermissionsTest.php

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use \Magento\Framework\Setup\FilePermissions;
99
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\App\State;
11+
use function Magento\NonComposerComponentRegistration\main;
1012

1113
class FilePermissionsTest extends \PHPUnit\Framework\TestCase
1214
{
@@ -25,6 +27,11 @@ class FilePermissionsTest extends \PHPUnit\Framework\TestCase
2527
*/
2628
private $directoryListMock;
2729

30+
/**
31+
* @var \PHPUnit_Framework_MockObject_MockObject|State
32+
*/
33+
private $stateMock;
34+
2835
/**
2936
* @var FilePermissions
3037
*/
@@ -34,6 +41,7 @@ public function setUp()
3441
{
3542
$this->directoryWriteMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
3643
$this->filesystemMock = $this->createMock(\Magento\Framework\Filesystem::class);
44+
$this->stateMock = $this->createMock(State::class);
3745

3846
$this->filesystemMock
3947
->expects($this->any())
@@ -43,13 +51,21 @@ public function setUp()
4351

4452
$this->filePermissions = new FilePermissions(
4553
$this->filesystemMock,
46-
$this->directoryListMock
54+
$this->directoryListMock,
55+
$this->stateMock
4756
);
4857
}
4958

50-
public function testGetInstallationWritableDirectories()
59+
/**
60+
* @param string $mageMode
61+
* @dataProvider modeDataProvider
62+
*/
63+
public function testGetInstallationWritableDirectories($mageMode)
5164
{
5265
$this->setUpDirectoryListInstallation();
66+
$this->stateMock->expects($this->once())
67+
->method('getMode')
68+
->willReturn($mageMode);
5369

5470
$expected = [
5571
BP . '/app/etc',
@@ -62,6 +78,23 @@ public function testGetInstallationWritableDirectories()
6278
$this->assertEquals($expected, $this->filePermissions->getInstallationWritableDirectories());
6379
}
6480

81+
public function testGetInstallationWritableDirectoriesInProduction()
82+
{
83+
$this->setUpDirectoryListInstallationInProduction();
84+
$this->stateMock->expects($this->once())
85+
->method('getMode')
86+
->willReturn(State::MODE_PRODUCTION);
87+
88+
$expected = [
89+
BP . '/app/etc',
90+
BP . '/var',
91+
BP . '/pub/media',
92+
BP . '/pub/static'
93+
];
94+
95+
$this->assertEquals($expected, $this->filePermissions->getInstallationWritableDirectories());
96+
}
97+
6598
public function testGetApplicationNonWritableDirectories()
6699
{
67100
$this->directoryListMock
@@ -134,13 +167,18 @@ public function getApplicationCurrentNonWritableDirectoriesDataProvider()
134167
}
135168

136169
/**
170+
* @param string $mageMode
171+
* @dataProvider modeDataProvider
137172
* @covers \Magento\Framework\Setup\FilePermissions::getMissingWritableDirectoriesForInstallation
138173
* @covers \Magento\Framework\Setup\FilePermissions::getMissingWritablePathsForInstallation
139174
*/
140-
public function testGetMissingWritableDirectoriesAndPathsForInstallation()
175+
public function testGetMissingWritableDirectoriesAndPathsForInstallation($mageMode)
141176
{
142177
$this->setUpDirectoryListInstallation();
143178
$this->setUpDirectoryWriteInstallation();
179+
$this->stateMock->expects($this->once())
180+
->method('getMode')
181+
->willReturn($mageMode);
144182

145183
$expected = [
146184
BP . '/var',
@@ -160,6 +198,31 @@ public function testGetMissingWritableDirectoriesAndPathsForInstallation()
160198
);
161199
}
162200

201+
public function testGetMissingWritableDirectoriesAndPathsForInstallationInProduction()
202+
{
203+
$this->setUpDirectoryListInstallationInProduction();
204+
$this->setUpDirectoryWriteInstallation();
205+
$this->stateMock->expects($this->once())
206+
->method('getMode')
207+
->willReturn(State::MODE_PRODUCTION);
208+
209+
$expected = [
210+
BP . '/var',
211+
BP . '/pub/media',
212+
BP . '/pub/static'
213+
];
214+
215+
$this->assertEquals(
216+
$expected,
217+
array_values($this->filePermissions->getMissingWritableDirectoriesForInstallation())
218+
);
219+
220+
$this->assertEquals(
221+
$expected,
222+
array_values($this->filePermissions->getMissingWritablePathsForInstallation())
223+
);
224+
}
225+
163226
public function testGetMissingWritableDirectoriesForDbUpgrade()
164227
{
165228
$directoryMethods = ['isExist', 'isDirectory', 'isReadable', 'isWritable'];
@@ -240,6 +303,30 @@ public function setUpDirectoryListInstallation()
240303
->will($this->returnValue(BP . '/generated'));
241304
}
242305

306+
public function setUpDirectoryListInstallationInProduction()
307+
{
308+
$this->directoryListMock
309+
->expects($this->at(0))
310+
->method('getPath')
311+
->with(DirectoryList::CONFIG)
312+
->will($this->returnValue(BP . '/app/etc'));
313+
$this->directoryListMock
314+
->expects($this->at(1))
315+
->method('getPath')
316+
->with(DirectoryList::VAR_DIR)
317+
->will($this->returnValue(BP . '/var'));
318+
$this->directoryListMock
319+
->expects($this->at(2))
320+
->method('getPath')
321+
->with(DirectoryList::MEDIA)
322+
->will($this->returnValue(BP . '/pub/media'));
323+
$this->directoryListMock
324+
->expects($this->at(3))
325+
->method('getPath')
326+
->with(DirectoryList::STATIC_VIEW)
327+
->will($this->returnValue(BP . '/pub/static'));
328+
}
329+
243330
public function setUpDirectoryWriteInstallation()
244331
{
245332
// CONFIG
@@ -294,4 +381,15 @@ public function setUpDirectoryWriteInstallation()
294381
->method('isWritable')
295382
->will($this->returnValue(false));
296383
}
384+
385+
/**
386+
* @return array
387+
*/
388+
public function modeDataProvider()
389+
{
390+
return [
391+
[State::MODE_DEFAULT],
392+
[State::MODE_DEVELOPER],
393+
];
394+
}
297395
}

0 commit comments

Comments
 (0)