Skip to content

Commit

Permalink
ISSUE-337: check
Browse files Browse the repository at this point in the history
  • Loading branch information
tatevikg1 committed Dec 11, 2024
1 parent dbdc96d commit 46df22f
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 63 deletions.
28 changes: 1 addition & 27 deletions config/config.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
26 changes: 26 additions & 0 deletions config/doctrine.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
>
<php>
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="5G"/>
<server name="KERNEL_CLASS" value="PhpList\Core\Core\ApplicationKernel"/>
</php>
</phpunit>
2 changes: 1 addition & 1 deletion src/Core/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
22 changes: 19 additions & 3 deletions src/Domain/Repository/Identity/AdministratorTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
class AdministratorTokenRepositoryTest extends KernelTestCase
class AdministratorTokenRepositoryTest extends WebTestCase
{
use DatabaseTestTrait;
use SimilarDatesAssertionTrait;
Expand All @@ -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';

Expand All @@ -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]);
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 46df22f

Please sign in to comment.