Skip to content

Commit 432a937

Browse files
imorlandStyleCIBot
andauthored
fix: misc (#120)
* fix: misc * Apply fixes from StyleCI * chore: remove debug code --------- Co-authored-by: StyleCI Bot <bot@styleci.io>
1 parent b1ea7ee commit 432a937

File tree

5 files changed

+78
-37
lines changed

5 files changed

+78
-37
lines changed

extend.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
->type(Notification\AwardedBestAnswerBlueprint::class, ['alert'])
6666
->type(Notification\BestAnswerSetInDiscussionBlueprint::class, []),
6767

68+
(new Extend\ApiResource(Resource\ForumResource::class))
69+
->fields(Api\ForumAttributes::class),
70+
6871
(new Extend\ApiResource(Resource\DiscussionResource::class))
6972
->fields(Api\DiscussionAttributes::class)
7073
->endpoint(Endpoint\Show::class, function (Endpoint\Show $endpoint) {
@@ -89,7 +92,8 @@
8992
->whenExtensionEnabled('flarum-tags', fn () => [
9093
(new Extend\ApiResource(\Flarum\Tags\Api\Resource\TagResource::class))
9194
->fields(fn () => [
92-
Schema\Boolean::make('isQnA'),
95+
Schema\Boolean::make('isQnA')
96+
->property('is_qna'),
9397
Schema\Boolean::make('reminders')
9498
->property('qna_reminders'),
9599
]),
@@ -100,9 +104,6 @@
100104
->default('fof-best-answer.stop_overnight', false)
101105
->default('fof-best-answer.store_log_output', false)
102106
->default('fof-best-answer.enabled-tags', '[]')
103-
->default('fof-best-answer.search.solution_search', true)
104-
->default('fof-best-answer.search.remove_solutions_from_main_search', false)
105-
->default('fof-best-answer.search.display_tags', true)
106107
->default('fof-best-answer.discussion_sidebar_jump_button', false)
107108
->serializeToForum('fof-best-answer.show_max_lines', 'fof-best-answer.show_max_lines', 'intVal'),
108109

js/src/admin/components/BestAnswerSettingsPage.tsx

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,6 @@ export default class BestAnswerSettingsPage extends ExtensionPage {
7676
help: app.translator.trans('fof-best-answer.admin.settings.discussion_sidebar_jump_button_help'),
7777
})}
7878
</div>
79-
<h3>{app.translator.trans('fof-best-answer.admin.settings.label.search')}</h3>
80-
<div className="Section">
81-
{this.buildSettingComponent({
82-
type: 'boolean',
83-
setting: 'fof-best-answer.search.solution_search',
84-
label: app.translator.trans('fof-best-answer.admin.settings.solution_search'),
85-
help: app.translator.trans('fof-best-answer.admin.settings.solution_search_help'),
86-
})}
87-
{this.buildSettingComponent({
88-
type: 'boolean',
89-
setting: 'fof-best-answer.search.remove_solutions_from_main_search',
90-
label: app.translator.trans('fof-best-answer.admin.settings.remove_solutions_from_main_search'),
91-
help: app.translator.trans('fof-best-answer.admin.settings.remove_solutions_from_main_search_help'),
92-
})}
93-
{this.buildSettingComponent({
94-
type: 'boolean',
95-
setting: 'fof-best-answer.search.display_tags',
96-
label: app.translator.trans('fof-best-answer.admin.settings.display_tags'),
97-
help: app.translator.trans('fof-best-answer.admin.settings.display_tags_help'),
98-
})}
99-
</div>
10079
<h3>{app.translator.trans('fof-best-answer.admin.settings.label.reminders')}</h3>
10180
<p className="helpText">
10281
{app.translator.trans('fof-best-answer.admin.settings.label.reminders_notice')}{' '}

resources/locale/en.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ fof-best-answer:
88
tags: Best Answer Tags
99
general: General
1010
reminders: Reminders
11-
search: Search
1211
advanced: Advanced
1312
reminders_notice: For reminders to function, you must have set up the Flarum scheduler correctly.
1413
allow_select_own_post: Select own post
@@ -35,12 +34,6 @@ fof-best-answer:
3534
remind_tags_help: Select which tags, if any, to send reminders for. Reminders will only be sent for discussions with these tags.
3635
tags_info: |
3736
When a tag is Best Answer enabled, assign permissions to each user group that may set answers (own discussion, any discussion). Permissions may be assigned globally (for any Best Answer anabled tag), or on a per tag basis.
38-
solution_search: Solution search
39-
solution_search_help: Enable the ability to search for discussions with Best Answers
40-
remove_solutions_from_main_search: Remove solutions from main search
41-
remove_solutions_from_main_search_help: Remove discussions with Best Answers from the main 'all discussions' search results
42-
display_tags: Display tags in search results
43-
display_tags_help: Display the tag(s) in search results for discussions with Best Answers
4437
discussion_sidebar_jump_button: Display jump to Best Answer button in discussion sidebar
4538
discussion_sidebar_jump_button_help: Adds a button to the discussion sidebar which jumps to the Best Answer in the stream of posts.
4639
edit_tag:
@@ -79,9 +72,6 @@ fof-best-answer:
7972
titlePlaceholder: Question title
8073
user:
8174
best-answer-count: "{count, plural, one { {count} best answer} other {{count} best answers}}"
82-
search:
83-
discussions_solutions_heading: "Discussions with solutions"
84-
all_discussions_solutions_button: "Search all discussions with solutions for \"{query}\""
8575
discussion:
8676
jump_to_best_answer_button: View Best Answer
8777
email:

src/Api/ForumAttributes.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of fof/best-answer.
5+
*
6+
* Copyright (c) FriendsOfFlarum.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FoF\BestAnswer\Api;
13+
14+
use Flarum\Api\Schema;
15+
use Flarum\Settings\SettingsRepositoryInterface;
16+
17+
class ForumAttributes
18+
{
19+
public function __construct(
20+
protected SettingsRepositoryInterface $settings
21+
) {
22+
}
23+
24+
public function __invoke(): array
25+
{
26+
return [
27+
Schema\Boolean::make('canSelectBestAnswerOwnPost')
28+
->get(fn () => (bool) $this->settings->get('fof-best-answer.allow_select_own_post')),
29+
30+
Schema\Boolean::make('bestAnswerDiscussionSidebarJumpButton')
31+
->get(fn () => (bool) $this->settings->get('fof-best-answer.discussion_sidebar_jump_button')),
32+
33+
Schema\Boolean::make('showBestAnswerFilterUi')
34+
->get(fn () => (bool) $this->settings->get('fof-best-answer.show_filter_dropdown')),
35+
36+
Schema\Boolean::make('bestAnswerDiscussionSidebarJumpButton')
37+
->get(fn () => (bool) $this->settings->get('fof-best-answer.discussion_sidebar_jump_button')),
38+
39+
Schema\Boolean::make('useAlternativeBestAnswerUi')
40+
->get(fn () => (bool) $this->settings->get('fof-best-answer.use_alternative_ui')),
41+
];
42+
}
43+
}

tests/integration/api/SetBestAnswerTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,18 @@ public function setUp(): void
4444
],
4545
Discussion::class => [
4646
['id' => 1, 'title' => __CLASS__, 'user_id' => 2, 'created_at' => Carbon::now(), 'comment_count' => 2],
47+
['id' => 2, 'title' => 'Another discussion', 'user_id' => 3, 'created_at' => Carbon::now(), 'comment_count' => 1],
4748
],
4849
Post::class => [
4950
['id' => 1, 'discussion_id' => 1, 'user_id' => 2, 'type' => 'comment', 'content' => 'post 1 - question', 'created_at' => Carbon::now()],
5051
['id' => 2, 'discussion_id' => 1, 'user_id' => 1, 'type' => 'comment', 'content' => 'post 2 - answer1', 'created_at' => Carbon::now()],
5152
['id' => 3, 'discussion_id' => 1, 'user_id' => 3, 'type' => 'comment', 'content' => 'post 2 - answer2', 'created_at' => Carbon::now()],
53+
['id' => 4, 'discussion_id' => 2, 'user_id' => 3, 'type' => 'comment', 'content' => 'question', 'created_at' => Carbon::now()],
54+
['id' => 5, 'discussion_id' => 2, 'user_id' => 3, 'type' => 'comment', 'content' => 'answer', 'created_at' => Carbon::now()],
5255
],
5356
'discussion_tag' => [
5457
['discussion_id' => 1, 'tag_id' => 2],
58+
['discussion_id' => 2, 'tag_id' => 2],
5559
],
5660
'group_permission' => [
5761
['group_id' => 4, 'permission' => 'discussion.selectBestAnswerNotOwnDiscussion', 'created_at' => Carbon::now()],
@@ -91,12 +95,12 @@ public function getDiscussion(int $userId): ResponseInterface
9195
);
9296
}
9397

94-
public function setBestAnswer(int $userId, int $postId): ResponseInterface
98+
public function setBestAnswer(int $userId, int $postId, int $discussionId = 1): ResponseInterface
9599
{
96100
return $this->send(
97101
$this->request(
98102
'PATCH',
99-
'/api/discussions/1',
103+
'/api/discussions/'.$discussionId,
100104
[
101105
'json' => [
102106
'data' => [
@@ -161,4 +165,28 @@ public function user_without_permission_cannot_set_best_answer(int $userId)
161165

162166
$this->assertEquals(403, $response->getStatusCode());
163167
}
168+
169+
#[Test]
170+
public function user_cannot_set_own_post_as_best_answer_if_not_permitted()
171+
{
172+
$response = $this->setBestAnswer(3, 5, 2);
173+
174+
$this->assertEquals(403, $response->getStatusCode());
175+
}
176+
177+
#[Test]
178+
public function user_can_set_own_post_as_best_answer_if_permitted()
179+
{
180+
$this->setting('fof-best-answer.allow_select_own_post', true);
181+
182+
$response = $this->setBestAnswer(3, 5, 2);
183+
184+
$this->assertEquals(200, $response->getStatusCode());
185+
186+
$data = json_decode($response->getBody()->getContents(), true);
187+
188+
$attributes = $data['data']['attributes'];
189+
190+
$this->assertEquals(5, $attributes['hasBestAnswer'], 'Expected best answer post ID to be 5');
191+
}
164192
}

0 commit comments

Comments
 (0)