From a6df50072c1b96620e12e9c86c24daf46a6a1bc7 Mon Sep 17 00:00:00 2001 From: rajeshreeputra Date: Tue, 7 Jan 2025 18:24:35 +0530 Subject: [PATCH] ACMS-4237: Updated Site Studio tests to reflect the removal of the acquia_cms_common dependency. --- .../acquia_cms_site_studio.info.yml | 1 + ...lTest.php => FilterFormatCohesionTest.php} | 8 +- .../tests/src/Traits/PermissionsTrait.php | 138 ++++++++++++++++++ .../permissions/basic/user.role.developer.yml | 2 - 4 files changed, 142 insertions(+), 7 deletions(-) rename modules/acquia_cms_site_studio/tests/src/Functional/{FilterFormatFilteredHtmlTest.php => FilterFormatCohesionTest.php} (85%) create mode 100644 modules/acquia_cms_site_studio/tests/src/Traits/PermissionsTrait.php diff --git a/modules/acquia_cms_site_studio/acquia_cms_site_studio.info.yml b/modules/acquia_cms_site_studio/acquia_cms_site_studio.info.yml index 40756fac0..ec5a14428 100644 --- a/modules/acquia_cms_site_studio/acquia_cms_site_studio.info.yml +++ b/modules/acquia_cms_site_studio/acquia_cms_site_studio.info.yml @@ -4,6 +4,7 @@ description: 'Module for Site Studio Installation Code.' core_version_requirement: ^9.5 || ^10 || ^11 type: module dependencies: + - drupal:media - cohesion_base_styles # phpcs:ignore - cohesion_custom_styles # phpcs:ignore - cohesion_elements # phpcs:ignore diff --git a/modules/acquia_cms_site_studio/tests/src/Functional/FilterFormatFilteredHtmlTest.php b/modules/acquia_cms_site_studio/tests/src/Functional/FilterFormatCohesionTest.php similarity index 85% rename from modules/acquia_cms_site_studio/tests/src/Functional/FilterFormatFilteredHtmlTest.php rename to modules/acquia_cms_site_studio/tests/src/Functional/FilterFormatCohesionTest.php index 7de3b4afd..40da54ea2 100644 --- a/modules/acquia_cms_site_studio/tests/src/Functional/FilterFormatFilteredHtmlTest.php +++ b/modules/acquia_cms_site_studio/tests/src/Functional/FilterFormatCohesionTest.php @@ -10,7 +10,7 @@ * @group acquia_cms * @group acquia_cms_site_studio */ -class FilterFormatFilteredHtmlTest extends BrowserTestBase { +class FilterFormatCohesionTest extends BrowserTestBase { /** * {@inheritdoc} @@ -44,13 +44,11 @@ class FilterFormatFilteredHtmlTest extends BrowserTestBase { */ public function testFilterBlackListHtmlTags() { $assert_session = $this->assertSession(); - $account = $this->drupalCreateUser(); - $account->addRole('administrator'); - $account->save(); + $account = $this->drupalCreateUser(['administer filters']); $this->drupalLogin($account); // Visit the filter page. - $this->drupalGet('/admin/config/content/formats/manage/filtered_html'); + $this->drupalGet('/admin/config/content/formats/manage/cohesion'); $assert_session->statusCodeEquals(200); $filter_element = $assert_session->elementExists('css', '#edit-filters-black-list-html-tags-status'); $this->assertFalse($filter_element->isChecked(), 'Expect uncheck, but found checked.'); diff --git a/modules/acquia_cms_site_studio/tests/src/Traits/PermissionsTrait.php b/modules/acquia_cms_site_studio/tests/src/Traits/PermissionsTrait.php new file mode 100644 index 000000000..b27516a1e --- /dev/null +++ b/modules/acquia_cms_site_studio/tests/src/Traits/PermissionsTrait.php @@ -0,0 +1,138 @@ +getPermissions()); + $this->assertEmpty($missing_permissions, print_r($missing_permissions, TRUE)); + } + + /** + * Asserts that a role does not have a set of permissions. + * + * @param string $role + * The ID of the role to check. + * @param string[] $permissions + * An array of permissions the role is not expected to have. + */ + private function assertNoPermissions(string $role, array $permissions) : void { + $role = Role::load($role); + $granted_permissions = array_intersect($role->getPermissions(), $permissions); + $this->assertEmpty($granted_permissions); + } + + /** + * Gets the basePath for permission fixtures. + * + * @return string + * Returns basePath for fixtures. + */ + abstract public function getFixtureBasePath(): string; + + /** + * @throws \Exception + */ + protected function getPermissionsByRole(string $role): array { + $roleConfig = $this->getFixtureBasePath() . "/user.role.$role.yml"; + $permissions = []; + if (file_exists($roleConfig)) { + $user_role = Yaml::parse(file_get_contents($roleConfig)); + $permissions = $user_role['permissions'] ?? []; + } + if (!$permissions) { + throw new \Exception("Permissions yaml file not exist for role: $role."); + } + return $permissions; + } + + /** + * Tests the role which should exists. + * + * @param array $roles + * An array of roles which should exist. + * @param array $roles_not_exist + * An array of roles which shouldn't exist. + * + * @dataProvider providerRoleExistNotExist + */ + public function testRoleExistNotExist(array $roles, array $roles_not_exist = []) { + foreach ($roles as $role) { + $this->assertInstanceOf(Role::class, Role::load($role), "Role $role should exist."); + } + foreach ($roles_not_exist as $role) { + $this->assertNotInstanceOf(Role::class, Role::load($role), "Role $role should not exist."); + } + } + + /** + * Tests basic capabilities of our user roles. + * + * - Content authors, editors, and administrators should all be able to access + * the toolbar and the content overview. + * - User administrator should be able to access the toolbar and the user + * overview. + * + * @dataProvider providerBasicPermissions + */ + public function testBasicPermissions(string $role, array $permissions, array $no_permissions = []) { + $contrib_module_permissions = $this->contribModulePermissions($role); + if (!empty($contrib_module_permissions)) { + $permissions = array_merge($permissions, $contrib_module_permissions); + } + $this->assertPermissions($role, $permissions); + if ($no_permissions) { + $this->assertNoPermissions($role, $no_permissions); + } + } + + /** + * Assign permissions only if the below modules are enabled. + * + * @param string $role + * User role. + * + * @return array + * Returns list of permissions. + */ + public function contribModulePermissions($role): ?array { + $module_permissions = []; + if ($role === 'user_administrator') { + $module_handler = $this->container->get('module_handler'); + $permissions = [ + 'shield' => 'administer shield', + 'honeypot' => 'administer honeypot', + 'captcha' => 'administer CAPTCHA settings', + 'recaptcha' => 'administer recaptcha', + ]; + foreach ($permissions as $module => $permission) { + if ($module_handler->moduleExists($module)) { + $module_permissions += [$permission]; + } + } + } + + return $module_permissions; + } + + abstract public static function providerRoleExistNotExist(): array; + + abstract public static function providerBasicPermissions(): array; + +} diff --git a/modules/acquia_cms_site_studio/tests/src/fixtures/permissions/basic/user.role.developer.yml b/modules/acquia_cms_site_studio/tests/src/fixtures/permissions/basic/user.role.developer.yml index 741c98b5c..cd5ce7e62 100644 --- a/modules/acquia_cms_site_studio/tests/src/fixtures/permissions/basic/user.role.developer.yml +++ b/modules/acquia_cms_site_studio/tests/src/fixtures/permissions/basic/user.role.developer.yml @@ -43,6 +43,4 @@ permissions: - 'administer view templates' - 'administer website settings' - 'use text format cohesion' - - 'use text format filtered_html' - - 'use text format full_html' - 'view the administration theme'