Skip to content

Commit 168d27c

Browse files
author
He, Joan(johe)
committed
Merge pull request #689 from magento-extensibility/MAGETWO-43220-zf1
[Extensibility] MAGETWO-38380 and bug fixes
2 parents 4517819 + 0c64ee6 commit 168d27c

File tree

45 files changed

+1184
-390
lines changed

Some content is hidden

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

45 files changed

+1184
-390
lines changed

.htaccess

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,10 @@
252252
## http://developer.yahoo.com/performance/rules.html#etags
253253

254254
#FileETag none
255+
256+
############################################
257+
## Add custom headers
258+
<IfModule mod_headers.c>
259+
Header set X-Content-Type-Options "nosniff"
260+
Header set X-XSS-Protection "1; mode=block"
261+
</IfModule>

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

Lines changed: 19 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
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;
118
use Magento\Framework\Cache\InvalidateLogger;
129
use Magento\Framework\App\DeploymentConfig;
13-
use Magento\Framework\Config\ConfigOptionsListConstants;
14-
use Magento\Framework\App\RequestInterface;
1510

1611
class PurgeCache
1712
{
13+
const HEADER_X_MAGENTO_TAGS_PATTERN = 'X-Magento-Tags-Pattern';
14+
1815
/**
19-
* @var UriFactory
16+
* @var \Magento\PageCache\Model\Cache\Server
2017
*/
21-
protected $uriFactory;
18+
protected $cacheServer;
2219

2320
/**
24-
* @var SocketFactory
21+
* @var \Magento\CacheInvalidate\Model\SocketFactory
2522
*/
2623
protected $socketAdapterFactory;
2724

@@ -30,75 +27,53 @@ class PurgeCache
3027
*/
3128
private $logger;
3229

33-
/**
34-
* @var DeploymentConfig
35-
*/
36-
private $config;
37-
38-
/**
39-
* @var RequestInterface
40-
*/
41-
private $request;
42-
43-
const DEFAULT_PORT = 80;
44-
4530
/**
4631
* Constructor
4732
*
48-
* @param UriFactory $uriFactory
49-
* @param SocketFactory $socketAdapterFactory
33+
* @param \Magento\PageCache\Model\Cache\Server $cacheServer
34+
* @param \Magento\CacheInvalidate\Model\SocketFactory $socketAdapterFactory
5035
* @param InvalidateLogger $logger
51-
* @param Reader $configReader
52-
* @param RequestInterface $request
5336
*/
5437
public function __construct(
55-
UriFactory $uriFactory,
56-
SocketFactory $socketAdapterFactory,
57-
InvalidateLogger $logger,
58-
DeploymentConfig $config,
59-
RequestInterface $request
38+
\Magento\PageCache\Model\Cache\Server $cacheServer,
39+
\Magento\CacheInvalidate\Model\SocketFactory $socketAdapterFactory,
40+
InvalidateLogger $logger
6041
) {
61-
$this->uriFactory = $uriFactory;
42+
$this->cacheServer = $cacheServer;
6243
$this->socketAdapterFactory = $socketAdapterFactory;
6344
$this->logger = $logger;
64-
$this->config = $config;
65-
$this->request = $request;
6645
}
6746

6847
/**
6948
* Send curl purge request
7049
* to invalidate cache by tags pattern
7150
*
7251
* @param string $tagsPattern
73-
* @return void
52+
* @return bool Return true if successful; otherwise return false
7453
*/
7554
public function sendPurgeRequest($tagsPattern)
7655
{
77-
$uri = $this->uriFactory->create();
7856
$socketAdapter = $this->socketAdapterFactory->create();
79-
$servers = $this->config->get(ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS)
80-
?: [['host' => $this->request->getHttpHost()]];
81-
$headers = ['X-Magento-Tags-Pattern' => $tagsPattern];
57+
$servers = $this->cacheServer->getUris();
58+
$headers = [self::HEADER_X_MAGENTO_TAGS_PATTERN => $tagsPattern];
8259
$socketAdapter->setOptions(['timeout' => 10]);
8360
foreach ($servers as $server) {
84-
$port = isset($server['port']) ? $server['port'] : self::DEFAULT_PORT;
85-
$uri->setScheme('http')
86-
->setHost($server['host'])
87-
->setPort($port);
8861
try {
89-
$socketAdapter->connect($server['host'], $port);
62+
$socketAdapter->connect($server->getHost(), $server->getPort());
9063
$socketAdapter->write(
9164
'PURGE',
92-
$uri,
65+
$server,
9366
'1.1',
9467
$headers
9568
);
9669
$socketAdapter->close();
97-
} catch (Exception $e) {
70+
} catch (\Exception $e) {
9871
$this->logger->critical($e->getMessage(), compact('server', 'tagsPattern'));
72+
return false;
9973
}
10074
}
10175

10276
$this->logger->execute(compact('servers', 'tagsPattern'));
77+
return true;
10378
}
10479
}

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 79 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -5,144 +5,115 @@
55
*/
66
namespace Magento\CacheInvalidate\Test\Unit\Model;
77

