Skip to content

Commit

Permalink
Merge pull request #589 from magento-extensibility/develop
Browse files Browse the repository at this point in the history
[Extensibility] Sprint 58 pull request
  • Loading branch information
He, Joan(johe) committed Sep 11, 2015
2 parents 3cf2916 + 39bbde0 commit b51f3e0
Show file tree
Hide file tree
Showing 64 changed files with 674 additions and 419 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/Backup/Model/Fs/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function _hideBackupsForApache()
$filename = '.htaccess';
if (!$this->_varDirectory->isFile($filename)) {
$this->_varDirectory->writeFile($filename, 'deny from all');
$this->_varDirectory->changePermissions($filename, 0644);
$this->_varDirectory->changePermissions($filename, 0640);
}
}

Expand Down
75 changes: 58 additions & 17 deletions app/code/Magento/CacheInvalidate/Model/PurgeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,64 @@
*/
namespace Magento\CacheInvalidate\Model;

use Symfony\Component\Config\Definition\Exception\Exception;
use Zend\Uri\Uri;
use Zend\Http\Client\Adapter\Socket;
use Magento\Framework\Cache\InvalidateLogger;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Framework\App\RequestInterface;

class PurgeCache
{
/**
* @var \Magento\PageCache\Helper\Data
* @var Uri
*/
protected $helper;
protected $uri;

/**
* @var \Magento\Framework\HTTP\Adapter\Curl
* @var Socket
*/
protected $curlAdapter;
protected $socketAdapter;

/**
* @var InvalidateLogger
*/
private $logger;

/**
* @var DeploymentConfig
*/
private $config;

/**
* @var RequestInterface
*/
private $request;

const DEFAULT_PORT = 80;

/**
* Constructor
*
* @param \Magento\PageCache\Helper\Data $helper
* @param \Magento\Framework\HTTP\Adapter\Curl $curlAdapter
* @param Uri $uri
* @param Socket $socketAdapter
* @param InvalidateLogger $logger
* @param Reader $configReader
* @param RequestInterface $request
*/
public function __construct(
\Magento\PageCache\Helper\Data $helper,
\Magento\Framework\HTTP\Adapter\Curl $curlAdapter,
InvalidateLogger $logger
Uri $uri,
Socket $socketAdapter,
InvalidateLogger $logger,
DeploymentConfig $config,
RequestInterface $request
) {
$this->helper = $helper;
$this->curlAdapter = $curlAdapter;
$this->uri = $uri;
$this->socketAdapter = $socketAdapter;
$this->logger = $logger;
$this->config = $config;
$this->request = $request;
}

/**
Expand All @@ -50,12 +74,29 @@ public function __construct(
*/
public function sendPurgeRequest($tagsPattern)
{
$headers = ["X-Magento-Tags-Pattern: {$tagsPattern}"];
$this->curlAdapter->setOptions([CURLOPT_CUSTOMREQUEST => 'PURGE']);
$this->curlAdapter->write('', $this->helper->getUrl('*'), '1.1', $headers);
$this->curlAdapter->read();
$this->curlAdapter->close();
$servers = $this->config->get(ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS)
?: [['host' => $this->request->getHttpHost()]];
$headers = ['X-Magento-Tags-Pattern' => $tagsPattern];
$this->socketAdapter->setOptions(['timeout' => 10]);
foreach ($servers as $server) {
$port = isset($server['port']) ? $server['port'] : self::DEFAULT_PORT;
$this->uri->setScheme('http')
->setHost($server['host'])
->setPort($port);
try {
$this->socketAdapter->connect($server['host'], $port);
$this->socketAdapter->write(
'PURGE',
$this->uri,
'1.1',
$headers
);
$this->socketAdapter->close();
} catch (Exception $e) {
$this->logger->critical($e->getMessage(), compact('server', 'tagsPattern'));
}
}

$this->logger->execute(compact('tagsPattern'));
$this->logger->execute(compact('servers', 'tagsPattern'));
}
}
162 changes: 115 additions & 47 deletions app/code/Magento/CacheInvalidate/Test/Unit/Model/PurgeCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,138 @@
*/
namespace Magento\CacheInvalidate\Test\Unit\Model;

