Rename custom module directories
- Rename `opdavies_blog` to `blog`. - Rename `opdavies_blog_test` to `blog_test`. - Rename `opdavies_talks` to `talks`. - Rename `opdavies_talks_test` to `talks_test`. The files within the directories haven't changed, so there is no breaking change caused by renaming the directories. Please enter the commit message for your changes. Lines starting
This commit is contained in:
parent
d7d5a6c8a3
commit
cbe60209e6
59 changed files with 0 additions and 0 deletions
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
namespace Drupal\Tests\opdavies_talks\Kernel;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\opdavies_talks\Service\TalkCounter;
|
||||
use PHPUnit\Framework\Assert;
|
||||
|
||||
class CountPreviousTalksTest extends TalksTestBase {
|
||||
|
||||
private TalkCounter $talkCounter;
|
||||
|
||||
/** @test */
|
||||
public function previous_talks_are_counted(): void {
|
||||
$this->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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,113 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\opdavies_talks\Kernel;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
|
||||
final class EventsAreReorderedByDateTest extends TalksTestBase {
|
||||
|
||||
public function testCreatingNode() {
|
||||
$events = [
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()->addWeeks(2),
|
||||
'field_name' => 'Drupal Bristol',
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::yesterday(),
|
||||
'field_name' => 'DrupalCamp London',
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::tomorrow(),
|
||||
'field_name' => 'PHP UK conference',
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()->addMonths(3),
|
||||
'field_name' => 'CMS Philly',
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()->subYear(),
|
||||
'field_name' => 'PHP South Wales',
|
||||
]),
|
||||
];
|
||||
|
||||
$talk = $this->createTalk([
|
||||
'field_events' => $events,
|
||||
]);
|
||||
|
||||
$this->assertSame(
|
||||
[
|
||||
'PHP South Wales',
|
||||
'DrupalCamp London',
|
||||
'PHP UK conference',
|
||||
'Drupal Bristol',
|
||||
'CMS Philly',
|
||||
],
|
||||
$talk->getEvents()
|
||||
->map(fn(ParagraphInterface $event) => $event->get('field_name')
|
||||
->getString())
|
||||
->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
public function testUpdatingNode() {
|
||||
$events = [
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()->addWeeks(2),
|
||||
'field_name' => 'Drupal Bristol',
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::yesterday(),
|
||||
'field_name' => 'DrupalCamp London',
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()->addMonths(3),
|
||||
'field_name' => 'CMS Philly',
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()->subYear(),
|
||||
'field_name' => 'PHP South Wales',
|
||||
]),
|
||||
];
|
||||
|
||||
$talk = $this->createTalk([
|
||||
'field_events' => $events,
|
||||
]);
|
||||
|
||||
$this->assertSame(
|
||||
[
|
||||
'PHP South Wales',
|
||||
'DrupalCamp London',
|
||||
'Drupal Bristol',
|
||||
'CMS Philly',
|
||||
],
|
||||
$talk->getEvents()
|
||||
->map(fn(ParagraphInterface $event) => $event->get('field_name')
|
||||
->getString())
|
||||
->toArray()
|
||||
);
|
||||
|
||||
$talk->addEvent($this->createEvent([
|
||||
'field_date' => Carbon::tomorrow(),
|
||||
'field_name' => 'PHP UK conference',
|
||||
]));
|
||||
$talk->save();
|
||||
|
||||
$this->assertSame(
|
||||
[
|
||||
'PHP South Wales',
|
||||
'DrupalCamp London',
|
||||
'PHP UK conference',
|
||||
'Drupal Bristol',
|
||||
'CMS Philly',
|
||||
],
|
||||
$talk->getEvents()
|
||||
->map(fn(ParagraphInterface $event) => $event->get('field_name')
|
||||
->getString())
|
||||
->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\opdavies_talks\Kernel\Repository;
|
||||
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
||||
use Drupal\opdavies_talks\Repository\TalkRepository;
|
||||
use Drupal\Tests\node\Traits\NodeCreationTrait;
|
||||
use Drupal\Tests\opdavies_talks\Kernel\TalksTestBase;
|
||||
|
||||
final class TalkRepositoryTest extends TalksTestBase {
|
||||
|
||||
use NodeCreationTrait;
|
||||
|
||||
private TalkRepository $talkRepository;
|
||||
|
||||
/** @test */
|
||||
public function get_all_talks() {
|
||||
$this->createTalk(['title' => 'TDD - Test Driven Drupal']);
|
||||
$this->createTalk(['title' => 'Taking Flight with Tailwind CSS']);
|
||||
$this->createTalk(['title' => 'Upgrading to Drupal 9']);
|
||||
|
||||
$talks = $this->talkRepository->getAll();
|
||||
|
||||
$this->assertCount(3, $talks);
|
||||
$this->assertSame(
|
||||
[
|
||||
1 => 'TDD - Test Driven Drupal',
|
||||
2 => 'Taking Flight with Tailwind CSS',
|
||||
3 => 'Upgrading to Drupal 9',
|
||||
],
|
||||
$talks->map(fn(Talk $talk) => $talk->label())->toArray()
|
||||
);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function get_all_published_talks() {
|
||||
$this->createTalk([
|
||||
'title' => 'TDD - Test Driven Drupal',
|
||||
'status' => NodeInterface::PUBLISHED,
|
||||
]);
|
||||
|
||||
$this->createTalk([
|
||||
'title' => 'Taking Flight with Tailwind CSS',
|
||||
'status' => NodeInterface::NOT_PUBLISHED,
|
||||
]);
|
||||
|
||||
$talks = $this->talkRepository->getAll(TRUE);
|
||||
|
||||
$this->assertCount(1, $talks);
|
||||
$this->assertSame('TDD - Test Driven Drupal', $talks->first()->label());
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function it_only_returns_talk_nodes() {
|
||||
$this->createNode(['type' => 'page']);
|
||||
|
||||
$talks = $this->talkRepository->getAll();
|
||||
|
||||
$this->assertEmpty($talks);
|
||||
}
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installConfig(['filter']);
|
||||
|
||||
$this->talkRepository = $this->container->get(TalkRepository::class);
|
||||
}
|
||||
|
||||
}
|
103
web/modules/custom/talks/tests/src/Kernel/TalkEventDateTest.php
Normal file
103
web/modules/custom/talks/tests/src/Kernel/TalkEventDateTest.php
Normal file
|
@ -0,0 +1,103 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\opdavies_talks\Kernel;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
||||
use Drupal\opdavies_talks\Service\TalkDateUpdater;
|
||||
|
||||
final class TalkEventDateTest extends TalksTestBase {
|
||||
|
||||
/** @test */
|
||||
public function talk_event_dates_are_set_to_the_next_future_date(): void {
|
||||
$dateFormat = DateTimeItemInterface::DATE_STORAGE_FORMAT;
|
||||
|
||||
$talk = $this->createTalk([
|
||||
'field_event_date' => NULL,
|
||||
'field_events' => [
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->subWeeks(2)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->subDays(2)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->addDays(4)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->addDays(10)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
],
|
||||
]);
|
||||
|
||||
$dateUpdater = $this->container->get(TalkDateUpdater::class);
|
||||
$dateUpdater->__invoke();
|
||||
|
||||
$expected = Carbon::today()->addDays(4)->getTimestamp();
|
||||
|
||||
$talk = Node::load($talk->id());
|
||||
$this->assertNextEventDateIs($talk, $expected);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function talk_event_dates_are_set_to_the_last_past_date(): void {
|
||||
$dateFormat = DateTimeItemInterface::DATE_STORAGE_FORMAT;
|
||||
|
||||
$talk = $this->createTalk([
|
||||
'field_event_date' => NULL,
|
||||
'field_events' => [
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->subDays(4)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
$this->createEvent([
|
||||
'field_date' => Carbon::today()
|
||||
->subDays(2)
|
||||
->format($dateFormat),
|
||||
]),
|
||||
],
|
||||
]);
|
||||
|
||||
$dateUpdater = $this->container->get(TalkDateUpdater::class);
|
||||
$dateUpdater->__invoke();
|
||||
|
||||
$expected = Carbon::today()->subDays(2)->getTimestamp();
|
||||
|
||||
$talk = Node::load($talk->id());
|
||||
$this->assertNextEventDateIs($talk, $expected);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function next_event_date_is_empty_if_there_are_no_events(): void {
|
||||
$talk = $this->createTalk([
|
||||
'field_event_date' => NULL,
|
||||
'field_events' => [],
|
||||
]);
|
||||
|
||||
$dateUpdater = $this->container->get(TalkDateUpdater::class);
|
||||
$dateUpdater->__invoke();
|
||||
|
||||
$talk = Node::load($talk->id());
|
||||
$this->assertNoNextEventDate($talk);
|
||||
}
|
||||
|
||||
private function assertNextEventDateIs(Talk $talk, $expected): void {
|
||||
$this->assertSame($expected, $talk->getNextDate());
|
||||
}
|
||||
|
||||
private function assertNoNextEventDate(Talk $talk): void {
|
||||
$this->assertNull($talk->getNextDate());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\opdavies_talks\Kernel;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\views\ResultRow;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
final class TalksPageSortTest extends TalksTestBase {
|
||||
|
||||
public static $modules = [
|
||||
'views',
|
||||
'opdavies_talks',
|
||||
];
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function upcoming_talks_are_shown_first_followed_by_past_talks_and_ordered_by_distance() {
|
||||
$this->createTalk([
|
||||
'field_event_date' => Carbon::today()->addDays(4)->getTimestamp(),
|
||||
]);
|
||||
$this->createTalk([
|
||||
'field_event_date' => Carbon::today()->subDays(2)->getTimestamp(),
|
||||
]);
|
||||
$this->createTalk([
|
||||
'field_event_date' => Carbon::today()->addDay()->getTimestamp(),
|
||||
]);
|
||||
$this->createTalk([
|
||||
'field_event_date' => Carbon::today()->subDays(10)->getTimestamp(),
|
||||
]);
|
||||
|
||||
$talkIds = (new Collection(views_get_view_result('talks')))
|
||||
->map(fn(ResultRow $row) => (int) $row->_entity->id());
|
||||
|
||||
$this->assertSame([3, 1, 2, 4], $talkIds->toArray());
|
||||
}
|
||||
|
||||
}
|
64
web/modules/custom/talks/tests/src/Kernel/TalksTestBase.php
Normal file
64
web/modules/custom/talks/tests/src/Kernel/TalksTestBase.php
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\opdavies_talks\Kernel;
|
||||
|
||||
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\opdavies_talks\Entity\Node\Talk;
|
||||
use Drupal\paragraphs\Entity\Paragraph;
|
||||
use Drupal\paragraphs\ParagraphInterface;
|
||||
|
||||
abstract class TalksTestBase extends EntityKernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = [
|
||||
// Core.
|
||||
'node',
|
||||
'file',
|
||||
'datetime',
|
||||
|
||||
// Contrib.
|
||||
'discoverable_entity_bundle_classes',
|
||||
'entity_reference_revisions',
|
||||
'paragraphs',
|
||||
'hook_event_dispatcher',
|
||||
|
||||
// Custom.
|
||||
'opdavies_talks',
|
||||
'opdavies_talks_test',
|
||||
];
|
||||
|
||||
protected $strictConfigSchema = FALSE;
|
||||
|
||||
protected function createEvent(array $overrides = []): ParagraphInterface {
|
||||
/** @var \Drupal\paragraphs\ParagraphInterface $event */
|
||||
$event = Paragraph::create(array_merge([
|
||||
'type' => 'event',
|
||||
], $overrides));
|
||||
|
||||
return tap($event)->save();
|
||||
}
|
||||
|
||||
protected function createTalk(array $overrides = []): Talk {
|
||||
$talk = Node::create(array_merge([
|
||||
'title' => 'Test Driven Drupal',
|
||||
'type' => 'talk',
|
||||
], $overrides));
|
||||
|
||||
return tap($talk)->save();
|
||||
}
|
||||
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installEntitySchema('paragraph');
|
||||
$this->installSchema('node', ['node_access']);
|
||||
|
||||
$this->installConfig(['opdavies_talks_test']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Drupal\Tests\opdavies_talks\Kernel;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
|
||||
|
||||
final class UpdatesTalkCreatedDateTest extends TalksTestBase {
|
||||
|
||||
public function testCreatingNode() {
|
||||
$eventDate = Carbon::today()->addWeek();
|
||||
$eventDateFormat = $eventDate
|
||||
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
|
||||
$eventDateTimestamp = $eventDate->getTimestamp();
|
||||
|
||||
$talk = $this->createTalk([
|
||||
'field_events' => [
|
||||
$this->createEvent(['field_date' => $eventDateFormat]),
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertEqual($eventDateTimestamp, $talk->getCreatedTime());
|
||||
}
|
||||
|
||||
public function testUpdatingNode() {
|
||||
$talk = $this->createTalk();
|
||||
$originalCreatedTime = $talk->getCreatedTime();
|
||||
|
||||
$eventDate = Carbon::today()->addWeek();
|
||||
$eventDateFormat = $eventDate
|
||||
->format(DateTimeItemInterface::DATE_STORAGE_FORMAT);
|
||||
$eventDateTimestamp = $eventDate->getTimestamp();
|
||||
|
||||
$talk->addEvent(
|
||||
$this->createEvent(['field_date' => $eventDateFormat])
|
||||
);
|
||||
$talk->save();
|
||||
|
||||
$this->assertNotSame($originalCreatedTime, $talk->getCreatedTime());
|
||||
$this->assertSame($eventDateTimestamp, $talk->getCreatedTime());
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue