Download tasks/019e0f0b88563440/levels/level3/patch.diff from FuCY/php: direct link, hf CLI and curl.
- Browser
- Download file 6.26 kB
-
https://huggingface.co/datasets/FuCY/php/resolve/main/tasks/019e0f0b88563440/levels/level3/patch.diff
- Command line
-
hf download hf://datasets/FuCY/php/tasks/019e0f0b88563440/levels/level3/patch.diff
-
curl -L -o patch.diff https://huggingface.co/datasets/FuCY/php/resolve/main/tasks/019e0f0b88563440/levels/level3/patch.diff
6.26 kB
| diff --git a/framework/core/src/Discussion/Command/StartDiscussionHandler.php b/framework/core/src/Discussion/Command/StartDiscussionHandler.php | |
| index bda8cb43955..25f7b09bdff 100644 | |
| --- a/framework/core/src/Discussion/Command/StartDiscussionHandler.php | |
| +++ b/framework/core/src/Discussion/Command/StartDiscussionHandler.php | |
| public function handle(StartDiscussion $command) | |
| // We will do this by running the PostReply command. | |
| try { | |
| $post = $this->bus->dispatch( | |
| - new PostReply($discussion->id, $actor, $data, $ipAddress) | |
| + new PostReply($discussion->id, $actor, $data, $ipAddress, true) | |
| ); | |
| } catch (Exception $e) { | |
| $discussion->delete(); | |
| diff --git a/framework/core/src/Post/Command/PostReply.php b/framework/core/src/Post/Command/PostReply.php | |
| index ae1d4c03c74..226464926ed 100644 | |
| --- a/framework/core/src/Post/Command/PostReply.php | |
| +++ b/framework/core/src/Post/Command/PostReply.php | |
| class PostReply | |
| */ | |
| public $ipAddress; | |
| + /** | |
| + * @var bool | |
| + */ | |
| + public $isFirstPost; | |
| + | |
| /** | |
| * @param int $discussionId The ID of the discussion to post the reply to. | |
| * @param User $actor The user who is performing the action. | |
| * @param array $data The attributes to assign to the new post. | |
| * @param string $ipAddress The IP address of the actor. | |
| */ | |
| - public function __construct($discussionId, User $actor, array $data, $ipAddress = null) | |
| + public function __construct($discussionId, User $actor, array $data, $ipAddress = null, bool $isFirstPost = false) | |
| { | |
| $this->discussionId = $discussionId; | |
| $this->actor = $actor; | |
| $this->data = $data; | |
| $this->ipAddress = $ipAddress; | |
| + $this->isFirstPost = $isFirstPost; | |
| } | |
| } | |
| diff --git a/framework/core/src/Post/Command/PostReplyHandler.php b/framework/core/src/Post/Command/PostReplyHandler.php | |
| index b0bfb887733..f634a2dac5c 100644 | |
| --- a/framework/core/src/Post/Command/PostReplyHandler.php | |
| +++ b/framework/core/src/Post/Command/PostReplyHandler.php | |
| public function handle(PostReply $command) | |
| // If this is the first post in the discussion, it's technically not a | |
| // "reply", so we won't check for that permission. | |
| - if ($discussion->first_post_id !== null) { | |
| + if (! $command->isFirstPost) { | |
| $actor->assertCan('reply', $discussion); | |
| } | |
| diff --git a/framework/core/tests/integration/api/posts/CreateTest.php b/framework/core/tests/integration/api/posts/CreateTest.php | |
| index a69d4278370..41b61276db0 100644 | |
| --- a/framework/core/tests/integration/api/posts/CreateTest.php | |
| +++ b/framework/core/tests/integration/api/posts/CreateTest.php | |
| namespace Flarum\Tests\integration\api\posts; | |
| use Carbon\Carbon; | |
| +use Flarum\Group\Group; | |
| use Flarum\Testing\integration\RetrievesAuthorizedUsers; | |
| use Flarum\Testing\integration\TestCase; | |
| protected function setUp(): void | |
| $this->prepareDatabase([ | |
| 'discussions' => [ | |
| - ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2], | |
| + ['id' => 1, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => 1], | |
| + // Discussion with deleted first post. | |
| + ['id' => 2, 'title' => __CLASS__, 'created_at' => Carbon::now()->toDateTimeString(), 'user_id' => 2, 'first_post_id' => null], | |
| + ], | |
| + 'posts' => [ | |
| + ['id' => 1, 'discussion_id' => 1, 'number' => 1, 'created_at' => Carbon::now()->subDay()->toDateTimeString(), 'user_id' => 2, 'type' => 'comment', 'content' => '<t></t>'], | |
| ], | |
| 'users' => [ | |
| $this->normalUser(), | |
| - ] | |
| + ['id' => 3, 'username' => 'restricted', 'email' => 'restricted@machine.local', 'is_email_confirmed' => 1], | |
| + ], | |
| + 'groups' => [ | |
| + ['id' => 40, 'name_singular' => 'tess', 'name_plural' => 'tess'], | |
| + ], | |
| + 'group_user' => [ | |
| + ['group_id' => 40, 'user_id' => 3], | |
| + ], | |
| + 'group_permission' => [ | |
| + ['group_id' => 40, 'permission' => 'discussion.reply'], | |
| + ], | |
| ]); | |
| } | |
| /** | |
| + * @dataProvider discussionRepliesPrvider | |
| * @test | |
| */ | |
| - public function can_create_reply() | |
| + public function can_create_reply_if_allowed(int $actorId, int $discussionId, int $responseStatus) | |
| { | |
| + // Reset permissions for normal users group. | |
| + $this->database() | |
| + ->table('group_permission') | |
| + ->where('permission', 'discussion.reply') | |
| + ->where('group_id', Group::MEMBER_ID) | |
| + ->delete(); | |
| + | |
| $response = $this->send( | |
| $this->request('POST', '/api/posts', [ | |
| - 'authenticatedAs' => 2, | |
| + 'authenticatedAs' => $actorId, | |
| 'json' => [ | |
| 'data' => [ | |
| 'attributes' => [ | |
| 'content' => 'reply with predetermined content for automated testing - too-obscure', | |
| ], | |
| 'relationships' => [ | |
| - 'discussion' => ['data' => ['id' => 1]], | |
| + 'discussion' => ['data' => ['id' => $discussionId]], | |
| ], | |
| ], | |
| ], | |
| ]) | |
| ); | |
| - $this->assertEquals(201, $response->getStatusCode()); | |
| + $this->assertEquals($responseStatus, $response->getStatusCode()); | |
| + } | |
| + | |
| + public function discussionRepliesPrvider(): array | |
| + { | |
| + return [ | |
| + // [$actorId, $discussionId, $responseStatus] | |
| + 'can_create_reply_with_ability' => [3, 1, 201], | |
| + 'cannot_create_reply_without_ability' => [2, 1, 403], | |
| + 'can_create_reply_with_ability_when_first_post_is_deleted' => [3, 2, 201], | |
| + 'cannot_create_reply_without_ability_when_first_post_is_deleted' => [2, 2, 403], | |
| + ]; | |
| } | |
| /** | |