-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathCivicrmStorageGetTest.php
148 lines (137 loc) · 4.64 KB
/
CivicrmStorageGetTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Drupal\Tests\civicrm_entity\Kernel;
use Drupal\civicrm_entity\CiviCrmApiInterface;
use Drupal\civicrm_entity\Entity\CivicrmEntity;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
/**
* Tests the storage.
*
* @group civicrm_entity
*/
class CivicrmStorageGetTest extends CivicrmEntityTestBase {
/**
* Tests getting a single entity.
*/
public function testGet() {
$result = $this->container->get('civicrm_entity.api')->get('event', [
'id' => [
'IN' => [1],
],
'return' => array_keys($this->sampleEventsGetFields()),
'options' => ['limit' => 0],
]);
$this->assertEquals('Fall Fundraiser Dinner', $result[0]['title']);
$result = $this->container->get('civicrm_entity.api')->get('contact', [
'id' => [
'IN' => [10],
],
'return' => array_keys($this->sampleContactGetFields()),
'options' => ['limit' => 0],
]);
$this->assertEquals('Emma Neal', $result[0]['display_name']);
}
/**
* Tests loading an entity through storage.
*/
public function testLoadEvent() {
$storage = $this->container->get('entity_type.manager')
->getStorage('civicrm_event');
/** @var \Drupal\civicrm_entity\Entity\CivicrmEntity $entity */
$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((bool) $entity->get('is_public')->first()->value);
}
/**
* Tests loading a contact.
*/
public function testLoadContact() {
$storage = $this->container->get('entity_type.manager')->getStorage('civicrm_contact');
/** @var \Drupal\civicrm_entity\Entity\CivicrmEntity $entity */
$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/28', $entity->get('birth_date')->date->format('Y/m/d'));
}
/**
* Tests datetime fields and timezone conversions.
*
* CiviCRM stores times in the user's timezone. However Drupal assumes all
* times are in UTC. When loading a date time, CiviEntityStorage converts
* the time into UTC so that Drupal handles the timezone correctly.
*
* @param array $original_datetimes
* Original datetimes.
* @param array $expected_utc_datetime
* Expected datetimes.
* @param string $timezone
* The timezone.
*
* @dataProvider datetimeTimezoneDataProvider
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
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');
/** @var \Drupal\civicrm_entity\Entity\CivicrmEntity $entity */
$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)
);
}
}
/**
* Provides datetime and timezone test data.
*/
public static function datetimeTimezoneDataProvider() {
yield [
[
'start_date' => '2018-05-02 17:00:00',
'end_date' => '2018-05-04 17:00:00',
],
// America/Chicago is UTC-5.
[
'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',
],
// Europe/Berlin if UTC-2.
[
'start_date' => '2018-05-02T15:00:00',
'end_date' => '2018-05-04T15:00:00',
],
'Europe/Berlin',
];
}
}