Skip to content

Commit

Permalink
Add DLC tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jskowronski39 committed Nov 1, 2023
1 parent 0aa2c6d commit 3e504ad
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Form/Dlc/Dto/DlcFormDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Symfony\Component\Validator\Constraints as Assert;

#[SteamStoreArma3DlcUrl(errorPath: 'url')]
#[UniqueSteamStoreDlc]
#[UniqueDirectoryDlc]
#[UniqueSteamStoreDlc(errorPath: 'url')]
#[UniqueDirectoryDlc(errorPath: 'directory')]
class DlcFormDto extends AbstractFormDto
{
protected ?UuidInterface $id = null;
Expand All @@ -25,6 +25,7 @@ class DlcFormDto extends AbstractFormDto
#[Assert\Length(min: 1, max: 255)]
protected ?string $description = null;

#[Assert\NotBlank]
protected ?string $url = null;

#[Assert\NotBlank]
Expand Down
3 changes: 3 additions & 0 deletions src/Validator/Dlc/SteamStoreArma3DlcUrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public function validate(mixed $value, Constraint $constraint): void
}

$url = $value->getUrl();
if ('' === $url || null === $url) {
return;
}

try {
$appId = SteamHelper::appUrlToAppId($url);
Expand Down
6 changes: 5 additions & 1 deletion src/Validator/Dlc/UniqueDirectoryDlcValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ public function validate(mixed $value, Constraint $constraint): void
}

$directory = $value->getDirectory();
if ('' === $directory || null === $directory) {
return;
}

