diff --git a/og.module b/og.module index 8ce39c756..49ef09b13 100755 --- a/og.module +++ b/og.module @@ -204,18 +204,23 @@ function og_entity_create_access(AccountInterface $account, array $context, $bun $required = FALSE; $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity_type_id, $bundle); - foreach ($field_definitions as $field_name => $field_definition) { + foreach ($field_definitions as $field_definition) { /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */ if (!\Drupal::service('og.group_audience_helper')->isGroupAudienceField($field_definition)) { continue; } - $handler = Og::getSelectionHandler($field_definition); + $options = [ + 'target_type' => $field_definition->getFieldStorageDefinition()->getSetting('target_type'), + 'handler' => $field_definition->getSetting('handler'), + 'field_mode' => 'admin', + ]; + /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager $handler */ + $handler = \Drupal::service('plugin.manager.entity_reference_selection'); - if ($handler->getReferenceableEntities()) { + if ($handler->createInstance('og:default', $options)) { return AccessResult::neutral(); } - // Allow users to create content outside of groups, if none of the // audience fields is required. $required = $field_definition->isRequired(); @@ -359,7 +364,7 @@ function og_invalidate_group_content_cache_tags(EntityInterface $entity) { } } - foreach ($membership_manager->getGroups($entity) as $group_entity_type_id => $groups) { + foreach ($membership_manager->getGroups($entity) as $groups) { /** @var \Drupal\Core\Entity\ContentEntityInterface $group */ foreach ($groups as $group) { $tags = Cache::mergeTags($tags, $group->getCacheTagsToInvalidate()); diff --git a/phpcs-ruleset.xml.dist b/phpcs-ruleset.xml.dist index 15c9ceb90..069d655b0 100644 --- a/phpcs-ruleset.xml.dist +++ b/phpcs-ruleset.xml.dist @@ -22,10 +22,6 @@ - - - - diff --git a/src/Cache/Context/OgRoleCacheContext.php b/src/Cache/Context/OgRoleCacheContext.php index 9a1372f14..b28ab35b9 100644 --- a/src/Cache/Context/OgRoleCacheContext.php +++ b/src/Cache/Context/OgRoleCacheContext.php @@ -121,9 +121,9 @@ public function getContext() { // Sort the memberships, so that the same key can be generated, even if // the memberships were defined in a different order. ksort($memberships); - foreach ($memberships as $entity_type_id => &$groups) { + foreach ($memberships as &$groups) { ksort($groups); - foreach ($groups as $group_id => &$role_names) { + foreach ($groups as &$role_names) { sort($role_names); } } diff --git a/src/ContextProvider/OgContext.php b/src/ContextProvider/OgContext.php index 9cd8fefed..6a492dcb2 100644 --- a/src/ContextProvider/OgContext.php +++ b/src/ContextProvider/OgContext.php @@ -126,7 +126,6 @@ protected function getOgContext() { */ protected function getBestCandidate() { $collection = new OgResolvedGroupCollection(); - $plugins = []; // Retrieve the list of group resolvers. These are stored in config, and are // ordered by priority. @@ -135,8 +134,6 @@ protected function getBestCandidate() { foreach ($group_resolvers as $plugin_id) { /** @var \Drupal\og\OgGroupResolverInterface $plugin */ if ($plugin = $this->pluginManager->createInstance($plugin_id)) { - $plugins[$plugin_id] = $plugin; - // Set the default vote weight according to the plugin's priority. $collection->setVoteWeight($priority); diff --git a/src/Event/DefaultRoleEvent.php b/src/Event/DefaultRoleEvent.php index 3e6679ce5..8ddd5e73e 100644 --- a/src/Event/DefaultRoleEvent.php +++ b/src/Event/DefaultRoleEvent.php @@ -73,7 +73,7 @@ public function setRole(OgRole $role) { * {@inheritdoc} */ public function setRoles(array $roles) { - foreach ($roles as $name => $properties) { + foreach ($roles as $properties) { $this->setRole($properties); } } diff --git a/src/GroupTypeManager.php b/src/GroupTypeManager.php index 40ed91939..4bb478244 100644 --- a/src/GroupTypeManager.php +++ b/src/GroupTypeManager.php @@ -131,6 +131,13 @@ class GroupTypeManager implements GroupTypeManagerInterface { */ protected $groupAudienceHelper; + /** + * The Entity type manager service. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + /** * Constructs a GroupTypeManager object. * @@ -155,6 +162,7 @@ class GroupTypeManager implements GroupTypeManagerInterface { */ public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, EventDispatcherInterface $event_dispatcher, CacheBackendInterface $cache, PermissionManagerInterface $permission_manager, OgRoleManagerInterface $og_role_manager, RouteBuilderInterface $route_builder, OgGroupAudienceHelperInterface $group_audience_helper) { $this->configFactory = $config_factory; + $this->entityTypeManager = $entity_type_manager; $this->entityTypeBundleInfo = $entity_type_bundle_info; $this->eventDispatcher = $event_dispatcher; $this->cache = $cache; @@ -200,7 +208,7 @@ public function getAllGroupBundles($entity_type = NULL) { */ public function getAllGroupContentBundleIds() { $bundles = []; - foreach ($this->getGroupRelationMap() as $group_entity_type_id => $group_bundle_ids) { + foreach ($this->getGroupRelationMap() as $group_bundle_ids) { foreach ($group_bundle_ids as $group_content_entity_type_ids) { foreach ($group_content_entity_type_ids as $group_content_entity_type_id => $group_content_bundle_ids) { $bundles[$group_content_entity_type_id] = array_merge(isset($bundles[$group_content_entity_type_id]) ? $bundles[$group_content_entity_type_id] : [], $group_content_bundle_ids); @@ -377,7 +385,7 @@ protected function populateGroupRelationMap(): void { $this->groupRelationMap = []; - $user_bundles = \Drupal::entityTypeManager()->getDefinition('user')->getKey('bundle') ?: ['user']; + $user_bundles = $this->entityTypeManager->getDefinition('user')->getKey('bundle') ?: ['user']; foreach ($this->entityTypeBundleInfo->getAllBundleInfo() as $group_content_entity_type_id => $bundles) { foreach ($bundles as $group_content_bundle_id => $bundle_info) { diff --git a/src/MembershipManager.php b/src/MembershipManager.php index 9b9d9117b..6d235087f 100644 --- a/src/MembershipManager.php +++ b/src/MembershipManager.php @@ -245,8 +245,8 @@ public function getGroupMembershipsByRoleNames(EntityInterface $group, array $ro /** * {@inheritdoc} */ - public function createMembership(EntityInterface $group, AccountInterface $user, $membership_type = OgMembershipInterface::TYPE_DEFAULT) { - /** @var \Drupal\user\UserInterface|\Drupal\Core\Session\AccountInterface $user */ + public function createMembership(EntityInterface $group, UserInterface $user, $membership_type = OgMembershipInterface::TYPE_DEFAULT) { + /** @var \Drupal\user\UserInterface $user */ /** @var \Drupal\og\OgMembershipInterface $membership */ $membership = OgMembership::create(['type' => $membership_type]); $membership diff --git a/src/MembershipManagerInterface.php b/src/MembershipManagerInterface.php index 7abc13fa3..2e8840d90 100644 --- a/src/MembershipManagerInterface.php +++ b/src/MembershipManagerInterface.php @@ -3,7 +3,7 @@ namespace Drupal\og; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Session\AccountInterface; +use Drupal\user\UserInterface; /** * Membership manager interface. @@ -169,7 +169,7 @@ public function getGroupMembershipsByRoleNames(EntityInterface $group, array $ro * * @param \Drupal\Core\Entity\EntityInterface $group * The group entity. - * @param \Drupal\Core\Session\AccountInterface $user + * @param \Drupal\user\UserInterface $user * The user object. * @param string $membership_type * (optional) The membership type. Defaults to @@ -178,7 +178,7 @@ public function getGroupMembershipsByRoleNames(EntityInterface $group, array $ro * @return \Drupal\og\OgMembershipInterface * The unsaved membership object. */ - public function createMembership(EntityInterface $group, AccountInterface $user, $membership_type = OgMembershipInterface::TYPE_DEFAULT); + public function createMembership(EntityInterface $group, UserInterface $user, $membership_type = OgMembershipInterface::TYPE_DEFAULT); /** * Returns all group IDs associated with the given group content entity. diff --git a/src/Og.php b/src/Og.php index 4232e9050..1b7feb218 100644 --- a/src/Og.php +++ b/src/Og.php @@ -363,7 +363,9 @@ public static function invalidateCache() { protected static function getFieldBaseDefinition($plugin_id) { /** @var OgFieldsPluginManager $plugin_manager */ $plugin_manager = \Drupal::service('plugin.manager.og.fields'); - if (!$field_config = $plugin_manager->getDefinition($plugin_id)) { + + $field_config = $plugin_manager->getDefinition($plugin_id); + if (!$field_config) { throw new \Exception("The Organic Groups field with plugin ID $plugin_id is not a valid plugin."); } @@ -384,8 +386,23 @@ protected static function getFieldBaseDefinition($plugin_id) { * @throws \Exception * Thrown when the passed in field definition is not of a group audience * field. + * + * @deprecated in og:8.x-1.0-alpha4 and is removed from og:8.x-1.0-alpha5. + * Use + * \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager::getInstance() + * Instead. + * @codingStandardsIgnoreStart + * @see https://github.com/Gizra/og/issues/580 + * @codingStandardsIgnoreEnd */ public static function getSelectionHandler(FieldDefinitionInterface $field_definition, array $options = []) { + // @codingStandardsIgnoreStart + @trigger_error('Og:getSelectionHandler() is deprecated in og:8.x-1.0-alpha4 + and is removed from og:8.x-1.0-alpha5. + Use \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager::getInstance() + instead. See https://github.com/Gizra/og/issues/580', E_USER_DEPRECATED + ); + // @codingStandardsIgnoreEnd if (!\Drupal::service('og.group_audience_helper')->isGroupAudienceField($field_definition)) { $field_name = $field_definition->getName(); throw new \Exception("The field $field_name is not an audience field."); diff --git a/src/Plugin/EntityReferenceSelection/OgSelection.php b/src/Plugin/EntityReferenceSelection/OgSelection.php index 3b63fa4f1..131e53784 100644 --- a/src/Plugin/EntityReferenceSelection/OgSelection.php +++ b/src/Plugin/EntityReferenceSelection/OgSelection.php @@ -89,7 +89,7 @@ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') $ids = []; if (!empty($this->getConfiguration()['field_mode']) && $this->getConfiguration()['field_mode'] === 'admin') { // Don't include the groups, the user doesn't have create permission. - foreach ($user_groups as $delta => $group) { + foreach ($user_groups as $group) { $ids[] = $group->id(); } diff --git a/tests/src/Kernel/Access/AccessByOgMembershipTest.php b/tests/src/Kernel/Access/AccessByOgMembershipTest.php index 37c50ead3..7c10e3b06 100644 --- a/tests/src/Kernel/Access/AccessByOgMembershipTest.php +++ b/tests/src/Kernel/Access/AccessByOgMembershipTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\og\Kernel\Access; +use Drupal\Core\Entity\EntityStorageException; use Drupal\KernelTests\KernelTestBase; use Drupal\Tests\node\Traits\ContentTypeCreationTrait; use Drupal\Tests\og\Traits\OgMembershipCreationTrait; @@ -182,8 +183,6 @@ public function testEntityOperationAccess($user, array $expected_results) { /** * Tests exception is thrown when trying to save non-member role. - * - * @expectedException \Drupal\Core\Entity\EntityStorageException */ public function testNonMemberRoleMembershipSave() { /** @var \Drupal\og\Entity\OgRole $role */ @@ -194,6 +193,7 @@ public function testNonMemberRoleMembershipSave() { ->save(); $membership = OgMembership::create(); + $this->expectException(EntityStorageException::class); $membership ->setOwner($this->users['non-member']) ->setGroup($this->group) diff --git a/tests/src/Kernel/Entity/GroupMembershipManagerTest.php b/tests/src/Kernel/Entity/GroupMembershipManagerTest.php index 14c1b76f2..42859160e 100644 --- a/tests/src/Kernel/Entity/GroupMembershipManagerTest.php +++ b/tests/src/Kernel/Entity/GroupMembershipManagerTest.php @@ -305,7 +305,7 @@ public function testGetGroups($group_type_id, $group_bundle, array $expected) { /** @var \Drupal\Core\Entity\EntityInterface $expected_group */ $expected_group = $this->groups[$expected_type][$expected_key]; /** @var \Drupal\Core\Entity\EntityInterface $group */ - foreach ($result[$expected_type] as $key => $group) { + foreach ($result[$expected_type] as $group) { if ($group->getEntityTypeId() === $expected_group->getEntityTypeId() && $group->id() === $expected_group->id()) { // The expected result was found. Continue the test. continue 2; diff --git a/tests/src/Kernel/Entity/OgMembershipTest.php b/tests/src/Kernel/Entity/OgMembershipTest.php index 3414285d3..a9c792e6a 100644 --- a/tests/src/Kernel/Entity/OgMembershipTest.php +++ b/tests/src/Kernel/Entity/OgMembershipTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\og\Kernel\Entity; +use Drupal\Core\Entity\EntityStorageException; use Drupal\entity_test\Entity\EntityTest; use Drupal\entity_test\Entity\EntityTestMul; use Drupal\KernelTests\KernelTestBase; @@ -138,12 +139,12 @@ public function testGetSetOwner() { * Tests getting the owner of a newly created membership. * * @covers ::getOwner - * @expectedException \LogicException */ public function testGetOwnerOnNewMembership() { // A brand new entity does not have an owner set yet. It should throw a // logic exception. $membership = OgMembership::create(); + $this->expectException(\LogicException::class); $membership->getOwner(); } @@ -151,12 +152,12 @@ public function testGetOwnerOnNewMembership() { * Tests getting the owner ID of a newly created membership. * * @covers ::getOwnerId - * @expectedException \LogicException */ public function testGetOwnerIdOnNewMembership() { // A brand new entity does not have an owner set yet. It should throw a // logic exception. $membership = OgMembership::create(); + $this->expectException(\LogicException::class); $membership->getOwnerId(); } @@ -225,11 +226,11 @@ public function testMembershipStaticCache() { * Tests exceptions are thrown when trying to save a membership with no user. * * @covers ::preSave - * @expectedException \Drupal\Core\Entity\EntityStorageException */ public function testSetNoUserException() { /** @var \Drupal\og\OgMembershipInterface $membership */ $membership = OgMembership::create(['type' => OgMembershipInterface::TYPE_DEFAULT]); + $this->expectException(EntityStorageException::class); $membership ->setGroup($this->group) ->save(); @@ -239,11 +240,11 @@ public function testSetNoUserException() { * Tests exceptions are thrown when trying to save a membership with no group. * * @covers ::preSave - * @expectedException \Drupal\Core\Entity\EntityStorageException */ public function testSetNoGroupException() { /** @var \Drupal\og\OgMembershipInterface $membership */ $membership = OgMembership::create(); + $this->expectException(EntityStorageException::class); $membership ->setOwner($this->user) ->save(); @@ -277,7 +278,6 @@ public function testNoOwnerException() { * Tests saving a membership with a non group entity. * * @covers ::preSave - * @expectedException \Drupal\Core\Entity\EntityStorageException */ public function testSetNonValidGroupException() { $non_group = EntityTest::create([ @@ -288,6 +288,8 @@ public function testSetNonValidGroupException() { $non_group->save(); /** @var \Drupal\og\OgMembershipInterface $membership */ $membership = Og::createMembership($non_group, $this->user); + + $this->expectException(EntityStorageException::class); $membership->save(); } @@ -295,7 +297,6 @@ public function testSetNonValidGroupException() { * Tests saving an existing membership. * * @covers ::preSave - * @expectedException \Drupal\Core\Entity\EntityStorageException */ public function testSaveExistingMembership() { $group = EntityTest::create([ @@ -312,6 +313,8 @@ public function testSaveExistingMembership() { $membership1->save(); $membership2 = Og::createMembership($group, $this->user); + + $this->expectException(EntityStorageException::class); $membership2->save(); } @@ -319,7 +322,6 @@ public function testSaveExistingMembership() { * Tests saving a membership with a role with a different group type. * * @covers ::preSave - * @expectedException \Drupal\Core\Entity\EntityStorageException * @dataProvider saveRoleWithWrongGroupTypeProvider */ public function testSaveRoleWithWrongGroupType($group_entity_type_id, $group_bundle_id) { @@ -338,6 +340,7 @@ public function testSaveRoleWithWrongGroupType($group_entity_type_id, $group_bun ->setName(mb_strtolower($this->randomMachineName())); $wrong_role->save(); + $this->expectException(EntityStorageException::class); Og::createMembership($group, $this->user)->addRole($wrong_role)->save(); } @@ -438,7 +441,6 @@ public function isRoleValidProvider() { * An array of test role metadata. * * @covers ::save - * @expectedException \Drupal\Core\Entity\EntityStorageException * @dataProvider saveMembershipWithInvalidRolesProvider */ public function testSaveMembershipWithInvalidRoles(array $roles_metadata): void { @@ -486,6 +488,7 @@ public function testSaveMembershipWithInvalidRoles(array $roles_metadata): void // Create a membership with the test group and roles. This should throw an // exception since the roles are invalid. + $this->expectException(EntityStorageException::class); OgMembership::create() ->setOwner($this->user) ->setGroup($this->group) @@ -567,7 +570,6 @@ public function saveMembershipWithInvalidRolesProvider(): array { * Tests the exception thrown if the validity of a role cannot be established. * * @covers ::isRoleValid - * @expectedException \LogicException */ public function testIsRoleValidException() { $role = OgRole::create([ @@ -578,6 +580,7 @@ public function testIsRoleValidException() { // If a membership doesn't have a group yet it is not possible to determine // wheter a role is valid. + $this->expectException(\LogicException::class); $membership->isRoleValid($role); } @@ -643,12 +646,12 @@ public function testGetGroup() { * Tests getting the group from a new membership. * * @covers ::getGroup - * @expectedException \LogicException */ public function testGetGroupOnNewMembership() { $membership = OgMembership::create(); // When no group has been set yet, the method should throw an assertion. + $this->expectException(\LogicException::class); $membership->getGroup(); } @@ -671,10 +674,11 @@ public function testGetGroupBundle() { * Tests getting the group bundle of a newly created membership. * * @covers ::getGroupBundle - * @expectedException \LogicException */ public function testGetGroupBundleOnNewMembership() { $membership = OgMembership::create(); + + $this->expectException(\LogicException::class); $membership->getGroupBundle(); } @@ -693,10 +697,11 @@ public function testGetGroupEntityType() { * Tests getting the group entity type ID of a newly created membership. * * @covers ::getGroupEntityType - * @expectedException \LogicException */ public function testGetGroupEntityTypeOnNewMembership() { $membership = OgMembership::create(); + + $this->expectException(\LogicException::class); $membership->getGroupEntityType(); } @@ -715,10 +720,11 @@ public function testGetGroupId() { * Tests getting the group ID of a newly created membership. * * @covers ::getGroupId - * @expectedException \LogicException */ public function testGetGroupIdOnNewMembership() { $membership = OgMembership::create(); + + $this->expectException(\LogicException::class); $membership->getGroupId(); } diff --git a/tests/src/Kernel/Entity/SelectionHandlerTest.php b/tests/src/Kernel/Entity/SelectionHandlerTest.php index bb0bf9d4a..97650b3e0 100644 --- a/tests/src/Kernel/Entity/SelectionHandlerTest.php +++ b/tests/src/Kernel/Entity/SelectionHandlerTest.php @@ -20,7 +20,7 @@ class SelectionHandlerTest extends KernelTestBase { /** * The selection handler. * - * @var \Drupal\og\Plugin\EntityReferenceSelection\OgSelection + * @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface|false|object */ protected $selectionHandler; @@ -71,6 +71,13 @@ class SelectionHandlerTest extends KernelTestBase { */ protected $fieldDefinition; + /** + * Selection plugin manager. + * + * @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManager + */ + protected $selectionPluginManager; + /** * {@inheritdoc} */ @@ -87,6 +94,7 @@ protected function setUp() { // Setting up variables. $this->groupBundle = mb_strtolower($this->randomMachineName()); $this->groupContentBundle = mb_strtolower($this->randomMachineName()); + $this->selectionPluginManager = $this->container->get('plugin.manager.entity_reference_selection'); // Create a group. NodeType::create([ @@ -107,7 +115,13 @@ protected function setUp() { $this->fieldDefinition = Og::createField(OgGroupAudienceHelperInterface::DEFAULT_FIELD, 'node', $this->groupContentBundle); // Get the storage of the field. - $this->selectionHandler = Og::getSelectionHandler($this->fieldDefinition, ['field_mode' => 'default']); + $options = [ + 'target_type' => $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type'), + 'handler' => $this->fieldDefinition->getSetting('handler'), + 'field_mode' => 'admin', + ]; + $this->selectionPluginManager->createInstance('og:default', $options); + $this->selectionHandler = $this->selectionPluginManager->getSelectionHandler($this->fieldDefinition); // Create two users. $this->user1 = User::create(['name' => $this->randomString()]); @@ -151,12 +165,16 @@ public function testSelectionHandlerResults() { $this->assertEquals($user2_groups, array_keys($groups[$this->groupBundle])); // Check the other groups. - $this->selectionHandler = Og::getSelectionHandler($this->fieldDefinition, ['field_mode' => 'admin']); + $options = [ + 'target_type' => $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type'), + 'handler' => $this->fieldDefinition->getSetting('handler'), + 'field_mode' => 'admin', + ]; + $this->selectionHandler = $this->selectionPluginManager->createInstance('og:default', $options); $this->setCurrentAccount($this->user1); $groups = $this->selectionHandler->getReferenceableEntities(); $this->assertEquals($user2_groups, array_keys($groups[$this->groupBundle])); - $this->setCurrentAccount($this->user2); $groups = $this->selectionHandler->getReferenceableEntities(); $this->assertEquals($user1_groups, array_keys($groups[$this->groupBundle])); diff --git a/tests/src/Unit/DefaultRoleEventTest.php b/tests/src/Unit/DefaultRoleEventTest.php index d3bbf4416..52f881971 100644 --- a/tests/src/Unit/DefaultRoleEventTest.php +++ b/tests/src/Unit/DefaultRoleEventTest.php @@ -325,7 +325,7 @@ public function testAddInvalidRole(array $invalid_roles) { $this->expectOgRoleCreation($invalid_roles); try { - foreach ($invalid_roles as $name => $invalid_role) { + foreach ($invalid_roles as $invalid_role) { $this->defaultRoleEvent->addRole($invalid_role); } $this->fail('An invalid role cannot be added.'); @@ -346,11 +346,11 @@ public function testAddInvalidRole(array $invalid_roles) { * @covers ::addRoles * * @dataProvider invalidDefaultRoleProvider - * @expectedException \InvalidArgumentException */ public function testAddInvalidRoles(array $invalid_roles) { $this->expectOgRoleCreation($invalid_roles); + $this->expectException(\InvalidArgumentException::class); $this->defaultRoleEvent->addRoles($invalid_roles); $this->fail('An array of invalid roles cannot be added.'); } @@ -364,12 +364,12 @@ public function testAddInvalidRoles(array $invalid_roles) { * @covers ::setRole * * @dataProvider invalidDefaultRoleProvider - * @expectedException \InvalidArgumentException */ public function testSetInvalidRole(array $invalid_roles) { $this->expectOgRoleCreation($invalid_roles); - foreach ($invalid_roles as $name => $invalid_role) { + $this->expectException(\InvalidArgumentException::class); + foreach ($invalid_roles as $invalid_role) { $this->defaultRoleEvent->setRole($invalid_role); } } @@ -383,11 +383,11 @@ public function testSetInvalidRole(array $invalid_roles) { * @covers ::setRoles * * @dataProvider invalidDefaultRoleProvider - * @expectedException \InvalidArgumentException */ public function testSetInvalidRoles(array $invalid_roles) { $this->expectOgRoleCreation($invalid_roles); + $this->expectException(\InvalidArgumentException::class); $this->defaultRoleEvent->setRoles($invalid_roles); } @@ -400,12 +400,12 @@ public function testSetInvalidRoles(array $invalid_roles) { * @covers ::offsetSet * * @dataProvider invalidDefaultRoleProvider - * @expectedException \InvalidArgumentException */ public function testInvalidOffsetSet(array $invalid_roles) { $this->expectOgRoleCreation($invalid_roles); foreach ($invalid_roles as $name => $invalid_role) { + $this->expectException(\InvalidArgumentException::class); $this->defaultRoleEvent[$name] = $invalid_role; } } diff --git a/tests/src/Unit/GroupCheckTest.php b/tests/src/Unit/GroupCheckTest.php index 4b9d2fcec..f96b90315 100644 --- a/tests/src/Unit/GroupCheckTest.php +++ b/tests/src/Unit/GroupCheckTest.php @@ -284,8 +284,6 @@ public function permissionsProvider() { /** * Tests fetching arguments from the route match without "getOption" defined. - * - * @expectedException BadMethodCallException */ public function testNoArgumentsFromRouteMatch() { $this @@ -300,6 +298,7 @@ public function testNoArgumentsFromRouteMatch() { // Call the group check without the entity related arguments. $group_check = new GroupCheck($this->entityTypeManager->reveal(), $this->ogAccess->reveal()); + $this->expectException(\BadMethodCallException::class); $group_check->access($this->user->reveal(), $this->route->reveal(), $this->routeMatch->reveal()); } diff --git a/tests/src/Unit/GroupContentOperationPermissionTest.php b/tests/src/Unit/GroupContentOperationPermissionTest.php index e305fca6c..8f89eaa84 100644 --- a/tests/src/Unit/GroupContentOperationPermissionTest.php +++ b/tests/src/Unit/GroupContentOperationPermissionTest.php @@ -332,11 +332,11 @@ public function testSetRestrictAccess(array $values) { * Tests getting an invalid property from a group content permission. * * @covers ::get - * - * @expectedException \InvalidArgumentException */ public function testGetInvalidProperty() { $permission = new GroupContentOperationPermission(); + + $this->expectException(\InvalidArgumentException::class); $permission->get('invalid property'); } @@ -344,11 +344,11 @@ public function testGetInvalidProperty() { * Tests setting an invalid property for a group content permission. * * @covers ::set - * - * @expectedException \InvalidArgumentException */ public function testSetInvalidProperty() { $permission = new GroupContentOperationPermission(); + + $this->expectException(\InvalidArgumentException::class); $permission->set('invalid property', 'a value'); } @@ -356,11 +356,11 @@ public function testSetInvalidProperty() { * Tests setting an invalid restrict access for a group content permission. * * @covers ::set - * - * @expectedException \InvalidArgumentException */ public function testSetInvalidRestrictAccessValue() { $permission = new GroupContentOperationPermission(); + + $this->expectException(\InvalidArgumentException::class); $permission->set('restrict access', 'invalid value'); } @@ -368,11 +368,11 @@ public function testSetInvalidRestrictAccessValue() { * Tests setting an invalid ownership property for a group content permission. * * @covers ::set - * - * @expectedException \InvalidArgumentException */ public function testSetInvalidOwnershipValue() { $permission = new GroupContentOperationPermission(); + + $this->expectException(\InvalidArgumentException::class); $permission->set('owner', 'invalid value'); } diff --git a/tests/src/Unit/GroupPermissionTest.php b/tests/src/Unit/GroupPermissionTest.php index 218f0d934..42f3d8397 100644 --- a/tests/src/Unit/GroupPermissionTest.php +++ b/tests/src/Unit/GroupPermissionTest.php @@ -239,11 +239,11 @@ public function testSetRestrictAccess(array $values) { * Tests getting an invalid property of a permission. * * @covers ::get - * - * @expectedException \InvalidArgumentException */ public function testGetInvalidProperty() { $permission = new GroupPermission(); + + $this->expectException(\InvalidArgumentException::class); $permission->get('invalid property'); } @@ -251,11 +251,11 @@ public function testGetInvalidProperty() { * Tests setting an invalid property for a permission. * * @covers ::set - * - * @expectedException \InvalidArgumentException */ public function testSetInvalidProperty() { $permission = new GroupPermission(); + + $this->expectException(\InvalidArgumentException::class); $permission->set('invalid property', 'a value'); } @@ -263,11 +263,11 @@ public function testSetInvalidProperty() { * Tests setting an invalid restrict access value for a permission. * * @covers ::set - * - * @expectedException \InvalidArgumentException */ public function testSetInvalidRestrictAccessValue() { $permission = new GroupPermission(); + + $this->expectException(\InvalidArgumentException::class); $permission->set('restrict access', 'invalid value'); } diff --git a/tests/src/Unit/GroupTypeManagerTest.php b/tests/src/Unit/GroupTypeManagerTest.php index 37ebdc1b3..c7e003045 100644 --- a/tests/src/Unit/GroupTypeManagerTest.php +++ b/tests/src/Unit/GroupTypeManagerTest.php @@ -221,7 +221,6 @@ public function testGetGroupBundleIdsByEntityType() { * Tests adding an existing group. * * @covers ::addGroup - * @expectedException \InvalidArgumentException */ public function testAddGroupExisting() { // It is expected that the group map will be retrieved from config. @@ -237,6 +236,7 @@ public function testAddGroupExisting() { $manager = $this->createGroupManager(); // Add to existing. + $this->expectException(\InvalidArgumentException::class); $manager->addGroup('test_entity', 'c'); $this->assertSame(['a', 'b', 'c'], $manager->getGroupBundleIdsByEntityType('test_entity')); diff --git a/tests/src/Unit/PermissionEventTest.php b/tests/src/Unit/PermissionEventTest.php index 454d86396..e7fe5f7ee 100644 --- a/tests/src/Unit/PermissionEventTest.php +++ b/tests/src/Unit/PermissionEventTest.php @@ -162,13 +162,12 @@ public function testSetPermission(array $permissions, $entity_type_id, $bundle_i * * @covers ::setPermission * - * @expectedException \InvalidArgumentException - * * @dataProvider invalidPermissionsProvider */ public function testSetInvalidPermission(array $permissions, $entity_type_id, $bundle_id, array $group_content_bundle_ids) { $event = new PermissionEvent($entity_type_id, $bundle_id, $group_content_bundle_ids); foreach ($permissions as $permission) { + $this->expectException(\InvalidArgumentException::class); $event->setPermission($permission); } } @@ -438,13 +437,15 @@ public function testOffsetSet(array $permissions, $entity_type_id, $bundle_id, a * @param mixed $permission * A test value to set through ArrayAccess. * - * @expectedException \InvalidArgumentException - * * @dataProvider offsetSetInvalidPermissionProvider */ public function testOffsetSetInvalidPermission($key, $permission) { + $this->expectException(\InvalidArgumentException::class); + + // phpcs:disable DrupalPractice.CodeAnalysis.VariableAnalysis.UnusedVariable $event = new PermissionEvent($this->randomMachineName(), $this->randomMachineName(), []); $event[$key] = $permission; + // phpcs:enable DrupalPractice.CodeAnalysis.VariableAnalysis.UnusedVariable } /** @@ -585,12 +586,11 @@ public function testIteratorAggregate(array $permissions, $entity_type_id, $bund /** * Tests creation of an invalid operation permission. - * - * @expectedException \InvalidArgumentException */ public function testInvalidGroupContentOperationPermissionCreation() { // An exception should be thrown when a group content operation permission // is created with an invalid owner type. + $this->expectException(\InvalidArgumentException::class); new GroupContentOperationPermission([ 'name' => 'invalid permission', 'title' => $this->t('This is an invalid permission.'), diff --git a/tests/src/Unit/SubscriptionControllerTest.php b/tests/src/Unit/SubscriptionControllerTest.php index fce082b78..274ba36df 100644 --- a/tests/src/Unit/SubscriptionControllerTest.php +++ b/tests/src/Unit/SubscriptionControllerTest.php @@ -15,6 +15,7 @@ use Drupal\og\OgMembershipInterface; use Drupal\Tests\UnitTestCase; use Drupal\user\EntityOwnerInterface; +use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; /** * Tests the subscription controller. @@ -126,7 +127,6 @@ public function setUp() { * Tests non-member trying to unsubscribe from group. * * @covers ::unsubscribe - * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */ public function testNotMember() { $states = [ @@ -140,6 +140,7 @@ public function testNotMember() { ->getMembership($this->group->reveal(), $this->userId, $states) ->willReturn(NULL); + $this->expectException(AccessDeniedHttpException::class); $this->unsubscribe(); } @@ -147,7 +148,6 @@ public function testNotMember() { * Tests blocked member trying to unsubscribe from group. * * @covers ::unsubscribe - * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException */ public function testBlockedMember() { $states = [ @@ -166,6 +166,7 @@ public function testBlockedMember() { ->getState() ->willReturn(OgMembershipInterface::STATE_BLOCKED); + $this->expectException(AccessDeniedHttpException::class); $this->unsubscribe(); }