View source
<?php
namespace Drupal\Tests\civicrm_entity\Kernel;
use Drupal\civicrm_entity\CiviCrmApiInterface;
use Drupal\civicrm_entity\Entity\CivicrmEntity;
use Drupal\Core\TypedData\Type\DateTimeInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
class CivicrmStorageGetTest extends CivicrmEntityTestBase {
public function testGet() {
$result = $this->container
->get('civicrm_entity.api')
->get('event', [
'id' => 1,
'return' => array_keys($this
->sampleEventsGetFields()),
]);
$this
->assertEquals('Fall Fundraiser Dinner', $result[0]['title']);
$result = $this->container
->get('civicrm_entity.api')
->get('contact', [
'id' => 10,
'return' => array_keys($this
->sampleContactGetFields()),
]);
$this
->assertEquals('Emma Neal', $result[0]['display_name']);
}
public function testLoadEvent() {
$storage = $this->container
->get('entity_type.manager')
->getStorage('civicrm_event');
$entity = $storage
->load(1);
$this
->assertInstanceOf(CivicrmEntity::class, $entity);
$this
->assertEquals($entity
->id(), 1);
$this
->assertEquals($entity
->get('title')->value, 'Fall Fundraiser Dinner');
$this
->assertEquals('2018-05-02T07:00:00', $entity
->get('start_date')->value);
$this
->assertEquals('2018/05/02', $entity
->get('start_date')->date
->format('Y/m/d'));
$this
->assertTrue($entity
->get('is_public')->value);
}
public function testLoadContact() {
$storage = $this->container
->get('entity_type.manager')
->getStorage('civicrm_contact');
$entity = $storage
->load(10);
$this
->assertInstanceOf(CivicrmEntity::class, $entity);
$this
->assertEquals($entity
->id(), 10);
$this
->assertEquals($entity
->get('display_name')->value, 'Emma Neal');
$this
->assertEquals('1982/06/27', $entity
->get('birth_date')->date
->format('Y/m/d'));
}
public function testDatetimeTimezone(array $original_datetimes, array $expected_utc_datetime, $timezone) {
date_default_timezone_set($timezone);
$civicrm_api_mock = $this
->prophesize(CiviCrmApiInterface::class);
$civicrm_api_mock
->get('event', [
'id' => 1,
'return' => array_keys($this
->sampleEventsGetFields()),
])
->willReturn([
$original_datetimes,
]);
$storage = $this->container
->get('entity_type.manager')
->getStorage('civicrm_event');
$entity = $storage
->load(1);
foreach ($expected_utc_datetime as $field_name => $field_data) {
$this
->assertEquals($field_data, $entity
->get($field_name)->date
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
}
}
public function datetimeTimezoneDataProvider() {
(yield [
[
'start_date' => '2018-05-02 17:00:00',
'end_date' => '2018-05-04 17:00:00',
],
[
'start_date' => '2018-05-02T22:00:00',
'end_date' => '2018-05-04T22:00:00',
],
'America/Chicago',
]);
(yield [
[
'start_date' => '2018-05-02 17:00:00',
'end_date' => '2018-05-04 17:00:00',
],
[
'start_date' => '2018-05-02T17:00:00',
'end_date' => '2018-05-04T17:00:00',
],
'UTC',
]);
(yield [
[
'start_date' => '2018-05-02 17:00:00',
'end_date' => '2018-05-04 17:00:00',
],
[
'start_date' => '2018-05-02T15:00:00',
'end_date' => '2018-05-04T15:00:00',
],
'Europe/Berlin',
]);
}
}