From aa74878547aea9bcb780a656bff78afeb40efeb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20Schoenenberger?= <54308193+hschoenenberger@users.noreply.github.com> Date: Fri, 6 Sep 2024 14:15:31 +0200 Subject: [PATCH] feat: test upgrade (#425) * feat: test upgrade * fix: be kind rewind * feat: method to increment version number for tests * fix: setUp parent method call * fix: move info log --- .../CommandHandler/UpgradeModuleHandler.php | 5 + .../UpgradeModuleHandlerTest.php | 238 ++++++++++++++++++ tests/Feature/FeatureTestCase.php | 9 +- 3 files changed, 249 insertions(+), 3 deletions(-) create mode 100644 tests/Feature/Account/CommandHandler/UpgradeModuleHandlerTest.php diff --git a/src/Account/CommandHandler/UpgradeModuleHandler.php b/src/Account/CommandHandler/UpgradeModuleHandler.php index 9358a5c3e..e41d6539a 100644 --- a/src/Account/CommandHandler/UpgradeModuleHandler.php +++ b/src/Account/CommandHandler/UpgradeModuleHandler.php @@ -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 @@ -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); diff --git a/tests/Feature/Account/CommandHandler/UpgradeModuleHandlerTest.php b/tests/Feature/Account/CommandHandler/UpgradeModuleHandlerTest.php new file mode 100644 index 000000000..632bf4357 --- /dev/null +++ b/tests/Feature/Account/CommandHandler/UpgradeModuleHandlerTest.php @@ -0,0 +1,238 @@ +=')) { + $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' => 'admin@prestashop.com', + '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'); + } +} diff --git a/tests/Feature/FeatureTestCase.php b/tests/Feature/FeatureTestCase.php index 076d21fb1..bba0d06d9 100644 --- a/tests/Feature/FeatureTestCase.php +++ b/tests/Feature/FeatureTestCase.php @@ -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 { @@ -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; } /**