Skip to content

Commit

Permalink
Merge pull request #30 from astoundcommerce/bugfix/AVASUP-403/404-err…
Browse files Browse the repository at this point in the history
…or-when-clicking-on-View-Certificate-on-Magento-2.3.2

Fixed AVASUP-403: 404 error when clicking on View Certificate on Magento 2.3.2 (admin panel)
  • Loading branch information
marynychsv authored Oct 7, 2019
2 parents 7127dc2 + a2844ac commit 4e1b571
Show file tree
Hide file tree
Showing 5 changed files with 382 additions and 29 deletions.
19 changes: 19 additions & 0 deletions Api/CertificatesInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace ClassyLlama\AvaTax\Api;

/**
* Interface CertificatesInterface
* @package ClassyLlama\AvaTax\Api
*/
interface CertificatesInterface
{

/**
* Get certificates list
*
* @param int|null $userId
* @return array
*/
public function getCertificatesList(int $userId = null): array;
}
97 changes: 97 additions & 0 deletions Model/Certificates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php declare(strict_types=1);

namespace ClassyLlama\AvaTax\Model;

use ClassyLlama\AvaTax\Api\CertificatesInterface;
use Magento\Backend\Model\Auth\Session as AuthSession;
use ClassyLlama\AvaTax\Helper\CertificateHelper;
use ClassyLlama\AvaTax\Model\Logger\AvaTaxLogger;
use Magento\Framework\DataObject;
use Magento\User\Model\User;

/**
* Class Certificates
* @package ClassyLlama\AvaTax\Model
*/
class Certificates implements CertificatesInterface
{
/**
* @var AuthSession
*/
private $authSession;

/**
* @var CertificateHelper
*/
private $certificateHelper;

/**
* @var AvaTaxLogger
*/
private $avaTaxLogger;

/**
* Certificates constructor.
* @param AuthSession $authSession
* @param CertificateHelper $certificateHelper
* @param AvaTaxLogger $avaTaxLogger
*/
public function __construct(
AuthSession $authSession,
CertificateHelper $certificateHelper,
AvaTaxLogger $avaTaxLogger
) {
$this->authSession = $authSession;
$this->certificateHelper = $certificateHelper;
$this->avaTaxLogger = $avaTaxLogger;
}

/**
* Get certificates list
*
* @param int|null $userId
* @return array
*/
public function getCertificatesList(int $userId = null): array
{
/** @var User|null $user */
$user = $this->authSession->getUser();
/** @var int|null $userId */
$userId = $userId ?? (null !== $user ? (int)$user->getId() : null);
$certificates = [];
try {
if (null !== $userId) {
/** @var array<int, DataObject> $certificates */
$certificates = $this->certificateHelper->getCertificates($userId);
$certificates = $this->prepareCertificateURLs($certificates, $userId);
}
} catch (\Throwable $exception) {
$this->avaTaxLogger->error($exception->getMessage(), [
'class' => self::class,
'trace' => $exception->getTraceAsString()
]);
}
return $certificates;
}

/**
* @param array $certificates
* @param int|null $userId
* @return array
*/
private function prepareCertificateURLs(array $certificates = [], int $userId = null): array
{
if (!empty($certificates) && null !== $userId) {
/** @var DataObject $certificate */
foreach ($certificates as $certificate) {
/** @var string $viewUrl */
$viewUrl = (string)$this->certificateHelper->getCertificateUrl($certificate->getData('id'), $userId);
/** @var string $deleteUrl */
$deleteUrl = (string)$this->certificateHelper->getCertificateDeleteUrl($certificate->getData('id'), $userId);
$certificate->setData('certificate_url', $viewUrl);
$certificate->setData('certificate_delete_url', $deleteUrl);
}
}
return $certificates;
}
}
188 changes: 188 additions & 0 deletions Test/Unit/Model/CertificatesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?php

namespace ClassyLlama\AvaTax\Test\Unit\Model;

use PHPUnit\Framework\TestCase;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use ClassyLlama\AvaTax\Model\Certificates;
use Magento\Backend\Model\Auth\Session as AuthSession;
use ClassyLlama\AvaTax\Helper\CertificateHelper;
use ClassyLlama\AvaTax\Model\Logger\AvaTaxLogger;
use Magento\Framework\DataObject;

/**
* Class СertificatesTest
* @package ClassyLlama\AvaTax\Test\Unit\Model
*/
class CertificatesTest extends TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var Certificates
*/
private $certificates;

/**
* @var AuthSession|\PHPUnit_Framework_MockObject_MockObject
*/
private $authSessionMock;

/**
* @var CertificateHelper|\PHPUnit_Framework_MockObject_MockObject
*/
private $certificateHelperMock;

/**
* @var AvaTaxLogger|\PHPUnit_Framework_MockObject_MockObject
*/
private $avaTaxLoggerMock;

/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->objectManager = new ObjectManager($this);
$this->authSessionMock = $this->createMock(AuthSession::class);
$this->certificateHelperMock = $this->createMock(CertificateHelper::class);
$this->avaTaxLoggerMock = $this->createMock(AvaTaxLogger::class);
$this->certificates = $this->objectManager->getObject(Certificates::class, [
'authSession' => $this->authSessionMock,
'certificateHelper' => $this->certificateHelperMock,
'avaTaxLogger' => $this->avaTaxLoggerMock
]);
}

/**
* @test
* @covers \ClassyLlama\AvaTax\Model\Certificates::getCertificatesList
*/
public function checkCorrectReturnTypeWhileGettingCertificatesList()
{
$userId = 1;
$viewUrl = 'example/view_url';
$deleteUrl = 'example/delete_url';

$this->certificateHelperMock->expects(static::atLeastOnce())
->method('getCertificates')
->willReturn($this->getMockData());
$this->certificateHelperMock->expects(static::atLeastOnce())
->method('getCertificateUrl')
->willReturn($viewUrl);
$this->certificateHelperMock->expects(static::atLeastOnce())
->method('getCertificateDeleteUrl')
->willReturn($deleteUrl);

/** @var array<int, DataObject> $result */
$result = $this->certificates->getCertificatesList($userId);

$this->assertInternalType('array', $result);
$this->assertCount(2, $result);
$this->assertInstanceOf(DataObject::class, $result[0]);
$this->assertInstanceOf(DataObject::class, $result[1]);
$this->assertSame($viewUrl, $result[0]->getData('certificate_url'));
$this->assertSame($deleteUrl, $result[0]->getData('certificate_delete_url'));
$this->assertSame($viewUrl, $result[1]->getData('certificate_url'));
$this->assertSame($deleteUrl, $result[1]->getData('certificate_delete_url'));
}

/**
* @test
* @covers \ClassyLlama\AvaTax\Model\Certificates::getCertificatesList
*/
public function checkCorrectReturnTypeInCaseOfError()
{
$userId = 1;

$this->certificateHelperMock->expects(static::atLeastOnce())
->method('getCertificates')
->willThrowException(new \Exception('Error happened'));

/** @var array $result */
$result = $this->certificates->getCertificatesList($userId);

$this->assertInternalType('array', $result);
$this->assertCount(0, $result);
}

/**
* Get mock data
*
* @return array
*/
public function getMockData(): array
{
return [
new DataObject([
'id' => 5,
'company_id' => 170111,
'signed_date' => '2018-04-23',
'expiration_date' => '2021-04-23',
'filename' => '71011_auto_5.pdf',
'document_exists' => true,
'valid' => true,
'verified' => false,
'exempt_percentage' => 100.0,
'is_single_certificate' => false,
'exemption_number' => '388383838383',
'validated_exemption_reason' => new DataObject([
'id' => 71,
'name' => 'RESALE'
]),
'exemption_reason' => new DataObject([
'id' => 71,
'name' => 'RESALE'
]),
'status' => 'COMPLETE',
'created_date' => '2018-04-23T20:49:40.860677',
'modified_date' => '2018-06-08',
'page_count' => 0,
'exposure_zone' => new DataObject([
'id' => 127,
'name' => 'Idaho',
'tag' => 'EZ_US_ID',
'description' => 'Idaho Sales Tax',
'region' => 'ID',
'country' => 'US'
])
]),
new DataObject([
'id' => 35,
'company_id' => 170111,
'signed_date' => '2018-07-04',
'expiration_date' => '2021-07-04',
'filename' => '71011_auto_35.pdf',
'document_exists' => true,
'valid' => true,
'verified' => false,
'exempt_percentage' => 100.0,
'is_single_certificate' => false,
'exemption_number' => 'asd',
'validated_exemption_reason' => new DataObject([
'id' => 71,
'name' => 'RESALE'
]),
'exemption_reason' => new DataObject([
'id' => 71,
'name' => 'RESALE'
]),
'status' => 'COMPLETE',
'created_date' => '2018-07-04T08:49:35.232304',
'modified_date' => '2018-07-04',
'page_count' => 1,
'exposure_zone' => new DataObject([
'id' => 127,
'name' => 'Idaho',
'tag' => 'EZ_US_ID',
'description' => 'Idaho Sales Tax',
'region' => 'ID',
'country' => 'US'
])
])
];
}
}
Loading

0 comments on commit 4e1b571

Please sign in to comment.