Skip to content

Commit

Permalink
Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sashas777 committed Jun 5, 2020
1 parent 326e257 commit a0588b8
Show file tree
Hide file tree
Showing 30 changed files with 2,709 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ composer.lock
/node_modules/*
package-lock.json
/test-reports/*
clover.xml
/test-coverage-html/*
2 changes: 2 additions & 0 deletions Controller/Customer/ConfigurationPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public function execute()
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, __('We can\'t save the TFA providers.'));
}
} else {
$this->messageManager->addErrorMessage(__('We can\'t save the TFA providers.'));
}
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath('*/*/configuration');
Expand Down
2 changes: 1 addition & 1 deletion Controller/Customer/Providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function execute()
$loginData = [];
try {
$loginData = $this->json->unserialize($this->getRequest()->getContent());
} catch (\Zend_Json_Exception $e) {
} catch (\InvalidArgumentException $e) {
return $resultRaw->setHttpResponseCode($httpBadRequestCode);
}

Expand Down
2 changes: 2 additions & 0 deletions Controller/Customer/ResetPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function execute()
} catch (\Exception $e) {
$this->messageManager->addExceptionMessage($e, __('We can\'t reset the configuration.'));
}
} else {
$this->messageManager->addErrorMessage(__('We can\'t reset the configuration.'));
}
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath('*/customer/configuration');
Expand Down
2 changes: 1 addition & 1 deletion Model/Config/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public function __construct(
*/
public function isTfaForced(): bool
{
return $this->scopeConfig->isSetFlag(static::XML_PATH_CUSTOMER_FORCE_TFA, ScopeInterface::SCOPE_WEBSITE);
return (bool) $this->scopeConfig->isSetFlag(static::XML_PATH_CUSTOMER_FORCE_TFA, ScopeInterface::SCOPE_WEBSITE);
}
}
16 changes: 5 additions & 11 deletions Model/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,24 @@ class Provider implements ProviderInterface
*/
private $customerConfigManager;

/**
* @var bool
*/
private $canReset;

/**
* Provider constructor.
*
* @param EngineInterface $engine
* @param CustomerConfigManagerInterface $customerConfigManager
* @param $code
* @param $name
* @param bool $canReset
*/
public function __construct(
EngineInterface $engine,
CustomerConfigManagerInterface $customerConfigManager,
$code,
$name,
$canReset = true
$name
) {
$this->engine = $engine;
$this->customerConfigManager = $customerConfigManager;
$this->code = $code;
$this->name = $name;
$this->canReset = $canReset;
}

