From 0db961b74178a60fd5dbea4284eb5dba1578498f Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Wed, 20 Nov 2024 13:23:17 +0000 Subject: [PATCH] Remove the hard-coded talk count --- .../custom/opdavies_talks/opdavies_talks.services.yml | 3 ++- .../opdavies_talks/src/Repository/TalkNodeRepository.php | 9 ++++++++- .../tests/src/Functional/TalksPageTest.php | 3 +++ .../src/Kernel/Repository/TalkNodeRepositoryTest.php | 8 +++++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/web/modules/custom/opdavies_talks/opdavies_talks.services.yml b/web/modules/custom/opdavies_talks/opdavies_talks.services.yml index 1c19963..d5e6a8a 100644 --- a/web/modules/custom/opdavies_talks/opdavies_talks.services.yml +++ b/web/modules/custom/opdavies_talks/opdavies_talks.services.yml @@ -1,4 +1,5 @@ services: Drupal\opdavies_talks\Controller\TalksPageController: autowire: true - Drupal\opdavies_talks\Repository\TalkNodeRepository: [] + Drupal\opdavies_talks\Repository\TalkNodeRepository: + autowire: true diff --git a/web/modules/custom/opdavies_talks/src/Repository/TalkNodeRepository.php b/web/modules/custom/opdavies_talks/src/Repository/TalkNodeRepository.php index cde85f8..9800a43 100644 --- a/web/modules/custom/opdavies_talks/src/Repository/TalkNodeRepository.php +++ b/web/modules/custom/opdavies_talks/src/Repository/TalkNodeRepository.php @@ -4,10 +4,17 @@ declare(strict_types=1); namespace Drupal\opdavies_talks\Repository; +use Drupal\Core\Entity\EntityTypeManagerInterface; + final class TalkNodeRepository { + public function __construct(private EntityTypeManagerInterface $entityTypeManager) { + } + public function getCount(): int { - return 2; + $talks = $this->entityTypeManager->getStorage('node')->loadByProperties(); + + return count($talks); } } diff --git a/web/modules/custom/opdavies_talks/tests/src/Functional/TalksPageTest.php b/web/modules/custom/opdavies_talks/tests/src/Functional/TalksPageTest.php index 2cfa158..febcd83 100644 --- a/web/modules/custom/opdavies_talks/tests/src/Functional/TalksPageTest.php +++ b/web/modules/custom/opdavies_talks/tests/src/Functional/TalksPageTest.php @@ -19,6 +19,9 @@ class TalksPageTest extends BrowserTestBase { } public function test_it_displays_the_talk_count(): void { + $this->createNode(['type' => 'talk']); + $this->createNode(['type' => 'talk']); + $this->drupalGet('/talks'); $session = $this->assertSession(); diff --git a/web/modules/custom/opdavies_talks/tests/src/Kernel/Repository/TalkNodeRepositoryTest.php b/web/modules/custom/opdavies_talks/tests/src/Kernel/Repository/TalkNodeRepositoryTest.php index 4bd89b9..8b5f300 100644 --- a/web/modules/custom/opdavies_talks/tests/src/Kernel/Repository/TalkNodeRepositoryTest.php +++ b/web/modules/custom/opdavies_talks/tests/src/Kernel/Repository/TalkNodeRepositoryTest.php @@ -3,13 +3,19 @@ namespace Drupal\Tests\opdavies_talks\Kernel\Repository; use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; +use Drupal\Tests\node\Traits\NodeCreationTrait; use Drupal\opdavies_talks\Repository\TalkNodeRepository; final class TalkNodeRepositoryTest extends EntityKernelTestBase { - protected static $modules = ['opdavies_talks']; + protected static $modules = ['node', 'opdavies_talks']; + + use NodeCreationTrait; public function test_it_returns_a_count(): void { + $this->createNode(['type' => 'talk']); + $this->createNode(['type' => 'talk']); + $talkRepository = $this->container->get(TalkNodeRepository::class); self::assertSame(2, $talkRepository->getCount());