Loading core/modules/history/src/Hook/HistoryTokensHooks.php +3 −1 Original line number Diff line number Diff line Loading @@ -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'] = [ Loading core/modules/history/tests/src/Unit/HistoryTokensHooksTest.php 0 → 100644 +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); } } Loading
core/modules/history/src/Hook/HistoryTokensHooks.php +3 −1 Original line number Diff line number Diff line Loading @@ -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'] = [ Loading
core/modules/history/tests/src/Unit/HistoryTokensHooksTest.php 0 → 100644 +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); } }