View source
<?php
namespace Drupal\Tests\feeds\Unit\Feeds\Target;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\feeds\Feeds\Target\DateTime;
class DateTimeTest extends FieldTargetWithContainerTestBase {
protected $feedType;
protected $targetDefinition;
public function setUp() {
parent::setUp();
$this->feedType = $this
->createMock('Drupal\\feeds\\FeedTypeInterface');
$method = $this
->getMethod('Drupal\\feeds\\Feeds\\Target\\DateTime', 'prepareTarget')
->getClosure();
$this->targetDefinition = $method($this
->getMockFieldDefinition([
'datetime_type' => 'time',
]));
}
protected function getTargetClass() {
return DateTime::class;
}
public function testPrepareValue() {
$method = $this
->getMethod('Drupal\\feeds\\Feeds\\Target\\DateTime', 'prepareTarget')
->getClosure();
$this->targetDefinition = $method($this
->getMockFieldDefinition([
'datetime_type' => 'date',
]));
$configuration = [
'feed_type' => $this->feedType,
'target_definition' => $this->targetDefinition,
];
$target = new DateTime($configuration, 'datetime', []);
$method = $this
->getProtectedClosure($target, 'prepareValue');
$values = [
'value' => 1411606273,
];
$method(0, $values);
$this
->assertSame(date(DateTimeItemInterface::DATE_STORAGE_FORMAT, 1411606273), $values['value']);
}
public function testWithErrors() {
$configuration = [
'feed_type' => $this->feedType,
'target_definition' => $this->targetDefinition,
];
$target = new DateTime($configuration, 'datetime', []);
$method = $this
->getProtectedClosure($target, 'prepareValue');
$values = [
'value' => '2000-05-32',
];
$method(0, $values);
$this
->assertSame('', $values['value']);
}
public function testYearValue() {
$configuration = [
'feed_type' => $this->feedType,
'target_definition' => $this->targetDefinition,
];
$target = new DateTime($configuration, 'datetime', []);
$method = $this
->getProtectedClosure($target, 'prepareValue');
$values = [
'value' => '2000',
];
$method(0, $values);
$this
->assertSame('2000-01-01T00:00:00', $values['value']);
}
public function testGetTimezoneConfiguration() {
$container = new ContainerBuilder();
$config = [
'system.date' => [
'timezone.default' => 'UTC',
],
];
$container
->set('config.factory', $this
->getConfigFactoryStub($config));
\Drupal::setContainer($container);
$method = $this
->getMethod('Drupal\\feeds\\Feeds\\Target\\DateTime', 'prepareTarget')
->getClosure();
$this->targetDefinition = $method($this
->getMockFieldDefinition([
'datetime_type' => 'date',
]));
$configuration = [
'feed_type' => $this->feedType,
'target_definition' => $this->targetDefinition,
'timezone' => 'Europe/Helsinki',
];
$target = new DateTime($configuration, 'datetime', []);
$method = $this
->getProtectedClosure($target, 'getTimezoneConfiguration');
$this
->assertSame('Europe/Helsinki', $method());
$configuration = [
'feed_type' => $this->feedType,
'target_definition' => $this->targetDefinition,
'timezone' => '__SITE__',
];
$target = new DateTime($configuration, 'datetime', []);
$method = $this
->getProtectedClosure($target, 'getTimezoneConfiguration');
$this
->assertSame('UTC', $method());
}
}