oliverdavies.uk/modules/opd_presentations/tests/src/Functional/PresentationCounterTest.php

103 lines
2.6 KiB
PHP
Raw Normal View History

2025-06-14 22:10:24 +01:00
<?php
declare(strict_types=1);
namespace Drupal\Tests\opd_presentations;
use Drupal\Tests\RandomGeneratorTrait;
use Drupal\Tests\opd_presentations\Traits\PresentationCreationTrait;
use Drupal\opd_presentations\Date;
2025-06-14 23:18:13 +01:00
use Drupal\opd_presentations\PresentationBuilder;
2025-06-14 22:10:24 +01:00
use Drupal\opd_presentations\PresentationCounter;
use weitzman\DrupalTestTraits\ExistingSiteBase;
final class PresentationCounterTest extends ExistingSiteBase {
use PresentationCreationTrait;
use RandomGeneratorTrait;
public function test_it_counts_events(): void {
$counter = $this->container->get(PresentationCounter::class);
assert($counter instanceof PresentationCounter);
2025-06-14 23:18:13 +01:00
$events = [
$this->createEvent(
eventDate: Date::fromString('yesterday'),
eventName: $this->randomString(),
),
];
PresentationBuilder::create('')
->setEvents($events)
->build();
2025-06-14 22:10:24 +01:00
$this->assertGreaterThanOrEqual(
actual: $counter->getPastCount(),
expected: 1,
);
}
public function test_it_only_counts_published_events(): void {
$counter = $this->container->get(PresentationCounter::class);
assert($counter instanceof PresentationCounter);
$count = $counter->getPastCount();
2025-06-14 23:18:13 +01:00
$events = [
$this->createEvent(
eventDate: Date::fromString('yesterday'),
eventName: $this->randomString(),
),
];
$presentation = PresentationBuilder::create('')
->setEvents($events)
->setPublished(FALSE)
->build();
2025-06-14 22:10:24 +01:00
$this->assertSame(
actual: $counter->getPastCount(),
expected: $count,
);
}
public function test_it_only_counts_past_events(): void {
$counter = $this->container->get(PresentationCounter::class);
assert($counter instanceof PresentationCounter);
// Get the existing presentation count (including existing nodes).
$originalCount = $counter->getPastCount();
$this->assertGreaterThanOrEqual(
actual: $originalCount,
expected: 0,
);
2025-06-14 23:18:13 +01:00
$events = [
$this->createEvent(
eventDate: Date::fromString('tomorrow'),
eventName: $this->randomString(),
),
$this->createEvent(
eventDate: Date::fromString('yesterday'),
eventName: $this->randomString(),
),
];
$presentation = PresentationBuilder::create($this->randomString())
->setEvents($events)
->build();
$presentation->save();
2025-06-14 22:10:24 +01:00
$counter = $this->container->get(PresentationCounter::class);
// Ensure the count has only increased by one, even though a future and past event were created.
$this->assertSame(
actual: $counter->getPastCount(),
expected: $originalCount + 1,
);
}
}