<commit_before><?php

namespace Bosnadev\Tests\Ronin;

use Bosnadev\Ronin\Contracts\Scope;
use Bosnadev\Ronin\Exceptions\ScopeNotFound;
use Bosnadev\Ronin\Contracts\Role as RoleContract;

class ScopableTest extends RoninTestCase
{
    public function testUserHasRoleScope()
    {
        $this->role->addScope('create');
        $this->role->addScope('search');
        $this->user->assignRole(1);

        $this->assertTrue($this->user->inScope('search'));
        $this->seeInDatabase('role_scope', ['scope_id' => app(Scope::class)->where('slug', 'search')->first()->id]);
    }

    public function testUserDoesNotHaveRoleScope()
    {
        $this->assertFalse($this->user->inScope('edit'));
        $this->dontSeeInDatabase('role_scope', ['scope_id' => app(Scope::class)->where('slug', 'edit')->first()->id]);
    }

    public function testUserHasRoleScopeInMultipleRoles()
    {
        $this->role->addScope('create');
        $this->role2->addScope('search');
        $this->user->assignRole(1);
        $this->user->assignRole(2);

        $role3 = app(RoleContract::class)->create(['name' => 'Moderator', 'slug' => 'slug']);
        $role3->addScope('delete');

        $this->refreshRoleInstance();

        $this->assertTrue($this->user->inScope('search'));
        $this->assertTrue($this->user->inScope('create'));
    }

    public function testUserDoesNotHaveRoleScopeInAnyRole()
    {
        $this->assertFalse($this->user->inScope('delete'));
    }

    public function testGrantingScopeToRole()
    {
        $this->role->addScope('create');
        $this->role->addScope('search');

        $this->refreshRoleInstance();
        $this->refreshScopeInstance();

        $this->assertFalse($this->role->inScope(['delete']));
        $this->assertTrue($this->role->inScope('create'));
        $this->assertTrue($this->role->inScope('search'));
    }

    public function testWhenScopeNotFound()
    {
        $this->expectException(ScopeNotFound::class);
        $this->assertFalse($this->role->inScope('delete'));
        $this->assertFalse($this->role->inScope('insert'));
    }

    public function testIfUserHasDirectPermission()
    {
        $this->user->addScope('edit');

        $this->refreshUserInstance();

        $this->assertTrue($this->user->inScope('edit'));
        $this->assertFalse($this->user->inScope('delete'));
    }

    public function testFindPermissionById()
    {
        $scope = app(Scope::class)->findById(1);

        $this->seeInDatabase('scopes', ['id' => $scope->id]);
    }

    public function testFindScopeByName()
    {
        $scope = app(Scope::class)->findByName('Edit');

        $this->seeInDatabase('scopes', ['name' => $scope->name]);
    }
}<commit_msg>test find scope by slug<commit_after><?php

namespace Bosnadev\Tests\Ronin;

use Bosnadev\Ronin\Contracts\Scope;
use Bosnadev\Ronin\Exceptions\ScopeNotFound;
use Bosnadev\Ronin\Contracts\Role as RoleContract;

class ScopableTest extends RoninTestCase
{
    public function testUserHasRoleScope()
    {
        $this->role->addScope('create');
        $this->role->addScope('search');
        $this->user->assignRole(1);

        $this->assertTrue($this->user->inScope('search'));
        $this->seeInDatabase('role_scope', ['scope_id' => app(Scope::class)->where('slug', 'search')->first()->id]);
    }

    public function testUserDoesNotHaveRoleScope()
    {
        $this->assertFalse($this->user->inScope('edit'));
        $this->dontSeeInDatabase('role_scope', ['scope_id' => app(Scope::class)->where('slug', 'edit')->first()->id]);
    }

    public function testUserHasRoleScopeInMultipleRoles()
    {
        $this->role->addScope('create');
        $this->role2->addScope('search');
        $this->user->assignRole(1);
        $this->user->assignRole(2);

        $role3 = app(RoleContract::class)->create(['name' => 'Moderator', 'slug' => 'slug']);
        $role3->addScope('delete');

        $this->refreshRoleInstance();

        $this->assertTrue($this->user->inScope('search'));
        $this->assertTrue($this->user->inScope('create'));
    }

    public function testUserDoesNotHaveRoleScopeInAnyRole()
    {
        $this->assertFalse($this->user->inScope('delete'));
    }

    public function testGrantingScopeToRole()
    {
        $this->role->addScope('create');
        $this->role->addScope('search');

        $this->refreshRoleInstance();
        $this->refreshScopeInstance();

        $this->assertFalse($this->role->inScope(['delete']));
        $this->assertTrue($this->role->inScope('create'));
        $this->assertTrue($this->role->inScope('search'));
    }

    public function testWhenScopeNotFound()
    {
        $this->expectException(ScopeNotFound::class);
        $this->assertFalse($this->role->inScope('delete'));
        $this->assertFalse($this->role->inScope('insert'));
    }

    public function testIfUserHasDirectScope()
    {
        $this->user->addScope('edit');

        $this->refreshUserInstance();

        $this->assertTrue($this->user->inScope('edit'));
        $this->assertFalse($this->user->inScope('delete'));
    }

    public function testFindScopeById()
    {
        $scope = app(Scope::class)->findById(1);

        $this->seeInDatabase('scopes', ['id' => $scope->id]);
    }

    public function testFindScopeBySlug()
    {
        $scope = app(Scope::class)->findBySlug('edit');

        $this->seeInDatabase('scopes', ['slug' => $scope->slug]);
    }

    public function testFindScopeByName()
    {
        $scope = app(Scope::class)->findByName('Edit');

        $this->seeInDatabase('scopes', ['name' => $scope->name]);
    }
}
{'fraction_non_alphanumeric': 0.1939900426742532, 'fraction_numerical': 0.0024893314366998577, 'mean_word_length': 3.37402799377916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 173, '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': '2062291', 'n_tokens_mistral': 1708, 'n_tokens_neox': 1685, 'n_words': 198}