use \Magento\Framework\Config\ConfigOptionsListConstants;

class PurgeCacheTest extends \PHPUnit_Framework_TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\PurgeCache */
protected $model;

/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\HTTP\Adapter\Curl */
protected $curlMock;
/** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Uri\Uri */
protected $uriMock;

/** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Http\Client\Adapter\Socket */
protected $socketAdapterMock;

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

/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Helper\Data */
protected $helperMock;
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\DeploymentConfig\Reader */
protected $configReaderMock;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $logger;
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\RequestInterface */
protected $requestMock;

/**
* Set up all mocks and data for test
*/
public function setUp()
{
$this->helperMock = $this->getMock('Magento\PageCache\Helper\Data', ['getUrl'], [], '', false);
$this->curlMock = $this->getMock(
'\Magento\Framework\HTTP\Adapter\Curl',
['setOptions', 'write', 'read', 'close'],
[],
'',
false
);
$this->logger = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false);
$this->uriMock = $this->getMock('\Zend\Uri\Uri', [], [], '', false);
$this->socketAdapterMock = $this->getMock('\Zend\Http\Client\Adapter\Socket', [], [], '', false);
$this->configMock = $this->getMock('Magento\Framework\App\DeploymentConfig', [], [], '', false);
$this->loggerMock = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false);
$this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
$this->socketAdapterMock->expects($this->once())
->method('setOptions')
->with(['timeout' => 10]);
$this->model = new \Magento\CacheInvalidate\Model\PurgeCache(
$this->helperMock,
$this->curlMock,
$this->logger
$this->uriMock,
$this->socketAdapterMock,
$this->loggerMock,
$this->configMock,
$this->requestMock
);
}

public function testSendPurgeRequest()
public function testSendPurgeRequestEmptyConfig()
{
$tags = 'tags';
$url = 'http://mangento.index.php';
$httpVersion = '1.1';
$headers = ["X-Magento-Tags-Pattern: {$tags}"];
$this->helperMock->expects(
$this->any()
)->method(
'getUrl'
)->with(
$this->equalTo('*'),
[]
)->will(
$this->returnValue($url)
);
$this->curlMock->expects($this->once())->method('setOptions')->with([CURLOPT_CUSTOMREQUEST => 'PURGE']);
$this->curlMock->expects(
$this->once()
)->method(
'write'
)->with(
$this->equalTo(''),
$this->equalTo($url),
$httpVersion,
$this->equalTo($headers)
);
$this->curlMock->expects($this->once())->method('read');
$this->curlMock->expects($this->once())->method('close');
$this->model->sendPurgeRequest($tags);
$this->socketAdapterMock->expects($this->once())
->method('write')
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
$this->socketAdapterMock->expects($this->once())
->method('close');
$this->configMock->expects($this->once())
->method('get')
->willReturn('');
$this->requestMock->expects($this->any())
->method('getHttpHost')
->willReturn('127.0.0.1');
$this->uriMock->expects($this->once())
->method('setScheme')
->with('http')
->willReturnSelf();
$this->uriMock->expects($this->once())
->method('setHost')
->with('127.0.0.1')
->willReturnSelf();
$this->uriMock->expects($this->once())
->method('setPort')
->with(\Magento\CacheInvalidate\Model\PurgeCache::DEFAULT_PORT);
$this->model->sendPurgeRequest('tags');
}

public function testSendPurgeRequestOneServer()
{
$this->socketAdapterMock->expects($this->once())
->method('write')
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
$this->socketAdapterMock->expects($this->once())
->method('close');
$this->configMock->expects($this->once())
->method('get')
->willReturn([['host' => '127.0.0.2', 'port' => 1234]]);
$this->uriMock->expects($this->once())
->method('setScheme')
->with('http')
->willReturnSelf();
$this->uriMock->expects($this->once())
->method('setHost')
->with('127.0.0.2')
->willReturnSelf();
$this->uriMock->expects($this->once())
->method('setPort')
->with(1234);
$this->model->sendPurgeRequest('tags');
}

