oxtotp/src/tests/unit/Application/Controller/d3totploginTest.php

268 lines
8.1 KiB
PHP
Raw Normal View History

2019-08-05 22:59:26 +02:00
<?php
/**
2022-09-26 15:22:26 +02:00
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2019-08-05 22:59:26 +02:00
*
2022-09-26 15:22:26 +02:00
* https://www.d3data.de
2019-08-05 22:59:26 +02:00
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
2022-09-26 15:22:26 +02:00
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
2019-08-05 22:59:26 +02:00
*/
namespace D3\Totp\tests\unit\Application\Controller;
use D3\Totp\Application\Controller\d3totplogin;
use D3\Totp\Application\Model\d3backupcodelist;
use D3\Totp\Application\Model\d3totp;
use D3\Totp\tests\unit\d3TotpUnitTestCase;
use OxidEsales\Eshop\Core\Registry;
use OxidEsales\Eshop\Core\Utils;
2022-09-28 00:08:36 +02:00
use PHPUnit\Framework\MockObject\MockObject;
2019-08-05 22:59:26 +02:00
use ReflectionException;
class d3totploginTest extends d3TotpUnitTestCase
{
/** @var d3totplogin */
protected $_oController;
/**
* setup basic requirements
*/
2022-09-28 00:08:36 +02:00
public function setUp(): void
2019-08-05 22:59:26 +02:00
{
parent::setUp();
$this->_oController = oxNew(d3totplogin::class);
Registry::getSession()->deleteVariable(d3totp::TOTP_SESSION_CURRENTUSER);
Registry::getSession()->deleteVariable(d3totp::TOTP_SESSION_CURRENTCLASS);
}
2022-09-28 00:08:36 +02:00
public function tearDown(): void
2019-08-05 22:59:26 +02:00
{
parent::tearDown();
unset($this->_oController);
}
/**
* @test
* @throws ReflectionException
2022-09-28 22:25:51 +02:00
* @covers \D3\Totp\Application\Controller\d3totplogin::render
2019-08-05 22:59:26 +02:00
*/
public function renderRedirectIfNoTotp()
{
2022-09-28 00:08:36 +02:00
/** @var Utils|MockObject $oUtilsMock */
$oUtilsMock = $this->getMockBuilder(Utils::class)
->onlyMethods(['redirect'])
->getMock();
2019-08-05 22:59:26 +02:00
$oUtilsMock->expects($this->once())->method('redirect')->willReturn(true);
2022-09-28 00:08:36 +02:00
/** @var d3totplogin|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3totplogin::class)
->onlyMethods(['getUtils'])
->getMock();
2019-08-05 22:59:26 +02:00
$oControllerMock->method('getUtils')->willReturn($oUtilsMock);
$this->_oController = $oControllerMock;
2022-09-30 21:06:30 +02:00
2019-08-05 22:59:26 +02:00
$this->callMethod($this->_oController, 'render');
}
/**
* @test
* @throws ReflectionException
2022-09-28 22:25:51 +02:00
* @covers \D3\Totp\Application\Controller\d3totplogin::render
2019-08-05 22:59:26 +02:00
*/
public function renderDontRedirect()
{
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_CURRENTUSER, 'foo');
2022-09-28 00:08:36 +02:00
/** @var Utils|MockObject $oUtilsMock */
$oUtilsMock = $this->getMockBuilder(Utils::class)
->onlyMethods(['redirect'])
->getMock();
2019-08-05 22:59:26 +02:00
$oUtilsMock->expects($this->never())->method('redirect')->willReturn(true);
2022-09-28 00:08:36 +02:00
/** @var d3totplogin|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3totplogin::class)
->onlyMethods(['getUtils'])
->getMock();
2019-08-05 22:59:26 +02:00
$oControllerMock->method('getUtils')->willReturn($oUtilsMock);
$this->_oController = $oControllerMock;
$this->assertSame(
'd3totplogin.tpl',
$this->callMethod($this->_oController, 'render')
);
}
/**
* @test
* @throws ReflectionException
2022-09-28 22:25:51 +02:00
* @covers \D3\Totp\Application\Controller\d3totplogin::getUtils
2019-08-05 22:59:26 +02:00
*/
public function getUtilsReturnsRightInstance()
{
$this->assertInstanceOf(
Utils::class,
2022-09-28 00:08:36 +02:00
$this->callMethod(
$this->_oController,
'getUtils'
)
2019-08-05 22:59:26 +02:00
);
}
/**
* @test
* @throws ReflectionException
2022-09-28 22:25:51 +02:00
* @covers \D3\Totp\Application\Controller\d3totplogin::getBackupCodeCountMessage
2019-08-05 22:59:26 +02:00
*/
public function getBackupCodeCountMessageReturnMessage()
{
2022-09-28 00:08:36 +02:00
/** @var d3backupcodelist|MockObject $oBackupCodesListMock */
$oBackupCodesListMock = $this->getMockBuilder(d3backupcodelist::class)
->onlyMethods(['getAvailableCodeCount'])
->getMock();
2019-08-05 22:59:26 +02:00
$oBackupCodesListMock->method('getAvailableCodeCount')->willReturn(1);
2022-09-30 21:06:30 +02:00
2022-09-28 00:08:36 +02:00
/** @var d3totplogin|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3totplogin::class)
->onlyMethods(['getBackupCodeListObject'])
->getMock();
2019-08-05 22:59:26 +02:00
$oControllerMock->method('getBackupCodeListObject')->willReturn($oBackupCodesListMock);
$this->_oController = $oControllerMock;
$this->assertGreaterThan(
0,
strpos(
$this->callMethod($this->_oController, 'getBackupCodeCountMessage'),
' 1 '
)
);
}
/**
* @test
* @throws ReflectionException
2022-09-28 22:25:51 +02:00
* @covers \D3\Totp\Application\Controller\d3totplogin::getBackupCodeCountMessage
2019-08-05 22:59:26 +02:00
*/
public function getBackupCodeCountMessageReturnNoMessage()
{
2022-09-28 00:08:36 +02:00
/** @var d3backupcodelist|MockObject $oBackupCodesListMock */
$oBackupCodesListMock = $this->getMockBuilder(d3backupcodelist::class)
->onlyMethods(['getAvailableCodeCount'])
->getMock();
2019-08-05 22:59:26 +02:00
$oBackupCodesListMock->method('getAvailableCodeCount')->willReturn(1234);
2022-09-28 00:08:36 +02:00
/** @var d3totplogin|MockObject $oControllerMock */
$oControllerMock = $this->getMockBuilder(d3totplogin::class)
->onlyMethods(['getBackupCodeListObject'])
->getMock();
2019-08-05 22:59:26 +02:00
$oControllerMock->method('getBackupCodeListObject')->willReturn($oBackupCodesListMock);
$this->_oController = $oControllerMock;
$this->assertEmpty(
$this->callMethod($this->_oController, 'getBackupCodeCountMessage')
);
}
/**
* @test
* @throws ReflectionException
2022-09-28 22:25:51 +02:00
* @covers \D3\Totp\Application\Controller\d3totplogin::getBackupCodeListObject
2019-08-05 22:59:26 +02:00
*/
public function getBackupCodeListObjectReturnsRightInstance()
{
$this->assertInstanceOf(
d3backupcodelist::class,
$this->callMethod($this->_oController, 'getBackupCodeListObject')
);
}
/**
* @test
* @throws ReflectionException
2022-09-28 22:25:51 +02:00
* @covers \D3\Totp\Application\Controller\d3totplogin::getPreviousClass
2019-08-05 22:59:26 +02:00
*/
public function canGetPreviousClass()
{
$className = "testClass";
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_CURRENTCLASS, $className);
$this->assertSame(
$className,
$this->callMethod($this->_oController, 'getPreviousClass')
);
}
/**
* @test
* @throws ReflectionException
2022-09-28 22:25:51 +02:00
* @covers \D3\Totp\Application\Controller\d3totplogin::previousClassIsOrderStep
* @dataProvider classIsOrderStepDataProvider
2019-08-05 22:59:26 +02:00
*/
2022-09-28 22:25:51 +02:00
public function classIsOrderStep($className, $expected)
2019-08-05 22:59:26 +02:00
{
2022-09-28 22:25:51 +02:00
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_CURRENTCLASS, $className);
2019-08-05 22:59:26 +02:00
2022-09-28 22:25:51 +02:00
$this->assertSame(
$expected,
2019-08-05 22:59:26 +02:00
$this->callMethod(
$this->_oController,
'previousClassIsOrderStep'
)
);
}
/**
2022-09-28 22:25:51 +02:00
* @return array[]
2019-08-05 22:59:26 +02:00
*/
2022-09-28 22:25:51 +02:00
public function classIsOrderStepDataProvider(): array
2019-08-05 22:59:26 +02:00
{
2022-09-28 22:25:51 +02:00
return [
'order step class' => ['order', true],
'no order step class' => ['start', false],
];
2019-08-05 22:59:26 +02:00
}
/**
* @test
* @throws ReflectionException
2022-09-30 00:06:20 +02:00
* @covers \D3\Totp\Application\Controller\d3totplogin::getIsOrderStep
2022-09-28 22:25:51 +02:00
* @dataProvider classIsOrderStepDataProvider
2019-08-05 22:59:26 +02:00
*/
2022-09-28 22:25:51 +02:00
public function getIsOrderStepIsSameLikeOrderClass($className, $expected)
2019-08-05 22:59:26 +02:00
{
2022-09-28 22:25:51 +02:00
Registry::getSession()->setVariable(d3totp::TOTP_SESSION_CURRENTCLASS, $className);
2019-08-05 22:59:26 +02:00
2022-09-28 22:25:51 +02:00
$this->assertSame(
$expected,
2019-08-05 22:59:26 +02:00
$this->callMethod(
$this->_oController,
'getIsOrderStep'
)
);
}
/**
* @test
* @throws ReflectionException
2022-09-28 22:25:51 +02:00
* @covers \D3\Totp\Application\Controller\d3totplogin::getBreadCrumb
2019-08-05 22:59:26 +02:00
*/
public function canGetBreadCrumb()
{
$aBreadCrumb = $this->callMethod($this->_oController, 'getBreadCrumb');
2022-09-28 00:08:36 +02:00
$this->assertIsString($aBreadCrumb[0]['title']);
2019-08-05 22:59:26 +02:00
$this->assertTrue(strlen($aBreadCrumb[0]['title']) > 1);
2022-09-28 00:08:36 +02:00
$this->assertIsString($aBreadCrumb[0]['link']);
2019-08-05 22:59:26 +02:00
$this->assertTrue(strlen($aBreadCrumb[0]['link']) > 1);
}
2022-09-30 21:06:30 +02:00
}