Skip to content
Snippets Groups Projects
Commit d1fe8ae5 authored by Kristiaan Van den Eynde's avatar Kristiaan Van den Eynde
Browse files

Issue #3448583: Add tests for the form alter and permission check on said alter

parent b41137a7
No related branches found
No related tags found
1 merge request!3Resolve #3448583 "Add tests for"
Pipeline #179146 passed
Group Context: Domain
@todo Write tests for:
- Permission and form alter
@todo Actually write this file :D
@todo Actually write this file.
<?php
namespace Drupal\Tests\group_context_domain\Functional;
use Drupal\Tests\group\Functional\GroupBrowserTestBase;
use Drupal\Tests\group_context_domain\Traits\GroupContextDomainTestTrait;
/**
* Provides a base class for Group Context: Domain functional tests.
*/
abstract class GroupContextDomainBrowserTestBase extends GroupBrowserTestBase {
use GroupContextDomainTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['domain', 'group_context_domain'];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
}
<?php
namespace Drupal\Tests\group_context_domain\Functional;
use Drupal\domain\DomainInterface;
use Drupal\group\PermissionScopeInterface;
use Drupal\user\RoleInterface;
/**
* Tests the form alterations of the domain entity form.
*
* @group group_context_domain
*/
class GroupContextDomainFormAlterTest extends GroupContextDomainBrowserTestBase {
/**
* Data to submit on a Domain form.
*
* @var array
*/
protected $formData = [
'hostname' => 'subdomain.example.com',
'name' => 'Test domain',
'validate_url' => FALSE,
];
/**
* Checks that no alterations take place if you do not have the permission.
*/
public function testFormWithoutPermission(): void {
$this->drupalLogin($this->drupalCreateUser(['administer domains']));
$this->drupalGet('admin/config/domain/add');
$this->assertSession()->pageTextNotContains('Select the group that represents this domain.');
}
/**
* Checks that the alterations take place if you have the permission.
*/
public function testFormWithPermission(): void {
$this->drupalLogin($this->drupalCreateUser(['administer domains', 'set domain group']));
$this->drupalGet('admin/config/domain/add');
$this->assertSession()->pageTextContains('Select the group that represents this domain.');
}
/**
* Checks that the domain's group can be set.
*
* @depends testFormWithPermission
*/
public function testSetDomainGroup(): void {
$group_type = $this->createGroupType();
$this->createGroupRole([
'group_type' => $group_type->id(),
'scope' => PermissionScopeInterface::OUTSIDER_ID,
'global_role' => RoleInterface::AUTHENTICATED_ID,
'permissions' => ['edit group'],
]);
$group = $this->createGroup(['type' => $group_type->id()]);
$domain = $this->createDomain();
$this->drupalLogin($this->drupalCreateUser(['administer domains', 'set domain group']));
$this->drupalGet('admin/config/domain/edit/' . $domain->id());
$this->submitForm(['group_uuid' => $group->uuid()] + $this->formData, 'Save');
$storage = $this->entityTypeManager->getStorage('domain');
$storage->resetCache();
$domain = $storage->load($domain->id());
assert($domain instanceof DomainInterface);
$this->assertSame($group->uuid(), $domain->getThirdPartySetting('group_context_domain', 'group_uuid'));
}
/**
* Checks that the domain's group can be unset.
*
* @depends testFormWithPermission
*/
public function testUnsetDomainGroup(): void {
$group_type = $this->createGroupType();
$this->createGroupRole([
'group_type' => $group_type->id(),
'scope' => PermissionScopeInterface::OUTSIDER_ID,
'global_role' => RoleInterface::AUTHENTICATED_ID,
'permissions' => ['edit group'],
]);
$group = $this->createGroup(['type' => $group_type->id()]);
$domain = $this->createDomain(['third_party_settings' => ['group_context_domain' => ['group_uuid' => $group->uuid()]]]);
$this->drupalLogin($this->drupalCreateUser(['administer domains', 'set domain group']));
$this->drupalGet('admin/config/domain/edit/' . $domain->id());
$this->submitForm(['group_uuid' => ''] + $this->formData, 'Save');
$storage = $this->entityTypeManager->getStorage('domain');
$storage->resetCache();
$domain = $storage->load($domain->id());
assert($domain instanceof DomainInterface);
$this->assertNull($domain->getThirdPartySetting('group_context_domain', 'group_uuid'));
}
}
......@@ -3,34 +3,18 @@
namespace Drupal\Tests\group_context_domain\Kernel;
use Drupal\Tests\group\Kernel\GroupKernelTestBase;
use Drupal\Tests\group_context_domain\Traits\GroupContextDomainTestTrait;
/**
* Defines an abstract test base for group kernel tests.
*/
abstract class GroupContextDomainKernelTestBase extends GroupKernelTestBase {
use GroupContextDomainTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['domain', 'group_context_domain'];
/**
* Creates a domain.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\domain\Entity\Domain
* The created domain entity.
*/
protected function createDomain(array $values = []) {
$storage = $this->entityTypeManager->getStorage('domain');
$domain = $storage->create($values + [
'id' => $this->randomMachineName(),
'label' => $this->randomString(),
]);
$storage->save($domain);
return $domain;
}
}
<?php
namespace Drupal\Tests\group_context_domain\Traits;
/**
* Functionality trait for a group_membership bundle class.
*/
trait GroupContextDomainTestTrait {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Creates a domain.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\domain\Entity\Domain
* The created domain entity.
*/
protected function createDomain(array $values = []) {
$storage = $this->entityTypeManager->getStorage('domain');
$domain = $storage->create($values + [
'id' => $this->randomMachineName(),
'label' => $this->randomString(),
]);
$storage->save($domain);
return $domain;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment