JobTest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 142
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetApplicationLink() 0 18 1
A testGetCMSFields() 0 6 1
A testGetApplyButton() 0 5 1
A testPopulateDefaults() 0 6 1
A testCanView() 0 6 1
A testCanDelete() 0 12 1
A testProvidePermissions() 0 8 1
A testCanEdit() 0 12 1
A testCanCreate() 0 12 1
1
<?php
2
3
namespace Dynamic\Jobs\Test\Page;
4
5
use Dynamic\Jobs\Page\Job;
6
use Dynamic\Jobs\Page\JobCollection;
7
use SilverStripe\Assets\File;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Dev\SapphireTest;
10
use SilverStripe\Forms\FieldList;
11
use SilverStripe\Security\Member;
12
13
/**
14
 * Class JobTest
15
 * @package Dynamic\Jobs\Tests
16
 */
17
class JobTest extends SapphireTest
18
{
19
    /**
20
     * @var string
21
     */
22
    protected static $fixture_file = '../fixtures.yml';
23
24
    /**
25
     * Tests populateDefaults()
26
     */
27
    public function testPopulateDefaults()
28
    {
29
        /** @var Job $object */
30
        $object = Injector::inst()->create(Job::class);
31
        $object->populateDefaults();
32
        $this->assertEquals(date('Y-m-d'), $object->PostDate);
33
    }
34
35
    /**
36
     * Tests getCMSFields()
37
     */
38
    public function testGetCMSFields()
39
    {
40
        /** @var Job $object */
41
        $object = $this->objFromFixture(Job::class, 'one');
42
        $fields = $object->getCMSFields();
43
        $this->assertInstanceOf(FieldList::class, $fields);
44
    }
45
46
    /**
47
     * Tests getApplyButton()
48
     */
49
    public function testGetApplyButton()
50
    {
51
        /** @var Job $object */
52
        $object = Injector::inst()->create(Job::class);
53
        $this->assertStringEndsWith('apply', $object->getApplyButton());
54
    }
55
56
    /**
57
     * Tests getApplicationLink()
58
     */
59
    public function testGetApplicationLink()
60
    {
61
        /** @var Job $object */
62
        $object = $this->objFromFixture(Job::class, 'one');
63
        /** @var JobCollection $parent */
64
        $parent = $this->objFromFixture(JobCollection::class, 'default');
65
66
        $object->ParentID = $parent->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
        $object->write();
68
69
        $this->assertFalse($object->getApplicationLink());
70
71
        // TODO - fix this part
72
        /** @var File $file */
73
        $file = $this->objFromFixture(File::class, 'File');
74
75
        $parent->ApplicationID = $file->ID;
0 ignored issues
show
Bug Best Practice introduced by
The property ApplicationID does not exist on Dynamic\Jobs\Page\JobCollection. Since you implemented __set, consider adding a @property annotation.
Loading history...
76
        $parent->write();
77
78
        // print_r($file->ID);
79
        // print_r($object->parent()->Application()->ID);
80
81
        // $this->assertEquals($file->URL, $object->getApplicationLink());
82
    }
83
84
    /**
85
     * Tests providePermissions()
86
     */
87
    public function testProvidePermissions()
88
    {
89
        /** @var Job $object */
90
        $object = Injector::inst()->create(Job::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $object is dead and can be removed.
Loading history...
91
        $perms = [
0 ignored issues
show
Unused Code introduced by
The assignment to $perms is dead and can be removed.
Loading history...
92
            'Job_EDIT' => 'Edit a Job',
93
            'Job_DELETE' => 'Delete a Job',
94
            'Job_CREATE' => 'Create a Job',
95
        ];
96
        //$this->assertEquals($perms, $object->providePermissions());
97
    }
98
99
    /**
100
     * Tests canCreate()
101
     */
102
    public function testCanCreate()
103
    {
104
        /** @var Job $object */
105
        $object = Injector::inst()->create(Job::class);
106
107
        $admin = $this->objFromFixture(Member::class, 'Admin');
108
        $manage = $this->objFromFixture(Member::class, 'Manager');
109
        $visitor = $this->objFromFixture(Member::class, 'Visitor');
110
111
        $this->assertTrue($object->canCreate($admin));
112
        $this->assertTrue($object->canCreate($manage));
113
        $this->assertFalse($object->canCreate($visitor));
114
    }
115
116
    /**
117
     * Tests canEdit()
118
     */
119
    public function testCanEdit()
120
    {
121
        /** @var Job $object */
122
        $object = Injector::inst()->create(Job::class);
123
124
        $admin = $this->objFromFixture(Member::class, 'Admin');
125
        $manage = $this->objFromFixture(Member::class, 'Manager');
126
        $visitor = $this->objFromFixture(Member::class, 'Visitor');
127
128
        $this->assertTrue($object->canEdit($admin));
129
        $this->assertTrue($object->canEdit($manage));
130
        $this->assertFalse($object->canEdit($visitor));
131
    }
132
133
    /**
134
     * Tests canDelete()
135
     */
136
    public function testCanDelete()
137
    {
138
        /** @var Job $object */
139
        $object = Injector::inst()->create(Job::class);
140
141
        $admin = $this->objFromFixture(Member::class, 'Admin');
142
        $manage = $this->objFromFixture(Member::class, 'Manager');
143
        $visitor = $this->objFromFixture(Member::class, 'Visitor');
144
145
        $this->assertTrue($object->canDelete($admin));
146
        $this->assertTrue($object->canDelete($manage));
147
        $this->assertFalse($object->canDelete($visitor));
148
    }
149
150
    /**
151
     * Tests canView()
152
     */
153
    public function testCanView()
154
    {
155
        /** @var Job $object */
156
        $object = Injector::inst()->create(Job::class);
157
        $manage = $this->objFromFixture(Member::class, 'Manager');
158
        $this->assertTrue($object->canView($manage));
159
    }
160
}
161