Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #601 from kbrodej/hotfix/Dependecy-injection-#546
Browse files Browse the repository at this point in the history
`User::load()` calls should be avoided in classes #546 fix
  • Loading branch information
pfrenssen authored Feb 9, 2020
2 parents 716e112 + 03dbe85 commit 873a140
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
2 changes: 0 additions & 2 deletions phpcs-ruleset.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
<exclude name="DrupalPractice.CodeAnalysis.VariableAnalysis.UndefinedVariable" />
<!-- https://github.com/Gizra/og/issues/544 -->
<exclude name="DrupalPractice.InfoFiles.NamespacedDependency.NonNamespaced" />
<!-- https://github.com/Gizra/og/issues/546 -->
<exclude name="DrupalPractice.Objects.GlobalClass.GlobalClass" />
<!-- https://github.com/Gizra/og/issues/543 -->
<exclude name="DrupalPractice.Objects.GlobalFunction.GlobalFunction" />
</rule>
Expand Down
3 changes: 1 addition & 2 deletions src/Controller/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Drupal\og\OgAccessInterface;
use Drupal\og\OgMembershipInterface;
use Drupal\og\OgMembershipTypeInterface;
use Drupal\user\Entity\User;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -84,7 +83,7 @@ public function subscribe($entity_type_id, EntityInterface $group, OgMembershipT
throw new AccessDeniedHttpException();
}

$user = User::load($this->currentUser()->id());
$user = $this->entityTypeManager()->getStorage('user')->load($this->currentUser()->id());

if ($user->isAnonymous()) {
// Anonymous user can't request membership.
Expand Down
23 changes: 19 additions & 4 deletions src/Plugin/Field/FieldFormatter/GroupSubscribeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\og\Plugin\Field\FieldFormatter;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
Expand All @@ -12,7 +13,6 @@
use Drupal\og\Og;
use Drupal\og\OgAccessInterface;
use Drupal\og\OgMembershipInterface;
use Drupal\user\Entity\User;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand Down Expand Up @@ -46,6 +46,13 @@ class GroupSubscribeFormatter extends FormatterBase implements ContainerFactoryP
*/
protected $ogAccess;

/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* Constructs a new GroupSubscribeFormatter object.
*
Expand All @@ -67,12 +74,18 @@ class GroupSubscribeFormatter extends FormatterBase implements ContainerFactoryP
* The current user.
* @param \Drupal\og\OgAccessInterface $og_access
* The OG access service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, OgAccessInterface $og_access) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, OgAccessInterface $og_access, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);

$this->currentUser = $current_user;
$this->ogAccess = $og_access;
$this->entityTypeManager = $entity_type_manager->getStorage('user');
}

/**
Expand All @@ -88,7 +101,8 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('current_user'),
$container->get('og.access')
$container->get('og.access'),
$container->get('entity_type.manager')
);
}

Expand All @@ -104,7 +118,8 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
$group = $items->getEntity();
$entity_type_id = $group->getEntityTypeId();

$user = User::load($this->currentUser->id());
// $user = User::load($this->currentUser->id());
$user = $this->entityTypeManager->load(($this->currentUser->id()));
if (($group instanceof EntityOwnerInterface) && ($group->getOwnerId() == $user->id())) {
// User is the group manager.
$elements[0] = [
Expand Down
3 changes: 2 additions & 1 deletion tests/src/Unit/GroupSubscribeFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ protected function getElements() {
'',
[],
$this->accountProxy->reveal(),
$this->ogAccess->reveal()
$this->ogAccess->reveal(),
$this->entityTypeManager->reveal()
);
return $formatter->viewElements($this->fieldItemList->reveal(), $this->randomMachineName());
}
Expand Down
12 changes: 11 additions & 1 deletion tests/src/Unit/SubscriptionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
Expand Down Expand Up @@ -87,6 +88,13 @@ class SubscriptionControllerTest extends UnitTestCase {
*/
protected $userId;

/**
* The mocked entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ObjectProphecy
*/
protected $entityTypeManager;

/**
* {@inheritdoc}
*/
Expand All @@ -99,6 +107,7 @@ public function setUp() {
$this->ogMembership = $this->prophesize(OgMembershipInterface::class);
$this->url = $this->prophesize(Url::class);
$this->user = $this->prophesize(AccountInterface::class);
$this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);

$this->userId = rand(20, 50);
$this->user->id()->willReturn($this->userId);
Expand All @@ -109,6 +118,7 @@ public function setUp() {
$container->set('entity.form_builder', $this->entityFormBuilder->reveal());
$container->set('og.membership_manager', $this->membershipManager->reveal());
$container->set('string_translation', $this->getStringTranslationStub());
$container->set('entity_type.manager', $this->entityTypeManager->reveal());
\Drupal::setContainer($container);

}
Expand Down Expand Up @@ -268,7 +278,7 @@ public function testGroupManager($state) {
* Invoke the unsubscribe method.
*/
protected function unsubscribe() {
$controller = new SubscriptionController($this->ogAccess->reveal(), $this->messenger->reveal());
$controller = new SubscriptionController($this->ogAccess->reveal(), $this->messenger->reveal(), $this->entityTypeManager->reveal());
$controller->unsubscribe($this->group->reveal());
}

Expand Down

0 comments on commit 873a140

Please sign in to comment.