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

Issue #3177542 by kristiaanvandeneynde, Matroskeen, ekes: Provide test trait with helper methods

parent c2ed0ddd
No related branches found
No related tags found
2 merge requests!193Issue #3304728 by kristiaanvandeneynde: Add member page missing due to...,!111Issue #3397021 by adamfranco: Add entity_mappings for phpstan-drupal
Pipeline #180309 passed with warnings
......@@ -12,7 +12,7 @@ parameters:
- "#Drupal calls should be avoided in classes, use dependency injection instead#"
# Can only remove use of membership loader in 4.0.0
- "#^Fetching class constant class of deprecated class Drupal\\\\group\\\\GroupMembership\\:#"
- "#^Fetching class constant class of deprecated class Drupal\\\\group\\\\GroupMembershipLoaderInterface\\:#"
- "#^Fetching class constant class of deprecated interface Drupal\\\\group\\\\GroupMembershipLoaderInterface\\:#"
- "#has typehint with deprecated class Drupal\\\\group\\\\GroupMembership\\:#"
- "#has typehint with deprecated interface Drupal\\\\group\\\\GroupMembershipLoaderInterface\\:#"
- "#^Constructor of class Drupal\\\\group\\\\GroupMembershipLoader has an unused parameter#"
......
<?php
declare(strict_types=1);
namespace Drupal\Tests\group\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\group\Traits\GroupTestTrait;
/**
* Provides a base class for Group functional tests.
*/
abstract class GroupBrowserTestBase extends BrowserTestBase {
use GroupTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = ['group'];
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
* {@inheritdoc}
*/
protected $entityTypeManager;
protected $defaultTheme = 'stark';
/**
* A test user with group creation rights.
......@@ -28,11 +31,6 @@ abstract class GroupBrowserTestBase extends BrowserTestBase {
*/
protected $groupCreator;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
......@@ -67,61 +65,4 @@ abstract class GroupBrowserTestBase extends BrowserTestBase {
];
}
/**
* Creates a group.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\Group
* The created group entity.
*/
protected function createGroup($values = []) {
$group = $this->entityTypeManager->getStorage('group')->create($values + [
'label' => $this->randomMachineName(),
]);
$group->enforceIsNew();
$group->save();
return $group;
}
/**
* Creates a group type.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\GroupType
* The created group type entity.
*/
protected function createGroupType(array $values = []) {
$storage = $this->entityTypeManager->getStorage('group_type');
$group_type = $storage->create($values + [
'id' => $this->randomMachineName(),
'label' => $this->randomString(),
'creator_wizard' => FALSE,
]);
$storage->save($group_type);
return $group_type;
}
/**
* Creates a group role.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\GroupRole
* The created group role entity.
*/
protected function createGroupRole(array $values = []) {
$storage = $this->entityTypeManager->getStorage('group_role');
$group_role = $storage->create($values + [
'id' => $this->randomMachineName(),
'label' => $this->randomString(),
]);
$storage->save($group_role);
return $group_role;
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\group\Kernel;
use Drupal\Core\Session\AccountInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\Tests\group\Traits\GroupTestTrait;
/**
* Defines an abstract test base for group kernel tests.
*/
abstract class GroupKernelTestBase extends EntityKernelTestBase {
use GroupTestTrait;
/**
* {@inheritdoc}
*/
......@@ -45,66 +51,8 @@ abstract class GroupKernelTestBase extends EntityKernelTestBase {
* @return \Drupal\Core\Session\AccountInterface
* The current user.
*/
protected function getCurrentUser() {
protected function getCurrentUser(): AccountInterface {
return $this->container->get('current_user')->getAccount();
}
/**
* Creates a group.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\Group
* The created group entity.
*/
protected function createGroup(array $values = []) {
$storage = $this->entityTypeManager->getStorage('group');
$group = $storage->create($values + [
'label' => $this->randomString(),
]);
$group->enforceIsNew();
$storage->save($group);
return $group;
}
/**
* Creates a group type.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\GroupType
* The created group type entity.
*/
protected function createGroupType(array $values = []) {
$storage = $this->entityTypeManager->getStorage('group_type');
$group_type = $storage->create($values + [
'id' => $this->randomMachineName(),
'label' => $this->randomString(),
'creator_wizard' => FALSE,
]);
$storage->save($group_type);
return $group_type;
}
/**
* Creates a group role.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\GroupRole
* The created group role entity.
*/
protected function createGroupRole(array $values = []) {
$storage = $this->entityTypeManager->getStorage('group_role');
$group_role = $storage->create($values + [
'id' => $this->randomMachineName(),
'label' => $this->randomString(),
]);
$storage->save($group_role);
return $group_role;
}
}
......@@ -147,7 +147,7 @@ abstract class QueryAlterTestBase extends GroupKernelTestBase {
bool $insider_published_simple_check,
bool $insider_published_owner_check,
bool $insider_unpublished_simple_check,
bool $insider_unpublished_owner_check
bool $insider_unpublished_owner_check,
) {
// Run some sanity checks on the passed in data and aggregate info.
$checks_status = $checks_owner = $checks_member = FALSE;
......
......@@ -28,7 +28,9 @@ class GroupIdArgumentTest extends GroupViewsKernelTestBase {
$view->setDisplay();
$this->createGroup();
$group2 = $this->createGroup();
// Use a machine name instead of string for the label because the view
// title escapes HTML and that may lead to random test fails.
$group2 = $this->createGroup(['label' => $this->randomMachineName()]);
$view->preview();
$this->assertEquals(2, count($view->result), 'Found the expected number of results.');
......
<?php
declare(strict_types=1);
namespace Drupal\Tests\group\Kernel\Views;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\PermissionScopeInterface;
use Drupal\Tests\group\Traits\GroupTestTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\RoleInterface;
use Drupal\user\UserInterface;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
......@@ -13,6 +18,10 @@ use Drupal\views\Views;
*/
abstract class GroupViewsKernelTestBase extends ViewsKernelTestBase {
use GroupTestTrait {
createGroup as traitCreateGroup;
}
/**
* {@inheritdoc}
*/
......@@ -27,13 +36,6 @@ abstract class GroupViewsKernelTestBase extends ViewsKernelTestBase {
'variationcache',
];
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The group type to use in testing.
*
......@@ -81,7 +83,7 @@ abstract class GroupViewsKernelTestBase extends ViewsKernelTestBase {
* @return \Drupal\views\ResultRow[]
* A list of view results.
*/
protected function getViewResults() {
protected function getViewResults(): array {
$view = Views::getView(reset($this::$testViews));
$view->setDisplay();
......@@ -93,61 +95,11 @@ abstract class GroupViewsKernelTestBase extends ViewsKernelTestBase {
}
/**
* Creates a group.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\Group
* The created group entity.
*/
protected function createGroup($values = []) {
$group = $this->entityTypeManager->getStorage('group')->create($values + [
'type' => $this->groupType->id(),
'label' => $this->randomMachineName(),
]);
$group->enforceIsNew();
$group->save();
return $group;
}
/**
* Creates a group type.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\GroupType
* The created group type entity.
*/
protected function createGroupType(array $values = []) {
$storage = $this->entityTypeManager->getStorage('group_type');
$group_type = $storage->create($values + [
'id' => $this->randomMachineName(),
'label' => $this->randomString(),
'creator_wizard' => FALSE,
]);
$storage->save($group_type);
return $group_type;
}
/**
* Creates a group role.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\GroupRole
* The created group role entity.
* {@inheritdoc}
*/
protected function createGroupRole(array $values = []) {
$storage = $this->entityTypeManager->getStorage('group_role');
$group_role = $storage->create($values + [
'id' => $this->randomMachineName(),
'label' => $this->randomString(),
]);
$storage->save($group_role);
return $group_role;
protected function createGroup(array $values = []): GroupInterface {
$values += ['type' => $this->groupType->id()];
return $this->traitCreateGroup($values);
}
/**
......@@ -156,10 +108,10 @@ abstract class GroupViewsKernelTestBase extends ViewsKernelTestBase {
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\user\Entity\User
* @return \Drupal\user\UserInterface
* The created user entity.
*/
protected function createUser($values = []) {
protected function createUser($values = []): UserInterface {
$account = $this->entityTypeManager->getStorage('user')->create($values + [
'name' => $this->randomMachineName(),
'status' => 1,
......
<?php
declare(strict_types=1);
namespace Drupal\Tests\group\Traits;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Entity\GroupRoleInterface;
use Drupal\group\Entity\GroupTypeInterface;
/**
* Provides common helper methods for Group module tests.
*/
trait GroupTestTrait {
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Creates a group.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\GroupInterface
* The created group entity.
*/
protected function createGroup(array $values = []): GroupInterface {
$storage = $this->entityTypeManager()->getStorage('group');
$group = $storage->create($values + [
'label' => $this->randomString(),
]);
$group->enforceIsNew();
$storage->save($group);
return $group;
}
/**
* Creates a group type.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\GroupTypeInterface
* The created group type entity.
*/
protected function createGroupType(array $values = []): GroupTypeInterface {
$storage = $this->entityTypeManager()->getStorage('group_type');
$group_type = $storage->create($values + [
'id' => $this->randomMachineName(),
'label' => $this->randomString(),
'creator_wizard' => FALSE,
]);
$storage->save($group_type);
return $group_type;
}
/**
* Creates a group role.
*
* @param array $values
* (optional) The values used to create the entity.
*
* @return \Drupal\group\Entity\GroupRoleInterface
* The created group role entity.
*/
protected function createGroupRole(array $values = []): GroupRoleInterface {
$storage = $this->entityTypeManager()->getStorage('group_role');
$group_role = $storage->create($values + [
'id' => $this->randomMachineName(),
'label' => $this->randomString(),
]);
$storage->save($group_role);
return $group_role;
}
/**
* Retrieves the entity type manager.
*
* @return \Drupal\Core\Entity\EntityTypeManagerInterface
* The entity type manager.
*/
protected function entityTypeManager(): EntityTypeManagerInterface {
if (!isset($this->entityTypeManager)) {
$this->entityTypeManager = \Drupal::entityTypeManager();
}
return $this->entityTypeManager;
}
}
......@@ -825,7 +825,7 @@ class AccessControlTest extends UnitTestCase {
$definition,
PermissionProviderInterface $permission_provider,
EntityTypeManagerInterface $entity_type_manager = NULL,
$set_up_chain = FALSE
$set_up_chain = FALSE,
) {
$this->assertNotEmpty($definition->getEntityTypeId());
......
......@@ -230,7 +230,7 @@ namespace Drupal\Tests\group\Unit {
ModuleHandlerInterface $module_handler = NULL,
AccountProxyInterface $current_user = NULL,
EntityTypeManagerInterface $entity_type_manager = NULL,
GroupRelationTypeManagerInterface $relation_type_manager = NULL
GroupRelationTypeManagerInterface $relation_type_manager = NULL,
) {
if (!isset($module_handler)) {
$module_handler = $this->prophesize(ModuleHandlerInterface::class)->reveal();
......
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