@@ -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