$id = $value->getId();
if (!$directory || $this->isColumnValueUnique(Dlc::class, ['directory' => $directory], $id)) {
if ($this->isColumnValueUnique(Dlc::class, ['directory' => $directory], $id)) {
return;
}

Expand Down
221 changes: 221 additions & 0 deletions tests/functional/Web/Dlc/CreateDlcCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<?php

declare(strict_types=1);

namespace App\Tests\Functional\Web\Dlc;

use App\DataFixtures\User\RegularUserFixture;
use App\Entity\Dlc\Dlc;
use App\Entity\User\User;
use App\Service\SteamApiClient\Helper\SteamHelper;
use App\Tests\FunctionalTester;
use Symfony\Component\HttpFoundation\Response;

class CreateDlcCest
{
public function _before(FunctionalTester $I): void
{
$I->stopFollowingRedirects();
$I->freezeTime('2020-01-01T00:00:00+00:00');
}

public function createDlcAsUnauthenticatedUser(FunctionalTester $I): void
{
$I->amOnPage('/dlc/create');
$I->seeResponseRedirectsToDiscordAuth();
}

public function createDlcAsUnauthorizedUser(FunctionalTester $I): void
{
$I->amDiscordAuthenticatedAs(RegularUserFixture::ID);

$I->amOnPage('/dlc/create');
$I->seeResponseCodeIs(Response::HTTP_FORBIDDEN);
}

public function createDlcAsAuthorizedUser(FunctionalTester $I): void
{
$user = $I->amDiscordAuthenticatedAs(RegularUserFixture::ID, function (User $user): void {
$user->getPermissions()->dlcCreate = true;
});

$I->amOnPage('/dlc/create');

// Default form values
$I->seeInField('Steam Store URL', '');
$I->seeInField('DLC directory', '');
$I->seeInField('DLC name', '');
$I->seeInField('DLC description', '');

// Fill form
$I->fillField('Steam Store URL', 'https://store.steampowered.com/app/1681170/Arma_3_Creator_DLC_Western_Sahara');
$I->fillField('DLC directory', 'ws');
$I->fillField('DLC name', 'Western Sahara');
$I->fillField('DLC description', 'Western Sahara Arma 3 DLC');
$I->click('Create DLC');

$I->seeResponseRedirectsTo('/dlc/list');

/** @var Dlc $dlc */
$dlc = $I->grabEntityFromRepository(Dlc::class, ['appId' => 1681170]);
$I->assertSame(1681170, $dlc->getAppId());
$I->assertSame('ws', $dlc->getDirectory());
$I->assertSame('Western Sahara', $dlc->getName());
$I->assertSame('Western Sahara Arma 3 DLC', $dlc->getDescription());

$I->assertSame('2020-01-01T00:00:00+00:00', $dlc->getCreatedAt()->format(DATE_ATOM));
// $I->assertSame($user, $dlc->getCreatedBy()); // FIXME
$I->assertSame(null, $dlc->getLastUpdatedAt());
$I->assertSame(null, $dlc->getLastUpdatedBy());
}

public function createDlcAsAuthorizedUserWithNameProvidedBySteamWorkshop(FunctionalTester $I): void
{
$user = $I->amDiscordAuthenticatedAs(RegularUserFixture::ID, function (User $user): void {
$user->getPermissions()->dlcCreate = true;
});

$I->amOnPage('/dlc/create');

// Default form values
$I->seeInField('Steam Store URL', '');
$I->seeInField('DLC directory', '');
$I->seeInField('DLC name', '');
$I->seeInField('DLC description', '');

// Fill form
$I->fillField('Steam Store URL', 'https://store.steampowered.com/app/1681170/Arma_3_Creator_DLC_Western_Sahara');
$I->fillField('DLC directory', 'ws');
$I->fillField('DLC name', '');
$I->fillField('DLC description', '');
$I->click('Create DLC');

$I->seeResponseRedirectsTo('/dlc/list');

/** @var Dlc $dlc */
$dlc = $I->grabEntityFromRepository(Dlc::class, ['appId' => 1681170]);
$I->assertSame(1681170, $dlc->getAppId());
$I->assertSame('ws', $dlc->getDirectory());
$I->assertSame('Arma 3 Creator DLC: Western Sahara', $dlc->getName()); // From Steam Workshop
$I->assertSame(null, $dlc->getDescription());

$I->assertSame('2020-01-01T00:00:00+00:00', $dlc->getCreatedAt()->format(DATE_ATOM));
// $I->assertSame($user, $dlc->getCreatedBy()); // FIXME
$I->assertSame(null, $dlc->getLastUpdatedAt());
$I->assertSame(null, $dlc->getLastUpdatedBy());
}

public function createDlcAsAuthorizedUserWhenDlcAlreadyExists(FunctionalTester $I): void
{
$I->amDiscordAuthenticatedAs(RegularUserFixture::ID, function (User $user): void {
$user->getPermissions()->dlcCreate = true;
});

$I->amOnPage('/dlc/create');

$url = 'https://store.steampowered.com/app/1227700/Arma_3_Creator_DLC_SOG_Prairie_Fire/';
$I->fillField('Steam Store URL', $url);
$I->fillField('DLC directory', 'vn');
$I->click('Create DLC');

$message = sprintf('DLC associated with url "%s" already exist.', $url);
$I->canSeeFormErrorMessage('url', $message);
$I->canSeeFormErrorMessage('directory', 'DLC associated with directory "vn" already exist.');
}

public function createDlcAsAuthorizedUserWithInvalidDlcUrl(FunctionalTester $I): void
{
$I->amDiscordAuthenticatedAs(RegularUserFixture::ID, function (User $user): void {
$user->getPermissions()->dlcCreate = true;
});

$I->amOnPage('/dlc/create');

$I->fillField('Steam Store URL', 'https://example.com');
$I->fillField('DLC directory', 'ws');
$I->click('Create DLC');

$I->canSeeFormErrorMessage('url', 'Invalid Steam Store DLC url.');
}

public function createDlcAsAuthorizedUserWhenDlcDoesNotExist(FunctionalTester $I): void
{
$I->amDiscordAuthenticatedAs(RegularUserFixture::ID, function (User $user): void {
$user->getPermissions()->dlcCreate = true;
});

$I->amOnPage('/dlc/create');

$url = SteamHelper::appIdToAppUrl(9999999);
$I->fillField('Steam Store URL', $url);
$I->fillField('DLC directory', 'ws');
$I->click('Create DLC');

$I->canSeeFormErrorMessage('url', 'DLC not found.');
}

public function createDlcAsAuthorizedUserWhenUrlIsNotAnArma3Dlc(FunctionalTester $I): void
{
$I->amDiscordAuthenticatedAs(RegularUserFixture::ID, function (User $user): void {
$user->getPermissions()->dlcCreate = true;
});

$I->amOnPage('/dlc/create');

$url = SteamHelper::appIdToAppUrl(2138330);
$I->fillField('Steam Store URL', $url);
$I->fillField('DLC directory', 'ws');
$I->click('Create DLC');

$I->canSeeFormErrorMessage('url', 'Url is not an Arma 3 DLC.');
}

public function createDlcAsAuthorizedUserWithInvalidDirectoryName(FunctionalTester $I): void
{
$I->amDiscordAuthenticatedAs(RegularUserFixture::ID, function (User $user): void {
$user->getPermissions()->dlcCreate = true;
});

$I->amOnPage('/dlc/create');

$I->fillField('Steam Store URL', 'https://store.steampowered.com/app/1681170/Arma_3_Creator_DLC_Western_Sahara');
$I->fillField('DLC directory', 'w/s');
$I->click('Create DLC');

$I->canSeeFormErrorMessage('directory', 'Invalid directory name.');
}

public function createDlcAsAuthorizedUserWithoutRequiredData(FunctionalTester $I): void
{
$I->amDiscordAuthenticatedAs(RegularUserFixture::ID, function (User $user): void {
$user->getPermissions()->dlcCreate = true;
});

$I->amOnPage('/dlc/create');

$I->fillField('Steam Store URL', '');
$I->fillField('DLC directory', '');
$I->click('Create DLC');

$I->canSeeFormErrorMessage('url', 'This value should not be blank.');
$I->canSeeFormErrorMessage('directory', 'This value should not be blank.');
}

public function createDlcAsAuthorizedUserWithDataTooLong(FunctionalTester $I): void
{
$I->amDiscordAuthenticatedAs(RegularUserFixture::ID, function (User $user): void {
$user->getPermissions()->dlcCreate = true;
});

$I->amOnPage('/dlc/create');

$I->fillField('Steam Store URL', 'https://store.steampowered.com/app/1681170/Arma_3_Creator_DLC_Western_Sahara');
$I->fillField('DLC directory', 'ws');
$I->fillField('DLC name', str_repeat('a', 256));
$I->fillField('DLC description', str_repeat('a', 256));
$I->click('Create DLC');

$I->canSeeFormErrorMessage('name', 'This value is too long. It should have 255 characters or less.');
$I->canSeeFormErrorMessage('description', 'This value is too long. It should have 255 characters or less.');
}
}
53 changes: 53 additions & 0 deletions tests/functional/Web/Dlc/DeleteDlcCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace App\Tests\Functional\Web\Dlc;

use App\DataFixtures\Dlc\SogPrairieFireDlcFixture;
use App\DataFixtures\User\RegularUserFixture;
use App\Entity\Dlc\Dlc;
use App\Entity\User\User;
use App\Tests\FunctionalTester;
use Symfony\Component\HttpFoundation\Response;

class DeleteDlcCest
{
public function _before(FunctionalTester $I): void
{
$I->stopFollowingRedirects();
}

public function deleteDlcAsUnauthenticatedUser(FunctionalTester $I): void
{
$id = SogPrairieFireDlcFixture::ID;
$I->amOnPage(sprintf('/dlc/%s/delete', $id));
$I->seeResponseRedirectsToDiscordAuth();

$I->seeInRepository(Dlc::class, ['appId' => SogPrairieFireDlcFixture::APP_ID]);
}

public function deleteDlcAsUnauthorizedUser(FunctionalTester $I): void
{
$I->amDiscordAuthenticatedAs(RegularUserFixture::ID);

$id = SogPrairieFireDlcFixture::ID;
$I->amOnPage(sprintf('/dlc/%s/delete', $id));
$I->seeResponseCodeIs(Response::HTTP_FORBIDDEN);

$I->seeInRepository(Dlc::class, ['appId' => SogPrairieFireDlcFixture::APP_ID]);
}

public function deleteDlcAsAuthorizedUser(FunctionalTester $I): void
{
$I->amDiscordAuthenticatedAs(RegularUserFixture::ID, function (User $user): void {
$user->getPermissions()->dlcDelete = true;
});

$id = SogPrairieFireDlcFixture::ID;
$I->amOnPage(sprintf('/dlc/%s/delete', $id));
$I->seeResponseRedirectsTo('/dlc/list');

$I->dontSeeInRepository(Dlc::class, ['appId' => SogPrairieFireDlcFixture::APP_ID]);
}
}
Loading

0 comments on commit 3e504ad

Please sign in to comment.