/**
Expand Down Expand Up @@ -120,9 +112,11 @@ public function isConfigured(int $customerId): bool
}

/**
* @inheritdoc
* @param $customerId
*
* @return array|null
*/
public function getConfiguration($customerId)
private function getConfiguration($customerId)
{
return $this->customerConfigManager->getProviderConfig($customerId, $this->getCode());
}
Expand Down
113 changes: 113 additions & 0 deletions Test/Unit/Block/Adminhtml/Customer/Edit/ResetTFAButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* @author The S Group <support@sashas.org>
* @copyright 2020 Sashas IT Support Inc. (https://www.sashas.org)
* @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3 (GPL-3.0)
*/

declare(strict_types=1);

namespace Magetarian\CustomerTwoFactorAuth\Test\Unit\Block\Adminhtml\Customer\Edit;

use Magetarian\CustomerTwoFactorAuth\Api\ProviderPoolInterface;
use Magetarian\CustomerTwoFactorAuth\Block\Adminhtml\Customer\Edit\ResetTFAButton;
use PHPUnit\Framework\TestCase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\UrlInterface;
use Magento\Framework\Registry;
use Magento\Backend\Block\Widget\Context;
use Magento\Framework\Phrase;

/**
* Class ResetTFAButtonTest
* Test for ResetTFAButtonTest class
*/
class ResetTFAButtonTest extends TestCase
{

/** @var ResetTFAButton object */
private $object;

/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $urlBuilder;

/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $registry;

/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $context;

/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $providerPool;

/**
*
*/
public function testGetResetUrl()
{
$result = 'customer_tfa/customer/reset/customer_id/1';

$this->registry->expects($this->atLeastOnce())->method('registry')->willReturn(1);
$this->urlBuilder->expects($this->atLeastOnce())->method('getUrl')->willReturn($result);

$this->assertEquals($result, $this->object->getResetUrl());
}

/**
*
*/
public function testGetButtonData()
{
$result = [
'label' => new Phrase('Reset TFA'),
'id' => 'customer-reset-tfa-button',
'on_click' => "location.href = 'test';",
'class' => 'add',
'aclResource' => 'Magetarian_CustomerTwoFactorAuth::reset_tfa',
'sort_order' => 100
];

$this->registry->expects($this->atLeastOnce())->method('registry')->willReturn(1);
$this->urlBuilder->expects($this->atLeastOnce())->method('getUrl')->willReturn('test');
$this->providerPool->expects($this->atLeastOnce())->method('getEnabledProviders')->willReturn(['test']);

$this->assertEquals($result, $this->object->getButtonData());
}

/**
*
*/
protected function setUp()
{
$this->registry = $this->getMockBuilder(Registry::class)
->disableOriginalConstructor()
->getMock();
$this->context = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
$this->providerPool = $this->getMockBuilder(ProviderPoolInterface::class)
->disableOriginalConstructor()
->getMock();
$this->urlBuilder = $this->getMockBuilder(UrlInterface::class)
->disableOriginalConstructor()
->getMock();
$this->context->expects($this->atLeastOnce())->method('getUrlBuilder')->willReturn($this->urlBuilder);

$this->object = (new ObjectManager($this))->getObject(
ResetTFAButton::class,
[
'context' => $this->context,
'registry' => $this->registry,
'providerPool' => $this->providerPool
]
);
}
}
146 changes: 146 additions & 0 deletions Test/Unit/Controller/Adminhtml/Customer/ResetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* @author The S Group <support@sashas.org>
* @copyright 2020 Sashas IT Support Inc. (https://www.sashas.org)
* @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3 (GPL-3.0)
*/

declare(strict_types=1);

namespace Magetarian\CustomerTwoFactorAuth\Test\Unit\Controller\Adminhtml\Customer;

use Magento\Backend\App\Action\Context;
use Magetarian\CustomerTwoFactorAuth\Api\ProviderPoolInterface;
use PHPUnit\Framework\TestCase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Message\ManagerInterface;
use Magetarian\CustomerTwoFactorAuth\Api\ProviderInterface;

/**
* Class ResetTest
* Test for ResetTest class
*/
class ResetTest extends TestCase
{

/** @var Reset object */
private $object;

/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $providerPool;

/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $context;

/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $resultRedirectFactory;

/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $request;

/**
* @var \PHPUnit\Framework\MockObject\MockObject
*/
private $messageManager;

/**
* @dataProvider dataProviderExecute
*/
public function testExecute($customerId)
{
$resultRedirect = $this->getMockBuilder(Redirect::class)
->disableOriginalConstructor()
->getMock();
$provider = $this->getMockBuilder(ProviderInterface::class)
->disableOriginalConstructor()
->getMock();
$resultRedirect->expects($this->atLeastOnce())->method('setPath')->willReturn($resultRedirect);
$this->resultRedirectFactory->expects($this->atLeastOnce())->method('create')->willReturn($resultRedirect);
$this->request->expects($this->atLeastOnce())->method('getParam')->willReturn($customerId);
if ($customerId) {
$provider->expects($this->atLeastOnce())->method('resetConfiguration');
$this->providerPool->expects($this->atLeastOnce())->method('getProviders')->willReturn([$provider]);
$this->messageManager->expects($this->atLeastOnce())->method('addSuccessMessage');
} else {
$this->providerPool->expects($this->never())->method('getProviders');
$this->messageManager->expects($this->never())->method('addSuccessMessage');
}

$this->assertEquals($resultRedirect, $this->object->execute());
}

/**
*
*/
public function testExecuteException()
{
$resultRedirect = $this->getMockBuilder(Redirect::class)
->disableOriginalConstructor()
->getMock();
$resultRedirect->expects($this->atLeastOnce())->method('setPath')->willReturn($resultRedirect);
$this->resultRedirectFactory->expects($this->atLeastOnce())->method('create')->willReturn($resultRedirect);

$this->request->expects($this->atLeastOnce())->method('getParam')->willReturn(1);
$this->providerPool->expects($this->atLeastOnce())->method('getProviders')
->willThrowException(new \Exception('test'));
$this->messageManager->expects($this->atLeastOnce())->method('addExceptionMessage');
$this->messageManager->expects($this->never())->method('addSuccessMessage');

$this->assertEquals($resultRedirect, $this->object->execute());
}

/**
* @return array
*/
public function dataProviderExecute(): array
{
return [
[1],
[false],
];
}

/**
*
*/
protected function setUp()
{
$this->providerPool = $this->getMockBuilder(ProviderPoolInterface::class)
->disableOriginalConstructor()
->getMock();
$this->context = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->getMock();
$this->resultRedirectFactory = $this->getMockBuilder(RedirectFactory::class)
->disableOriginalConstructor()
->getMock();
$this->request = $this->getMockBuilder(RequestInterface::class)
->disableOriginalConstructor()
->getMock();
$this->messageManager = $this->getMockBuilder(ManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->context->expects($this->any())->method('getResultRedirectFactory')
->willReturn($this->resultRedirectFactory);
$this->context->expects($this->any())->method('getRequest')->willReturn($this->request);
$this->context->expects($this->any())->method('getMessageManager')->willReturn($this->messageManager);
$this->object = (new ObjectManager($this))->getObject(
Reset::class,
[
'context' => $this->context,
'providerPool' => $this->providerPool,
]
);
}
}
Loading

0 comments on commit a0588b8

Please sign in to comment.