Skip to content

Commit

Permalink
Added headers and server by setters
Browse files Browse the repository at this point in the history
  • Loading branch information
eadesignro committed Apr 3, 2017
1 parent a5c3fd2 commit 4cb2e9b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Helper/Detect.php
Expand Up @@ -21,7 +21,7 @@

use Magento\Framework\App\Helper\Context;
use Magento\Framework\App\Helper\AbstractHelper;
use Detection\MobileDetect;
use Eadesigndev\Mobiledetect\Helper\MobileDetectModifier as MobileDetect;

/**
* Helper to be used for mobile detect and validations
Expand Down
46 changes: 46 additions & 0 deletions Helper/MobileDetectModifier.php
@@ -0,0 +1,46 @@
<?php
/**
* EaDesgin
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE_AFL.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@eadesign.ro so we can send you a copy immediately.
*
* @category eadesigndev_pdfgenerator
* @copyright Copyright (c) 2008-2016 EaDesign by Eco Active S.R.L.
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
*/

namespace Eadesigndev\Mobiledetect\Helper;

use Detection\MobileDetect;

/**
* Helper to be used for mobile detect and validations
*
* Class Validations
* @package Eadesigndev\Detect\Helper
*/
class MobileDetectModifier extends MobileDetect
{
/**
* MobileDetectModifier constructor.
* @param null $headers
* @param null $userAgent
*/
public function __construct(
$headers = null,
$userAgent = null
) {
if (!is_array($headers)) {
$headers = [];
}
parent::__construct($headers, $userAgent);
}
}
16 changes: 15 additions & 1 deletion Test/Unit/MobiledetectPluginTest.php
Expand Up @@ -5,10 +5,12 @@
use Eadesigndev\Mobiledetect\Helper\Detect;
use Eadesigndev\Mobiledetect\Helper\Redirect;
use Eadesigndev\Mobiledetect\View\Plugin\DesignExceptions;
use Eadesigndev\Mobiledetect\Helper\MobileDetectModifier;
use Magento\Framework\View\DesignExceptions as InitialDesignExceptions;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Request\Http;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Unserialize\Unserialize;

/**
* Copyright © 2017 EaDesign by Eco Active S.R.L. All rights reserved.
Expand Down Expand Up @@ -76,6 +78,7 @@ public function setup()
->getMock();

$this->detectHelper = $this->getMockBuilder(Detect::class)
->setMethods(['getMobileDetect', 'isMobile', 'isTablet','isDetected'])
->disableOriginalConstructor()
->getMock();

Expand All @@ -89,12 +92,15 @@ public function setup()

$this->subject = $this->objectManager->getObject(InitialDesignExceptions::class);

$unSerialize = new Unserialize();

$this->designExceptions = new DesignExceptions(
$this->scopeConfigInterface,
$this->exceptionConfigPath,
$this->scopeType,
$this->detectHelper,
$this->redirectHelper
$this->redirectHelper,
$unSerialize
);
}

Expand Down Expand Up @@ -175,6 +181,14 @@ public function testIfModuleIfMobileException(
->with($this->equalTo('HTTP_USER_AGENT'))
->will($this->returnValue($userAgent));

$mobileDetectModifier = $this->getMockBuilder(MobileDetectModifier::class)
->setMethods(['setHttpHeaders','setUserAgent'])
->disableOriginalConstructor()
->getMock();

$this->detectHelper->method('getMobileDetect')
->will($this->returnValue($mobileDetectModifier));

$this->detectHelper->method('is' . $case)
->willReturn(true);

Expand Down
18 changes: 16 additions & 2 deletions View/Plugin/DesignExceptions.php
Expand Up @@ -21,6 +21,7 @@

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\Request\Http as HttpRequest;
use Magento\Framework\Unserialize\Unserialize;
use Magento\Framework\View\DesignExceptions as InitialDesignExceptions;
use Eadesigndev\Mobiledetect\Helper\Detect;
use Eadesigndev\Mobiledetect\Helper\Redirect;
Expand All @@ -42,24 +43,32 @@ class DesignExceptions extends InitialDesignExceptions
*/
private $userAgent;

/**
* @var Unserialize
*/
private $unSerialize;

/**
* DesignExceptions constructor.
* @param ScopeConfigInterface $scopeConfig
* @param string $exceptionConfigPath
* @param string $scopeType
* @param Detect $detect
* @param Redirect $redirect
* @param Unserialize $unSerialize
*/
public function __construct(
ScopeConfigInterface $scopeConfig,
$exceptionConfigPath,
$scopeType,
Detect $detect,
Redirect $redirect
Redirect $redirect,
Unserialize $unSerialize
) {
parent::__construct($scopeConfig, $exceptionConfigPath, $scopeType);
$this->detect = $detect;
$this->redirect = $redirect;
$this->unSerialize = $unSerialize;
}

/**
Expand All @@ -69,6 +78,7 @@ public function __construct(
* @return bool|string
* @SuppressWarnings("unused")
*/
// @codingStandardsIgnoreLine
public function aroundGetThemeByRequest($subject, callable $proceed, HttpRequest $request)
{
$rules = $proceed($request);
Expand All @@ -85,6 +95,10 @@ public function aroundGetThemeByRequest($subject, callable $proceed, HttpRequest
return $rules;
}

$mobileDetect = $this->detect->getMobileDetect();
$mobileDetect->setHttpHeaders($request->getHeaders());
$mobileDetect->setUserAgent($userAgent);

$exception = $this->ifThemeChange();

if (!$exception) {
Expand All @@ -100,7 +114,7 @@ public function aroundGetThemeByRequest($subject, callable $proceed, HttpRequest
return $rules;
}

$expressions = unserialize($expressions);
$expressions = $this->unSerialize->unserialize($expressions);

foreach ($expressions as $rule) {
if (preg_match($rule['regexp'], $exception)) {
Expand Down

0 comments on commit 4cb2e9b

Please sign in to comment.