<commit_before><?php

namespace Tests\Browser;

use App\Models\Beatmap;
use App\Models\BeatmapDiscussion;
use App\Models\BeatmapDiscussionPost;
use App\Models\Beatmapset;
use App\Models\User;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class BeatmapDiscussionPostsTest extends DuskTestCase
{
    private $new_reply_widget_selector = '.beatmap-discussion__full .beatmap-discussion-post--new-reply';

    public function testConcurrentPostAfterResolve()
    {
        $this->browse(function (Browser $first, Browser $second) {
            // Setup both browsers.
            $this->visitDiscussionPageAsUser($first, $this->mapper);
            $this->visitDiscussionPageAsUser($second, $this->user);

            // Write a reply...
            $this->writeReply($first, 'Fixed');
            $this->writeReply($second, 'Hey!');

            // And send the replies.
            $this->postReply($first, 'resolve');
            $this->postReply($second, 'reply');

            $first->pause(2000);
            $second->pause(2000);

            $this->assertSame(true, $this->beatmapDiscussion->fresh()->resolved);
        });
    }

    protected function writeReply(Browser $browser, $reply)
    {
        $browser->with($this->new_reply_widget_selector, function ($new_reply) use ($reply) {
            $new_reply->press('Respond')
                ->waitFor('textarea')
                ->type('textarea', $reply);
        });
    }

    protected function postReply(Browser $browser, $action)
    {
        $browser->with($this->new_reply_widget_selector, function ($new_reply) use ($action) {
            switch ($action) {
                case 'resolve':
                    $new_reply->press('Reply and Resolve');
                    break;
                default:
                    $new_reply->keys('textarea', '{enter}');
                    break;
            }
        });
    }

    protected function visitDiscussionPageAsUser(Browser $browser, User $user): void
    {
        $browser->loginAs($user)
            ->visit('/_dusk/verify')
            ->visitRoute('beatmap-discussions.show', [
                'beatmapset' => $this->beatmapset->getKey(),
                'beatmap_discussion' => $this->beatmapDiscussion->getKey(),
            ]);
    }

    protected function createUserCapableOfDiscussing(): User
    {
        $user = factory(User::class)->create();
        $user->statisticsOsu()->create(['playcount' => $this->minPlays]);

        return $user;
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->minPlays = config('osu.user.min_plays_for_posting');

        $this->mapper = $this->createUserCapableOfDiscussing();
        $this->user = $this->createUserCapableOfDiscussing();

        $this->beatmapset = factory(Beatmapset::class)->create([
            'user_id' => $this->mapper->getKey(),
        ]);
        $this->beatmap = $this->beatmapset->beatmaps()->save(factory(Beatmap::class)->make());
        $this->beatmapDiscussion = factory(BeatmapDiscussion::class, 'timeline')->create([
            'beatmapset_id' => $this->beatmapset->getKey(),
            'beatmap_id' => $this->beatmap->getKey(),
            'user_id' => $this->user->getKey(),
        ]);
        $post = factory(BeatmapDiscussionPost::class, 'timeline')->make([
            'user_id' => $this->user->getKey(),
        ]);
        $this->beatmapDiscussionPost = $this->beatmapDiscussion->beatmapDiscussionPosts()->save($post);
    }
}<commit_msg>Clean up models created during test<commit_after><?php

namespace Tests\Browser;

use App\Models\Beatmap;
use App\Models\BeatmapDiscussion;
use App\Models\BeatmapDiscussionPost;
use App\Models\Beatmapset;
use App\Models\User;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

class BeatmapDiscussionPostsTest extends DuskTestCase
{
    private $new_reply_widget_selector = '.beatmap-discussion__full .beatmap-discussion-post--new-reply';

    public function testConcurrentPostAfterResolve()
    {
        $this->browse(function (Browser $first, Browser $second) {
            // Setup both browsers.
            $this->visitDiscussionPageAsUser($first, $this->mapper);
            $this->visitDiscussionPageAsUser($second, $this->user);

            // Write a reply...
            $this->writeReply($first, 'Fixed');
            $this->writeReply($second, 'Hey!');

            // And send the replies.
            $this->postReply($first, 'resolve');
            $this->postReply($second, 'reply');

            $first->pause(2000);
            $second->pause(2000);

            $this->assertSame(true, $this->beatmapDiscussion->fresh()->resolved);
        });
    }

    protected function writeReply(Browser $browser, $reply)
    {
        $browser->with($this->new_reply_widget_selector, function ($new_reply) use ($reply) {
            $new_reply->press('Respond')
                ->waitFor('textarea')
                ->type('textarea', $reply);
        });
    }

    protected function postReply(Browser $browser, $action)
    {
        $browser->with($this->new_reply_widget_selector, function ($new_reply) use ($action) {
            switch ($action) {
                case 'resolve':
                    $new_reply->press('Reply and Resolve');
                    break;
                default:
                    $new_reply->keys('textarea', '{enter}');
                    break;
            }
        });
    }

    protected function visitDiscussionPageAsUser(Browser $browser, User $user): void
    {
        $browser->loginAs($user)
            ->visit('/_dusk/verify')
            ->visitRoute('beatmap-discussions.show', [
                'beatmapset' => $this->beatmapset->getKey(),
                'beatmap_discussion' => $this->beatmapDiscussion->getKey(),
            ]);
    }

    protected function createUserCapableOfDiscussing(): User
    {
        $user = factory(User::class)->create();
        $user->statisticsOsu()->create(['playcount' => $this->minPlays]);

        return $user;
    }

    protected function deleteUser(User $user): void
    {
        $user->userProfileCustomization()->forceDelete();
        $user->forceDelete();
    }

    protected function cleanup(): void
    {
        // Delete all models we created.
        $this->beatmapDiscussion->beatmapDiscussionPosts()->forceDelete();
        $this->beatmapDiscussion->forceDelete();
        $this->beatmap->forceDelete();
        $this->beatmapset->events()->forceDelete();
        $this->beatmapset->forceDelete();
        $this->deleteUser($this->user);
        $this->deleteUser($this->mapper);
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->minPlays = config('osu.user.min_plays_for_posting');

        $this->mapper = $this->createUserCapableOfDiscussing();
        $this->user = $this->createUserCapableOfDiscussing();

        $this->beatmapset = factory(Beatmapset::class)->create([
            'user_id' => $this->mapper->getKey(),
        ]);
        $this->beatmap = $this->beatmapset->beatmaps()->save(factory(Beatmap::class)->make());
        $this->beatmapDiscussion = factory(BeatmapDiscussion::class, 'timeline')->create([
            'beatmapset_id' => $this->beatmapset->getKey(),
            'beatmap_id' => $this->beatmap->getKey(),
            'user_id' => $this->user->getKey(),
        ]);
        $post = factory(BeatmapDiscussionPost::class, 'timeline')->make([
            'user_id' => $this->user->getKey(),
        ]);
        $this->beatmapDiscussionPost = $this->beatmapDiscussion->beatmapDiscussionPosts()->save($post);

        $this->beforeApplicationDestroyed(function () {
            // Similar case to SanityTest, cleanup the models we created during the test.
            $this->cleanup();
        });
    }
}
{'fraction_non_alphanumeric': 0.15615384615384614, 'fraction_numerical': 0.0020512820512820513, 'mean_word_length': 2.433538732394366, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 168, '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': '5441566', 'n_tokens_mistral': 2267, 'n_tokens_neox': 2140, 'n_words': 346}