Extract a base test case for talks tests
This commit is contained in:
parent
5393186141
commit
bcfe01f62c
70
web/modules/custom/custom/tests/src/Kernel/TalksTestBase.php
Normal file
70
web/modules/custom/custom/tests/src/Kernel/TalksTestBase.php
Normal 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -5,33 +5,9 @@ declare(strict_types=1);
|
|||
namespace Drupal\Tests\custom\Kernel;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
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 {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public static $modules = [
|
||||
// Core.
|
||||
'node',
|
||||
'file',
|
||||
'datetime',
|
||||
|
||||
// Contrib.
|
||||
'entity_reference_revisions',
|
||||
'paragraphs',
|
||||
'hook_event_dispatcher',
|
||||
|
||||
// Custom.
|
||||
'custom',
|
||||
'custom_test',
|
||||
];
|
||||
final class UpdatesTalkCreatedDateTest extends TalksTestBase {
|
||||
|
||||
public function testCreatingNode() {
|
||||
$eventDate = Carbon::today()->addWeek();
|
||||
|
@ -59,43 +35,4 @@ final class UpdatesTalkCreatedDateTest extends EntityKernelTestBase {
|
|||
->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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue