Skip to content

Commit

Permalink
Merge pull request #1 from lizardmedia/bugfix/url-encoding-fix
Browse files Browse the repository at this point in the history
url encoding fix
  • Loading branch information
Bartosz Kubicki authored Apr 28, 2020
2 parents bb7ae71 + 3a301ae commit 75a3f7b
Show file tree
Hide file tree
Showing 14 changed files with 5,621 additions and 1,275 deletions.
2 changes: 1 addition & 1 deletion Api/Rewrite/GeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
interface GeneratorInterface
{
/**
* @param RewriteInterface[] ...$rewrites
* @param RewriteInterface ...$rewrites
* @return void
*/
public function generateVerificationFileRewrite(RewriteInterface ...$rewrites);
Expand Down
5 changes: 4 additions & 1 deletion Api/Rewrite/RewriteDataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
*/
interface RewriteDataInterface
{
const GENERATED_REWRITE_DATA = [
/**
* @var array
*/
public const GENERATED_REWRITE_DATA = [
'redirect_type' => 0,
'entity_type' => 'custom',
'is_autogenerated' => '0',
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### 1.0.1 ###
* fix for passing filepath through URL param
12 changes: 8 additions & 4 deletions Controller/File/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\Result\Raw;
use Magento\Framework\Controller\Result\RawFactory;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\ResultFactory;

/**
Expand Down Expand Up @@ -53,9 +54,9 @@ public function __construct(
}

/**
* @return Raw
* @return Raw|Redirect
*/
public function execute(): Raw
public function execute()
{
try {
$verificationFileContent = $this->getVerificationFileContent();
Expand All @@ -64,7 +65,8 @@ public function execute(): Raw
$result->setContents($verificationFileContent);
return $result;
} catch (VerificationFileNotFoundException $e) {
$this->messageManager->addError($e->getMessage());
$this->messageManager->addErrorMessage($e->getMessage());
/** @var Redirect $result */
$result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$result->setUrl('/');
return $result;
Expand All @@ -77,13 +79,15 @@ public function execute(): Raw
*/
private function getVerificationFileContent(): string
{
$requestedFile = $this->getRequest()->getParam('request_path');
$requestedFile = urldecode($this->getRequest()->getParam('request_path'));
$rewrites = $this->configProvider->getRewritesDataArray();

foreach ($rewrites as $rewrite) {
if ($requestedFile === $rewrite->getFileName()) {
return $rewrite->getFileContent();
}
}

throw new VerificationFileNotFoundException(__('Requested verification file was not found'));
}
}
11 changes: 9 additions & 2 deletions Model/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,15 @@
*/
class ConfigProvider implements ConfigProviderInterface
{
const XML_PATH_VERIFICATION_CODE = 'lizardmedia_google_analytics_verifier/head/verification_code';
const XML_PATH_VERIFICATION_FILES = 'lizardmedia_google_analytics_verifier/file/files';
/**
* @var string
*/
public const XML_PATH_VERIFICATION_CODE = 'lizardmedia_google_analytics_verifier/head/verification_code';

/**
* @var string
*/
public const XML_PATH_VERIFICATION_FILES = 'lizardmedia_google_analytics_verifier/file/files';

/**
* @var ScopeConfigInterface
Expand Down
14 changes: 8 additions & 6 deletions Model/Rewrite/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

namespace LizardMedia\GoogleAnalyticsVerifier\Model\Rewrite;

use Exception;
use LizardMedia\GoogleAnalyticsVerifier\Api\Data\RewriteInterface;
use LizardMedia\GoogleAnalyticsVerifier\Api\Rewrite\GeneratorInterface;
use LizardMedia\GoogleAnalyticsVerifier\Api\Rewrite\RewriteDataInterface;
use LizardMedia\GoogleAnalyticsVerifier\Controller\File\Index;
use Magento\Framework\Exception\AlreadyExistsException;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\UrlRewrite\Model\ResourceModel\UrlRewrite as UrlRewriteResource;
Expand Down Expand Up @@ -58,9 +60,9 @@ public function __construct(
}

/**
* @param RewriteInterface[] ...$rewrites
* @throws \Exception
* @throws \Magento\Framework\Exception\AlreadyExistsException
* @param RewriteInterface ...$rewrites
* @throws Exception
* @throws AlreadyExistsException
*/
public function generateVerificationFileRewrite(RewriteInterface ...$rewrites)
{
Expand All @@ -75,8 +77,8 @@ public function generateVerificationFileRewrite(RewriteInterface ...$rewrites)
/**
* @param StoreInterface $store
* @param RewriteInterface $rewriteDataObject
* @throws \Exception
* @throws \Magento\Framework\Exception\AlreadyExistsException
* @throws Exception
* @throws AlreadyExistsException
*/
private function generateRewriteForStore(StoreInterface $store, RewriteInterface $rewriteDataObject)
{
Expand All @@ -89,7 +91,7 @@ private function generateRewriteForStore(StoreInterface $store, RewriteInterface
$rewrite->setData(UrlRewriteService::REQUEST_PATH, $fileName);
$rewrite->setData(
UrlRewriteService::TARGET_PATH,
Index::URL_PATH . Index::FILENAME_PARAM . '/' . $fileName
Index::URL_PATH . Index::FILENAME_PARAM . '/' . urlencode($fileName)
);

$this->urlRewriteResource->save($rewrite);
Expand Down
3 changes: 2 additions & 1 deletion Model/Rewrite/Purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace LizardMedia\GoogleAnalyticsVerifier\Model\Rewrite;

use Exception;
use LizardMedia\GoogleAnalyticsVerifier\Api\Rewrite\PurgerInterface;
use LizardMedia\GoogleAnalyticsVerifier\Api\Rewrite\RewriteDataInterface;
use LizardMedia\GoogleAnalyticsVerifier\Controller\File\Index;
Expand Down Expand Up @@ -47,7 +48,7 @@ public function __construct(
}

/**
* @throws \Exception
* @throws Exception
*/
public function purgeVerificationFileRewrites()
{
Expand Down
11 changes: 7 additions & 4 deletions Test/Unit/Model/ConfigProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ConfigProviderTest extends TestCase
/**
* @return void
*/
protected function setUp()
protected function setUp(): void
{
$this->scopeConfig = $this->getMockBuilder(ScopeConfigInterface::class)->getMock();
$this->rewriteFactory = $this->getMockBuilder(RewriteFactory::class)
Expand All @@ -65,8 +65,9 @@ protected function setUp()

/**
* @test
* @return void
*/
public function testGetVerificationCodeIsString()
public function testGetVerificationCodeIsString(): void
{
$this->scopeConfig->expects($this->once())->method('getValue')
->with(ConfigProvider::XML_PATH_VERIFICATION_CODE)->willReturn('test');
Expand All @@ -76,8 +77,9 @@ public function testGetVerificationCodeIsString()

/**
* @test
* @return void
*/
public function testGetVerificationCodeIsNull()
public function testGetVerificationCodeIsNull(): void
{
$this->scopeConfig->expects($this->once())->method('getValue')
->with(ConfigProvider::XML_PATH_VERIFICATION_CODE)->willReturn(null);
Expand All @@ -87,8 +89,9 @@ public function testGetVerificationCodeIsNull()

/**
* @test
* @return void
*/
public function testGetRewritesDataArray()
public function testGetRewritesDataArray(): void
{
$value = '{"_1533030582256_256":{"file_name":"google800000000","file_content":"dupa1"}}';
$this->scopeConfig->expects($this->once())
Expand Down
11 changes: 7 additions & 4 deletions Test/Unit/Model/Rewrite/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class GeneratorTest extends TestCase
/**
* @return void
*/
protected function setUp()
protected function setUp(): void
{
$this->urlRewrite = $this->getMockBuilder(UrlRewrite::class)
->disableOriginalConstructor()->getMock();
Expand All @@ -78,8 +78,9 @@ protected function setUp()

/**
* @test
* @return void
*/
public function testGenerateVerificationFileRewriteCreatesRewrite()
public function testGenerateVerificationFileRewriteCreatesRewrite(): void
{
$fileName = 'fileName';
$rewriteMock = $this->getMockBuilder(Rewrite::class)
Expand All @@ -95,8 +96,9 @@ public function testGenerateVerificationFileRewriteCreatesRewrite()

/**
* @test
* @return void
*/
public function testGenerateVerificationFileRewriteWhenUrlResourceThrowsException()
public function testGenerateVerificationFileRewriteWhenUrlResourceThrowsException(): void
{
$fileName = '';
$rewriteMock = $this->getMockBuilder(Rewrite::class)
Expand All @@ -116,8 +118,9 @@ public function testGenerateVerificationFileRewriteWhenUrlResourceThrowsExceptio
/**
* @param string $fileName
* @param int $storeId
* @return void
*/
private function runGenerateVerificationFileRewriteMethod(string $fileName, int $storeId)
private function runGenerateVerificationFileRewriteMethod(string $fileName, int $storeId): void
{
$store = $this->getMockBuilder(StoreInterface::class)->getMock();
$stores = [$store];
Expand Down
10 changes: 6 additions & 4 deletions Test/Unit/Model/Rewrite/PurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PurgerTest extends TestCase
/**
* @return void
*/
protected function setUp()
protected function setUp(): void
{
$this->urlRewriteCollectionFactory = $this->getMockBuilder(UrlRewriteCollectionFactory::class)
->disableOriginalConstructor()->getMock();
Expand All @@ -66,8 +66,9 @@ protected function setUp()

/**
* @test
* @return void
*/
public function testPurgeVerificationFileRewrites()
public function testPurgeVerificationFileRewrites(): void
{
$this->setupCollection();
$this->urlRewriteResource->expects($this->once())->method('delete')->with($this->urlRewrite);
Expand All @@ -76,8 +77,9 @@ public function testPurgeVerificationFileRewrites()

/**
* @test
* @return void
*/
public function testPurgeVerificationFileRewritesWhenThrowsExceptionWhileDelete()
public function testPurgeVerificationFileRewritesWhenThrowsExceptionWhileDelete(): void
{
$this->setupCollection();
$this->urlRewriteResource->expects($this->once())->method('delete')
Expand All @@ -90,7 +92,7 @@ public function testPurgeVerificationFileRewritesWhenThrowsExceptionWhileDelete(
/**
* @return void
*/
private function setupCollection()
private function setupCollection(): void
{
$rewriteCollection = $this->getMockBuilder(UrlRewriteCollection::class)
->disableOriginalConstructor()->getMock();
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "lizardmedia/module-ga-verifier",
"description": "Magento2 module for adding Google Analytics verification scripts without uploading files to the server",
"type": "magento2-module",
"version": "1.0.0",
"version": "1.0.1",
"license": [
"MIT"
],
Expand All @@ -18,7 +18,8 @@
],
"require": {
"php": "^7.1.0",
"magento/module-url-rewrite": "*"
"magento/module-url-rewrite": "*",
"zendframework/zend-json": "*"
},
"require-dev": {
"phpunit/phpunit": ">=7.0.0"
Expand Down
Loading

0 comments on commit 75a3f7b

Please sign in to comment.