Skip to content

Commit b51f3e0

Browse files
author
He, Joan(johe)
committed
Merge pull request #589 from magento-extensibility/develop
[Extensibility] Sprint 58 pull request
2 parents 3cf2916 + 39bbde0 commit b51f3e0

File tree

64 files changed

+674
-419
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+674
-419
lines changed

app/code/Magento/Backup/Model/Fs/Collection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected function _hideBackupsForApache()
9191
$filename = '.htaccess';
9292
if (!$this->_varDirectory->isFile($filename)) {
9393
$this->_varDirectory->writeFile($filename, 'deny from all');
94-
$this->_varDirectory->changePermissions($filename, 0644);
94+
$this->_varDirectory->changePermissions($filename, 0640);
9595
}
9696
}
9797

app/code/Magento/CacheInvalidate/Model/PurgeCache.php

+58-17
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,64 @@
55
*/
66
namespace Magento\CacheInvalidate\Model;
77

8+
use Symfony\Component\Config\Definition\Exception\Exception;
9+
use Zend\Uri\Uri;
10+
use Zend\Http\Client\Adapter\Socket;
811
use Magento\Framework\Cache\InvalidateLogger;
12+
use Magento\Framework\App\DeploymentConfig;
13+
use Magento\Framework\Config\ConfigOptionsListConstants;
14+
use Magento\Framework\App\RequestInterface;
915

1016
class PurgeCache
1117
{
1218
/**
13-
* @var \Magento\PageCache\Helper\Data
19+
* @var Uri
1420
*/
15-
protected $helper;
21+
protected $uri;
1622

1723
/**
18-
* @var \Magento\Framework\HTTP\Adapter\Curl
24+
* @var Socket
1925
*/
20-
protected $curlAdapter;
26+
protected $socketAdapter;
2127

2228
/**
2329
* @var InvalidateLogger
2430
*/
2531
private $logger;
2632

33+
/**
34+
* @var DeploymentConfig
35+
*/
36+
private $config;
37+
38+
/**
39+
* @var RequestInterface
40+
*/
41+
private $request;
42+
43+
const DEFAULT_PORT = 80;
44+
2745
/**
2846
* Constructor
2947
*
30-
* @param \Magento\PageCache\Helper\Data $helper
31-
* @param \Magento\Framework\HTTP\Adapter\Curl $curlAdapter
48+
* @param Uri $uri
49+
* @param Socket $socketAdapter
3250
* @param InvalidateLogger $logger
51+
* @param Reader $configReader
52+
* @param RequestInterface $request
3353
*/
3454
public function __construct(
35-
\Magento\PageCache\Helper\Data $helper,
36-
\Magento\Framework\HTTP\Adapter\Curl $curlAdapter,
37-
InvalidateLogger $logger
55+
Uri $uri,
56+
Socket $socketAdapter,
57+
InvalidateLogger $logger,
58+
DeploymentConfig $config,
59+
RequestInterface $request
3860
) {
39-
$this->helper = $helper;
40-
$this->curlAdapter = $curlAdapter;
61+
$this->uri = $uri;
62+
$this->socketAdapter = $socketAdapter;
4163
$this->logger = $logger;
64+
$this->config = $config;
65+
$this->request = $request;
4266
}
4367

4468
/**
@@ -50,12 +74,29 @@ public function __construct(
5074
*/
5175
public function sendPurgeRequest($tagsPattern)
5276
{
53-
$headers = ["X-Magento-Tags-Pattern: {$tagsPattern}"];
54-
$this->curlAdapter->setOptions([CURLOPT_CUSTOMREQUEST => 'PURGE']);
55-
$this->curlAdapter->write('', $this->helper->getUrl('*'), '1.1', $headers);
56-
$this->curlAdapter->read();
57-
$this->curlAdapter->close();
77+
$servers = $this->config->get(ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS)
78+
?: [['host' => $this->request->getHttpHost()]];
79+
$headers = ['X-Magento-Tags-Pattern' => $tagsPattern];
80+
$this->socketAdapter->setOptions(['timeout' => 10]);
81+
foreach ($servers as $server) {
82+
$port = isset($server['port']) ? $server['port'] : self::DEFAULT_PORT;
83+
$this->uri->setScheme('http')
84+
->setHost($server['host'])
85+
->setPort($port);
86+
try {
87+
$this->socketAdapter->connect($server['host'], $port);
88+
$this->socketAdapter->write(
89+
'PURGE',
90+
$this->uri,
91+
'1.1',
92+
$headers
93+
);
94+
$this->socketAdapter->close();
95+
} catch (Exception $e) {
96+
$this->logger->critical($e->getMessage(), compact('server', 'tagsPattern'));
97+
}
98+
}
5899

59-
$this->logger->execute(compact('tagsPattern'));
100+
$this->logger->execute(compact('servers', 'tagsPattern'));
60101
}
61102
}

app/code/Magento/CacheInvalidate/Test/Unit/Model/PurgeCacheTest.php

+115-47
Original file line numberDiff line numberDiff line change
@@ -5,70 +5,138 @@
55
*/
66
namespace Magento\CacheInvalidate\Test\Unit\Model;
77

8+
use \Magento\Framework\Config\ConfigOptionsListConstants;
9+
810
class PurgeCacheTest extends \PHPUnit_Framework_TestCase
911
{
1012
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\PurgeCache */
1113
protected $model;
1214

13-
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\HTTP\Adapter\Curl */
14-
protected $curlMock;
15+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Uri\Uri */
16+
protected $uriMock;
17+
18+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Http\Client\Adapter\Socket */
19+
protected $socketAdapterMock;
20+
21+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Cache\InvalidateLogger */
22+
protected $loggerMock;
1523

16-
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Helper\Data */
17-
protected $helperMock;
24+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\DeploymentConfig\Reader */
25+
protected $configReaderMock;
1826

19-
/** @var \PHPUnit_Framework_MockObject_MockObject */
20-
protected $logger;
27+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\RequestInterface */
28+
protected $requestMock;
2129

2230
/**
2331
* Set up all mocks and data for test
2432
*/
2533
public function setUp()
2634
{
27-
$this->helperMock = $this->getMock('Magento\PageCache\Helper\Data', ['getUrl'], [], '', false);
28-
$this->curlMock = $this->getMock(
29-
'\Magento\Framework\HTTP\Adapter\Curl',
30-
['setOptions', 'write', 'read', 'close'],
31-
[],
32-
'',
33-
false
34-
);
35-
$this->logger = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false);
35+
$this->uriMock = $this->getMock('\Zend\Uri\Uri', [], [], '', false);
36+
$this->socketAdapterMock = $this->getMock('\Zend\Http\Client\Adapter\Socket', [], [], '', false);
37+
$this->configMock = $this->getMock('Magento\Framework\App\DeploymentConfig', [], [], '', false);
38+
$this->loggerMock = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false);
39+
$this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
40+
$this->socketAdapterMock->expects($this->once())
41+
->method('setOptions')
42+
->with(['timeout' => 10]);
3643
$this->model = new \Magento\CacheInvalidate\Model\PurgeCache(
37-
$this->helperMock,
38-
$this->curlMock,
39-
$this->logger
44+
$this->uriMock,
45+
$this->socketAdapterMock,
46+
$this->loggerMock,
47+
$this->configMock,
48+
$this->requestMock
4049
);
4150
}
4251

43-
public function testSendPurgeRequest()
52+
public function testSendPurgeRequestEmptyConfig()
4453
{
45-
$tags = 'tags';
46-
$url = 'http://mangento.index.php';
47-
$httpVersion = '1.1';
48-
$headers = ["X-Magento-Tags-Pattern: {$tags}"];
49-
$this->helperMock->expects(
50-
$this->any()
51-
)->method(
52-
'getUrl'
53-
)->with(
54-
$this->equalTo('*'),
55-
[]
56-
)->will(
57-
$this->returnValue($url)
58-
);
59-
$this->curlMock->expects($this->once())->method('setOptions')->with([CURLOPT_CUSTOMREQUEST => 'PURGE']);
60-
$this->curlMock->expects(
61-
$this->once()
62-
)->method(
63-
'write'
64-
)->with(
65-
$this->equalTo(''),
66-
$this->equalTo($url),
67-
$httpVersion,
68-
$this->equalTo($headers)
69-
);
70-
$this->curlMock->expects($this->once())->method('read');
71-
$this->curlMock->expects($this->once())->method('close');
72-
$this->model->sendPurgeRequest($tags);
54+
$this->socketAdapterMock->expects($this->once())
55+
->method('write')
56+
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
57+
$this->socketAdapterMock->expects($this->once())
58+
->method('close');
59+
$this->configMock->expects($this->once())
60+
->method('get')
61+
->willReturn('');
62+
$this->requestMock->expects($this->any())
63+
->method('getHttpHost')
64+
->willReturn('127.0.0.1');
65+
$this->uriMock->expects($this->once())
66+
->method('setScheme')
67+
->with('http')
68+
->willReturnSelf();
69+
$this->uriMock->expects($this->once())
70+
->method('setHost')
71+
->with('127.0.0.1')
72+
->willReturnSelf();
73+
$this->uriMock->expects($this->once())
74+
->method('setPort')
75+
->with(\Magento\CacheInvalidate\Model\PurgeCache::DEFAULT_PORT);
76+
$this->model->sendPurgeRequest('tags');
77+
}
78+
79+
public function testSendPurgeRequestOneServer()
80+
{
81+
$this->socketAdapterMock->expects($this->once())
82+
->method('write')
83+
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
84+
$this->socketAdapterMock->expects($this->once())
85+
->method('close');
86+
$this->configMock->expects($this->once())
87+
->method('get')
88+
->willReturn([['host' => '127.0.0.2', 'port' => 1234]]);
89+
$this->uriMock->expects($this->once())
90+
->method('setScheme')
91+
->with('http')
92+
->willReturnSelf();
93+
$this->uriMock->expects($this->once())
94+
->method('setHost')
95+
->with('127.0.0.2')
96+
->willReturnSelf();
97+
$this->uriMock->expects($this->once())
98+
->method('setPort')
99+
->with(1234);
100+
$this->model->sendPurgeRequest('tags');
101+
}
102+
103+
public function testSendPurgeRequestMultipleServers()
104+
{
105+
$this->socketAdapterMock->expects($this->exactly(2))
106+
->method('write')
107+
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
108+
$this->socketAdapterMock->expects($this->exactly(2))
109+
->method('close');
110+
$this->configMock->expects($this->once())
111+
->method('get')
112+
->willReturn(
113+
[
114+
['host' => '127.0.0.1', 'port' => 8080],
115+
['host' => '127.0.0.2', 'port' => 1234]
116+
]
117+
);
118+
$this->uriMock->expects($this->at(0))
119+
->method('setScheme')
120+
->with('http')
121+
->willReturnSelf();
122+
$this->uriMock->expects($this->at(1))
123+
->method('setHost')
124+
->with('127.0.0.1')
125+
->willReturnSelf();
126+
$this->uriMock->expects($this->at(2))
127+
->method('setPort')
128+
->with(8080);
129+
$this->uriMock->expects($this->at(3))
130+
->method('setScheme')
131+
->with('http')
132+
->willReturnSelf();
133+
$this->uriMock->expects($this->at(4))
134+
->method('setHost')
135+
->with('127.0.0.2')
136+
->willReturnSelf();
137+
$this->uriMock->expects($this->at(5))
138+
->method('setPort')
139+
->with(1234);
140+
$this->model->sendPurgeRequest('tags');
73141
}
74142
}

