Extract a base test case for talks tests

This commit is contained in:
Oliver Davies 2020-05-19 01:42:44 +01:00
parent 5393186141
commit bcfe01f62c
2 changed files with 71 additions and 64 deletions

View file

@ -0,0 +1,70 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\custom\Kernel;
use Drupal\Core\Entity\EntityInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
abstract class TalksTestBase extends EntityKernelTestBase {
/**
* {@inheritDoc}
*/
public static $modules = [
// Core.
'node',
'file',
'datetime',
// Contrib.
'entity_reference_revisions',
'paragraphs',
'hook_event_dispatcher',
// Custom.
'custom',
'custom_test',
];
protected function createEvent(string $eventDateToFormat): ParagraphInterface {
/** @var \Drupal\paragraphs\ParagraphInterface $event */
$event = Paragraph::create([
'field_date' => $eventDateToFormat,
'type' => 'event',
]);
return tap($event)->save();
}
protected function setUp() {
parent::setUp();
$this->installEntitySchema('paragraph');
$this->installSchema('node', ['node_access']);
$this->installConfig(['custom_test']);
}
protected function createTalk(?string $eventDateToFormat = NULL): EntityInterface {
if ($eventDateToFormat) {
$event = $this->createEvent($eventDateToFormat);
}
$talk = Node::create([
'title' => 'TDD - Test Driven Drupal',
'type' => 'talk',
]);
if (isset($event)) {
$talk->set('field_events', [$event]);
}
return tap($talk)->save();
}
}

View file

@ -5,33 +5,9 @@ declare(strict_types=1);
namespace Drupal\Tests\custom\Kernel; namespace Drupal\Tests\custom\Kernel;
use Carbon\Carbon; use Carbon\Carbon;
use Drupal\Core\Entity\EntityInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
final class UpdatesTalkCreatedDateTest extends EntityKernelTestBase { final class UpdatesTalkCreatedDateTest extends TalksTestBase {
/**
* {@inheritDoc}
*/
public static $modules = [
// Core.
'node',
'file',
'datetime',
// Contrib.
'entity_reference_revisions',
'paragraphs',
'hook_event_dispatcher',
// Custom.
'custom',
'custom_test',
];
public function testCreatingNode() { public function testCreatingNode() {
$eventDate = Carbon::today()->addWeek(); $eventDate = Carbon::today()->addWeek();
@ -59,43 +35,4 @@ final class UpdatesTalkCreatedDateTest extends EntityKernelTestBase {
->getString()); ->getString());
} }
protected function setUp() {
parent::setUp();
$this->installEntitySchema('paragraph');
$this->installSchema('node', ['node_access']);
$this->installConfig(['custom_test']);
}
private function createTalk(?string $eventDateToFormat = NULL): EntityInterface {
if ($eventDateToFormat) {
$event = $this->createEvent($eventDateToFormat);
}
$talk = Node::create([
'title' => 'TDD - Test Driven Drupal',
'type' => 'talk',
]);
if (isset($event)) {
$talk->set('field_events', [$event]);
}
$talk->save();
return $talk;
}
private function createEvent(string $eventDateToFormat): ParagraphInterface {
$event = Paragraph::create([
'field_date' => $eventDateToFormat,
'type' => 'event',
]);
$event->save();
return $event;
}
} }