Rename and re-organise custom modules
- Rename `opd_talks` to `opdavies_talks` - Rename `custom` to `opdavies_blog`
This commit is contained in:
parent
e4e898f22c
commit
9b1a8fb3be
53 changed files with 125 additions and 116 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,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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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