8+
use \Zend\Uri\UriFactory;
9+
810
class PurgeCacheTest extends \PHPUnit_Framework_TestCase
911
{
10-
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\PurgeCache */
12+
/** @var \Magento\CacheInvalidate\Model\PurgeCache */
1113
protected $model;
1214

13-
/** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Uri\Uri */
14-
protected $uriMock;
15-
1615
/** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Http\Client\Adapter\Socket */
1716
protected $socketAdapterMock;
1817

1918
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Cache\InvalidateLogger */
2019
protected $loggerMock;
2120

22-
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\DeploymentConfig\Reader */
23-
protected $configReaderMock;
21+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Model\Cache\Server */
22+
protected $cacheServer;
2423

25-
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\RequestInterface */
26-
protected $requestMock;
27-
28-
/**
29-
* Set up all mocks and data for test
30-
*/
3124
public function setUp()
3225
{
33-
$this->uriFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\UriFactory', [], [], '', false);
34-
$this->uriMock = $this->getMock('\Zend\Uri\Uri', [], [], '', false);
35-
$this->socketFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\SocketFactory', [], [], '', false);
26+
$socketFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\SocketFactory', [], [], '', false);
3627
$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);
4028
$this->socketAdapterMock->expects($this->once())
4129
->method('setOptions')
4230
->with(['timeout' => 10]);
43-
$this->uriFactoryMock->expects($this->once())
44-
->method('create')
45-
->willReturn($this->uriMock);
46-
$this->socketFactoryMock->expects($this->once())
31+
$socketFactoryMock->expects($this->once())
4732
->method('create')
4833
->willReturn($this->socketAdapterMock);
49-
$this->model = new \Magento\CacheInvalidate\Model\PurgeCache(
50-
$this->uriFactoryMock,
51-
$this->socketFactoryMock,
52-
$this->loggerMock,
53-
$this->configMock,
54-
$this->requestMock
34+
35+
$this->loggerMock = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false);
36+
$this->cacheServer = $this->getMock('Magento\PageCache\Model\Cache\Server', [], [], '', false);
37+
38+
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
39+
$this->model = $objectManager->getObject(
40+
'Magento\CacheInvalidate\Model\PurgeCache',
41+
[
42+
'cacheServer' => $this->cacheServer,
43+
'socketAdapterFactory' => $socketFactoryMock,
44+
'logger' => $this->loggerMock,
45+
]
5546
);
5647
}
5748

58-
public function testSendPurgeRequestEmptyConfig()
49+
/**
50+
* @param string[] $hosts
51+
* @dataProvider sendPurgeRequestDataProvider
52+
*/
53+
public function testSendPurgeRequest($hosts)
5954
{
60-
$this->socketAdapterMock->expects($this->once())
61-
->method('write')
62-
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
63-
$this->socketAdapterMock->expects($this->once())
64-
->method('close');
65-
$this->configMock->expects($this->once())
66-
->method('get')
67-
->willReturn('');
68-
$this->requestMock->expects($this->any())
69-
->method('getHttpHost')
70-
->willReturn('127.0.0.1');
71-
$this->uriMock->expects($this->once())
72-
->method('setScheme')
73-
->with('http')
74-
->willReturnSelf();
75-
$this->uriMock->expects($this->once())
76-
->method('setHost')
77-
->with('127.0.0.1')
78-
->willReturnSelf();
79-
$this->uriMock->expects($this->once())
80-
->method('setPort')
81-
->with(\Magento\CacheInvalidate\Model\PurgeCache::DEFAULT_PORT);
82-
$this->model->sendPurgeRequest('tags');
83-
}
55+
$uris = [];
56+
foreach ($hosts as $host) {
57+
$port = isset($host['port']) ? $host['port'] : \Magento\PageCache\Model\Cache\Server::DEFAULT_PORT;
58+
$uris[] = UriFactory::factory('')->setHost($host['host'])
59+
->setPort($port)
60+
->setScheme('http');
61+
}
62+
$this->cacheServer->expects($this->once())
63+
->method('getUris')
64+
->willReturn($uris);
8465

