From 46df22f9cdf8a3add47731e4becfb1c8734a6a1a Mon Sep 17 00:00:00 2001 From: Tatevik Date: Wed, 11 Dec 2024 20:47:51 +0400 Subject: [PATCH] ISSUE-337: check --- config/config.yml | 28 +----------- config/doctrine.yml | 26 +++++++++++ phpunit.xml.dist | 1 - src/Core/Bootstrap.php | 2 +- .../Identity/AdministratorTokenRepository.php | 22 ++++++++-- .../DetachedAdministratorTokenFixture.php | 6 ++- .../AdministratorTokenRepositoryTest.php | 43 ++++++------------- 7 files changed, 65 insertions(+), 63 deletions(-) create mode 100644 config/doctrine.yml diff --git a/config/config.yml b/config/config.yml index 0b9de88..7d4125b 100644 --- a/config/config.yml +++ b/config/config.yml @@ -1,6 +1,7 @@ imports: - { resource: services.yml } - { resource: repositories.yml } + - { resource: doctrine.yml } # Put parameters here that don't need to change on each machine where the app is deployed # https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration @@ -39,30 +40,3 @@ framework: serializer: enabled: true enable_attributes: true - -# Doctrine Configuration -doctrine: - dbal: - # These variables come from parameters.yml. There, the values are read from environment variables - # and can also be set directly in the parameters.yml file. - driver: '%database_driver%' - host: '%database_host%' - path: '%database_path%' - port: '%database_port%' - dbname: '%database_name%' - user: '%database_user%' - password: '%database_password%' - charset: UTF8 - - orm: - auto_generate_proxy_classes: '%kernel.debug%' - naming_strategy: doctrine.orm.naming_strategy.underscore - auto_mapping: true - mappings: - PhpList\Core\Domain\Model: - is_bundle: false - type: attribute - dir: '%kernel.project_dir%/src/Domain/Model/' - prefix: 'PhpList\Core\Domain\Model\' - controller_resolver: - auto_mapping: true diff --git a/config/doctrine.yml b/config/doctrine.yml new file mode 100644 index 0000000..10f7e1c --- /dev/null +++ b/config/doctrine.yml @@ -0,0 +1,26 @@ +# Doctrine Configuration +doctrine: + dbal: + # These variables come from parameters.yml. There, the values are read from environment variables + # and can also be set directly in the parameters.yml file. + driver: '%database_driver%' + host: '%database_host%' + path: '%database_path%' + port: '%database_port%' + dbname: '%database_name%' + user: '%database_user%' + password: '%database_password%' + charset: UTF8 + + orm: + auto_generate_proxy_classes: '%kernel.debug%' + naming_strategy: doctrine.orm.naming_strategy.underscore + auto_mapping: true + mappings: + PhpList\Core\Domain\Model: + is_bundle: false + type: attribute + dir: '%kernel.project_dir%/src/Domain/Model/' + prefix: 'PhpList\Core\Domain\Model\' + controller_resolver: + auto_mapping: false diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ccf4fa1..12e03ee 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,7 +9,6 @@ > - diff --git a/src/Core/Bootstrap.php b/src/Core/Bootstrap.php index 9e5569e..76cefd7 100644 --- a/src/Core/Bootstrap.php +++ b/src/Core/Bootstrap.php @@ -157,7 +157,7 @@ public function ensureDevelopmentOrTestingEnvironment(): self $usesProxy = isset($_SERVER['HTTP_CLIENT_IP']) || isset($_SERVER['HTTP_X_FORWARDED_FOR']); $isOnCli = PHP_SAPI === 'cli' || PHP_SAPI === 'cli-server'; $isLocalRequest = isset($_SERVER['REMOTE_ADDR']) - && in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1', 'localhost'], true); + && in_array($_SERVER['REMOTE_ADDR'], ['127.0.0.1', '::1'], true); if ($usesProxy || (!$isOnCli && !$isLocalRequest)) { header('HTTP/1.0 403 Forbidden'); exit('You are not allowed to access this file.'); diff --git a/src/Domain/Repository/Identity/AdministratorTokenRepository.php b/src/Domain/Repository/Identity/AdministratorTokenRepository.php index 14e3ed1..818b6bd 100644 --- a/src/Domain/Repository/Identity/AdministratorTokenRepository.php +++ b/src/Domain/Repository/Identity/AdministratorTokenRepository.php @@ -5,6 +5,8 @@ namespace PhpList\Core\Domain\Repository\Identity; use DateTime; +use DateTimeImmutable; +use DateTimeZone; use Doctrine\Common\Collections\Criteria; use PhpList\Core\Domain\Model\Identity\AdministratorToken; use PhpList\Core\Domain\Repository\AbstractRepository; @@ -46,9 +48,23 @@ public function findOneUnexpiredByKey(string $key): ?AdministratorToken */ public function removeExpired(): int { - $queryBuilder = $this->getEntityManager()->createQueryBuilder(); - $queryBuilder->delete(AdministratorToken::class, 'token')->where('token.expiry <= CURRENT_TIMESTAMP()'); + $now = new DateTimeImmutable('now', new DateTimeZone('UTC')); - return (int)$queryBuilder->getQuery()->execute(); + $expiredTokens = $this->createQueryBuilder('at') + ->where('at.expiry <= :date') + ->setParameter('date', $now) + ->getQuery() + ->getResult(); + + $deletedCount = 0; + + foreach ($expiredTokens as $token) { + $this->getEntityManager()->remove($token); + $deletedCount++; + } + + $this->getEntityManager()->flush(); + + return $deletedCount; } } diff --git a/tests/Integration/Domain/Repository/Fixtures/DetachedAdministratorTokenFixture.php b/tests/Integration/Domain/Repository/Fixtures/DetachedAdministratorTokenFixture.php index cf465df..15f5d2a 100644 --- a/tests/Integration/Domain/Repository/Fixtures/DetachedAdministratorTokenFixture.php +++ b/tests/Integration/Domain/Repository/Fixtures/DetachedAdministratorTokenFixture.php @@ -39,9 +39,11 @@ public function load(ObjectManager $manager): void $adminToken = new AdministratorToken(); $this->setSubjectId($adminToken, (int)$row['id']); $adminToken->setKey($row['value']); - $this->setSubjectProperty($adminToken, 'expiry', new DateTime($row['expires'])); - $this->setSubjectProperty($adminToken, 'creationDate', (bool) $row['entered']); + $manager->persist($adminToken); + + $this->setSubjectProperty($adminToken, 'expiry', new DateTime($row['expires'])); + $this->setSubjectProperty($adminToken, 'creationDate', $row['entered']); } while (true); fclose($handle); diff --git a/tests/Integration/Domain/Repository/Identity/AdministratorTokenRepositoryTest.php b/tests/Integration/Domain/Repository/Identity/AdministratorTokenRepositoryTest.php index af8c2e0..e6b9ba3 100644 --- a/tests/Integration/Domain/Repository/Identity/AdministratorTokenRepositoryTest.php +++ b/tests/Integration/Domain/Repository/Identity/AdministratorTokenRepositoryTest.php @@ -13,15 +13,17 @@ use PhpList\Core\TestingSupport\Traits\DatabaseTestTrait; use PhpList\Core\TestingSupport\Traits\SimilarDatesAssertionTrait; use PhpList\Core\Tests\Integration\Domain\Repository\Fixtures\AdministratorFixture; +use PhpList\Core\Tests\Integration\Domain\Repository\Fixtures\AdministratorTokenWithAdministratorFixture; use PhpList\Core\Tests\Integration\Domain\Repository\Fixtures\DetachedAdministratorTokenFixture; use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; /** * Testcase. * * @author Oliver Klee */ -class AdministratorTokenRepositoryTest extends KernelTestCase +class AdministratorTokenRepositoryTest extends WebTestCase { use DatabaseTestTrait; use SimilarDatesAssertionTrait; @@ -47,8 +49,7 @@ public function testFindReadsModelFromDatabase() $this->loadFixtures([DetachedAdministratorTokenFixture::class]); $id = 1; - // prePersist - $creationDate = new DateTime(); + $creationDate = new DateTime('2017-12-06 17:41:40'); $expiry = new DateTime('2017-06-22 16:43:29'); $key = 'cfdf64eecbbf336628b0f3071adba762'; @@ -62,21 +63,6 @@ public function testFindReadsModelFromDatabase() self::assertSame($key, $model->getKey()); } -// public function testCreatesAdministratorAssociationAsProxy() -// { -// $this->loadFixtures([AdministratorFixture::class, AdministratorTokenWithAdministratorFixture::class]); -// -// $tokenId = 1; -// $administratorId = 1; -// /** @var AdministratorToken $model */ -// $model = $this->repository->find($tokenId); -// $administrator = $model->getAdministrator(); -// -// self::assertInstanceOf(Administrator::class, $administrator); -// self::assertInstanceOf(Proxy::class, $administrator); -// self::assertSame($administratorId, $administrator->getId()); -// } - public function testCreationDateOfExistingModelStaysUnchangedOnUpdate() { $this->loadFixtures([DetachedAdministratorTokenFixture::class]); @@ -138,17 +124,16 @@ public function testFindOneUnexpiredByKeyNotFindsUnexpiredTokenWithNonMatchingKe self::assertNull($model); } -// public function testRemoveExpiredRemovesExpiredToken() -// { -// $this->loadFixtures([DetachedAdministratorTokenFixture::class]); -// -// $idOfExpiredToken = 1; -// $this->repository->removeExpired(); -// $this->entityManager->flush(); -// -// $token = $this->repository->find($idOfExpiredToken); -// self::assertNull($token); -// } + public function testRemoveExpiredRemovesExpiredToken() + { + $this->loadFixtures([DetachedAdministratorTokenFixture::class]); + + $idOfExpiredToken = 1; + $this->repository->removeExpired(); + + $token = $this->repository->find($idOfExpiredToken); + self::assertNull($token); + } public function testRemoveExpiredKeepsUnexpiredToken() {