Commit c6954e6e authored by catch's avatar catch
Browse files

fix: #3562753 History module triggers ServiceNotFoundException for...

fix: #3562753 History module triggers ServiceNotFoundException for comment.manager when Comment module is not enabled

By: phoang
By: smustgrave
By: quietone
By: dcam
By: mingsong
By: dww
(cherry picked from commit dbb19e2a)
parent 6a164398
Loading
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -22,12 +22,14 @@ class HistoryTokensHooks {
  #[Hook('token_info')]
  public function tokenInfo(): array {
    $tokens = [];
    // Check if the comment manager service exists before processing.
    $commentManager = \Drupal::hasService('comment.manager') ? \Drupal::service('comment.manager') : NULL;
    // Provides an integration for each entity type except comment.
    foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type_id => $entity_type) {
      if ($entity_type_id == 'comment' || !$entity_type->entityClassImplements(ContentEntityInterface::class)) {
        continue;
      }
      if (\Drupal::service('comment.manager')->getFields($entity_type_id)) {
      if ($commentManager && $commentManager->getFields($entity_type_id)) {
        // Get the correct token type.
        $token_type = $entity_type_id == 'taxonomy_term' ? 'term' : $entity_type_id;
        $tokens[$token_type]['comment-count-new'] = [
+53 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\Tests\history\Unit;

use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\history\Hook\HistoryTokensHooks;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests for HistoryTokensHooks.
 */
#[CoversMethod(HistoryTokensHooks::class, 'tokenInfo')]
#[Group('history')]
class HistoryTokensHooksTest extends UnitTestCase {

  /**
   * Tests that tokenInfo() handles missing comment module gracefully.
   */
  public function testTokenInfoWithoutCommentModuleInstalled(): void {
    // Create a mock entity type that implements ContentEntityInterface.
    $entityType = $this->createMock(EntityTypeInterface::class);
    $entityType->expects($this->once())
      ->method('entityClassImplements')
      ->with(ContentEntityInterface::class)
      ->willReturn(TRUE);

    // Create a mock entity type manager that returns our entity type.
    $entityTypeManager = $this->createMock(EntityTypeManagerInterface::class);
    $entityTypeManager->expects($this->once())
      ->method('getDefinitions')
      ->willReturn(['node' => $entityType]);

    // Create a container WITHOUT the comment module (service) installed.
    $container = new ContainerBuilder();
    $container->set('entity_type.manager', $entityTypeManager);
    \Drupal::setContainer($container);

    // Create the hooks class.
    $historyTokensHooks = new HistoryTokensHooks();

    $result = $historyTokensHooks->tokenInfo();

    $this->assertEquals(['tokens' => []], $result);
  }

}