app/code/Magento/Captcha/Helper/Data.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\Framework\Filesystem;
10+
use Magento\Framework\Filesystem\DriverInterface;
1011

1112
/**
1213
* Captcha image model
@@ -149,7 +150,7 @@ public function getImgDir($website = null)
149150
$mediaDir = $this->_filesystem->getDirectoryWrite(DirectoryList::MEDIA);
150151
$captchaDir = '/captcha/' . $this->_getWebsiteCode($website);
151152
$mediaDir->create($captchaDir);
152-
$mediaDir->changePermissions($captchaDir, 0775);
153+
$mediaDir->changePermissions($captchaDir, DriverInterface::WRITEABLE_DIRECTORY_MODE);
153154

154155
return $mediaDir->getAbsolutePath($captchaDir) . '/';
155156
}

app/code/Magento/Catalog/Model/Product/Attribute/Backend/Media.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Magento\Framework\App\Filesystem\DirectoryList;
1515
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Filesystem\DriverInterface;
1617

1718
/**
1819
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
@@ -396,7 +397,7 @@ public function addImage(
396397
$this->_mediaDirectory->copyFile($file, $destinationFile);
397398

398399
$storageHelper->saveFile($this->_mediaConfig->getTmpMediaShortUrl($fileName));
399-
$this->_mediaDirectory->changePermissions($destinationFile, 0777);
400+
$this->_mediaDirectory->changePermissions($destinationFile, DriverInterface::WRITEABLE_FILE_MODE);
400401
}
401402
} catch (\Exception $e) {
402403
throw new LocalizedException(__('We couldn\'t move this file: %1.', $e->getMessage()));

app/code/Magento/Cms/Model/Block.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function beforeSave()
6565
*/
6666
public function getIdentities()
6767
{
68-
return [self::CACHE_TAG . '_' . $this->getId()];
68+
return [self::CACHE_TAG . '_' . $this->getId(), self::CACHE_TAG . '_' . $this->getIdentifier()];
6969
}
7070

7171
/**

app/code/Magento/Developer/Model/Config/Backend/AllowedIps.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,21 @@ class AllowedIps extends \Magento\Framework\App\Config\Value
1717
*/
1818
private $messageManager;
1919

20+
/**
21+
* Escaper
22+
*
23+
* @var \Magento\Framework\Escaper
24+
*/
25+
protected $escaper;
26+
2027
/**
2128
* Constructor
2229
*
2330
* @param \Magento\Framework\Model\Context $context
2431
* @param \Magento\Framework\Registry $registry
2532
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
2633
* @param \Magento\Framework\Message\ManagerInterface $messageManager
34+
* @param \Magento\Framework\Escaper $escaper
2735
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
2836
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
2937
* @param array $data
@@ -33,11 +41,13 @@ public function __construct(
3341
\Magento\Framework\Registry $registry,
3442
\Magento\Framework\App\Config\ScopeConfigInterface $config,
3543
\Magento\Framework\Message\ManagerInterface $messageManager,
44+
\Magento\Framework\Escaper $escaper,
3645
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
3746
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
3847
array $data = []
3948
) {
4049
$this->messageManager = $messageManager;
50+
$this->escaper = $escaper;
4151
parent::__construct($context, $registry, $config, $resource, $resourceCollection, $data);
4252
}
4353

@@ -48,7 +58,7 @@ public function __construct(
4858
*/
4959
public function beforeSave()
5060
{
51-
$allowedIpsRaw = $this->getValue();
61+
$allowedIpsRaw = $this->escaper->escapeHtml($this->getValue());
5262
$noticeMsgArray = [];
5363
$allowedIpsArray = [];
5464

0 commit comments

Comments
 (0)