View source
<?php
namespace Drupal\Tests\feeds\Kernel\Feeds\Target;
use Drupal\node\Entity\Node;
use Drupal\Tests\feeds\Kernel\FeedsKernelTestBase;
class TimestampTest extends FeedsKernelTestBase {
protected $feedType;
public function setUp() {
parent::setUp();
$this->feedType = $this
->createFeedTypeForCsv([
'guid' => 'guid',
'title' => 'title',
'created' => 'created',
'datetime_start' => 'datetime_start',
'year' => 'year',
]);
}
public function testImportTimestamp() {
$this->feedType
->addMapping([
'target' => 'created',
'map' => [
'value' => 'created',
],
]);
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/content_date.csv',
]);
$feed
->import();
$this
->assertNodeCount(3);
$expected = [
1 => 1251936720,
2 => 1251932360,
3 => 1190835120,
];
foreach ($expected as $nid => $value) {
$node = Node::load($nid);
$this
->assertEquals($value, $node->created->value);
}
}
public function testWithConfig(array $expected, $source, array $settings = []) {
$this->feedType
->addMapping([
'target' => 'created',
'map' => [
'value' => $source,
],
'settings' => $settings,
]);
$this->feedType
->save();
$feed = $this
->createFeed($this->feedType
->id(), [
'source' => $this
->resourcesPath() . '/csv/content_date.csv',
]);
$feed
->import();
$this
->assertNodeCount(3);
foreach ($expected as $nid => $value) {
$node = Node::load($nid);
$this
->assertEquals($value, $node->created->value);
}
$messages = \Drupal::messenger()
->messagesByType('warning');
$this
->assertCount(1, $messages);
$this
->assertStringContainsString('The content <em class="placeholder">Eodem modo typi</em> failed to validate', (string) $messages[0]);
$this
->assertStringContainsString('created.0.value: This value should be of the correct primitive type.', (string) $messages[0]);
}
public function withConfigProvider() {
$return = [];
$return['ignore-timezone'] = [
'expected' => [
1 => 1251936720,
2 => 1251932360,
3 => 1190835120,
],
'source' => 'created',
'settings' => [
'timezone' => 'Europe/Amsterdam',
],
];
$return['year'] = [
'expected' => [
1 => -473385600,
2 => 1420070400,
3 => 1514764800,
],
'source' => 'year',
'settings' => [
'timezone' => 'UTC',
],
];
$return['year-with-timezone'] = [
'expected' => [
1 => -473356800,
2 => 1420099200,
3 => 1514793600,
],
'source' => 'year',
'settings' => [
'timezone' => 'America/Los_Angeles',
],
];
$return['los-angeles'] = [
'expected' => [
1 => -446702400,
2 => 1445495340,
3 => 1518134400,
],
'source' => 'datetime_start',
'settings' => [
'timezone' => 'America/Los_Angeles',
],
];
$return['amsterdam'] = [
'expected' => [
1 => -446734800,
2 => 1445462940,
3 => 1518134400,
],
'source' => 'datetime_start',
'settings' => [
'timezone' => 'Europe/Amsterdam',
],
];
$return['sydney'] = [
'expected' => [
1 => -446767200,
2 => 1445430540,
3 => 1518134400,
],
'source' => 'datetime_start',
'settings' => [
'timezone' => 'Australia/Sydney',
],
];
return $return;
}
}