Skip to content

Commit

Permalink
feat: test upgrade (#425)
Browse files Browse the repository at this point in the history
* feat: test upgrade

* fix: be kind rewind

* feat: method to increment version number for tests

* fix: setUp parent method call

* fix: move info log
  • Loading branch information
hschoenenberger authored Sep 6, 2024
1 parent 91dd1b2 commit aa74878
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Account/CommandHandler/UpgradeModuleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use PrestaShop\Module\PsAccounts\Context\ShopContext;
use PrestaShop\Module\PsAccounts\Cqrs\CommandBus;
use PrestaShop\Module\PsAccounts\Exception\RefreshTokenException;
use PrestaShop\Module\PsAccounts\Log\Logger;
use PrestaShop\Module\PsAccounts\Repository\ConfigurationRepository;

class UpgradeModuleHandler
Expand Down Expand Up @@ -92,6 +93,10 @@ public function handle(UpgradeModuleCommand $command)
$lastUpgrade = $this->configRepo->getLastUpgrade(false);

if (version_compare($lastUpgrade, $command->payload->version, '<')) {
Logger::getInstance()->info(
'attempt upgrade [' . $lastUpgrade . ' to ' . $command->payload->version . ']'
);

// Set new version a soon as we can to avoid duplicate calls
$this->configRepo->updateLastUpgrade($command->payload->version);

Expand Down
238 changes: 238 additions & 0 deletions tests/Feature/Account/CommandHandler/UpgradeModuleHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
<?php

namespace PrestaShop\Module\PsAccounts\Tests\Feature\Api\Account\CommandHandler;

use PrestaShop\Module\PsAccounts\Adapter\ConfigurationKeys;
use PrestaShop\Module\PsAccounts\Adapter\Link;
use PrestaShop\Module\PsAccounts\Tests\Feature\FeatureTestCase;
use PrestaShop\Module\PsAccounts\Vendor\GuzzleHttp\Cookie\CookieJar;

class UpgradeModuleHandlerTest extends FeatureTestCase
{
/**
* @inject
*
* @var Link
*/
protected $link;

/**
* @var CookieJar
*/
private $cookieJar;

public function setUp(): void
{
parent::setUp();

if (version_compare(_PS_VERSION_, '1.7.0.0', '<') ||
version_compare(_PS_VERSION_, '9', '>=')) {
$this->markTestSkipped('Login test compatible with 1.7 & 8 only');
}

$this->cookieJar = new CookieJar();
}

/**
* @test
*/
public function itShouldUpdateModuleVersion()
{
$this->setVersion($this->incrementVersion(\Ps_accounts::VERSION, -1));

$this->assertResponseOk(
$this->loginIntoBackoffice()
);

$this->assertEquals(\Ps_accounts::VERSION, $this->getVersion());
}

/**
* @test
*/
public function itShouldNotUpdateModuleVersion()
{
$incrementedVersion = $this->incrementVersion(\Ps_accounts::VERSION, +1);

$this->setVersion($incrementedVersion);

$this->assertResponseOk(
$this->loginIntoBackoffice()
);

$this->assertEquals($incrementedVersion, $this->getVersion());
}

// /**
// * @test
// */
// public function itShouldUpdateModuleVersionOnlyOnce()
// {
// $this->setVersion('7.0.1');
//
// $response = $this->loginIntoBackoffice();
//
// $json = $this->getResponseJson($response);
//
// $conf = $this->getUncachedConfiguration(ConfigurationKeys::PS_ACCOUNTS_LAST_UPGRADE);
//
// $this->assertEquals(\Ps_accounts::VERSION, $conf->value);
//
// $t1 = $conf->date_upd;
// echo "T1: " . $t1 . "(" . $conf->date_add . ")\n";
//
// sleep(5);
//
// $this->displayBackofficePage($json['redirect'], $this->cookieJar);
//
// $conf2 = $this->getUncachedConfiguration(ConfigurationKeys::PS_ACCOUNTS_LAST_UPGRADE);
//
// $this->assertEquals(\Ps_accounts::VERSION, $conf2->value);
//
// $t2 = $conf2->date_upd;
// echo "T1: " . $t2 . "(" . $conf2->date_add . ")\n";
//
// $this->assertEquals($t1, $t2);
// }

/**
* @param string $version
* @param int|null $idGroup
* @param int|null $idShop
*
* @return void
*/
protected function setVersion($version, $idGroup = null, $idShop = null)
{
//$this->configurationRepository->updateLastUpgrade($version);
$this->configuration->setRaw(ConfigurationKeys::PS_ACCOUNTS_LAST_UPGRADE, $version, false, $idGroup, $idShop);
}

/**
* @param int|null $idGroup
* @param int|null $idShop
*
* @return string
*/
protected function getVersion($idGroup = null, $idShop = null)
{
//return $this->configurationRepository->getLastUpgrade();
return $this->configuration->getUncached(ConfigurationKeys::PS_ACCOUNTS_LAST_UPGRADE, $idGroup, $idShop);
}

/**
* @return \Psr\Http\Message\ResponseInterface
*/
protected function loginIntoBackoffice()
{
$jar = $this->cookieJar;

$this->assertResponseOk(
$this->displayLoginPage($jar)
);

$res = $this->postLogionForm([
'ajax' => 1,
//'token' => '',
'controller' => 'AdminLogin',
'submitLogin' => 1,
'email' => '[email protected]',
'passwd' => 'prestashop',
//'redirect' => '/admin-dev/index.php?controller=AdminDashboard',
], $jar);
$this->assertResponseOk($res);

$json = $this->getResponseJson($res);

$this->assertResponseOk(
$this->displayBackofficePage($json['redirect'], $jar)
);

return $res;
}

/**
* @param CookieJar $jar
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function displayLoginPage(CookieJar $jar)
{
return $this->client->get('/admin-dev/index.php?controller=AdminLogin', [
'cookies' => $jar,
]);
}

/**
* @param CookieJar $jar
* @param array $form
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function postLogionForm(array $form, CookieJar $jar)
{
return $this->client->post('/admin-dev/index.php?rand=' . time(), [
'form_params' => $form,
'cookies' => $jar,
]);
}

/**
* @param string $redirect
* @param CookieJar $jar
*
* @return \Psr\Http\Message\ResponseInterface
*/
protected function displayBackofficePage($redirect, CookieJar $jar)
{
return $this->client->get($redirect, [
'cookies' => $jar,
]);
}

/**
* @param string $version
* @param int $increment
*
* @return string
*/
protected function incrementVersion($version, $increment)
{
list($major, $minor, $patch) = explode('.', $version);

if ($increment > 0) {
return $major . '.' . $minor . '.' . ($patch + 1);
} else {
foreach (['patch', 'minor', 'major'] as $part) {
if ($$part -1 > 0) {
$$part -= 1;
break;
}
}
return $major . '.' . $minor . '.' . $patch;
}
}

/**
* @param string $key
* @param int|null $idShopGroup
* @param int|null $idShop
* @param mixed $default
*
* @return \Configuration
*
* @throw \Exception
*/
protected function getUncachedConfiguration($key, $idShopGroup = null, $idShop = null, $default = false)
{
$id = \Configuration::getIdByName($key, $idShopGroup, $idShop);
if ($id > 0) {
$found = (new \Configuration($id));
$found->clearCache();

return $found;
}

throw new \Exception('Configuration entry not found');
}
}
9 changes: 6 additions & 3 deletions tests/Feature/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace PrestaShop\Module\PsAccounts\Tests\Feature;

use Db;
use GuzzleHttp\Client;
use GuzzleHttp\Message\ResponseInterface;
use PrestaShop\Module\PsAccounts\Provider\RsaKeysProvider;
use PrestaShop\Module\PsAccounts\Repository\UserTokenRepository;
use PrestaShop\Module\PsAccounts\Tests\GuzzleTestClient;
use PrestaShop\Module\PsAccounts\Tests\TestCase;
use PrestaShop\Module\PsAccounts\Vendor\GuzzleHttp\Client;
use PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Builder;
use PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Signer\Hmac\Sha256;
use PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Signer\Key;
use PrestaShop\Module\PsAccounts\Vendor\Lcobucci\JWT\Token;
use Psr\Http\Message\ResponseInterface;

class FeatureTestCase extends TestCase
{
Expand Down Expand Up @@ -173,7 +173,10 @@ public function assertResponseError($response)
*/
public function getResponseJson($response)
{
return json_decode($response->getBody()->getContents(), true);
$ary = json_decode($response->getBody()->getContents(), true);
$response->getBody()->rewind();

return $ary;
}

/**
Expand Down

0 comments on commit aa74878

Please sign in to comment.