summaryrefslogtreecommitdiffstats
path: root/tests/Psecio/Gatekeeper/PermissionModelTest.php
blob: e47f8be3e7a746e4a29e1fb7c80946627b167f03 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php

namespace Psecio\Gatekeeper;

class PermissionModelTest extends \Psecio\Gatekeeper\Base
{
    /**
     * Test the addition of a child by ID to a permission
     */
    public function testAddChildByIdValid()
    {
        $ds = $this->buildMock(true, 'save');
        $perm = new PermissionModel($ds, array('id' => 1234));

        $this->assertTrue($perm->addChild(1));
    }

    /**
     * Test the addition of a child by model instance to a permission
     */
    public function testAddChildByModelValid()
    {
        $ds = $this->buildMock(true, 'save');
        $perm1 = new PermissionModel($ds, array('id' => 1234));
        $perm2 = new PermissionModel($ds, array('id' => 4321));

        $this->assertTrue($perm1->addChild($perm2));
    }

    /**
     * Test the invalid addition of a permission by ID
     */
    public function testAddChildByIdInvalid()
    {
        $ds = $this->buildMock(true, 'save');
        $perm = new PermissionModel($ds);

        $this->assertFalse($perm->addChild(1));
    }

    /**
     * Test the valid addition of a child by ID
     */
    public function testRemoveChildByIdValid()
    {
        $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql')
            ->disableOriginalConstructor()
            ->setMethods(array('find', 'delete'))
            ->getMock();

        $perm = new PermissionModel($ds, array('id' => 1234));

        $ds->method('find')->willReturn($perm);
        $ds->method('delete')->willReturn(true);

        $this->assertTrue($perm->removeChild(1));
    }

    /**
     * Test the valid removal of a child by model instance
     */
    public function testRemoveChildByModelValid()
    {
        $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql')
            ->disableOriginalConstructor()
            ->setMethods(array('find', 'delete'))
            ->getMock();

        $perm1 = new PermissionModel($ds, array('id' => 1234));
        $perm2 = new PermissionModel($ds);

        $ds->method('find')->willReturn($perm1);
        $ds->method('delete')->willReturn(true);

        $this->assertTrue($perm1->removeChild($perm2));
    }

    /**
     * Test the invalid removal of a child by ID
     */
    public function testRemoveChildByIdInvalid()
    {
        $ds = $this->getMockBuilder('\Psecio\Gatekeeper\DataSource\Mysql')
            ->disableOriginalConstructor()
            ->getMock();

        $perm = new PermissionModel($ds);
        $this->assertFalse($perm->removeChild(1));
    }

    /**
     * Test that a permission is not expired
     */
    public function testPermissionNotExpired()
    {
        $ds = $this->buildMock(true);
        $perm = new PermissionModel($ds, [
            'id' => 1234,
            'expire' => strtotime('+1 day')
        ]);

        $this->assertFalse($perm->isExpired());
    }

    /**
     * Test that a permission is marked as expired
     */
    public function testPermissionIsExpired()
    {
        $ds = $this->buildMock(true);
        $perm = new PermissionModel($ds, [
            'id' => 1234,
            'expire' => strtotime('-1 day')
        ]);

        $this->assertTrue($perm->isExpired());
    }
}