<commit_before><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace Tests\Controllers;

use App\Events\NewPrivateNotificationEvent;
use App\Models\Beatmap;
use App\Models\BeatmapDiscussion;
use App\Models\BeatmapDiscussionPost;
use App\Models\Beatmapset;
use App\Models\Notification;
use App\Models\User;
use App\Models\UserNotification;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;

class BeatmapDiscussionPostsControllerTest extends TestCase
{
    private Beatmap $beatmap;
    private BeatmapDiscussion $beatmapDiscussion;
    private BeatmapDiscussionPost $beatmapDiscussionPost;
    private Beatmapset $beatmapset;
    private User $mapper;
    private User $user;

    /**
     * @dataProvider postStoreNewDiscussionMinPlaysDataProvider
     */
    public function testPostStoreNewDiscussionMinPlays(?int $minPlays, bool $verified, bool $success)
    {
        config()->set('osu.user.post_action_verification', false);
        $user = User::factory()->withPlays($minPlays)->create();
        $watcher = User::factory()->create();
        $this->beatmapset->watches()->create(['user_id' => $watcher->getKey()]);
        $params = $this->makeBeatmapsetDiscussionPostParams($this->beatmapset, 'praise');

        $currentDiscussions = BeatmapDiscussion::count();
        $currentDiscussionPosts = BeatmapDiscussionPost::count();
        $currentNotifications = Notification::count();
        $currentUserNotifications = UserNotification::count();

        $this->actAsUser($user, $verified);
        $request = $this->post(route('beatmapsets.discussions.posts.store'), $params);

        if ($success) {
            $request->assertStatus(200);
        } else {
            $request->assertStatus(401)->assertViewIs('users.verify');
        }

        $change = $success ? 1 : 0;
        $this->assertSame($currentDiscussions + $change, BeatmapDiscussion::count());
        $this->assertSame($currentDiscussionPosts + $change, BeatmapDiscussionPost::count());
        $this->assertSame($currentNotifications + $change, Notification::count());
        $this->assertSame($currentUserNotifications + $change, UserNotification::count());

        if ($success) {
            Event::assertDispatched(NewPrivateNotificationEvent::class, function (NewPrivateNotificationEvent $event) use ($watcher) {
                // assert watchers in receivers and sender is not.
                return in_array($watcher->getKey(), $event->getReceiverIds(), true)
                    && !in_array($this->user->getKey(), $event->getReceiverIds(), true);
            });
        } else {
            Event::assertNotDispatched(NewPrivateNotificationEvent::class);
        }
    }

    public function testPostStoreNewDiscussionInactiveBeatmapset()
    {
        $beatmapset = Beatmapset::factory()->owner()->inactive()->create();

        $this
            ->actingAsVerified($this->user)
            ->post(route('beatmapsets.discussions.posts.store'), $this->makeBeatmapsetDiscussionPostParams($beatmapset, 'praise'))
            ->assertStatus(404);
    }

    public function testPostStoreNewDiscussionNoteByMapper()
    {
        $currentDiscussions = BeatmapDiscussion::count();
        $currentDiscussionPosts = BeatmapDiscussionPost::count();

        $this
            ->actingAsVerified($this->mapper)
            ->post(route('beatmapsets.discussions.posts.store'), $this->makeBeatmapsetDiscussionPostParams($this->beatmapset, 'mapper_note'))
            ->assertStatus(200);

        $this->assertSame($currentDiscussions + 1, BeatmapDiscussion::count());
        $this->assertSame($currentDiscussionPosts + 1, BeatmapDiscussionPost::count());
    }

    public function testPostStoreNewDiscussionNoteByMapperOnGuestBeatmap()
    {<commit_msg>remove migrated test<commit_after><?php

// Copyright (c) ppy Pty Ltd <<EMAIL>>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

namespace Tests\Controllers;

use App\Events\NewPrivateNotificationEvent;
use App\Models\Beatmap;
use App\Models\BeatmapDiscussion;
use App\Models\BeatmapDiscussionPost;
use App\Models\Beatmapset;
use App\Models\Notification;
use App\Models\User;
use App\Models\UserNotification;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;

class BeatmapDiscussionPostsControllerTest extends TestCase
{
    private Beatmap $beatmap;
    private BeatmapDiscussion $beatmapDiscussion;
    private BeatmapDiscussionPost $beatmapDiscussionPost;
    private Beatmapset $beatmapset;
    private User $mapper;
    private User $user;

    public function testPostStoreNewDiscussionInactiveBeatmapset()
    {
        $beatmapset = Beatmapset::factory()->owner()->inactive()->create();

        $this
            ->actingAsVerified($this->user)
            ->post(route('beatmapsets.discussions.posts.store'), $this->makeBeatmapsetDiscussionPostParams($beatmapset, 'praise'))
            ->assertStatus(404);
    }

    public function testPostStoreNewDiscussionNoteByMapper()
    {
        $currentDiscussions = BeatmapDiscussion::count();
        $currentDiscussionPosts = BeatmapDiscussionPost::count();

        $this
            ->actingAsVerified($this->mapper)
            ->post(route('beatmapsets.discussions.posts.store'), $this->makeBeatmapsetDiscussionPostParams($this->beatmapset, 'mapper_note'))
            ->assertStatus(200);

        $this->assertSame($currentDiscussions + 1, BeatmapDiscussion::count());
        $this->assertSame($currentDiscussionPosts + 1, BeatmapDiscussionPost::count());
    }

    public function testPostStoreNewDiscussionNoteByMapperOnGuestBeatmap()
    {
{'fraction_non_alphanumeric': 0.12275242047026279, 'fraction_numerical': 0.004840940525587829, 'mean_word_length': 3.8410041841004183, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5524868', 'n_tokens_mistral': 1662, 'n_tokens_neox': 1544, 'n_words': 292}