85-
public function testSendPurgeRequestOneServer()
86-
{
87-
$this->socketAdapterMock->expects($this->once())
88-
->method('write')
89-
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
90-
$this->socketAdapterMock->expects($this->once())
66+
$i = 1;
67+
foreach ($uris as $uri) {
68+
$this->socketAdapterMock->expects($this->at($i++))
69+
->method('connect')
70+
->with($uri->getHost(), $uri->getPort());
71+
$this->socketAdapterMock->expects($this->at($i++))
72+
->method('write')
73+
->with('PURGE', $uri, '1.1', ['X-Magento-Tags-Pattern' => 'tags']);
74+
$i++;
75+
}
76+
$this->socketAdapterMock->expects($this->exactly(count($uris)))
9177
->method('close');
92-
$this->configMock->expects($this->once())
93-
->method('get')
94-
->willReturn([['host' => '127.0.0.2', 'port' => 1234]]);
95-
$this->uriMock->expects($this->once())
96-
->method('setScheme')
97-
->with('http')
98-
->willReturnSelf();
99-
$this->uriMock->expects($this->once())
100-
->method('setHost')
101-
->with('127.0.0.2')
102-
->willReturnSelf();
103-
$this->uriMock->expects($this->once())
104-
->method('setPort')
105-
->with(1234);
106-
$this->model->sendPurgeRequest('tags');
78+
79+
$this->loggerMock->expects($this->once())
80+
->method('execute');
81+
82+
$this->assertTrue($this->model->sendPurgeRequest('tags'));
10783
}
10884

109-
public function testSendPurgeRequestMultipleServers()
85+
public function sendPurgeRequestDataProvider()
11086
{
111-
$this->socketAdapterMock->expects($this->exactly(2))
112-
->method('write')
113-
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
114-
$this->socketAdapterMock->expects($this->exactly(2))
115-
->method('close');
116-
$this->configMock->expects($this->once())
117-
->method('get')
118-
->willReturn(
87+
return [
88+
[
89+
[['host' => '127.0.0.1', 'port' => 8080],]
90+
],
91+
[
11992
[
12093
['host' => '127.0.0.1', 'port' => 8080],
121-
['host' => '127.0.0.2', 'port' => 1234]
94+
['host' => '127.0.0.2', 'port' => 1234],
95+
['host' => 'host']
12296
]
123-
);
124-
$this->uriMock->expects($this->at(0))
125-
->method('setScheme')
126-
->with('http')
127-
->willReturnSelf();
128-
$this->uriMock->expects($this->at(1))
129-
->method('setHost')
130-
->with('127.0.0.1')
131-
->willReturnSelf();
132-
$this->uriMock->expects($this->at(2))
133-
->method('setPort')
134-
->with(8080);
135-
$this->uriMock->expects($this->at(3))
136-
->method('setScheme')
137-
->with('http')
138-
->willReturnSelf();
139-
$this->uriMock->expects($this->at(4))
140-
->method('setHost')
141-
->with('127.0.0.2')
142-
->willReturnSelf();
143-
$this->uriMock->expects($this->at(5))
144-
->method('setPort')
145-
->with(1234);
146-
$this->model->sendPurgeRequest('tags');
97+
]
98+
];
99+
}
100+
101+
public function testSendPurgeRequestWithException()
102+
{
103+
$uris[] = UriFactory::factory('')->setHost('127.0.0.1')
104+
->setPort(8080)
105+
->setScheme('http');
106+
107+
$this->cacheServer->expects($this->once())
108+
->method('getUris')
109+
->willReturn($uris);
110+
$this->socketAdapterMock->method('connect')
111+
->willThrowException(new \Zend\Http\Client\Adapter\Exception\RuntimeException());
112+
$this->loggerMock->expects($this->never())
113+
->method('execute');
114+
$this->loggerMock->expects($this->once())
115+
->method('critical');
116+
117+
$this->assertFalse($this->model->sendPurgeRequest('tags'));
147118
}
148119
}

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

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/code/Magento/Integration/Model/Resource/Oauth/Consumer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function getTimeInSecondsSinceCreation($consumerId)
7474
$select = $connection->select()
7575
->from($this->getMainTable())
7676
->reset(\Magento\Framework\DB\Select::COLUMNS)
77-
->columns('CURRENT_TIMESTAMP() - created_at')
77+
->columns(new \Zend_Db_Expr('CURRENT_TIMESTAMP() - created_at'))
7878
->where('entity_id = ?', $consumerId);
7979

8080
return $connection->fetchOne($select);

0 commit comments

Comments
 (0)