Skip to content

Commit

Permalink
ACMS-4373: Add permission test coverage to Tour module.
Browse files Browse the repository at this point in the history
  • Loading branch information
rajeshreeputra committed Dec 26, 2024
1 parent b882573 commit 8a65096
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
5 changes: 3 additions & 2 deletions modules/acquia_cms_tour/acquia_cms_tour.install
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use Drupal\user\Entity\Role;
*/
function acquia_cms_tour_install($is_syncing) {
if (!$is_syncing) {
if ($role = \Drupal::entityTypeManager()->getStorage('user_role')->load('content_administrator')) {
$role->grantPermission('access toolbar')->trustData()->save();
$role = \Drupal::entityTypeManager()->getStorage('user_role')->load('content_administrator');
if($role) {
$role->grantPermission('access acquia cms tour dashboard')->trustData()->save();
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions modules/acquia_cms_tour/acquia_cms_tour.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Drupal\Core\Entity\EntityInterface;
use Drupal\user\RoleInterface;

/**
* Implements hook_menu_links_discovered_alter().
Expand Down Expand Up @@ -60,10 +61,11 @@ function acquia_cms_tour_modules_uninstalled(array $modules) {
* Implements hook_cms_tour_entity_insert().
*/
function acquia_cms_tour_entity_insert(EntityInterface $entity) {
switch ($entity->id()) {
case 'content_administrator':
$role = \Drupal::entityTypeManager()->getStorage('user_role')->load('content_administrator');
$role->grantPermission('access acquia cms tour dashboard')->trustData()->save();
break;
if($entity instanceof RoleInterface &&
!$entity->isSyncing() &&
$entity->id() == 'content_administrator'
) {
$role = \Drupal::entityTypeManager()->getStorage('user_role')->load('content_administrator');
$role->grantPermission('access acquia cms tour dashboard')->trustData()->save();
}
}
53 changes: 53 additions & 0 deletions modules/acquia_cms_tour/tests/src/Functional/PermissionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Drupal\tests\acquia_cms_tour\Functional;

use Drupal\Tests\BrowserTestBase;

/**
* Tests the PermissionManager class.
*
* @group acquia_cms_tour
*/
class PermissionsTest extends BrowserTestBase {

/**
* {@inheritdoc}
*/
protected static $modules = [
'acquia_cms_tour'
];

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';

/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->drupalCreateRole([],'content_administrator');
$this->entityTypeManager = $this->container->get("entity_type.manager");
}

/**
* Tests permissions are granted to content_administrator role.
*/
public function testGrantPermissionsOnInstall(): void {
// Load the role and check its permissions.
$role_permissions = $this->entityTypeManager->getStorage('user_role')->load('content_administrator')->getPermissions();
$this->assertEquals(['access acquia cms tour dashboard'], $role_permissions);
}

}

0 comments on commit 8a65096

Please sign in to comment.