<commit_before><?php

namespace Tests\Unit\Http\Controllers;

use App\Entities\Comment;
use App\Entities\Grammar;
use App\Entities\Right;
use App\Entities\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Pagination\LengthAwarePaginator;
use Tests\BrowserKitTestCase;

class GrammarsControllerTest extends BrowserKitTestCase
{
    use DatabaseMigrations;

    public function testIndex()
    {
        $user = factory(User::class)->create();
        factory(Grammar::class, 10)->create();

        $this
            ->actingAs($user)
            ->get(route('grammars.index'));

        $this->assertResponseOk();
        $this->assertViewHas('grammars');

        $grammars = $this->response->original->getData()['grammars'];
        $this->assertInstanceOf(LengthAwarePaginator::class, $grammars);
    }

    /**
     * @dataProvider indexWithVersionsProvider
     */
    public function testIndexWithVersions(callable $setupCb)
    {
        $user = $setupCb();
        $parent = Grammar::create([
            'user_id' => $user->id,
            'name' => 'name',
            'content' => 'content',
            'public_view' => true,
        ]);
        $child = Grammar::create([
            'user_id' => $user->id,
            'name' => 'name',
            'content' => 'content',
            'public_view' => true,
        ]);
        $child->makeChildOf($parent);

        $this
            ->actingAs($user)
            ->get(route('grammars.index'));

        $this->assertResponseOk();
        $this->assertViewHas('grammars');

        $grammars = $this->response->original->getData()['grammars'];
        $this->assertInstanceOf(LengthAwarePaginator::class, $grammars);
        $this->assertEquals(1, $grammars->total());
    }

    public function indexWithVersionsProvider()
    {
        return [
            'regular user' => [
                function () {
                    return factory(User::class)->create();
                },
            ],
            'admin' => [
                function () {
                    return factory(User::class, 'admin')->create();
                },
            ],
        ];
    }

    public function testShow()
    {
        $user = factory(User::class)->create();
        $grammar = factory(Grammar::class)->create(['user_id' => $user->id]);

        $this
            ->actingAs($user)
            ->get(route('grammars.show', $grammar->id));

        $this->assertResponseOk();
        $this->assertViewHas('grammar');
    }

    public function testCreate()
    {
        $this
            ->actingAs(factory(User::class)->create())
            ->visit(route('grammars.create'))
            ->type('title1', 'name')
            ->type('content', 'content')
            ->check('public_view')
            ->check('allow_to_comment')
            ->press('Create')
            ->seePageIs(route('grammars.show', 1));

        $this->seeInDatabase('grammars', [
            'name' => 'title1',
            'content' => 'content',
            'public_view' => 1,
            'allow_to_comment' => 1,
        ]);
    }

    public function testDestroy()
    {
        $user = factory(User::class)->create();
        $grammar = factory(Grammar::class)->create(['user_id' => $user->id]);<commit_msg>test(laravel): test for warning about outdated version<commit_after><?php

namespace Tests\Unit\Http\Controllers;

use App\Entities\Comment;
use App\Entities\Grammar;
use App\Entities\Right;
use App\Entities\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Pagination\LengthAwarePaginator;
use Tests\BrowserKitTestCase;

class GrammarsControllerTest extends BrowserKitTestCase
{
    use DatabaseMigrations;

    public function testIndex()
    {
        $user = factory(User::class)->create();
        factory(Grammar::class, 10)->create();

        $this
            ->actingAs($user)
            ->get(route('grammars.index'));

        $this->assertResponseOk();
        $this->assertViewHas('grammars');

        $grammars = $this->response->original->getData()['grammars'];
        $this->assertInstanceOf(LengthAwarePaginator::class, $grammars);
    }

    /**
     * @dataProvider indexWithVersionsProvider
     */
    public function testIndexWithVersions(callable $setupCb)
    {
        $user = $setupCb();
        $parent = Grammar::create([
            'user_id' => $user->id,
            'name' => 'name',
            'content' => 'content',
            'public_view' => true,
        ]);
        $child = Grammar::create([
            'user_id' => $user->id,
            'name' => 'name',
            'content' => 'content',
            'public_view' => true,
        ]);
        $child->makeChildOf($parent);

        $this
            ->actingAs($user)
            ->get(route('grammars.index'));

        $this->assertResponseOk();
        $this->assertViewHas('grammars');

        $grammars = $this->response->original->getData()['grammars'];
        $this->assertInstanceOf(LengthAwarePaginator::class, $grammars);
        $this->assertEquals(1, $grammars->total());
    }

    public function indexWithVersionsProvider()
    {
        return [
            'regular user' => [
                function () {
                    return factory(User::class)->create();
                },
            ],
            'admin' => [
                function () {
                    return factory(User::class, 'admin')->create();
                },
            ],
        ];
    }

    public function testShow()
    {
        $user = factory(User::class)->create();
        $grammar = factory(Grammar::class)->create(['user_id' => $user->id]);

        $this
            ->actingAs($user)
            ->get(route('grammars.show', $grammar->id));

        $this->assertResponseOk();
        $this->assertViewHas('grammar', 'lastVersion');
    }

    public function testShowOutdatedVersion()
    {
        $user = factory(User::class)->create();
        $parent = Grammar::create([
            'user_id' => $user->id,
            'name' => 'name',
            'content' => 'content',
            'public_view' => true,
        ]);
        $child = Grammar::create([
            'user_id' => $user->id,
            'name' => 'name',
            'content' => 'content',
            'public_view' => true,
        ]);
        $child->makeChildOf($parent);

        $this
            ->actingAs($user)
            ->get(route('grammars.show', $parent->id));

        $this->assertResponseOk();
        $this->assertViewHas(['grammar', 'lastVersion']);

        $this->see('You are looking not the');
        $this->see('latest');
        $this->see('version of grammar');
    }

    public function testCreate()
    {
        $this
            ->actingAs(factory(User::class)->create())
            ->visit(route('grammars.create'))
            ->type('title1', 'name')
            ->type('content', 'content')
            ->check('public_view')
            ->check('allow_to_comment')
            ->press('Create')
            ->seePageIs(route('grammars.show', 1));

        $this->seeInDatabase('grammars', [
            'name' => 'title1',
            'content' => 'content',
            'public_view' => 1,
            'allow_to_comment' => 1,
        ]);
    }

    public function testDestroy()
    {
        $user = factory(User::class)->create();
        $grammar = factory(Grammar::class)->create(['user_id' => $user->id]);
{'fraction_non_alphanumeric': 0.16709166554824856, 'fraction_numerical': 0.0021473627700979736, 'mean_word_length': 2.0145631067961167, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 147, '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': '3397913', 'n_tokens_mistral': 2164, 'n_tokens_neox': 2128, 'n_words': 326}