Skip to content

Commit 98ab066

Browse files
authored
Merge pull request #35 from itk-event-database/hotfix/DOKK-1170-handle-partnerOrganizers
DOKK-1170: Handled partnerOrganizers
2 parents 62dc159 + 11cd054 commit 98ab066

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

features/events.organizer.feature

+82
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,87 @@ Feature: Events
190190
And the JSON node "organizer.name" should be equal to "Damage, Inc."
191191
And the JSON node "organizer.@id" should be equal to "/api/organizers/4"
192192

193+
Scenario: Create an event with existing partner organizers
194+
When I authenticate as "api-write"
195+
And I add "Content-Type" header equal to "application/ld+json"
196+
And I add "Accept" header equal to "application/ld+json"
197+
And I send a "POST" request to "/api/events" with body:
198+
"""
199+
{
200+
"name": "The first event (again)",
201+
"organizer": {
202+
"name": "The organizer",
203+
"email": "[email protected]",
204+
"url": "https://example.com/organizer"
205+
},
206+
"partnerOrganizers": [
207+
{
208+
"name": "The First Partner",
209+
"email": "[email protected]",
210+
"url": "https://partner-1.com/organizer"
211+
},
212+
{
213+
"name": "The Second Partner",
214+
"email": "[email protected]",
215+
"url": "https://partner-2.com/organizer"
216+
}
217+
],
218+
"occurrences": [ {
219+
"startDate": "2000-01-01T00:00:00+00:00",
220+
"endDate": "2001-01-01T00:00:00+00:00"
221+
} ]
222+
}
223+
"""
224+
Then the response status code should be 201
225+
And the response should be in JSON
226+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
227+
And the JSON node "organizer.name" should be equal to "The organizer"
228+
And the JSON node "organizer.@id" should be equal to "/api/organizers/1"
229+
And the JSON node "partnerOrganizers" should have 2 elements
230+
And the JSON node "partnerOrganizers[0].name" should be equal to "The First Partner"
231+
And the JSON node "partnerOrganizers[0].@id" should be equal to "/api/organizers/2"
232+
And the JSON node "partnerOrganizers[1].name" should be equal to "The Second Partner"
233+
And the JSON node "partnerOrganizers[1].@id" should be equal to "/api/organizers/3"
234+
235+
Scenario: Create an event with existing partner organizers
236+
When I authenticate as "api-write"
237+
And I add "Content-Type" header equal to "application/ld+json"
238+
And I add "Accept" header equal to "application/ld+json"
239+
And I send a "POST" request to "/api/events" with body:
240+
"""
241+
{
242+
"name": "The first event (again)",
243+
"organizer": {
244+
"name": "The organizer",
245+
"email": "[email protected]",
246+
"url": "https://example.com/organizer"
247+
},
248+
"partnerOrganizers": [
249+
{
250+
"@id": "/api/organizers/2"
251+
},
252+
{
253+
"name": "The Second Partner",
254+
"email": "[email protected]",
255+
"url": "https://partner-2.com/organizer"
256+
}
257+
],
258+
"occurrences": [ {
259+
"startDate": "2000-01-01T00:00:00+00:00",
260+
"endDate": "2001-01-01T00:00:00+00:00"
261+
} ]
262+
}
263+
"""
264+
Then the response status code should be 201
265+
And the response should be in JSON
266+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
267+
And the JSON node "organizer.name" should be equal to "The organizer"
268+
And the JSON node "organizer.@id" should be equal to "/api/organizers/1"
269+
And the JSON node "partnerOrganizers" should have 2 elements
270+
And the JSON node "partnerOrganizers[0].name" should be equal to "The First Partner"
271+
And the JSON node "partnerOrganizers[0].@id" should be equal to "/api/organizers/2"
272+
And the JSON node "partnerOrganizers[1].name" should be equal to "The Second Partner"
273+
And the JSON node "partnerOrganizers[1].@id" should be equal to "/api/organizers/3"
274+
193275
@dropSchema
194276
Scenario: Drop schema

src/AppBundle/Serializer/CustomItemNormalizer.php

+22
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,28 @@ protected function setAttributeValue($object, $attribute, $value, $format = null
162162
}
163163
}
164164
}
165+
if ($object instanceof Event && 'partnerOrganizers' === $attribute) {
166+
if (is_array($value)) {
167+
// Try to map all items without an `@id` key to an organizer
168+
// provided by the organizer factory.
169+
$value = array_map(
170+
function ($data) {
171+
if (is_array($data) && empty($data['@id'])) {
172+
// Get unidentified organizer (with no specified id) from factory.
173+
$organizer = $this->organizerFactory->get($data);
174+
if ($organizer) {
175+
return sprintf('/api/organizers/%s', $organizer->getId());
176+
}
177+
}
178+
179+
// We have an `@id` or the factory cannot provide an organizer.
180+
return $data;
181+
},
182+
$value
183+
);
184+
}
185+
}
186+
165187
parent::setAttributeValue($object, $attribute, $value, $format, $context);
166188
}
167189

0 commit comments

Comments
 (0)