From f1b939db333f43e89b677434af961e2f4a34bf87 Mon Sep 17 00:00:00 2001 From: AntoineAresu Date: Fri, 15 Mar 2024 16:14:27 +0100 Subject: [PATCH] :white_check_mark: [Admin] Add smoke tests for admin --- .github/workflows/ci.yml | 2 + Makefile | 5 + composer.json | 1 + composer.lock | 175 +++++++++++++++++- config/bundles.php | 1 + phpstan.neon.dist | 1 - phpunit.xml.dist | 3 + src/DataFixtures/UserFixtures.php | 30 +++ symfony.lock | 24 +++ tests/AuthenticatedTestCase.php | 20 ++ .../Admin/AdminAvailabilityFunctionalTest.php | 120 ++++++++++++ 11 files changed, 379 insertions(+), 3 deletions(-) create mode 100644 src/DataFixtures/UserFixtures.php create mode 100644 tests/AuthenticatedTestCase.php create mode 100644 tests/Smoke/Admin/AdminAvailabilityFunctionalTest.php diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 69e575f3..904b7600 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,6 +46,8 @@ jobs: run: vendor/bin/php-cs-fixer fix src -v --dry-run --stop-on-violation --using-cache=no - name: Run Php-cs-fixer on tests run: vendor/bin/php-cs-fixer fix tests -v --dry-run --stop-on-violation --using-cache=no + - name: Load fixtures + run: php bin/console doctrine:fixtures:load --env=test -n - name: Execute tests (Unit and Feature tests) via PHPUnit run: vendor/bin/simple-phpunit tests - name: Run PhpStan diff --git a/Makefile b/Makefile index f3127ca4..3abc60d4 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +SYMFONY = symfony +CONSOLE = $(SYMFONY) console BIN = ./vendor/bin RECTOR = $(BIN)/rector PHPSTAN = $(BIN)/phpstan @@ -9,6 +11,9 @@ DEPLOYER = $(BIN)/dep cs: rector fix stan test +fixture: + @$(CONSOLE) doctrine:fixtures:load --env=test -n + test: @$(PHPUNIT) tests diff --git a/composer.json b/composer.json index e7d1fe26..d0929153 100644 --- a/composer.json +++ b/composer.json @@ -43,6 +43,7 @@ }, "require-dev": { "deployer/deployer": "^7.0.0", + "doctrine/doctrine-fixtures-bundle": "^3.5", "friendsofphp/php-cs-fixer": "3.*", "phpstan/phpstan": "1.10.*", "phpstan/phpstan-doctrine": "1.3.*", diff --git a/composer.lock b/composer.lock index 6e1e076a..5d70fccc 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bd4619c85f486873b49e050d334cd18e", + "content-hash": "df9613f281a250a88e6ec43b811a7294", "packages": [ { "name": "api-platform/core", @@ -8878,6 +8878,177 @@ ], "time": "2023-04-05T09:24:30+00:00" }, + { + "name": "doctrine/data-fixtures", + "version": "1.7.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/data-fixtures.git", + "reference": "bbcb74f2ac6dbe81a14b3c3687d7623490a0448f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/data-fixtures/zipball/bbcb74f2ac6dbe81a14b3c3687d7623490a0448f", + "reference": "bbcb74f2ac6dbe81a14b3c3687d7623490a0448f", + "shasum": "" + }, + "require": { + "doctrine/deprecations": "^0.5.3 || ^1.0", + "doctrine/persistence": "^2.0|^3.0", + "php": "^7.4 || ^8.0" + }, + "conflict": { + "doctrine/dbal": "<3.5 || >=5", + "doctrine/orm": "<2.14 || >=4", + "doctrine/phpcr-odm": "<1.3.0" + }, + "require-dev": { + "doctrine/annotations": "^1.12 || ^2", + "doctrine/coding-standard": "^12", + "doctrine/dbal": "^3.5 || ^4", + "doctrine/mongodb-odm": "^1.3.0 || ^2.0.0", + "doctrine/orm": "^2.14 || ^3", + "ext-sqlite3": "*", + "phpstan/phpstan": "^1.10", + "phpunit/phpunit": "^9.6.13 || ^10.4.2", + "symfony/cache": "^5.4 || ^6.3 || ^7", + "symfony/var-exporter": "^5.4 || ^6.3 || ^7", + "vimeo/psalm": "^5.9" + }, + "suggest": { + "alcaeus/mongo-php-adapter": "For using MongoDB ODM 1.3 with PHP 7 (deprecated)", + "doctrine/mongodb-odm": "For loading MongoDB ODM fixtures", + "doctrine/orm": "For loading ORM fixtures", + "doctrine/phpcr-odm": "For loading PHPCR ODM fixtures" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Common\\DataFixtures\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + } + ], + "description": "Data Fixtures for all Doctrine Object Managers", + "homepage": "https://www.doctrine-project.org", + "keywords": [ + "database" + ], + "support": { + "issues": "https://github.com/doctrine/data-fixtures/issues", + "source": "https://github.com/doctrine/data-fixtures/tree/1.7.0" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdata-fixtures", + "type": "tidelift" + } + ], + "time": "2023-11-24T11:18:31+00:00" + }, + { + "name": "doctrine/doctrine-fixtures-bundle", + "version": "3.5.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/DoctrineFixturesBundle.git", + "reference": "c808a0c85c38c8ee265cc8405b456c1d2b38567d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/DoctrineFixturesBundle/zipball/c808a0c85c38c8ee265cc8405b456c1d2b38567d", + "reference": "c808a0c85c38c8ee265cc8405b456c1d2b38567d", + "shasum": "" + }, + "require": { + "doctrine/data-fixtures": "^1.3", + "doctrine/doctrine-bundle": "^2.2", + "doctrine/orm": "^2.14.0 || ^3.0", + "doctrine/persistence": "^2.4|^3.0", + "php": "^7.4 || ^8.0", + "symfony/config": "^5.4|^6.0|^7.0", + "symfony/console": "^5.4|^6.0|^7.0", + "symfony/dependency-injection": "^5.4|^6.0|^7.0", + "symfony/deprecation-contracts": "^2.1|^3", + "symfony/doctrine-bridge": "^5.4|^6.0|^7.0", + "symfony/http-kernel": "^5.4|^6.0|^7.0" + }, + "conflict": { + "doctrine/dbal": "< 3" + }, + "require-dev": { + "doctrine/coding-standard": "^12", + "phpstan/phpstan": "^1.10.39", + "phpunit/phpunit": "^9.6.13", + "symfony/phpunit-bridge": "^6.3.6", + "vimeo/psalm": "^5.15" + }, + "type": "symfony-bundle", + "autoload": { + "psr-4": { + "Doctrine\\Bundle\\FixturesBundle\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Doctrine Project", + "homepage": "https://www.doctrine-project.org" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony DoctrineFixturesBundle", + "homepage": "https://www.doctrine-project.org", + "keywords": [ + "Fixture", + "persistence" + ], + "support": { + "issues": "https://github.com/doctrine/DoctrineFixturesBundle/issues", + "source": "https://github.com/doctrine/DoctrineFixturesBundle/tree/3.5.1" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdoctrine-fixtures-bundle", + "type": "tidelift" + } + ], + "time": "2023-11-19T12:48:54+00:00" + }, { "name": "friendsofphp/php-cs-fixer", "version": "v3.18.0", @@ -10088,5 +10259,5 @@ "ext-json": "*" }, "platform-dev": [], - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/config/bundles.php b/config/bundles.php index 93928b5b..8aa1a781 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -14,4 +14,5 @@ Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true], KnpU\OAuth2ClientBundle\KnpUOAuth2ClientBundle::class => ['all' => true], Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true], + Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true], ]; diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 32c4edbb..e360596f 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -8,7 +8,6 @@ parameters: level: 7 paths: - ./src - - ./tests bootstrapFiles: - ./vendor/bin/.phpunit/phpunit/vendor/autoload.php - ./vendor/autoload.php diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6c4bfed7..c71d8f17 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -15,6 +15,9 @@ + + + diff --git a/src/DataFixtures/UserFixtures.php b/src/DataFixtures/UserFixtures.php new file mode 100644 index 00000000..bdf33ed3 --- /dev/null +++ b/src/DataFixtures/UserFixtures.php @@ -0,0 +1,30 @@ +persist($this->createUser(self::ADMIN_USER_MAIL, ['ROLE_ADMIN'])); + $manager->persist($this->createUser(self::SUPER_ADMIN_USER_MAIL, ['ROLE_SUPER_ADMIN'])); + + $manager->flush(); + } + + /** + * @param string[] $roles + */ + private function createUser(string $email, array $roles): User + { + return (new User())->setEmail($email)->setRoles($roles)->setPassword(self::PASSWORD_HASH); + } +} diff --git a/symfony.lock b/symfony.lock index 16308e08..0078cf47 100644 --- a/symfony.lock +++ b/symfony.lock @@ -54,6 +54,18 @@ "src/Repository/.gitignore" ] }, + "doctrine/doctrine-fixtures-bundle": { + "version": "3.5", + "recipe": { + "repo": "github.com/symfony/recipes", + "branch": "main", + "version": "3.0", + "ref": "1f5514cfa15b947298df4d771e694e578d4c204d" + }, + "files": [ + "src/DataFixtures/AppFixtures.php" + ] + }, "doctrine/doctrine-migrations-bundle": { "version": "3.2", "recipe": { @@ -178,6 +190,18 @@ "phpdocumentor/type-resolver": { "version": "1.0.1" }, + "phpstan/phpstan": { + "version": "1.10", + "recipe": { + "repo": "github.com/symfony/recipes-contrib", + "branch": "main", + "version": "1.0", + "ref": "5e490cc197fb6bb1ae22e5abbc531ddc633b6767" + }, + "files": [ + "phpstan.dist.neon" + ] + }, "psr/cache": { "version": "1.0.1" }, diff --git a/tests/AuthenticatedTestCase.php b/tests/AuthenticatedTestCase.php new file mode 100644 index 00000000..404c50f2 --- /dev/null +++ b/tests/AuthenticatedTestCase.php @@ -0,0 +1,20 @@ +get(UserRepository::class); + $user = $userRepository->findOneBy(['email' => $email]); + + if ($user) { + $client->loginUser($user); + } + } +} diff --git a/tests/Smoke/Admin/AdminAvailabilityFunctionalTest.php b/tests/Smoke/Admin/AdminAvailabilityFunctionalTest.php new file mode 100644 index 00000000..2cd3acee --- /dev/null +++ b/tests/Smoke/Admin/AdminAvailabilityFunctionalTest.php @@ -0,0 +1,120 @@ +ensureKernelShutdown(); + $this->client = self::createClient(); + $this->urlGenerator = $this->getContainer()->get(AdminUrlGenerator::class); + parent::setUp(); + } + + /** + * @dataProvider adminUrlsProvider + * + * @param string[] $actions + */ + public function testAdminPageIsSuccessful(string $controllerClass, array $actions): void + { + $this->login($this->client, 'admin_user@mail.com'); + foreach ($actions as $action) { + $this->client->request( + 'GET', + $this->buildAdminUrl( + $controllerClass, + $action, + ), + ); + $this->assertLessThan(400, $this->client->getResponse()->getStatusCode()); + } + } + + public function adminUrlsProvider(): \Generator + { + yield [PermanenceCrudController::class, [Crud::PAGE_INDEX]]; + yield [WorkshopCrudController::class, [Crud::PAGE_INDEX]]; + } + + /** + * @dataProvider SuperAdminUrlsProvider + * + * @param string[] $actions + */ + public function testAdminCanNotSeeSuperAdminPage(string $controllerClass, array $actions): void + { + $this->login($this->client, 'admin_user@mail.com'); + foreach ($actions as $action) { + $this->client->request( + 'GET', + $this->buildAdminUrl( + $controllerClass, + $action, + ), + ); + $this->assertEquals(403, $this->client->getResponse()->getStatusCode()); + } + } + + /** + * @dataProvider SuperAdminUrlsProvider + * + * @param string[] $actions + */ + public function testSuperAdminPageIsSuccessful(string $controllerClass, array $actions): void + { + $this->login($this->client, 'super_admin_user@mail.com'); + foreach ($actions as $action) { + $this->client->request( + 'GET', + $this->buildAdminUrl( + $controllerClass, + $action, + ), + ); + $this->assertLessThan(400, $this->client->getResponse()->getStatusCode()); + } + } + + public function SuperAdminUrlsProvider(): \Generator + { + yield [CenterCrudController::class, [Crud::PAGE_INDEX]]; + yield [CenterTagCrudController::class, [Crud::PAGE_INDEX]]; + yield [TopicCrudController::class, [Crud::PAGE_INDEX]]; + yield [SkillCrudController::class, [Crud::PAGE_INDEX]]; + yield [AgeBreakpointCrudController::class, [Crud::PAGE_INDEX]]; + yield [DurationCrudController::class, [Crud::PAGE_INDEX]]; + yield [ParticipantKindCrudController::class, [Crud::PAGE_INDEX]]; + yield [EquipmentSupplierCrudController::class, [Crud::PAGE_INDEX]]; + yield [UsedEquipmentCrudController::class, [Crud::PAGE_INDEX]]; + } + + private function buildAdminUrl(string $controllerClass, string $action): string + { + return $this->urlGenerator + ->setController($controllerClass) + ->setAction($action) + ->generateUrl(); + } +}