Skip to content

Commit

Permalink
Remove incorrect argument to json_encode() and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
david-scarratt-lrn committed May 6, 2024
1 parent cd8c1c9 commit 3b64732
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/Services/PreHashStrings/LegacyPreHashString.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function getPreHashString(
if (in_array($this->service, static::SERVICES_REQUIRING_SIGNED_REQUEST)) {
$requestJson = $request;
if (is_array($request)) {
$requestJson = json_encode($request, true);
$requestJson = json_encode($request);
}
$signatureArray[] = $requestJson;
}
Expand All @@ -162,6 +162,11 @@ public static function supportsService(string $service): bool
return in_array($service, static::SUPPORTED_SERVICES);
}

/**
* Validate and normalise the security and request packets. Currently
* nothing is done with the request packet, but future pre-hash schemes
* may require it.
*/
public function validate(array $security, array $request): array
{
foreach (array_keys($security) as $key) {
Expand Down
42 changes: 41 additions & 1 deletion tests/Services/PreHashStrings/LegacyPreHashStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,46 @@ public function preHashStringProvider()
}
}

return $testCases;
return array_merge($testCases, $this->additionalTests());
}

/**
* Additional test cases that detect bugs found or anticipated.
* @returns array <string $name, array <
* string $service
* array $security
* string $secret
* array $request
* ?string $action
* bool $v1Compat
* string $expected
* >>
*/
public function additionalTests(): array
{
return [
'api-items-json_encode-bug' => $this->jsonEncodeBugParams(),
];
}

/**
* Test case for a bug found when the request contains escapable characters.
* @returns array <
* string $service
* array $security
* string $secret
* array $request
* ?string $action
* bool $v1Compat
* string $expected
* >
*/
public function jsonEncodeBugParams(): array
{
$params = ParamsFixture::getWorkingItemsApiParams(true);

Check notice on line 121 in tests/Services/PreHashStrings/LegacyPreHashStringTest.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/Services/PreHashStrings/LegacyPreHashStringTest.php#L121

Avoid using static access to class '\LearnositySdk\Fixtures\ParamsFixture' in method 'jsonEncodeBugTest'.
$params['request']['name'] = str_replace('-', '<&\'>', $params['request']['name']);
$params['v1Compat'] = false;
$params['expected'] = 'yis0TYCu7U9V4o7M_localhost_20140626-0528_$ANONYMIZED_USER_ID_{"user_id":"$ANONYMIZED_USER_ID","rendering_type":"assess","name":"' . $params['request']['name'] . '","state":"initial","activity_id":"items_assess_demo","session_id":"demo_session_uuid","type":"submit_practice","config":{"configuration":{"responsive_regions":true},"navigation":{"scrolling_indicator":true},"regions":"main","time":{"show_pause":true,"max_time":300},"title":"ItemsAPI Assess Isolation Demo","subtitle":"Testing Subtitle Text"},"items":["Demo3"]}';
return $params;
}
}

0 comments on commit 3b64732

Please sign in to comment.