Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functional tests for all actions #751

Merged
merged 3 commits into from Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .php-cs-fixer.dist.php
Expand Up @@ -35,6 +35,9 @@
'phpdoc_types_order' => [
'null_adjustment' => 'always_last',
],
'phpdoc_to_comment' => [
'ignored_tags' => ['psalm-suppress'],
],
'php_unit_internal_class' => false,
'php_unit_test_class_requires_covers' => false,
'no_superfluous_phpdoc_tags' => [
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Expand Up @@ -69,12 +69,14 @@
},
"require-dev": {
"ext-mongodb": "*",
"dama/doctrine-test-bundle": "^8.0",
"doctrine/doctrine-bundle": "^2.4",
"doctrine/mongodb-odm": "^2.3",
"doctrine/mongodb-odm-bundle": "^4.4 || ^5.0",
"doctrine/orm": "^2.18 || ^3.0",
"ergebnis/composer-normalize": "^2.0.1",
"symfony/browser-kit": "^6.4 || ^7.0",
"symfony/css-selector": "^6.4 || ^7.0",
"symfony/doctrine-bridge": "^6.4 || ^7.0",
"symfony/yaml": "^6.4 || ^7.0"
},
Expand Down
7 changes: 7 additions & 0 deletions phpunit.xml.dist
Expand Up @@ -3,12 +3,19 @@
<coverage/>
<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/>
<env name="APP_DEBUG" value="false"/>
<env name="APP_ENV" value="test"/>
<env name="KERNEL_CLASS" value="Nucleos\UserBundle\Tests\App\AppKernel"/>
<ini name="date.timezone" value="UTC"/>
</php>
<testsuites>
<testsuite name="UserBundle Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<extensions>
<bootstrap class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
</extensions>
<source>
<include>
<directory suffix=".php">./src/</directory>
Expand Down
8 changes: 6 additions & 2 deletions src/Action/ResetAction.php
Expand Up @@ -115,11 +115,15 @@ public function __invoke(Request $request, string $token): Response

