Skip to content

Commit 43e71aa

Browse files
committed
UserData/UserApi tests and helpers method
1 parent 6318f87 commit 43e71aa

File tree

5 files changed

+157
-5
lines changed

5 files changed

+157
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22

3-
## 0.0.5 - 2025-02-25
3+
## 0.0.6 - 2025-02-07
4+
5+
- Adding helper methods to UserData
6+
- UserData / UserApi tests
7+
8+
## 0.0.5 - 2025-02-05
49

510
- Re-structuring bulk operations and pagination with `StoryBulkApi` class
611
- Refactoring creating new Api instance from Api classes

src/Data/UserData.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,31 @@ public function orgName(): string
2727
return $this->getString('org.name');
2828
}
2929

30+
public function username(): string
31+
{
32+
return $this->getString('username');
33+
}
34+
35+
public function firstname(): string
36+
{
37+
return $this->getString('firstname');
38+
}
39+
40+
public function lastname(): string
41+
{
42+
return $this->getString('lastname');
43+
}
44+
3045
public function id(): string
3146
{
3247
return $this->getString('id');
3348
}
3449

50+
public function orgRole(): string
51+
{
52+
return $this->getString('org_role');
53+
}
54+
3555
public function userId(): string
3656
{
3757
return $this->getString('userid');
@@ -42,6 +62,14 @@ public function email(): string
4262
return $this->getString('email');
4363
}
4464

65+
public function createdAt(string $format = 'Y-m-d H:i:s'): string|null
66+
{
67+
return $this->getFormattedDateTime(
68+
'created_at',
69+
format: $format,
70+
);
71+
}
72+
4573

4674

4775
public function hasOrganization(): bool
@@ -54,14 +82,28 @@ public function hasPartner(): bool
5482
return $this->getBoolean('has_partner');
5583
}
5684

57-
public function lastSignInAt(): null|string
85+
public function partnerStatus(): string
5886
{
59-
return $this->getFormattedDateTime('last_sign_in_at', "", format: "Y-m-d");
87+
return $this->getString('partner_status');
6088
}
6189

62-
public function createdAt(): null|string
90+
public function timezone(): string
6391
{
64-
return $this->getFormattedDateTime('created_at', "", format: "Y-m-d");
92+
return $this->getString('timezone');
6593
}
6694

95+
public function avatarUrl(?int $size = 72): string
96+
{
97+
$sizeString = "";
98+
if (null !== $size) {
99+
$sizeString = $size . 'x' . $size . "/";
100+
}
101+
102+
return "https://img2.storyblok.com/" . $sizeString . $this->getString('avatar');
103+
}
104+
105+
106+
107+
108+
67109
}

tests/Feature/Data/empty-user.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
["This record could not be found"]

tests/Feature/Data/one-user.json

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"user": {
3+
"userid": "myuserid",
4+
"email": "[email protected]",
5+
"organization": null,
6+
"username": "myusername",
7+
"use_username": true,
8+
"alt_email": null,
9+
"firstname": "John",
10+
"lastname": "Doe",
11+
"phone": null,
12+
"id": 123456,
13+
"login_strategy": "otp_email",
14+
"created_at": "2022-06-01T16:54:34.759Z",
15+
"org_role": "admin",
16+
"has_org": true,
17+
"has_partner": true,
18+
"partner_status": "approved",
19+
"org": {
20+
"name": "Storyblok",
21+
"settings": []
22+
},
23+
"timezone": "Europe\/Rome",
24+
"avatar": "avatars\/118830\/01290bd7fa\/myimage.JPG",
25+
"friendly_name": "John Doe",
26+
"notified": [],
27+
"lang": "en",
28+
"partner_role": "partner_member",
29+
"favourite_spaces": [
30+
123456,
31+
123457
32+
],
33+
"favourite_ideas": [],
34+
"role": null,
35+
"beta_user": true,
36+
"track_statistics": true,
37+
"ui_theme": {
38+
"theme": "default"
39+
},
40+
"is_editor": false,
41+
"sso": false,
42+
"job_role": "developer",
43+
"totp_factor_verified": false,
44+
"configured_2fa_options": [
45+
"otp_email"
46+
],
47+
"disclaimer_ids": [
48+
1,
49+
2
50+
]
51+
}
52+
}

tests/Feature/UserTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
use Storyblok\ManagementApi\Endpoints\UserApi;
7+
use Storyblok\ManagementApi\ManagementApiClient;
8+
use Symfony\Component\HttpClient\MockHttpClient;
9+
10+
test('Testing current user', function (): void {
11+
$responses = [
12+
\mockResponse("one-user", 200),
13+
\mockResponse("empty-user", 404),
14+
//\mockResponse("empty-asset", 404),
15+
];
16+
17+
$client = new MockHttpClient($responses);
18+
$mapiClient = ManagementApiClient::initTest($client);
19+
$userApi = new UserApi($mapiClient);
20+
21+
$storyblokResponse = $userApi->me();
22+
$string = $storyblokResponse->getLastCalledUrl();
23+
expect($string)->toMatch('/.*users.*/');
24+
/* @var \Storyblok\ManagementApi\Data\UserData $userData */
25+
$userData = $storyblokResponse->data();
26+
expect($userData->firstname())->toBe("John");
27+
expect($userData->lastname())->toBe("Doe");
28+
expect($userData->id())->toBe("123456");
29+
expect($userData->orgName())->toBe("Storyblok");
30+
expect($userData->username())->toBe("myusername");
31+
expect($userData->orgRole())->toBe("admin");
32+
expect($userData->userId())->toBe("myuserid");
33+
expect($userData->email())->toBe("[email protected]");
34+
expect($userData->email())->toBe("[email protected]");
35+
expect($userData->createdAt())->toBe("2022-06-01 16:54:34");
36+
expect($userData->createdAt("Y-m-d"))->toBe("2022-06-01");
37+
expect($userData->hasOrganization())->toBe(true);
38+
expect($userData->hasPartner())->toBe(true);
39+
expect($userData->partnerStatus())->toBe("approved");
40+
expect($userData->timezone())->toBe("Europe/Rome");
41+
expect($userData->avatarUrl())->toBe("https://img2.storyblok.com/72x72/avatars/118830/01290bd7fa/myimage.JPG");
42+
expect($userData->avatarUrl(null))->toBe("https://img2.storyblok.com/avatars/118830/01290bd7fa/myimage.JPG");
43+
44+
$userData = \Storyblok\ManagementApi\Data\UserData::make([]);
45+
expect($userData)->toBeInstanceOf(\Storyblok\ManagementApi\Data\UserData::class);
46+
47+
$userApi = $mapiClient->userApi();
48+
expect($userApi)->toBeInstanceOf(UserApi::class);
49+
50+
51+
52+
});

0 commit comments

Comments
 (0)