-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77130b9
commit 6fe372a
Showing
8 changed files
with
299 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2020. Volodymyr Hryvinskyi. All rights reserved. | ||
* @author: <mailto:[email protected]> | ||
* @github: <https://github.com/hryvinskyi> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hryvinskyi\DeferJs\Block\Adminhtml\System; | ||
|
||
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray; | ||
|
||
/** | ||
* Class ExcludeUrlPattern | ||
*/ | ||
class ExcludeUrlPattern extends AbstractFieldArray | ||
{ | ||
/** | ||
* @return void | ||
*/ | ||
protected function _construct() | ||
{ | ||
$this->addColumn('pattern', [ | ||
'label' => __('Expression') | ||
]); | ||
|
||
$this->_addAfter = false; | ||
$this->_addButtonLabel = __('Add'); | ||
|
||
parent::_construct(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
Model/PassesValidator/Validators/SkipScriptsByURLPattern.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2020. Volodymyr Hryvinskyi. All rights reserved. | ||
* @author: <mailto:[email protected]> | ||
* @github: <https://github.com/hryvinskyi> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hryvinskyi\DeferJs\Model\PassesValidator\Validators; | ||
|
||
use Hryvinskyi\Base\Helper\ArrayHelper; | ||
use Hryvinskyi\Base\Helper\Json; | ||
use Hryvinskyi\DeferJs\Helper\Config; | ||
use Hryvinskyi\DeferJs\Model\PassesValidator\ValidatorInterface; | ||
use Magento\Framework\App\Request\Http as RequestHttp; | ||
use Magento\Framework\App\Response\Http; | ||
|
||
/** | ||
* Class SkipScriptsByURLPattern | ||
*/ | ||
class SkipScriptsByURLPattern implements ValidatorInterface | ||
{ | ||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var RequestHttp | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* SkipScriptsByController constructor. | ||
* | ||
* @param Config $config | ||
* @param RequestHttp $request | ||
*/ | ||
public function __construct( | ||
Config $config, | ||
RequestHttp $request | ||
) { | ||
$this->config = $config; | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* Validator function, handle javascript or not | ||
* | ||
* @param string $script | ||
* @param Http $http | ||
* | ||
* @return bool | ||
*/ | ||
public function validate(string $script, Http $http): bool | ||
{ | ||
$executeUrlPattern = Json::decode($this->config->getExcludeUrlPattern()); | ||
$executeUrlPattern = ArrayHelper::getColumn($executeUrlPattern, 'pattern', false); | ||
|
||
$return = false; | ||
|
||
foreach ($executeUrlPattern as $pattern) { | ||
if ($this->checkPattern($this->request->getRequestUri(), $pattern)) { | ||
$return = true; | ||
break; | ||
} | ||
} | ||
|
||
return $return; | ||
} | ||
|
||
/** | ||
* @param $string | ||
* @param $pattern | ||
* | ||
* @return bool | ||
*/ | ||
public function checkPattern(string $string, string $pattern): bool | ||
{ | ||
$parts = explode('*', $pattern); | ||
$index = 0; | ||
|
||
$shouldBeFirst = true; | ||
|
||
foreach ($parts as $part) { | ||
if ($part == '') { | ||
$shouldBeFirst = false; | ||
continue; | ||
} | ||
|
||
$index = strpos($string, $part, $index); | ||
|
||
if ($index === false) { | ||
return false; | ||
} | ||
|
||
if ($shouldBeFirst && $index > 0) { | ||
return false; | ||
} | ||
|
||
$shouldBeFirst = false; | ||
$index += strlen($part); | ||
} | ||
|
||
if (count($parts) == 1) { | ||
return $string == $pattern; | ||
} | ||
|
||
$last = end($parts); | ||
|
||
if ($last == '') { | ||
return true; | ||
} | ||
|
||
if (strrpos($string, $last) === false) { | ||
return false; | ||
} | ||
|
||
if (strlen($string) - strlen($last) - strrpos($string, $last) > 0) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
Test/Unit/Model/PassesValidator/Validators/SkipScriptsByURLPatternTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2019. Volodymyr Hryvinskyi. All rights reserved. | ||
* @author: <mailto:[email protected]> | ||
* @github: <https://github.com/hryvinskyi> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hryvinskyi\DeferJs\Test\Unit\Model\PassesValidator\Validators; | ||
|
||
use Hryvinskyi\DeferJs\Helper\Config; | ||
use Hryvinskyi\DeferJs\Model\PassesValidator\Validators\SkipScriptsByURLPattern; | ||
use Magento\Framework\App\Request\Http as RequestHttp; | ||
use Magento\Framework\App\Response\Http; | ||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use PHPUnit\Framework\TestCase; | ||
use PHPUnit_Framework_MockObject_MockObject; | ||
|
||
class SkipScriptsByURLPatternTest extends TestCase | ||
{ | ||
/** | ||
* @var Config|PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var RequestHttp|PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $request; | ||
|
||
/** | ||
* @var Http | ||
*/ | ||
private $http; | ||
|
||
/** | ||
* @var SkipScriptsByURLPattern | ||
*/ | ||
private $model; | ||
|
||
/** | ||
* Sets up the fixture | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->config = $this->createPartialMock( | ||
Config::class, | ||
['getExcludeUrlPattern'] | ||
); | ||
|
||
$this->request = $this->createPartialMock( | ||
RequestHttp::class, | ||
['getRequestUri'] | ||
); | ||
|
||
$this->config->expects($this->any())->method('getExcludeUrlPattern') | ||
->willReturn('{"_1589359003110_110":{"pattern":"*argus-all-weather*"}}'); | ||
|
||
|
||
$this->http = (new ObjectManager($this))->getObject(Http::class); | ||
$this->model = (new ObjectManager($this))->getObject(SkipScriptsByURLPattern::class, [ | ||
'config' => $this->config, | ||
'request' => $this->request, | ||
]); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testSkipScript(): void | ||
{ | ||
$this->request->expects($this->any())->method('getRequestUri')->willReturn('/argus-all-weather-tank.html'); | ||
$this->assertEquals(true, $this->model->validate('', $this->http)); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
public function testNoSkipScript(): void | ||
{ | ||
$this->request->expects($this->any())->method('getRequestUri')->willReturn('/someUrl.html'); | ||
$this->assertEquals(false, $this->model->validate('', $this->http)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.