From 9e3064ca219ec82799485c2b35d4d6a308884d5b Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 30 Jun 2020 12:50:21 +0100 Subject: [PATCH] Add tests for counting previous talks References #31 --- .idea/php.xml | 2 + web/modules/custom/custom/custom.services.yml | 3 + .../custom/custom/src/Service/TalkCounter.php | 41 ++++++++++++ .../src/Kernel/CountPreviousTalksTest.php | 66 +++++++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 web/modules/custom/custom/src/Service/TalkCounter.php create mode 100644 web/modules/custom/custom/tests/src/Kernel/CountPreviousTalksTest.php diff --git a/.idea/php.xml b/.idea/php.xml index 6d17c06..d34eca7 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -139,6 +139,8 @@ + + diff --git a/web/modules/custom/custom/custom.services.yml b/web/modules/custom/custom/custom.services.yml index 21bb957..1993b99 100644 --- a/web/modules/custom/custom/custom.services.yml +++ b/web/modules/custom/custom/custom.services.yml @@ -2,3 +2,6 @@ services: Drupal\custom\EventSubscriber\UpdateTalkNodeBeforeSave: tags: - { name: event_subscriber } + + Drupal\custom\Service\TalkCounter: + autowire: true diff --git a/web/modules/custom/custom/src/Service/TalkCounter.php b/web/modules/custom/custom/src/Service/TalkCounter.php new file mode 100644 index 0000000..933ab00 --- /dev/null +++ b/web/modules/custom/custom/src/Service/TalkCounter.php @@ -0,0 +1,41 @@ +nodeStorage = $entityTypeManager->getStorage('node'); + } + + public function getCount(): int { + $today = Carbon::today()->format('Y-m-d H:i:s'); + + return $this->getTalks() + ->flatMap->getEvents() + ->filter(fn(ParagraphInterface $event) => $event->get('field_date')->getString() <= $today) + ->count(); + } + + private function getTalks(): Collection { + $talks = $this->nodeStorage->loadByProperties([ + 'status' => NodeInterface::PUBLISHED, + 'type' => 'talk', + ]); + + return new Collection($talks); + } + +} diff --git a/web/modules/custom/custom/tests/src/Kernel/CountPreviousTalksTest.php b/web/modules/custom/custom/tests/src/Kernel/CountPreviousTalksTest.php new file mode 100644 index 0000000..861f5fa --- /dev/null +++ b/web/modules/custom/custom/tests/src/Kernel/CountPreviousTalksTest.php @@ -0,0 +1,66 @@ +createTalk([ + 'field_events' => [ + $this->createEvent(), + $this->createEvent(), + ], + ]); + + $this->createTalk([ + 'field_events' => [ + $this->createEvent(), + ], + ]); + + Assert::assertSame(3, $this->talkCounter->getCount()); + } + + /** @test */ + public function future_talks_are_not_counted(): void { + $this->createTalk([ + 'field_events' => [ + $this->createEvent([ + 'field_date' => Carbon::now()->subDay(), + ]), + $this->createEvent([ + 'field_date' => Carbon::now()->addDay(), + ]), + ], + ]); + + Assert::assertSame(1, $this->talkCounter->getCount()); + } + + /** @test */ + public function unpublished_talks_are_not_counted(): void { + $this->createTalk([ + 'field_events' => [$this->createEvent()], + 'status' => NodeInterface::NOT_PUBLISHED, + ]); + + Assert::assertSame(0, $this->talkCounter->getCount()); + } + + protected function setUp() { + parent::setUp(); + + $this->talkCounter = $this->container->get(TalkCounter::class); + } + +}