/** * @test * @group AttributeReleasePolicy */ public function consent_list_and_attributes_are_correctly_converted_to_a_request_and_the_response_is_mapped_correctly_to_a_result() { $someAttributeDefinition = new AttributeDefinition('someAttribute', 'urn:mace:some-attribute', 'urn:oid:0.0.0.0.0.1'); $anotherAttributeDefinition = new AttributeDefinition('anotherAttribute', null, 'urn:oid:0.0.0.0.0.2'); $attributeDictionary = new AttributeDictionary(); $attributeDictionary->addAttributeDefinition($someAttributeDefinition); $attributeDictionary->addAttributeDefinition($anotherAttributeDefinition); $client = Mockery::mock(JsonApiClient::class); $arpService = new AttributeReleasePolicyService($client, $attributeDictionary); $client->shouldReceive('post')->withArgs([['entityIds' => ['some-entity-id', 'another-entity-id'], 'attributes' => ['urn:mace:some-attribute' => ['some-value'], 'urn:oid:0.0.0.0.0.1' => ['some-value'], 'urn:oid:0.0.0.0.0.2' => ['another-value']]], '/arp'])->andReturn(['some-entity-id' => ['urn:mace:some-attribute' => ['some-value'], 'urn:oid:0.0.0.0.0.1' => ['some-value'], 'urn:oid:0.0.0.0.0.2' => ['another-value']], 'another-entity-id' => ['urn:oid:0.0.0.0.0.2' => ['another-value']]]); $someConsent = new Consent(new ServiceProvider(new Entity(new EntityId('some-entity-id'), EntityType::SP()), new DisplayName(['en' => 'Some display name']), new Url('http://some-eula-url.example'), new ContactEmailAddress('*****@*****.**')), new DateTimeImmutable(), new DateTimeImmutable(), ConsentType::explicit()); $anotherConsent = new Consent(new ServiceProvider(new Entity(new EntityId('another-entity-id'), EntityType::SP()), new DisplayName(['en' => 'Another display name']), new Url('http://another-eula-url.example'), new ContactEmailAddress('*****@*****.**')), new DateTimeImmutable(), new DateTimeImmutable(), ConsentType::explicit()); $consentList = new ConsentList([$someConsent, $anotherConsent]); $someAttribute = new Attribute($someAttributeDefinition, ['some-value']); $anotherAttribute = new Attribute($anotherAttributeDefinition, ['another-value']); $attributeSet = AttributeSet::create([$someAttribute, $anotherAttribute]); $expectedResult = SpecifiedConsentList::createWith([SpecifiedConsent::specifies($someConsent, AttributeSetWithFallbacks::create([$someAttribute, $anotherAttribute])), SpecifiedConsent::specifies($anotherConsent, AttributeSetWithFallbacks::create([$anotherAttribute]))]); $result = $arpService->applyAttributeReleasePolicies($consentList, $attributeSet); $this->assertEquals($expectedResult, $result); }
/** * @param ConsentList $consentList * @param AttributeSetInterface $attributeSet * @return SpecifiedConsentList * @SuppressWarnings(PHPMD.NPathComplexity) Build and mapping logic causes complexity */ public function applyAttributeReleasePolicies(ConsentList $consentList, AttributeSetInterface $attributeSet) { $entityIds = $consentList->map(function (Consent $consent) { return $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId(); }); $mappedAttributes = []; foreach ($attributeSet as $attribute) { $mace = $attribute->getAttributeDefinition()->getUrnMace(); $oid = $attribute->getAttributeDefinition()->getUrnOid(); if ($mace !== null) { $mappedAttributes[$mace] = $attribute->getValue(); } if ($oid !== null) { $mappedAttributes[$oid] = $attribute->getValue(); } } $data = ['entityIds' => $entityIds, 'attributes' => !empty($mappedAttributes) ? $mappedAttributes : new stdClass()]; $response = $this->jsonApiClient->post($data, '/arp'); $specifiedConsents = $consentList->map(function (Consent $consent) use($response) { $entityId = $consent->getServiceProvider()->getEntity()->getEntityId()->getEntityId(); if (!isset($response[$entityId])) { throw new InvalidResponseException(sprintf('EntityID "%s" was not found in the ARP response (entityIDs: %s)', $entityId, join(', ', array_keys($response)))); } $attributes = []; foreach ($response[$entityId] as $attributeName => $attributeValue) { try { $attributeDefinition = $this->attributeDictionary->getAttributeDefinitionByUrn($attributeName); } catch (UnknownUrnException $exception) { $attributeDefinition = new AttributeDefinition($attributeName, $attributeName, $attributeName); } $attribute = new Attribute($attributeDefinition, $attributeValue); if (!in_array($attribute, $attributes)) { $attributes[] = $attribute; } } return SpecifiedConsent::specifies($consent, AttributeSetWithFallbacks::create($attributes)); }); return SpecifiedConsentList::createWith($specifiedConsents); }