Skip to content

Commit fb2d438

Browse files
Grimur Vid NeystGrimur Vid Neyst
authored andcommitted
Fixed test and a reference error from post model
1 parent f42c9bc commit fb2d438

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

js/src/@types/shims.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ declare module 'flarum/common/models/User' {
3939

4040
declare module 'flarum/common/models/Post' {
4141
export default interface Post {
42-
canSelectBestAnswer(): boolean;
42+
canSelectAsBestAnswer(): boolean;
4343
}
4444
}

js/src/forum/addBestAnswerAction.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import extractText from 'flarum/common/utils/extractText';
1010

1111
export default function addBestAnswerAction() {
1212
const ineligible = (discussion: Discussion, post: Post) => {
13-
return post.isHidden() || post.number() === 1 || !post.canSelectBestAnswer() || !app.session.user;
13+
return post.isHidden() || post.number() === 1 || !post.canSelectAsBestAnswer() || !app.session.user;
1414
};
1515

1616
const isThisBestAnswer = (discussion: Discussion, post: Post): boolean => {

js/src/forum/extend.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ export default [
1818
.attribute<number>('bestAnswerCount'),
1919

2020
new Extend.Model(Post) //
21-
.attribute<boolean>('canSelectBestAnswer'),
21+
.attribute<boolean>('canSelectAsBestAnswer'),
2222
];

src/Api/PostAttributes.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717

1818
class PostAttributes
1919
{
20+
protected $bestAnswerRepository;
21+
2022
public function __construct(
21-
protected BestAnswerRepository $bestAnswerRepository,
23+
BestAnswerRepository $bestAnswerRepository,
2224
) {
25+
$this->bestAnswerRepository = $bestAnswerRepository;
2326
}
2427

2528
public function __invoke(PostSerializer $serializer, Post $post, array $attributes): array
2629
{
27-
$attributes['canSelectBestAnswer'] = $this->bestAnswerRepository->canSelectPostAsBestAnswer($serializer->getActor(), $post);
30+
$attributes['canSelectAsBestAnswer'] = $this->bestAnswerRepository->canSelectPostAsBestAnswer($serializer->getActor(), $post);
2831

2932
return $attributes;
3033
}

tests/integration/api/SetBestAnswerTest.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,16 @@ public function notAllowedUsersProvider(): array
8282
private function getCanSelectBestAnswer(array $included, int $userId): bool
8383
{
8484
foreach ($included as $item) {
85-
if (($item['type'] ?? null) === 'posts' && isset($item['attributes']['canSelectBestAnswer']) && $item['relationships']['user']['data']['id'] == $userId) {
86-
return $item['attributes']['canSelectBestAnswer'];
85+
if (isset($item['attributes']['canSelectAsBestAnswer']) && $item['relationships']['user']['data']['id'] == $userId) {
86+
$currentPostId = $item['attributes']['number'];
87+
88+
if ($currentPostId === 1) {
89+
continue;
90+
}
91+
92+
if ($item['attributes']['canSelectAsBestAnswer'] == true) {
93+
return true;
94+
}
8795
}
8896
}
8997

@@ -98,6 +106,7 @@ public function getDiscussion(int $userId): ResponseInterface
98106
'/api/discussions/1',
99107
[
100108
'authenticatedAs' => $userId,
109+
'queryParam' => ['include' => 'posts'],
101110
]
102111
)
103112
);

tests/integration/api/UnsetBestAnswerTest.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function getBestAnswerDiscussion(int $userId = 2): ResponseInterface
6565
'/api/discussions/1',
6666
[
6767
'authenticatedAs' => $userId,
68+
'queryParam' => ['include' => 'posts'],
6869
]
6970
)
7071
);
@@ -85,6 +86,7 @@ public function user_can_unset_best_answer_in_own_discussion_and_select_a_differ
8586
$attributes = $data['data']['attributes'];
8687
$this->assertEquals(2, $attributes['hasBestAnswer'], 'Expected best answer post ID to be 2');
8788

89+
$postId = 0;
8890
// Unset best answer
8991
$response = $this->send(
9092
$this->request(
@@ -94,7 +96,7 @@ public function user_can_unset_best_answer_in_own_discussion_and_select_a_differ
9496
'json' => [
9597
'data' => [
9698
'attributes' => [
97-
'bestAnswerPostId' => 0,
99+
'bestAnswerPostId' => $postId,
98100
],
99101
],
100102

@@ -120,7 +122,7 @@ public function user_can_unset_best_answer_in_own_discussion_and_select_a_differ
120122

121123
$attributes = $data['data']['attributes'];
122124
$this->assertFalse($attributes['hasBestAnswer']);
123-
$this->assertTrue($this->getCanSelectBestAnswer($data['included'], 2), 'Expected user to be able to set a best answer');
125+
$this->assertTrue($this->getCanSelectBestAnswer($data['included'], $postId), 'Expected user to be able to set a best answer');
124126

125127
// Set a different post as best answer
126128
$response = $this->send(
@@ -149,12 +151,19 @@ public function user_can_unset_best_answer_in_own_discussion_and_select_a_differ
149151
$this->assertEquals(3, $attributes['hasBestAnswer'], 'Expected best answer post ID to be 3');
150152
}
151153

152-
private function getCanSelectBestAnswer(array $included, int $userId): bool
154+
private function getCanSelectBestAnswer(array $included, int $postId): bool
153155
{
154156
foreach ($included as $item) {
155-
if (($item['type'] ?? null) === 'posts' && isset($item['attributes']['canSelectBestAnswer'])
156-
&& $item['relationships']['user']['data']['id'] == $userId) {
157-
return $item['attributes']['canSelectBestAnswer'];
157+
if (!isset($item['attributes']['canSelectAsBestAnswer'])) {
158+
continue;
159+
}
160+
$currentPostId = $item['attributes']['number'];
161+
if ($currentPostId === 1 || $currentPostId !== $postId) {
162+
continue;
163+
}
164+
165+
if ($item['attributes']['canSelectAsBestAnswer'] == true) {
166+
return true;
158167
}
159168
}
160169

0 commit comments

Comments
 (0)