Skip to content

Commit

Permalink
Rationalise tests to use group ids 1, 2, 3, 4, 5 and add test for adm…
Browse files Browse the repository at this point in the history
…in access (#459)

* Add a test to confirm a site admin cannot manage conent on another site, on which they are not a site admin.

* Update tests to use consistent group ids 1,2,3,4,5

* Fix php coding standards.

* Update tests to use group keys that match the group ids.

* Fix (hopefully) MicrositeCachingTest.
  • Loading branch information
finnlewis authored Apr 23, 2024
1 parent ee7774d commit 0621525
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ protected function setUp(): void {

$this->createMicrositeGroups([], 2);
$this->createMicrositeGroupsDomains($this->groups);
$this->domain1 = $this->getDomainFromGroup($this->groups[0]);
$this->domain2 = $this->getDomainFromGroup($this->groups[1]);
$this->domain1 = $this->getDomainFromGroup($this->groups[1]);
$this->domain2 = $this->getDomainFromGroup($this->groups[2]);

// Create some directory content.
$this->channel1 = $this->createDirectoryChannel($this->groups[0]);
$this->pages1 = $this->createDirectoryPages($this->channel1, $this->groups[0], 2);
$this->channel2 = $this->createDirectoryChannel($this->groups[1]);
$this->pages2 = $this->createDirectoryPages($this->channel2, $this->groups[1], 2);
$this->channel1 = $this->createDirectoryChannel($this->groups[1]);
$this->pages1 = $this->createDirectoryPages($this->channel1, $this->groups[1], 2);
$this->channel2 = $this->createDirectoryChannel($this->groups[2]);
$this->pages2 = $this->createDirectoryPages($this->channel2, $this->groups[2], 2);

// Index directory content.
$index = Index::load('localgov_directories_index_default');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ protected function setUp(): void {

$this->createMicrositeGroups([], 2);
$this->createMicrositeGroupsDomains($this->groups);
$this->domain1 = $this->getDomainFromGroup($this->groups[0]);
$this->domain2 = $this->getDomainFromGroup($this->groups[1]);
$this->domain1 = $this->getDomainFromGroup($this->groups[1]);
$this->domain2 = $this->getDomainFromGroup($this->groups[2]);

// Create a user.
$this->user = $this->drupalCreateUser();
$this->groups[0]->addMember($this->user, ['group_roles' => ['microsite-admin']]);
$this->groups[1]->addMember($this->user, ['group_roles' => ['microsite-admin']]);
$this->groups[2]->addMember($this->user, ['group_roles' => ['microsite-admin']]);
}

/**
Expand All @@ -87,14 +87,14 @@ public function testMicrositeDirectoryFacetForms() {
// Create facet type.
$type_listing_url = Url::fromRoute('entity.group_relationship.group_localgov_directories_facet_type.list',
[
'group' => $this->groups[0]->id(),
'group' => $this->groups[1]->id(),
],
)->toString();
$this->drupalGet($this->domain1->getUrl() . $type_listing_url);
$this->assertSession()->pageTextNotContains($facet_type);
$type_add_url = Url::fromRoute('entity.group_relationship.group_localgov_directories_facet_type.add',
[
'group' => $this->groups[0]->id(),
'group' => $this->groups[1]->id(),
],
)->toString();
$this->drupalGet($this->domain1->getUrl() . $type_add_url);
Expand All @@ -108,15 +108,15 @@ public function testMicrositeDirectoryFacetForms() {
// Create facet.
$facet_listing_url = Url::fromRoute('view.lgms_group_directory_facets.page',
[
'group' => $this->groups[0]->id(),
'group' => $this->groups[1]->id(),
'localgov_directories_facets_type' => $facet_type_id,
],
)->toString();
$this->drupalGet($this->domain1->getUrl() . $facet_listing_url);
$this->assertSession()->pageTextContains('There are no directory facets yet.');
$facet_add_url = Url::fromRoute('entity.group_relationship.group_localgov_directories_facets.add',
[
'group' => $this->groups[0]->id(),
'group' => $this->groups[1]->id(),
'localgov_directories_facets_type' => $facet_type_id,
],
)->toString();
Expand All @@ -137,7 +137,7 @@ public function testMicrositeDirectoryFacetForms() {
// Check facet type is listed.
$type_listing_url = Url::fromRoute('entity.group_relationship.group_localgov_directories_facet_type.list',
[
'group' => $this->groups[1]->id(),
'group' => $this->groups[2]->id(),
],
)->toString();
$this->drupalGet($this->domain2->getUrl() . $type_listing_url);
Expand All @@ -146,7 +146,7 @@ public function testMicrositeDirectoryFacetForms() {
// Check facet isn't listed.
$facet_listing_url = Url::fromRoute('view.lgms_group_directory_facets.page',
[
'group' => $this->groups[1]->id(),
'group' => $this->groups[2]->id(),
'localgov_directories_facets_type' => $facet_type_id,
],
)->toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ protected function setUp(): void {

$this->createMicrositeGroups([], 2);
$this->createMicrositeGroupsDomains($this->groups);
$this->domain1 = $this->getDomainFromGroup($this->groups[0]);
$this->domain2 = $this->getDomainFromGroup($this->groups[1]);
$this->domain1 = $this->getDomainFromGroup($this->groups[1]);
$this->domain2 = $this->getDomainFromGroup($this->groups[2]);

// Create some content.
$this->pages1 = $this->createEvents($this->groups[0], 2);
$this->pages2 = $this->createEvents($this->groups[1], 2);
$this->pages1 = $this->createEvents($this->groups[1], 2);
$this->pages2 = $this->createEvents($this->groups[2], 2);

// Index events.
$index = Index::load('localgov_events');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ protected function setUp(): void {

$this->createMicrositeGroups([], 2);
$this->createMicrositeGroupsDomains($this->groups);
$this->domain1 = $this->getDomainFromGroup($this->groups[0]);
$this->domain2 = $this->getDomainFromGroup($this->groups[1]);
$this->domain1 = $this->getDomainFromGroup($this->groups[1]);
$this->domain2 = $this->getDomainFromGroup($this->groups[2]);

// Create some directory content.
$this->newsroom1 = $this->createNewsroom($this->groups[0]);
$this->article1 = $this->createNewsArticles($this->newsroom1, $this->groups[0], 2);
$this->newsroom2 = $this->createNewsroom($this->groups[1]);
$this->article2 = $this->createNewsArticles($this->newsroom2, $this->groups[1], 2);
$this->newsroom1 = $this->createNewsroom($this->groups[1]);
$this->article1 = $this->createNewsArticles($this->newsroom1, $this->groups[1], 2);
$this->newsroom2 = $this->createNewsroom($this->groups[2]);
$this->article2 = $this->createNewsArticles($this->newsroom2, $this->groups[2], 2);

// Index directory content.
$index = Index::load('localgov_news');
Expand Down
145 changes: 145 additions & 0 deletions tests/src/Functional/GroupAdminAccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace Drupal\Tests\localgov_microsites_group\Functional;

use Drupal\Core\Test\AssertMailTrait;
use Drupal\Core\Url;
use Drupal\domain\DomainInterface;
use Drupal\localgov_microsites_group\DomainFromGroupTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\localgov_microsites_group\Traits\GroupCreationTrait;
use Drupal\Tests\localgov_microsites_group\Traits\InitializeGroupsTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;

/**
* Tests the group and group content access.
*
* @group localgov_microsites_group
*/
class GroupAdminAccessTest extends BrowserTestBase {

use UserCreationTrait;
use InitializeGroupsTrait;
use AssertMailTrait;
use GroupCreationTrait, DomainFromGroupTrait {
GroupCreationTrait::getEntityTypeManager insteadof DomainFromGroupTrait;
}
/**
* Will be removed when issue #3204455 on Domain Site Settings gets merged.
*
* See https://www.drupal.org/project/domain_site_settings/issues/3204455.
*
* @var bool
* @see \Drupal\Core\Config\Development\ConfigSchemaChecker
* phpcs:disable DrupalPractice.Objects.StrictSchemaDisabled.StrictConfigSchema
*/
protected $strictConfigSchema = FALSE;

/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'block',
'group',
'domain',
'localgov_microsites_group',
];

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

/**
* Regular authenticated User for tests.
*
* @var \Drupal\user\UserInterface
*/
protected $otherUser;

/**
* User administrator of group 1.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Test uses groups on control domain, so disable group sites.
$this->ownerUser = $this->createUser(['use group_sites admin mode']);
$this->adminUser1 = $this->createUser(['use group_sites admin mode']);
$this->adminUser2 = $this->createUser(['use group_sites admin mode']);
$this->memberUser1 = $this->createUser(['use group_sites admin mode']);
$this->memberUser2 = $this->createUser(['use group_sites admin mode']);
$this->otherUser = $this->createUser(['use group_sites admin mode']);
$this->createMicrositeGroups([
'uid' => $this->ownerUser->id(),
]);
$this->groups[1]->addMember($this->adminUser1, ['group_roles' => 'microsite-admin']);
$this->groups[2]->addMember($this->adminUser2, ['group_roles' => 'microsite-admin']);

$this->createMicrositeGroupsDomains($this->groups);
}

/**
* Test access to manage content.
*/
public function testGroupAdminAccess() {
$group1 = $this->groups[1];
$group2 = $this->groups[2];

// Admin1 can access content on site 1.
$group1_domain = $this->getDomainFromGroup($group1);
$group2_domain = $this->getDomainFromGroup($group2);
assert($group1_domain instanceof DomainInterface);
$this->drupalGet($group1_domain->getUrl() . Url::fromRoute('user.login')->toString());
$this->submitForm([
'name' => $this->adminUser1->getAccountName(),
'pass' => $this->adminUser1->passRaw,
], 'Log in');

// Confirm that adminUser1 can manage content and entities on group2.
$this->drupalGet($group1_domain->getUrl() . '/group/' . $group1->id());
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet($group1_domain->getUrl() . '/group/' . $group1->id() . '/edit');
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet($group1_domain->getUrl() . '/group/' . $group1->id() . '/nodes');
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet($group1_domain->getUrl() . '/group/' . $group1->id() . '/domain-settings');
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet($group1_domain->getUrl() . '/group/' . $group1->id() . '/menus');
$this->assertSession()->statusCodeEquals(200);
$this->drupalGet($group1_domain->getUrl() . '/group/' . $group1->id() . '/members');
$this->assertSession()->statusCodeEquals(200);

// First login to group2 domain as adminUser1.
$this->drupalGet($group2_domain->getUrl() . Url::fromRoute('user.login')->toString());
$this->submitForm([
'name' => $this->adminUser1->getAccountName(),
'pass' => $this->adminUser1->passRaw,
], 'Log in');

// Now confirm that adminUser1 cannot manage content and settings on group2.
$this->drupalGet($group2_domain->getUrl() . '/group/' . $group2->id());
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet($group2_domain->getUrl() . '/group/' . $group2->id() . '/edit');
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet($group2_domain->getUrl() . '/group/' . $group2->id() . '/nodes');
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet($group2_domain->getUrl() . '/group/' . $group2->id() . '/domain-settings');
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet($group2_domain->getUrl() . '/group/' . $group2->id() . '/menus');
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet($group2_domain->getUrl() . '/group/' . $group2->id() . '/members');
$this->assertSession()->statusCodeEquals(403);
$this->drupalGet($group2_domain->getUrl() . '/group/' . $group2->id() . '/nodes');

}

}
6 changes: 3 additions & 3 deletions tests/src/Functional/GroupInvitationAccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@ protected function setUp(): void {
$this->createMicrositeGroups([
'uid' => $this->ownerUser->id(),
]);
$this->groups[0]->addMember($this->adminUser, ['group_roles' => 'microsite-admin']);
$this->groups[0]->addMember($this->memberUser);
$this->groups[1]->addMember($this->adminUser, ['group_roles' => 'microsite-admin']);
$this->groups[1]->addMember($this->memberUser);
$this->createMicrositeGroupsDomains($this->groups);
}

/**
* Test content access when unique group access is enabled.
*/
public function testInvitationPermissions() {
$group = $this->groups[0];
$group = $this->groups[1];

// Admin can check invitations.
$this->drupalLogin($this->adminUser);
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Functional/LoginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function testLoginRedirect() {
// Can't use drupalLogin as we want to do it on the form with the microsite
// domain.
// @todo move this into a trait, probably on domain_group.
$ga1_domain = $this->getDomainFromGroup($this->groups[0]);
$ga1_domain = $this->getDomainFromGroup($this->groups[1]);
assert($ga1_domain instanceof DomainInterface);
$this->drupalGet($ga1_domain->getUrl() . Url::fromRoute('user.login')->toString());
$this->submitForm([
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Functional/MicrositeCachingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function setUp(): void {

// Create some microsites.
$this->group = $this->createGroup([
'label' => 'group-0',
'label' => 'group-1',
'type' => 'microsite',
]);
$this->domain = \Drupal::entityTypeManager()->getStorage('domain')->create([
Expand Down
10 changes: 5 additions & 5 deletions tests/src/Functional/MicrositeSitewideSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ protected function setUp(): void {

$this->createMicrositeGroups([], 2);
$this->createMicrositeGroupsDomains($this->groups);
$this->domain1 = $this->getDomainFromGroup($this->groups[0]);
$this->domain2 = $this->getDomainFromGroup($this->groups[1]);
$this->domain1 = $this->getDomainFromGroup($this->groups[1]);
$this->domain2 = $this->getDomainFromGroup($this->groups[2]);

// Create some content.
$this->pages1 = [
$this->createPage($this->groups[0]),
$this->createPage($this->groups[0]),
$this->createPage($this->groups[1]),
$this->createPage($this->groups[1]),
];
$this->pages2 = [$this->createPage($this->groups[1])];
$this->pages2 = [$this->createPage($this->groups[2])];

// Index directory content.
$index = Index::load('localgov_sitewide_search');
Expand Down
8 changes: 4 additions & 4 deletions tests/src/Functional/ModuleSettingsFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ protected function setUp(): void {
$this->createMicrositeGroups([
'uid' => $this->ownerUser->id(),
]);
$this->groups[0]->addMember($this->adminUser, ['group_roles' => 'microsite-admin']);
$this->groups[0]->addMember($this->memberUser);
$this->groups[1]->addMember($this->adminUser, ['group_roles' => 'microsite-admin']);
$this->groups[1]->addMember($this->memberUser);
$this->createMicrositeGroupsDomains($this->groups);
}

/**
* Test group domain settings form.
*/
public function testDomainGroupForm() {
$group = $this->groups[0];
$group = $this->groups[1];
// Going to domain group settings form.
$this->drupalLogin($this->adminUser);
\Drupal::service('group_sites.admin_mode')->setAdminMode(TRUE);
Expand Down Expand Up @@ -105,7 +105,7 @@ public function testDomainGroupForm() {
* Test access to group management pages.
*/
public function testGroupManagementAccess() {
$group = $this->groups[0];
$group = $this->groups[1];

// Test admin access.
$this->drupalLogin($this->adminUser);
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Traits/InitializeGroupsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ trait InitializeGroupsTrait {
* Groups generated also assigned to $this->groups.
*/
public function createMicrositeGroups(array $settings = [], int $count = 5) {
for ($i = 0; $i < $count; $i++) {
for ($i = 1; $i <= $count; $i++) {
$group = Group::create($settings + [
'type' => 'microsite',
'label' => $this->randomString(),
Expand Down

0 comments on commit 0621525

Please sign in to comment.