public function testSendPurgeRequestMultipleServers()
{
$this->socketAdapterMock->expects($this->exactly(2))
->method('write')
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
$this->socketAdapterMock->expects($this->exactly(2))
->method('close');
$this->configMock->expects($this->once())
->method('get')
->willReturn(
[
['host' => '127.0.0.1', 'port' => 8080],
['host' => '127.0.0.2', 'port' => 1234]
]
);
$this->uriMock->expects($this->at(0))
->method('setScheme')
->with('http')
->willReturnSelf();
$this->uriMock->expects($this->at(1))
->method('setHost')
->with('127.0.0.1')
->willReturnSelf();
$this->uriMock->expects($this->at(2))
->method('setPort')
->with(8080);
$this->uriMock->expects($this->at(3))
->method('setScheme')
->with('http')
->willReturnSelf();
$this->uriMock->expects($this->at(4))
->method('setHost')
->with('127.0.0.2')
->willReturnSelf();
$this->uriMock->expects($this->at(5))
->method('setPort')
->with(1234);
$this->model->sendPurgeRequest('tags');
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/Captcha/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\DriverInterface;

/**
* Captcha image model
Expand Down Expand Up @@ -149,7 +150,7 @@ public function getImgDir($website = null)
$mediaDir = $this->_filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$captchaDir = '/captcha/' . $this->_getWebsiteCode($website);
$mediaDir->create($captchaDir);
$mediaDir->changePermissions($captchaDir, 0775);
$mediaDir->changePermissions($captchaDir, DriverInterface::WRITEABLE_DIRECTORY_MODE);

return $mediaDir->getAbsolutePath($captchaDir) . '/';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DriverInterface;

/**
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
Expand Down Expand Up @@ -396,7 +397,7 @@ public function addImage(
$this->_mediaDirectory->copyFile($file, $destinationFile);

$storageHelper->saveFile($this->_mediaConfig->getTmpMediaShortUrl($fileName));
$this->_mediaDirectory->changePermissions($destinationFile, 0777);
$this->_mediaDirectory->changePermissions($destinationFile, DriverInterface::WRITEABLE_FILE_MODE);
}
} catch (\Exception $e) {
throw new LocalizedException(__('We couldn\'t move this file: %1.', $e->getMessage()));
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Cms/Model/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function beforeSave()
*/
public function getIdentities()
{
return [self::CACHE_TAG . '_' . $this->getId()];
return [self::CACHE_TAG . '_' . $this->getId(), self::CACHE_TAG . '_' . $this->getIdentifier()];
}

/**
Expand Down
12 changes: 11 additions & 1 deletion app/code/Magento/Developer/Model/Config/Backend/AllowedIps.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ class AllowedIps extends \Magento\Framework\App\Config\Value
*/
private $messageManager;

/**
* Escaper
*
* @var \Magento\Framework\Escaper
*/
protected $escaper;

/**
* Constructor
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Framework\Message\ManagerInterface $messageManager
* @param \Magento\Framework\Escaper $escaper
* @param \Magento\Framework\Model\Resource\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
Expand All @@ -33,11 +41,13 @@ public function __construct(
\Magento\Framework\Registry $registry,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Framework\Escaper $escaper,
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
) {
$this->messageManager = $messageManager;
$this->escaper = $escaper;
parent::__construct($context, $registry, $config, $resource, $resourceCollection, $data);
}

Expand All @@ -48,7 +58,7 @@ public function __construct(
*/
public function beforeSave()
{
$allowedIpsRaw = $this->getValue();
$allowedIpsRaw = $this->escaper->escapeHtml($this->getValue());
$noticeMsgArray = [];
$allowedIpsArray = [];

Expand Down
Loading

0 comments on commit b51f3e0

Please sign in to comment.