diff --git a/modules/social_features/social_activity/modules/social_activity_filter/src/Plugin/views/display/FilterBlock.php b/modules/social_features/social_activity/modules/social_activity_filter/src/Plugin/views/display/FilterBlock.php index 513a8d001af..115c7015452 100755 --- a/modules/social_features/social_activity/modules/social_activity_filter/src/Plugin/views/display/FilterBlock.php +++ b/modules/social_features/social_activity/modules/social_activity_filter/src/Plugin/views/display/FilterBlock.php @@ -214,7 +214,7 @@ public static function processFilterTags(array &$element, FormStateInterface $fo /** * Handles switching the available terms based on the selected vocabulary. */ - public static function updateTagsOptions($form, FormStateInterface $form_state) { + public static function updateTagsOptions(array $form, FormStateInterface $form_state) { // Check if there is triggered parent of element. if ($triggered = $form_state->getTriggeringElement()) { diff --git a/modules/social_features/social_group/social_group.services.yml b/modules/social_features/social_group/social_group.services.yml index be9610d19c8..f334336f281 100644 --- a/modules/social_features/social_group/social_group.services.yml +++ b/modules/social_features/social_group/social_group.services.yml @@ -49,3 +49,7 @@ services: arguments: [] tags: - { name: cache.context } + + Drupal\social_group\CurrentGroupService: + social_group.current_group_service: '@Drupal\social_group\CurrentGroupService' + autowire: true diff --git a/modules/social_features/social_group/src/CurrentGroupService.php b/modules/social_features/social_group/src/CurrentGroupService.php new file mode 100644 index 00000000000..8a3ccbcb466 --- /dev/null +++ b/modules/social_features/social_group/src/CurrentGroupService.php @@ -0,0 +1,52 @@ +contextRepository = $context_repository; + } + + /** + * Get group from runtime contexts. + * + * @return \Drupal\group\Entity\GroupInterface|null + * The current group or NULL. + */ + public function fromRunTimeContexts(): ?GroupInterface { + $group_runtime_context = $this->contextRepository->getRuntimeContexts(['@group.group_route_context:group']); + + $group = $group_runtime_context['@group.group_route_context:group']->getContextData()->getValue(); + if ($group === NULL) { + return NULL; + } + + assert($group instanceof GroupInterface, "The group context resolver returned a context value that is not a GroupInterface instance which violates the services contract."); + return $group; + } + +} diff --git a/modules/social_features/social_group/src/Tests/Unit/CurrentGroupServiceTest.php b/modules/social_features/social_group/src/Tests/Unit/CurrentGroupServiceTest.php new file mode 100644 index 00000000000..6a909eeef87 --- /dev/null +++ b/modules/social_features/social_group/src/Tests/Unit/CurrentGroupServiceTest.php @@ -0,0 +1,91 @@ +contextRepository = $this->createMock(ContextRepositoryInterface::class); + + $this->currentGroupService = new CurrentGroupService($this->contextRepository); + } + + /** + * Test that fromRunTimeContexts() returns a Group when a group is present. + */ + public function testFromRunTimeContextsWithGroup(): void { + $group = $this->createMock(GroupInterface::class); + $this->mockContext($group); + + // Call the method and assert the group is returned. + $result = $this->currentGroupService->fromRunTimeContexts(); + $this->assertSame($group, $result, 'Group context should return a GroupInterface instance.'); + } + + /** + * Test that fromRunTimeContexts() returns NULL when group is not present. + */ + public function testFromRunTimeContextsWithoutGroup(): void { + $this->mockContext(NULL); + + // Call the method and assert NULL is returned. + $result = $this->currentGroupService->fromRunTimeContexts(); + $this->assertNull($result, 'Group context should return NULL.'); + } + + /** + * Mock the context. + * + * @param \PHPUnit\Framework\MockObject\MockObject|\Drupal\group\Entity\GroupInterface|null $group + * The mocked group or NULL. + */ + private function mockContext(MockObject|GroupInterface|NULL $group): void { + $typedData = $this->createMock(TypedDataInterface::class); + $typedData->expects($this->once()) + ->method('getValue') + ->willReturn($group); + + $context = $this->createMock(ContextInterface::class); + $context->expects($this->once()) + ->method('getContextData') + ->willReturn($typedData); + + $this->contextRepository + ->expects($this->once()) + ->method('getRuntimeContexts') + ->with(['@group.group_route_context:group']) + ->willReturn(['@group.group_route_context:group' => $context]); + } + +} diff --git a/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoBlock.php b/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoBlock.php index 8fa39f9a56f..e6f1a07676c 100644 --- a/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoBlock.php +++ b/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoBlock.php @@ -5,7 +5,9 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormBuilderInterface; +use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\Core\Session\AccountProxyInterface; +use Drupal\social_group\CurrentGroupService; use Drupal\social_post\Plugin\Block\PostBlock; /** @@ -28,7 +30,9 @@ public function __construct( EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user, FormBuilderInterface $form_builder, - ModuleHandlerInterface $module_handler + ModuleHandlerInterface $module_handler, + CurrentRouteMatch $route_match, + CurrentGroupService $current_group_service, ) { parent::__construct( $configuration, @@ -37,7 +41,9 @@ public function __construct( $entity_type_manager, $current_user, $form_builder, - $module_handler + $module_handler, + $route_match, + $current_group_service, ); // Override the bundle type. diff --git a/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoGroupBlock.php b/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoGroupBlock.php index a20aeabce57..d6b7e02bc94 100644 --- a/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoGroupBlock.php +++ b/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoGroupBlock.php @@ -5,7 +5,9 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormBuilderInterface; +use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\Core\Session\AccountProxyInterface; +use Drupal\social_group\CurrentGroupService; use Drupal\social_post\Plugin\Block\PostGroupBlock; /** @@ -28,7 +30,9 @@ public function __construct( EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user, FormBuilderInterface $form_builder, - ModuleHandlerInterface $module_handler + ModuleHandlerInterface $module_handler, + CurrentRouteMatch $route_match, + CurrentGroupService $current_group_service, ) { parent::__construct( $configuration, @@ -37,7 +41,9 @@ public function __construct( $entity_type_manager, $current_user, $form_builder, - $module_handler + $module_handler, + $route_match, + $current_group_service, ); // Override the bundle type. diff --git a/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoProfileBlock.php b/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoProfileBlock.php index 67796d80042..be0d3a57f2b 100644 --- a/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoProfileBlock.php +++ b/modules/social_features/social_post/modules/social_post_photo/src/Plugin/Block/PostPhotoProfileBlock.php @@ -5,7 +5,9 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormBuilderInterface; +use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\Core\Session\AccountProxyInterface; +use Drupal\social_group\CurrentGroupService; use Drupal\social_post\Plugin\Block\PostProfileBlock; /** @@ -29,7 +31,8 @@ public function __construct( AccountProxyInterface $current_user, FormBuilderInterface $form_builder, ModuleHandlerInterface $module_handler, - $account + CurrentRouteMatch $route_match, + CurrentGroupService $current_group_service, ) { parent::__construct( $configuration, @@ -39,7 +42,8 @@ public function __construct( $current_user, $form_builder, $module_handler, - $account + $route_match, + $current_group_service, ); // Override the bundle type. diff --git a/modules/social_features/social_post/social_post.info.yml b/modules/social_features/social_post/social_post.info.yml index 018f17b0bf5..8b27dc98d8a 100644 --- a/modules/social_features/social_post/social_post.info.yml +++ b/modules/social_features/social_post/social_post.info.yml @@ -5,13 +5,14 @@ core_version_requirement: ^9 || ^10 dependencies: - drupal:block - drupal:comment - - social:dropdown - drupal:field - - group:group - drupal:options - - social:social_comment - drupal:text - drupal:user - drupal:views + - group:group - social:activity_creator + - social:dropdown + - social:social_comment + - social:social_group package: Social diff --git a/modules/social_features/social_post/social_post.module b/modules/social_features/social_post/social_post.module index 2e9c2d1dacd..293b3ae7484 100644 --- a/modules/social_features/social_post/social_post.module +++ b/modules/social_features/social_post/social_post.module @@ -15,45 +15,6 @@ use Drupal\group\Entity\GroupInterface; use Drupal\group\Entity\Group; use Drupal\user\UserInterface; -/** - * Implements hook_form_FORM_ID_alter(). - */ -function social_post_form_post_form_alter(&$form, FormStateInterface $form_state, $form_id) { - - if ( - $form_state->getFormObject()->getEntity()->isNew() && - ($content = \Drupal::service('social_post.helper')->buildCurrentUserImage()) - ) { - $form['current_user_image'] = $content; - } - - // Reset title display. - $form['field_post']['widget'][0]['#title_display'] = ""; - - // Set submit button caption to Post instead of Save. - $form['actions']['submit']['#value'] = t('Post', [], ['context' => 'Post button']); - - if (!empty($form['field_post']) && !empty($form['field_post']['widget'][0])) { - // For posting on the stream on the group stream. - if (!empty(_social_group_get_current_group())) { - $form['field_post']['widget'][0]['#placeholder'] = t('Say something to the group'); - $form['field_post']['widget'][0]['#title'] = t('Say something to the group'); - } - // For the post on a users profile. - elseif (!empty(\Drupal::routeMatch()->getParameter('user')) && \Drupal::routeMatch()->getParameter('user')->id() != \Drupal::currentUser()->id()) { - $user_profile = \Drupal::routeMatch()->getParameter('user'); - $name = $user_profile->getDisplayName(); - $form['field_post']['widget'][0]['#placeholder'] = t('Leave a message to @name', ['@name' => $name]); - $form['field_post']['widget'][0]['#title'] = t('Leave a message to @name', ['@name' => $name]); - } - else { - $title = t('Say something to the Community'); - $form['field_post']['widget'][0]['#title'] = $title; - $form['field_post']['widget'][0]['#placeholder'] = $title; - } - } -} - /** * Implements hook_form_FORM_ID_alter(). * diff --git a/modules/social_features/social_post/social_post.services.yml b/modules/social_features/social_post/social_post.services.yml index b85d22d9036..e9e0a330a00 100644 --- a/modules/social_features/social_post/social_post.services.yml +++ b/modules/social_features/social_post/social_post.services.yml @@ -11,3 +11,17 @@ services: social_post.helper: class: Drupal\social_post\Service\SocialPostHelper arguments: ['@entity_type.manager', '@current_user'] + + #hooks replacement + social_post.form.hooks: + class: Drupal\social_post\Hooks\SocialPostFormHooks + arguments: + - '@social_post.helper' + - '@current_user' + tags: + - { name: hooks } + + Drupal\social_post\Hooks\SocialPostFormHooks: + class: Drupal\social_post\Hooks\SocialPostFormHooks + autowire: true + social_post.form.hooks: '@Drupal\social_post\Hooks\SocialPostFormHooks' diff --git a/modules/social_features/social_post/src/Hooks/SocialPostFormHooks.php b/modules/social_features/social_post/src/Hooks/SocialPostFormHooks.php new file mode 100644 index 00000000000..fe748b91b07 --- /dev/null +++ b/modules/social_features/social_post/src/Hooks/SocialPostFormHooks.php @@ -0,0 +1,95 @@ +socialPostHelper = $social_post_helper; + $this->currentUser = $current_user; + } + + /** + * From alter hook: replacement of social_post_form_post_form_alter. + * + * The method definition contains all standard parameters defined by the + * definition of the hook. + * + * @param array $form + * The drupal form. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The form state. + */ + #[Alter('form_post_form')] + public function formPostFormAlter(array &$form, FormStateInterface $form_state): void { + $form_object = $form_state->getFormObject(); + assert($form_object instanceof ContentEntityForm, 'Expected $form_object to be an instance of ContentEntityForm.'); + + if ($this->socialPostHelper->buildCurrentUserImage() !== NULL + && $form_object->getEntity()->isNew()) { + $form['current_user_image'] = $this->socialPostHelper->buildCurrentUserImage(); + } + + // Reset title display. + $form['field_post']['widget'][0]['#title_display'] = ''; + + // Set submit button caption to Post instead of Save. + $form['actions']['submit']['#value'] = t('Post', [], ['context' => 'Post button']); + + // Default value. + $titleAndPlaceholderValue = t('Say something to the Community'); + + if ($form_state->get('currentGroup') !== NULL) { + $titleAndPlaceholderValue = t('Say something to the group'); + } + + $user_profile = $form_state->get('recipientUser'); + if ($user_profile !== NULL + && $user_profile->id() !== $this->currentUser->id()) { + $titleAndPlaceholderValue = t('Leave a message to @name', [ + '@name' => $user_profile->getDisplayName(), + ]); + } + + // Set the title and placeholder value. + $form['field_post']['widget'][0]['#title'] = $titleAndPlaceholderValue; + $form['field_post']['widget'][0]['#placeholder'] = $titleAndPlaceholderValue; + } + +} diff --git a/modules/social_features/social_post/src/Plugin/Block/PostBlock.php b/modules/social_features/social_post/src/Plugin/Block/PostBlock.php index a72ee60e329..b748dc04498 100644 --- a/modules/social_features/social_post/src/Plugin/Block/PostBlock.php +++ b/modules/social_features/social_post/src/Plugin/Block/PostBlock.php @@ -8,8 +8,10 @@ use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormState; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountProxyInterface; +use Drupal\social_group\CurrentGroupService; use Drupal\user\EntityOwnerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -28,49 +30,63 @@ class PostBlock extends BlockBase implements ContainerFactoryPluginInterface { * * @var string */ - public $entityType; + public string $entityType; /** * The bundle. * * @var string */ - public $bundle; + public string $bundle; /** * The form display. * * @var string */ - public $formDisplay; + public string $formDisplay; /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $entityTypeManager; + protected EntityTypeManagerInterface $entityTypeManager; /** * The current user. * * @var \Drupal\Core\Session\AccountProxyInterface */ - protected $currentUser; + protected AccountProxyInterface $currentUser; /** * The form builder. * * @var \Drupal\Core\Form\FormBuilderInterface */ - protected $formBuilder; + protected FormBuilderInterface $formBuilder; /** * The module handler. * * @var \Drupal\Core\Extension\ModuleHandlerInterface */ - protected $moduleHandler; + protected ModuleHandlerInterface $moduleHandler; + + /** + * The current rouge match. + * + * @var \Drupal\Core\Routing\CurrentRouteMatch + */ + protected CurrentRouteMatch $routeMatch; + + /** + * The current group service. + * + * @var \Drupal\social_group\CurrentGroupService + */ + protected CurrentGroupService $currentGroupService; /** * PostBlock constructor. @@ -92,6 +108,10 @@ class PostBlock extends BlockBase implements ContainerFactoryPluginInterface { * The form builder. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. + * @param \Drupal\Core\Routing\CurrentRouteMatch $route_match + * The current route match. + * @param \Drupal\social_group\CurrentGroupService $current_group_service + * The current group service. */ public function __construct( array $configuration, @@ -100,7 +120,9 @@ public function __construct( EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user, FormBuilderInterface $form_builder, - ModuleHandlerInterface $module_handler + ModuleHandlerInterface $module_handler, + CurrentRouteMatch $route_match, + CurrentGroupService $current_group_service, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); @@ -111,6 +133,8 @@ public function __construct( $this->currentUser = $current_user; $this->formBuilder = $form_builder; $this->moduleHandler = $module_handler; + $this->routeMatch = $route_match; + $this->currentGroupService = $current_group_service; if ($module_handler->moduleExists('social_post_photo')) { $this->bundle = 'photo'; @@ -128,7 +152,9 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('entity_type.manager'), $container->get('current_user'), $container->get('form_builder'), - $container->get('module_handler') + $container->get('module_handler'), + $container->get('current_route_match'), + $container->get(CurrentGroupService::class), ); } @@ -170,6 +196,12 @@ public function build() { $form_state = (new FormState())->setFormState([]); $form_state->set('form_display', $display); + + // Add the current group from the runtime context. + $form_state->set('currentGroup', $this->currentGroupService->fromRunTimeContexts()); + // Add recipient user from the url. + $form_state->set('recipientUser', $this->routeMatch->getParameter('user')); + return $this->formBuilder->buildForm($form_object, $form_state); } diff --git a/modules/social_features/social_post/src/Plugin/Block/PostGroupBlock.php b/modules/social_features/social_post/src/Plugin/Block/PostGroupBlock.php index 4e87d085cad..678658bb0c1 100644 --- a/modules/social_features/social_post/src/Plugin/Block/PostGroupBlock.php +++ b/modules/social_features/social_post/src/Plugin/Block/PostGroupBlock.php @@ -5,10 +5,12 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormBuilderInterface; +use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Access\AccessResult; use Drupal\Core\Session\AccountProxyInterface; use Drupal\group\Entity\GroupInterface; +use Drupal\social_group\CurrentGroupService; /** * Provides a 'PostGroupBlock' block. @@ -30,7 +32,9 @@ public function __construct( EntityTypeManagerInterface $entity_type_manager, AccountProxyInterface $current_user, FormBuilderInterface $form_builder, - ModuleHandlerInterface $module_handler + ModuleHandlerInterface $module_handler, + CurrentRouteMatch $route_match, + CurrentGroupService $current_group_service, ) { parent::__construct( $configuration, @@ -39,7 +43,9 @@ public function __construct( $entity_type_manager, $current_user, $form_builder, - $module_handler + $module_handler, + $route_match, + $current_group_service, ); $this->entityType = 'post'; diff --git a/modules/social_features/social_post/src/Plugin/Block/PostProfileBlock.php b/modules/social_features/social_post/src/Plugin/Block/PostProfileBlock.php index 1c264422267..c685c538b77 100644 --- a/modules/social_features/social_post/src/Plugin/Block/PostProfileBlock.php +++ b/modules/social_features/social_post/src/Plugin/Block/PostProfileBlock.php @@ -5,8 +5,9 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormBuilderInterface; +use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\Core\Session\AccountProxyInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\social_group\CurrentGroupService; /** * Provides a 'PostProfileBlock' block. @@ -19,27 +20,7 @@ class PostProfileBlock extends PostBlock { /** - * PostProfileBlock constructor. - * - * @param array $configuration - * The plugin configuration, i.e. an array with configuration values keyed - * by configuration option name. The special key 'context' may be used to - * initialize the defined contexts by setting it to an array of context - * values keyed by context names. - * @param string $plugin_id - * The plugin_id for the plugin instance. - * @param mixed $plugin_definition - * The plugin implementation definition. - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * The entity type manager. - * @param \Drupal\Core\Session\AccountProxyInterface $current_user - * The current user. - * @param \Drupal\Core\Form\FormBuilderInterface $form_builder - * The form builder. - * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler - * The module handler. - * @param mixed $account - * The user object or user ID. + * {@inheritdoc} */ public function __construct( array $configuration, @@ -49,7 +30,8 @@ public function __construct( AccountProxyInterface $current_user, FormBuilderInterface $form_builder, ModuleHandlerInterface $module_handler, - $account + CurrentRouteMatch $route_match, + CurrentGroupService $current_group_service, ) { parent::__construct( $configuration, @@ -58,7 +40,9 @@ public function __construct( $entity_type_manager, $current_user, $form_builder, - $module_handler + $module_handler, + $route_match, + $current_group_service, ); $this->entityType = 'post'; @@ -67,26 +51,11 @@ public function __construct( // Check if current user is the same as the profile. // In this case use the default form display. + $account = $this->routeMatch->getParameter('user'); $uid = $this->currentUser->id(); if (isset($account) && ($account === $uid || (is_object($account) && $uid === $account->id()))) { $this->formDisplay = 'default'; } } - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('entity_type.manager'), - $container->get('current_user'), - $container->get('form_builder'), - $container->get('module_handler'), - $container->get('current_route_match')->getParameter('user') - ); - } - } diff --git a/modules/social_features/social_post/src/Test/Unit/SocialPostFormHooksTest.php b/modules/social_features/social_post/src/Test/Unit/SocialPostFormHooksTest.php new file mode 100644 index 00000000000..fb3fd1498b4 --- /dev/null +++ b/modules/social_features/social_post/src/Test/Unit/SocialPostFormHooksTest.php @@ -0,0 +1,220 @@ +socialPostHelper = $this->createMock(SocialPostHelperInterface::class); + $this->currentUser = $this->createMock(AccountProxyInterface::class); + $this->formState = $this->createMock(FormStateInterface::class); + + $this->socialPostFormHooks = new SocialPostFormHooks( + $this->socialPostHelper, + $this->currentUser + ); + + $translation = $this->createMock(TranslationInterface::class); + $translation->method('translate') + ->willReturnCallback(function ($string, array $args = [], array $options = []) { + // phpcs:ignore + return new TranslatableMarkup($string, $args, $options); + }); + + $container = new ContainerBuilder(); + $container->set('string_translation', $translation); + \Drupal::setContainer($container); + + $this->form = [ + 'field_post' => ['widget' => [0 => []]], + 'actions' => ['submit' => ['#value' => '']], + ]; + } + + /** + * Test formPostFormAlter with a new post and valid current user image. + */ + public function testFormPostFormAlterWithNewPostAndImage(): void { + $this->mockContentEntityForm(); + $currentUserImage = $this->mockUserImage(); + + $this->socialPostFormHooks->formPostFormAlter($this->form, $this->formState); + + $this->assertEquals($currentUserImage, $this->form['current_user_image']); + $this->assertEquals( + new TranslatableMarkup('Post', [], ['context' => 'Post button']), + $this->form['actions']['submit']['#value'] + ); + $this->assertEquals( + new TranslatableMarkup('Say something to the Community'), + $this->form['field_post']['widget'][0]['#title'] + ); + } + + /** + * Test formPostFormAlter when there's a current group set. + */ + public function testFormPostFormAlterWithCurrentGroup(): void { + $this->mockContentEntityForm(); + $currentUserImage = $this->mockUserImage(); + + $this->formState->expects($this->exactly(2)) + ->method('get') + ->willReturnMap([ + ['currentGroup', 2], + ['recipientUser', NULL], + ]); + + $this->socialPostFormHooks->formPostFormAlter($this->form, $this->formState); + + $this->assertEquals($currentUserImage, $this->form['current_user_image']); + $this->assertEquals( + new TranslatableMarkup('Post', [], ['context' => 'Post button']), + $this->form['actions']['submit']['#value'] + ); + $this->assertEquals( + new TranslatableMarkup('Say something to the group'), + $this->form['field_post']['widget'][0]['#title'] + ); + $this->assertEquals( + new TranslatableMarkup('Say something to the group'), + $this->form['field_post']['widget'][0]['#placeholder'] + ); + } + + /** + * Test formPostFormAlter when it's a private message. + */ + public function testFormPostFormAlterWithRecipientUser(): void { + $this->mockContentEntityForm(); + $currentUserImage = $this->mockUserImage(); + + $recipientUser = $this->createMock(User::class); + + $this->formState->expects($this->exactly(2)) + ->method('get') + ->willReturnMap([ + ['currentGroup', NULL], + ['recipientUser', $recipientUser], + ]); + + $recipientUser->expects($this->once()) + ->method('getDisplayName') + ->willReturn('John Doe'); + $recipientUser->expects($this->once()) + ->method('id') + ->willReturn(2); + + $this->currentUser->expects($this->once()) + ->method('id') + ->willReturn(1); + + $this->socialPostFormHooks->formPostFormAlter($this->form, $this->formState); + + $this->assertEquals($currentUserImage, $this->form['current_user_image']); + $this->assertEquals( + new TranslatableMarkup('Post', [], ['context' => 'Post button']), + $this->form['actions']['submit']['#value'] + ); + $this->assertEquals( + new TranslatableMarkup('Leave a message to @name', ['@name' => 'John Doe']), + $this->form['field_post']['widget'][0]['#title'] + ); + $this->assertEquals( + new TranslatableMarkup('Leave a message to @name', ['@name' => 'John Doe']), + $this->form['field_post']['widget'][0]['#placeholder'] + ); + } + + /** + * Mock user image. + * + * @return array + * The mocked user image. + */ + private function mockUserImage(): array { + $currentUserImage = ['#markup' => 'User Image']; + $this->socialPostHelper + ->method('buildCurrentUserImage') + ->willReturn($currentUserImage); + return $currentUserImage; + } + + /** + * Mock content entity form. + */ + private function mockContentEntityForm(): void { + $contentEntityForm = $this->createMock(ContentEntityForm::class); + $entity = $this->createMock(ContentEntityInterface::class); + $entity->expects($this->once()) + ->method('isNew') + ->willReturn(TRUE); + + $contentEntityForm->expects($this->once()) + ->method('getEntity') + ->willReturn($entity); + + $this->formState->expects($this->once()) + ->method('getFormObject') + ->willReturn($contentEntityForm); + } + +} diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 0851ad545fb..d7b0c8e5d8c 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -2808,11 +2808,6 @@ parameters: count: 1 path: modules/social_features/social_activity/modules/social_activity_filter/src/Plugin/views/display/FilterBlock.php - - - message: "#^Method Drupal\\\\social_activity_filter\\\\Plugin\\\\views\\\\display\\\\FilterBlock\\:\\:updateTagsOptions\\(\\) has parameter \\$form with no type specified\\.$#" - count: 1 - path: modules/social_features/social_activity/modules/social_activity_filter/src/Plugin/views/display/FilterBlock.php - - message: "#^Offset '\\#parents' does not exist on array\\|null\\.$#" count: 2 @@ -9714,11 +9709,6 @@ parameters: count: 1 path: modules/social_features/social_post/social_post.install - - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Form\\\\FormInterface\\:\\:getEntity\\(\\)\\.$#" - count: 1 - path: modules/social_features/social_post/social_post.module - - message: "#^Function _social_post_comment_post_comment_form_submit\\(\\) has no return type specified\\.$#" count: 1 @@ -9749,21 +9739,6 @@ parameters: count: 1 path: modules/social_features/social_post/social_post.module - - - message: "#^Function social_post_form_post_form_alter\\(\\) has no return type specified\\.$#" - count: 1 - path: modules/social_features/social_post/social_post.module - - - - message: "#^Function social_post_form_post_form_alter\\(\\) has parameter \\$form with no type specified\\.$#" - count: 1 - path: modules/social_features/social_post/social_post.module - - - - message: "#^Function social_post_form_post_form_alter\\(\\) has parameter \\$form_id with no type specified\\.$#" - count: 1 - path: modules/social_features/social_post/social_post.module - - message: "#^Function social_post_preprocess_activity\\(\\) has no return type specified\\.$#" count: 1 @@ -9829,11 +9804,6 @@ parameters: count: 1 path: modules/social_features/social_post/social_post.module - - - message: "#^Not allowed to call private function _social_group_get_current_group from module social_post\\.$#" - count: 1 - path: modules/social_features/social_post/social_post.module - - message: "#^Cannot call method fetchAll\\(\\) on Drupal\\\\Core\\\\Database\\\\StatementInterface\\|null\\.$#" count: 1 diff --git a/social.info.yml b/social.info.yml index 0e62eecf57e..204dc99adcd 100644 --- a/social.info.yml +++ b/social.info.yml @@ -18,9 +18,9 @@ dependencies: - color:color - improved_theme_settings:improved_theme_settings # Open Social + - social:social_group - social:social_activity - social:social_core - - social:social_group - social:social_swiftmail - social:social_user