Skip to content

Commit f425833

Browse files
committed
UHF-12723: Support new options
1 parent 95c4a98 commit f425833

File tree

4 files changed

+115
-62
lines changed

4 files changed

+115
-62
lines changed

src/Controller/HakuvahtiSubscribeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function post(Request $request): JsonResponse {
5454
}
5555

5656
// Use site_id from configuration entity.
57-
$requestData['site_id'] = $config->getSiteId();
57+
$requestData['siteId'] = $config->getSiteId();
5858

5959
// The frontend gets api url from the DrupalSettings service. The
6060
// service sets language prefixed subscribe urls, so the frontend

src/HakuvahtiRequest.php

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@
1111
*/
1212
final readonly class HakuvahtiRequest {
1313

14-
private const MAX_SEARCH_DESCRIPTION_LENGTH = 999;
14+
private const int MAX_SEARCH_DESCRIPTION_LENGTH = 999;
15+
private const array REQUIRED_FIELDS = [
16+
'lang',
17+
'siteId',
18+
'query',
19+
'elasticQuery',
20+
'searchDescription',
21+
];
1522

1623
/**
1724
* The email address.
1825
*/
19-
public string $email;
26+
public ?string $email;
27+
28+
/**
29+
* User phone number.
30+
*/
31+
public ?string $sms;
2032

2133
/**
2234
* Language id.
@@ -29,7 +41,7 @@
2941
public string $siteId;
3042

3143
/**
32-
* The request parameters from the request uli.
44+
* The request parameters from the request uri.
3345
*/
3446
public string $query;
3547

@@ -40,6 +52,13 @@
4052
*/
4153
public string $elasticQuery;
4254

55+
/**
56+
* If true, the elastic query is stored in ATV.
57+
*
58+
* Use this if the query can contain user data.
59+
*/
60+
public bool $elasticQueryAtv;
61+
4362
/**
4463
* The search description.
4564
*
@@ -50,43 +69,43 @@
5069
public string $searchDescription;
5170

5271
public function __construct(array $requestData) {
53-
$requiredFields = ['email', 'lang', 'site_id', 'query', 'elastic_query', 'search_description'];
54-
55-
foreach ($requiredFields as $fieldName) {
72+
foreach (self::REQUIRED_FIELDS as $fieldName) {
5673
if (!isset($requestData[$fieldName])) {
5774
throw new \InvalidArgumentException("Request is missing field: $fieldName");
5875
}
5976
}
6077

61-
if (!filter_var($requestData['email'], FILTER_VALIDATE_EMAIL)) {
78+
$this->lang = $requestData['lang'];
79+
$this->siteId = $requestData['siteId'];
80+
$this->query = $requestData['query'];
81+
$this->elasticQuery = $requestData['elasticQuery'];
82+
$this->elasticQueryAtv = $requestData['elasticQueryAtv'] ?? FALSE;
83+
$this->searchDescription = $requestData['searchDescription'];
84+
$this->email = $requestData['email'] ?? NULL;
85+
$this->sms = $requestData['sms'] ?? NULL;
86+
87+
// User chooses which notification type they get. Either field can be NULL.
88+
if ($this->email && !filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
6289
throw new \InvalidArgumentException("Email must be a valid email address");
6390
}
6491

65-
if (strlen($requestData['search_description']) > self::MAX_SEARCH_DESCRIPTION_LENGTH) {
92+
if (strlen($this->searchDescription) > self::MAX_SEARCH_DESCRIPTION_LENGTH) {
6693
throw new \InvalidArgumentException("Search description is too long.");
6794
}
68-
69-
$this->email = $requestData['email'];
70-
$this->lang = $requestData['lang'];
71-
$this->siteId = $requestData['site_id'];
72-
$this->query = $requestData['query'];
73-
$this->elasticQuery = $requestData['elastic_query'];
74-
$this->searchDescription = $requestData['search_description'];
7595
}
7696

7797
/**
7898
* Return the data to be sent for hakuvahti services subscription endpoint.
79-
*
80-
* @return array
81-
* The data for hakuvahti subscription request.
8299
*/
83100
public function getServiceRequestData(): array {
84101
return [
85102
'email' => $this->email,
103+
'sms' => $this->sms,
86104
'lang' => $this->lang,
87105
'site_id' => $this->siteId,
88106
'query' => $this->query,
89107
'elastic_query' => $this->elasticQuery,
108+
'elastic_query_atv' => $this->elasticQueryAtv,
90109
'search_description' => $this->searchDescription,
91110
];
92111
}

tests/src/Kernel/HakuvahtiSubscribeControllerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public function testHandleConfirmFormSubmission(): void {
8282
$response = $this->makeRequest([
8383
'email' => 'valid@email.fi',
8484
'query' => '?query=123&parameters=4567',
85-
'elastic_query' => 'eyJxdWVyeSI6eyJib29sIjp7ImZpbHRlciI6W3sidGVybSI6eyJlbnRpdHlfdHlwZSI6Im5vZGUifX1dfX19',
86-
'search_description' => 'This, is the query filters string, separated, by comma',
85+
'elasticQuery' => 'eyJxdWVyeSI6eyJib29sIjp7ImZpbHRlciI6W3sidGVybSI6eyJlbnRpdHlfdHlwZSI6Im5vZGUifX1dfX19',
86+
'searchDescription' => 'This, is the query filters string, separated, by comma',
8787
]);
8888
$this->assertEquals(500, $response->getStatusCode());
8989

@@ -95,17 +95,17 @@ public function testHandleConfirmFormSubmission(): void {
9595
$response = $this->makeRequest([
9696
'email' => 'valid@email.fi',
9797
'query' => '?query=123&parameters=4567',
98-
'elastic_query' => 'eyJxdWVyeSI6eyJib29sIjp7ImZpbHRlciI6W3sidGVybSI6eyJlbnRpdHlfdHlwZSI6Im5vZGUifX1dfX19',
99-
'search_description' => 'This, is the query filters string, separated, by comma',
98+
'elasticQuery' => 'eyJxdWVyeSI6eyJib29sIjp7ImZpbHRlciI6W3sidGVybSI6eyJlbnRpdHlfdHlwZSI6Im5vZGUifX1dfX19',
99+
'searchDescription' => 'This, is the query filters string, separated, by comma',
100100
]);
101101
$this->assertEquals(500, $response->getStatusCode());
102102

103103
// Success.
104104
$response = $this->makeRequest([
105105
'email' => 'valid@email.fi',
106106
'query' => '?query=123&parameters=4567',
107-
'elastic_query' => 'eyJxdWVyeSI6eyJib29sIjp7ImZpbHRlciI6W3sidGVybSI6eyJlbnRpdHlfdHlwZSI6Im5vZGUifX1dfX19',
108-
'search_description' => 'This, is the query filters string, separated, by comma',
107+
'elasticQuery' => 'eyJxdWVyeSI6eyJib29sIjp7ImZpbHRlciI6W3sidGVybSI6eyJlbnRpdHlfdHlwZSI6Im5vZGUifX1dfX19',
108+
'searchDescription' => 'This, is the query filters string, separated, by comma',
109109
]);
110110
$this->assertEquals(200, $response->getStatusCode());
111111
}

tests/src/Unit/HakuvahtiRequestClassTest.php

Lines changed: 71 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,94 @@
66

77
use Drupal\helfi_hakuvahti\HakuvahtiRequest;
88
use Drupal\Tests\UnitTestCase;
9+
use PHPUnit\Framework\Attributes\DataProvider;
910

1011
/**
1112
* Tests rekry specific hakuvahti features.
12-
*
13-
* @group helfi_rekry_content
1413
*/
14+
#[Group('helfi_hakuvahti')]
1515
class HakuvahtiRequestClassTest extends UnitTestCase {
1616

17-
/**
18-
* Test that an exception is thrown if request has missing parameters.
19-
*/
20-
public function testMissingRequestParameter(): void {
21-
$this->expectException(\InvalidArgumentException::class);
22-
new HakuvahtiRequest([]);
23-
}
24-
2517
/**
2618
* Test the request class.
2719
*/
28-
public function testRequestClass(): void {
29-
$requiredFields = $this->getRequiredData();
30-
31-
try {
32-
$requiredFields['email'] = 'invalid@email';
33-
new HakuvahtiRequest($requiredFields);
34-
}
35-
catch (\InvalidArgumentException $e) {
36-
$this->assertIsObject($e, 'Validated email address format');
20+
#[DataProvider('data')]
21+
public function testRequestClass(array $request, ?array $expected = NULL): void {
22+
if (!$expected) {
23+
$this->expectException(\InvalidArgumentException::class);
3724
}
3825

39-
$requiredFields = $this->getRequiredData();
40-
$request = new HakuvahtiRequest($requiredFields);
26+
$hakuvahtiRequest = new HakuvahtiRequest($request);
4127

42-
$serviceRequestData = $request->getServiceRequestData();
43-
$this->assertEquals($serviceRequestData, $this->getRequiredData());
44-
$this->assertEquals($serviceRequestData['elastic_query'], $request->elasticQuery);
45-
$this->assertEquals($serviceRequestData['search_description'], $request->searchDescription);
46-
$this->assertEquals($serviceRequestData['query'], $request->query);
28+
if ($expected) {
29+
$serviceRequestData = $hakuvahtiRequest->getServiceRequestData();
30+
$this->assertEquals($serviceRequestData, $expected);
31+
}
4732
}
4833

4934
/**
50-
* Get the initial request data.
51-
*
52-
* @return array
53-
* The hakuvahti initial request data.
35+
* Get tests data.
5436
*/
55-
private function getRequiredData(): array {
37+
public static function data(): array {
5638
return [
57-
'email' => 'valid@email.fi',
58-
'lang' => 'fi',
59-
'site_id' => 'rekry',
60-
'query' => '?query=123&parameters=4567',
61-
'elastic_query' => 'this-is_the_base64_encoded_elasticsearch_query',
62-
'search_description' => 'This, is the query filters string, separated, by comma',
39+
// Email only request.
40+
[
41+
'request' => [
42+
'email' => 'valid@email.fi',
43+
'lang' => 'fi',
44+
'siteId' => 'rekry',
45+
'query' => '?query=123&parameters=4567',
46+
'elasticQuery' => 'this-is_the_base64_encoded_elasticsearch_query',
47+
'searchDescription' => 'This, is the query filters string, separated, by comma',
48+
],
49+
'expected' => [
50+
'email' => 'valid@email.fi',
51+
'sms' => NULL,
52+
'lang' => 'fi',
53+
'site_id' => 'rekry',
54+
'query' => '?query=123&parameters=4567',
55+
'elastic_query' => 'this-is_the_base64_encoded_elasticsearch_query',
56+
'elastic_query_atv' => FALSE,
57+
'search_description' => 'This, is the query filters string, separated, by comma',
58+
],
59+
],
60+
// Phone-number-only request.
61+
[
62+
'request' => [
63+
'sms' => '044 123 4567',
64+
'lang' => 'fi',
65+
'siteId' => 'rekry',
66+
'query' => '?query=123&parameters=4567',
67+
'elasticQuery' => 'this-is_the_base64_encoded_elasticsearch_query',
68+
'elasticQueryAtv' => TRUE,
69+
'searchDescription' => 'This, is the query filters string, separated, by comma',
70+
],
71+
'expected' => [
72+
'email' => NULL,
73+
'sms' => '044 123 4567',
74+
'lang' => 'fi',
75+
'site_id' => 'rekry',
76+
'query' => '?query=123&parameters=4567',
77+
'elastic_query' => 'this-is_the_base64_encoded_elasticsearch_query',
78+
'elastic_query_atv' => TRUE,
79+
'search_description' => 'This, is the query filters string, separated, by comma',
80+
],
81+
],
82+
// Test that an exception is thrown if the request has missing parameters.
83+
[
84+
'request' => [],
85+
],
86+
// Test that an exception is thrown if parameters fail validation.
87+
[
88+
'request' => [
89+
'email' => 'invalid@email',
90+
'lang' => 'fi',
91+
'siteId' => 'rekry',
92+
'query' => '?query=123&parameters=4567',
93+
'elasticQuery' => 'this-is_the_base64_encoded_elasticsearch_query',
94+
'searchDescription' => 'This, is the query filters string, separated, by comma',
95+
],
96+
],
6397
];
6498
}
6599

0 commit comments

Comments
 (0)