summaryrefslogtreecommitdiff
path: root/tests/Unit/Models/Auth/RoleTest.php
blob: 10c22f9ca1a20aa324b5f6e2b0bdd6136a0d2a30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

namespace Engelsystem\Test\Unit\Models\Auth;

use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use Engelsystem\Models\Auth\Role;

class RoleTest extends AuthModelTest
{
    use ArraySubsetAsserts;

    /** @var array */
    protected $data = [
        'name'        => 'Test Role',
        'description' => 'Lorem Ipsum',
    ];

    /**
     * @covers \Engelsystem\Models\Auth\Role::permissions
     */
    public function testPermissions()
    {
        $permission = $this->getPermission();
        $permission2 = $this->getPermission(['name' => 'Another Permission']);

        $role = new Role($this->data);
        $role->save();
        $role->permissions()->attach($permission);
        $role->permissions()->attach($permission2);

        $this->assertCount(2, $role->permissions);
        $this->assertEquals('foo.bar', $role->permissions()->get()->first()->name);
    }

    /**
     * @covers \Engelsystem\Models\Auth\Role::teams
     */
    public function testTeams()
    {
        $team = $this->getTeam();

        $role = new Role($this->data);
        $role->save();
        $role->teams()->attach($team);

        $this->assertEquals('Test Team', $role->teams()->get()->first()->name);
    }

    /**
     * @covers \Engelsystem\Models\Auth\Role::users
     */
    public function testUsers()
    {
        $team = $this->getTeam();
        $team2 = $this->getTeam([], ['name' => 'Dev']);
        $this->getUser($team);
        $this->getUser($team, ['name' => 'Foo', 'email' => 'foo@xample.com']);
        $this->getUser($team2, ['name' => 'Bar', 'email' => 'bar@xample.com']);

        $role = new Role($this->data);
        $role->save();
        $role->teams()->attach($team);
        $role->teams()->attach($team2);

        $this->assertCount(3, $role->users()->get());
        $this->assertEquals('Test User', $role->users()->get()->first()->name);
    }
}