-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VAGAOV-TEAM-97941: Adds Form Builder role and permission (#20192)
* Changes module name to include VA.gov prefix. * Adds `access form builder` permission. * Adds `Form Builder user` (form_builder_user) role with `access form builder` permission. * Adds unit test for permissions.yml * Updates annotation on existing test to be more accurate. * Updates routing: - Use RouteSubscriber rather than repeating same permission on every route. - Update to use new permission. * Updates existing tests to align with new permission. Note that these existing tests effectively test the RouteSubscriber functionality. * Small fix to `hasRequirement` check. Cannot take a specific permission as a second parameter. We'll set the permission whenever one is not set.
- Loading branch information
Showing
13 changed files
with
141 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
uuid: 2793e5e4-37aa-49b3-9c29-5e1c7ddc3772 | ||
langcode: en | ||
status: true | ||
dependencies: | ||
module: | ||
- va_gov_backend | ||
- va_gov_form_builder | ||
third_party_settings: | ||
va_gov_backend: | ||
vgb_description: "A user of the VA.gov Form Builder tool." | ||
id: form_builder_user | ||
label: "Form Builder user" | ||
weight: 14 | ||
is_admin: null | ||
permissions: | ||
- "access form builder" |
26 changes: 26 additions & 0 deletions
26
docroot/modules/custom/va_gov_form_builder/src/Routing/RouteSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace Drupal\va_gov_form_builder\Routing; | ||
|
||
use Drupal\Core\Routing\RouteSubscriberBase; | ||
use Symfony\Component\Routing\RouteCollection; | ||
|
||
/** | ||
* Listens to the dynamic route events to alter routes. | ||
*/ | ||
class RouteSubscriber extends RouteSubscriberBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function alterRoutes(RouteCollection $collection) { | ||
foreach ($collection->all() as $route_name => $route) { | ||
if (strpos($route_name, 'va_gov_form_builder.') === 0) { | ||
if (!$route->hasRequirement('_permission')) { | ||
$route->setRequirement('_permission', 'access form builder'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
docroot/modules/custom/va_gov_form_builder/va_gov_form_builder.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
docroot/modules/custom/va_gov_form_builder/va_gov_form_builder.permissions.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
access form builder: | ||
title: "Access Form Builder" | ||
description: "Allows the user to access the VA.gov Form Builder tool." | ||
restrict access: FALSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
docroot/modules/custom/va_gov_form_builder/va_gov_form_builder.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
va_gov_form_builder.route_subscriber: | ||
class: Drupal\va_gov_form_builder\Routing\RouteSubscriber | ||
tags: | ||
- { name: "event_subscriber" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
tests/phpunit/va_gov_form_builder/unit/PermissionsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
namespace tests\phpunit\va_gov_form_builder\unit; | ||
|
||
use DrupalFinder\DrupalFinder; | ||
use Symfony\Component\Yaml\Yaml; | ||
use Tests\Support\Classes\VaGovUnitTestBase; | ||
|
||
/** | ||
* Unit tests for va_gov_form_builder.permissions.yml. | ||
* | ||
* @group unit | ||
* @group all | ||
*/ | ||
class PermissionsTest extends VaGovUnitTestBase { | ||
|
||
/** | ||
* The path to the module being tested. | ||
* | ||
* @var string | ||
*/ | ||
private $modulePath = 'modules/custom/va_gov_form_builder'; | ||
|
||
/** | ||
* The permissions defined in permissions.yml. | ||
* | ||
* @var array<string, array{ | ||
* title: string, | ||
* description?: string, | ||
* restrict access?: bool, | ||
* }> | ||
*/ | ||
private $permissions; | ||
|
||
/** | ||
* Set up the environment for each test. | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
// Find Drupal root. | ||
$drupalFinder = new DrupalFinder(); | ||
$drupalFinder->locateRoot(__DIR__); | ||
$drupalRoot = $drupalFinder->getDrupalRoot(); | ||
|
||
$yamlFile = $drupalRoot . '/' . $this->modulePath . '/va_gov_form_builder.permissions.yml'; | ||
|
||
$this->permissions = Yaml::parseFile($yamlFile); | ||
} | ||
|
||
/** | ||
* Tests that the `access form builder` permission is present. | ||
*/ | ||
public function testPermissionPresent() { | ||
$this->assertArrayHasKey('access form builder', $this->permissions); | ||
|
||
// Assert expected keys are present. | ||
$formBuilderPermission = $this->permissions['access form builder']; | ||
$this->assertArrayHasKey('title', $formBuilderPermission); | ||
$this->assertArrayHasKey('description', $formBuilderPermission); | ||
$this->assertArrayHasKey('restrict access', $formBuilderPermission); | ||
|
||
// Assert `restrict access` is false. | ||
$restrictAccess = $formBuilderPermission['restrict access']; | ||
$this->assertFalse($restrictAccess); | ||
} | ||
|
||
} |