Skip to content

Commit be1c047

Browse files
committed
SDK-766: Ensure empty strings are allowed
1 parent 1302f78 commit be1c047

File tree

2 files changed

+80
-11
lines changed

2 files changed

+80
-11
lines changed

src/Yoti/Util/Profile/AttributeConverter.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ class AttributeConverter
2929
*/
3030
private static function convertValueBasedOnAttributeName($value, $attrName)
3131
{
32-
self::validateInput($value);
33-
3432
switch ($attrName) {
3533
case Profile::ATTR_DOCUMENT_DETAILS:
3634
return new DocumentDetails($value);
@@ -58,7 +56,7 @@ private static function convertValueBasedOnAttributeName($value, $attrName)
5856
*/
5957
private static function convertValueBasedOnContentType($value, $contentType)
6058
{
61-
self::validateInput($value);
59+
self::validateInput($value, $contentType);
6260

6361
switch ($contentType) {
6462
case self::CONTENT_TYPE_JPEG:
@@ -201,12 +199,13 @@ public static function convertTimestampToDate($value)
201199

202200
/**
203201
* @param string $value
202+
* @param int $value
204203
*
205204
* @throws AttributeException
206205
*/
207-
private static function validateInput($value)
206+
private static function validateInput($value, $contentType)
208207
{
209-
if (empty($value)) {
208+
if (empty($value) && ($contentType !== self::CONTENT_TYPE_STRING)) {
210209
throw new AttributeException("Warning: Value is NULL");
211210
}
212211
}

tests/Util/Profile/AttributeConverterTest.php

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Yoti\ActivityDetails;
99
use Yoti\Util\Profile\AttributeConverter;
1010
use Yoti\Entity\MultiValue;
11+
use Yoti\Entity\Attribute;
1112

1213
/**
1314
* @coversDefaultClass \Yoti\Util\Profile\AttributeConverter
@@ -19,7 +20,9 @@ class AttributeConverterTest extends TestCase
1920
*/
2021
const CONTENT_TYPE_STRING = 1;
2122
const CONTENT_TYPE_JPEG = 2;
23+
const CONTENT_TYPE_DATE = 3;
2224
const CONTENT_TYPE_PNG = 4;
25+
const CONTENT_TYPE_BYTES = 5;
2326
const CONTENT_TYPE_MULTI_VALUE = 6;
2427

2528
/**
@@ -84,10 +87,30 @@ public function testConvertToYotiAttribute()
8487
/**
8588
* @covers ::convertToYotiAttribute
8689
*/
87-
public function testConvertToYotiAttributeNullValue()
90+
public function testConvertToYotiAttributeEmptyStringValue()
8891
{
89-
$attr = AttributeConverter::convertToYotiAttribute($this->getMockForProtobufAttribute('test_attr', ''));
90-
$this->assertNull($attr);
92+
$attr = AttributeConverter::convertToYotiAttribute($this->getMockForProtobufAttribute(
93+
'test_attr',
94+
'',
95+
self::CONTENT_TYPE_STRING
96+
));
97+
$this->assertEquals('test_attr', $attr->getName());
98+
$this->assertEquals('', $attr->getValue());
99+
}
100+
101+
/**
102+
* @covers ::convertToYotiAttribute
103+
*/
104+
public function testConvertToYotiAttributeEmptyNonStringValue()
105+
{
106+
foreach ($this->getNonStringContentTypes() as $contentType) {
107+
$attr = AttributeConverter::convertToYotiAttribute($this->getMockForProtobufAttribute(
108+
'test_attr',
109+
'',
110+
$contentType
111+
));
112+
$this->assertNull($attr);
113+
}
91114
}
92115

93116
/**
@@ -215,11 +238,41 @@ public function testConvertToYotiAttributeMultiValue()
215238
}
216239

217240
/**
218-
* Check that empty MultiValue Values result in no attribute being returned.
241+
* Check that empty non-string MultiValue Values result in no attribute being returned.
242+
*
243+
* @covers ::convertToYotiAttribute
244+
*/
245+
public function testEmptyNonStringAttributeMultiValueValue()
246+
{
247+
foreach ($this->getNonStringContentTypes() as $contentType) {
248+
// Get MultiValue values.
249+
$values = $this->createMultiValueValues();
250+
251+
// Add an empty MultiValue.
252+
$values[] = $this->createMultiValueValue('', $contentType);
253+
254+
// Create top-level MultiValue.
255+
$protoMultiValue = new \Attrpubapi\MultiValue();
256+
$protoMultiValue->setValues($values);
257+
258+
// Create mock Attribute that will return MultiValue as the value.
259+
$protobufAttribute = $this->getMockForProtobufAttribute(
260+
'test_attr',
261+
$protoMultiValue->serializeToString(),
262+
self::CONTENT_TYPE_MULTI_VALUE
263+
);
264+
265+
$attr = AttributeConverter::convertToYotiAttribute($protobufAttribute);
266+
$this->assertNull($attr);
267+
}
268+
}
269+
270+
/**
271+
* Check that empty string MultiValue Values are allowed.
219272
*
220273
* @covers ::convertToYotiAttribute
221274
*/
222-
public function testEmptyAttributeMultiValueValue()
275+
public function testEmptyStringAttributeMultiValueValue()
223276
{
224277
// Get MultiValue values.
225278
$values = $this->createMultiValueValues();
@@ -239,7 +292,8 @@ public function testEmptyAttributeMultiValueValue()
239292
);
240293

241294
$attr = AttributeConverter::convertToYotiAttribute($protobufAttribute);
242-
$this->assertNull($attr);
295+
$this->assertInstanceOf(Attribute::class, $attr);
296+
$this->assertEquals('', $attr->getValue()[3]);
243297
}
244298

245299
/**
@@ -292,4 +346,20 @@ private function createMultiValueValue($data, $contentType)
292346
$protoMultiValueValue->setContentType($contentType);
293347
return $protoMultiValueValue;
294348
}
349+
350+
/**
351+
* List of non-string content types.
352+
*
353+
* @return array
354+
*/
355+
private function getNonStringContentTypes()
356+
{
357+
return [
358+
self::CONTENT_TYPE_JPEG,
359+
self::CONTENT_TYPE_DATE,
360+
self::CONTENT_TYPE_PNG,
361+
self::CONTENT_TYPE_BYTES,
362+
self::CONTENT_TYPE_MULTI_VALUE,
363+
];
364+
}
295365
}

0 commit comments

Comments
 (0)