private function changePassword(UserInterface $user, Resetting $formModel): void
{
$user->setPlainPassword($formModel->getPlainPassword());
$password = $formModel->getPlainPassword();

\assert(null !== $password);

$user->setPlainPassword($password);
$this->userManager->updateUser($user);

if (null !== $this->userManipulator) {
$this->userManipulator->changePassword($user->getUsername(), $formModel->getPlainPassword());
$this->userManipulator->changePassword($user->getUsername(), $password);
}
}
}
6 changes: 1 addition & 5 deletions src/DependencyInjection/NucleosUserExtension.php
Expand Up @@ -31,7 +31,7 @@
final class NucleosUserExtension extends Extension implements PrependExtensionInterface
{
/**
* @var array<string, array<string, string>>
* @var array<array{registry: string, tag: string, events: string[]}>
*/
private static array $doctrineDrivers = [
'orm' => [
Expand Down Expand Up @@ -103,10 +103,6 @@ public function load(array $configs, ContainerBuilder $container): void

if ($config['use_listener'] && isset(self::$doctrineDrivers[$config['db_driver']])) {
$listenerDefinition = $container->getDefinition('nucleos_user.user_listener');
if (isset(self::$doctrineDrivers[$config['db_driver']]['listener_class'])) {
$listenerDefinition->setClass(self::$doctrineDrivers[$config['db_driver']]['listener_class']);
}

foreach (self::$doctrineDrivers[$config['db_driver']]['events'] as $event) {
$listenerDefinition->addTag(self::$doctrineDrivers[$config['db_driver']]['tag'], ['event' => $event]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/NucleosUserBundle.php
Expand Up @@ -39,8 +39,8 @@ private function addRegisterMappingsPass(ContainerBuilder $container): void
if (class_exists(DoctrineOrmMappingsPass::class)) {
$container->addCompilerPass(DoctrineOrmMappingsPass::createXmlMappingDriver($mappings, ['nucleos_user.model_manager_name'], 'nucleos_user.backend_type_orm'));
}

if (class_exists(DoctrineMongoDBMappingsPass::class)) {
/** @psalm-suppress InternalClass, InternalMethod */
$container->addCompilerPass(DoctrineMongoDBMappingsPass::createXmlMappingDriver($mappings, ['nucleos_user.model_manager_name'], 'nucleos_user.backend_type_mongodb'));
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/Validator/Constraints/Pattern.php
Expand Up @@ -43,8 +43,6 @@ final class Pattern extends Constraint

/**
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*
* @param array<string, mixed> $options
*/
public function __construct(
mixed $options = null,
Expand Down
10 changes: 8 additions & 2 deletions tests/App/AppKernel.php
Expand Up @@ -13,6 +13,8 @@

namespace Nucleos\UserBundle\Tests\App;

use DAMA\DoctrineTestBundle\DAMADoctrineTestBundle;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Nucleos\UserBundle\NucleosUserBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
Expand All @@ -26,9 +28,9 @@ final class AppKernel extends Kernel
{
use MicroKernelTrait;

public function __construct()
public function __construct(string $environment = 'test', bool $debug = false)
{
parent::__construct('test', false);
parent::__construct($environment, $debug);
}

public function registerBundles(): iterable
Expand All @@ -39,6 +41,10 @@ public function registerBundles(): iterable

yield new SecurityBundle();

yield new DoctrineBundle();

yield new DAMADoctrineTestBundle();

yield new NucleosUserBundle();
}

Expand Down
12 changes: 11 additions & 1 deletion tests/App/Entity/TestGroup.php
Expand Up @@ -13,6 +13,16 @@

namespace Nucleos\UserBundle\Tests\App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Nucleos\UserBundle\Model\Group;

class TestGroup extends Group {}
#[ORM\Entity]
#[ORM\Table(name: 'user__group')]
class TestGroup extends Group
{
#[ORM\Id]
#[ORM\Column(type: Types::INTEGER)]
#[ORM\GeneratedValue]
protected ?int $id = null;
}
35 changes: 34 additions & 1 deletion tests/App/Entity/TestUser.php
Expand Up @@ -13,10 +13,43 @@

namespace Nucleos\UserBundle\Tests\App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Nucleos\UserBundle\Model\GroupInterface;
use Nucleos\UserBundle\Model\User;

/**
* @phpstan-extends User<GroupInterface>
*/
class TestUser extends User {}
#[ORM\Entity]
#[ORM\Table(name: 'user__user')]
class TestUser extends User
{
#[ORM\Id]
#[ORM\Column(type: Types::INTEGER)]
#[ORM\GeneratedValue]
protected int $id;

/**
* @var Collection<array-key, GroupInterface>
*/
#[ORM\ManyToMany(targetEntity: TestGroup::class)]
#[ORM\JoinTable(name: 'user__user_group')]
protected Collection $groups;
private static int $index = 1;

public function __construct()
{
parent::__construct();

$this->id = self::$index++;
$this->groups = new ArrayCollection();
}

public function getId(): int
{
return $this->id;
}
}
39 changes: 37 additions & 2 deletions tests/App/config/config.php
Expand Up @@ -25,12 +25,25 @@

$containerConfigurator->extension('framework', ['session' => ['storage_factory_id' => 'session.storage.factory.mock_file', 'handler_id' => null]]);

$containerConfigurator->extension('framework', [
'mailer' => [
'dsn' => 'null://null',
],
]);

$containerConfigurator->extension('twig', ['strict_variables' => true]);

$containerConfigurator->extension('twig', ['exception_controller' => null]);

$securityConfig = [
'firewalls' => ['main' => ['security' => true]],
'firewalls' => ['main' => [
'security' => true,
'form_login' => [
'login_path' => 'nucleos_user_security_login',
'check_path' => 'nucleos_user_security_check',
'default_target_path' => '/profile',
],
]],
];

// TODO: Remove if when dropping support of Symfony 5.4
Expand All @@ -50,6 +63,23 @@
'algorithm' => 'plaintext',
]]]);

$containerConfigurator->extension('doctrine', ['dbal' => ['url' => 'sqlite:///%kernel.cache_dir%/data.db', 'logging' => false, 'use_savepoints' => true]]);

$containerConfigurator->extension('doctrine', ['orm' => [
'auto_mapping' => true,
'mappings' => [
'App' => [
'is_bundle' => false,
'type' => 'attribute',
'dir' => '%kernel.project_dir%/Entity',
'prefix' => 'Nucleos\UserBundle\Tests\App\Entity',
'alias' => 'App',
],
],
]]);

$containerConfigurator->extension('nucleos_user', ['db_driver' => 'orm']);

$containerConfigurator->extension('nucleos_user', ['firewall_name' => 'main']);

$containerConfigurator->extension('nucleos_user', ['from_email' => 'no-reply@localhost']);
Expand All @@ -58,5 +88,10 @@

$containerConfigurator->extension('nucleos_user', ['group' => ['group_class' => TestGroup::class]]);

$containerConfigurator->extension('nucleos_user', ['loggedin' => ['route' => 'home']]);
$containerConfigurator->extension('nucleos_user', ['loggedin' => ['route' => 'nucleos_user_update_security']]);

$containerConfigurator->extension('nucleos_user', ['deletion' => [
]]);

$containerConfigurator->extension('dama_doctrine_test', ['enable_static_connection' => true, 'enable_static_meta_data_cache' => true, 'enable_static_query_cache' => true]);
};
76 changes: 76 additions & 0 deletions tests/Functional/Action/AccountDeletionWebTest.php
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the NucleosUserBundle package.
*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nucleos\UserBundle\Tests\Functional\Action;

use Nucleos\UserBundle\Action\AccountDeletionAction;
use Nucleos\UserBundle\Tests\Functional\DoctrineSetupTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

#[CoversClass(AccountDeletionAction::class)]
final class AccountDeletionWebTest extends WebTestCase
{
use DoctrineSetupTrait;

#[Test]
public function execute(): void
{
$client = self::createClient();

$this->persist(
$user = self::createUser(),
);

$client->loginUser($user);
$client->request('GET', '/delete');

self::assertResponseIsSuccessful();

$client->submitForm('account_deletion_form[delete]', [
'account_deletion_form[current_password]' => $user->getPlainPassword(),
'account_deletion_form[confirm]' => true,
]);

self::assertResponseRedirects('/logout');
$client->followRedirect();

self::assertNull($this->getSecurity()->getUser());
self::assertNull($this->getUser($user->getId()));
}

#[Test]
public function executeWithInvalidPassword(): void
{
$client = self::createClient();

$this->persist(
$user = self::createUser(),
);

$client->loginUser($user);
$client->request('GET', '/delete');

self::assertResponseIsSuccessful();

$client->submitForm('account_deletion_form[delete]', [
'account_deletion_form[current_password]' => 'wrong',
'account_deletion_form[confirm]' => true,
]);

self::assertResponseIsSuccessful();

self::assertSelectorTextContains('#account_deletion_form', 'The entered password is invalid');

self::assertNotNull($this->getUser($user->getId()));
}
}