Passed
Push — master ( 05e846...9de07a )
by Jason
02:44
created

JobTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 153
rs 10
c 0
b 0
f 0
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanView() 0 5 1
A testCanDelete() 0 16 1
A testGetApplicationLink() 0 18 1
A testProvidePermissions() 0 10 1
A testGetCMSFields() 0 6 1
A testGetApplyButton() 0 5 1
A testCanEdit() 0 16 1
A testCanCreate() 0 16 1
A testPopulateDefaults() 0 6 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);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
79
        // print_r($object->parent()->Application()->ID);
0 ignored issues
show
Unused Code Comprehensibility introduced by
69% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
81
        // $this->assertEquals($file->URL, $object->getApplicationLink());
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
82
    }
83
84
    /**
85
     * Tests providePermissions()
86
     */
87
    public function testProvidePermissions()
88
    {
89
        /** @var Job $object */
90
        $object = Injector::inst()->create(Job::class);
91
        $perms = [
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
        $create = $this->objFromFixture(Member::class, 'Create');
109
        $edit = $this->objFromFixture(Member::class, 'Edit');
110
        $delete = $this->objFromFixture(Member::class, 'Delete');
111
        $visitor = $this->objFromFixture(Member::class, 'Visitor');
112
113
        $this->assertTrue($object->canCreate($admin));
114
        $this->assertTrue($object->canCreate($create));
115
        $this->assertFalse($object->canCreate($edit));
116
        $this->assertFalse($object->canCreate($delete));
117
        $this->assertFalse($object->canCreate($visitor));
118
    }
119
120
    /**
121
     * Tests canEdit()
122
     */
123
    public function testCanEdit()
124
    {
125
        /** @var Job $object */
126
        $object = Injector::inst()->create(Job::class);
127
128
        $admin = $this->objFromFixture(Member::class, 'Admin');
129
        $create = $this->objFromFixture(Member::class, 'Create');
130
        $edit = $this->objFromFixture(Member::class, 'Edit');
131
        $delete = $this->objFromFixture(Member::class, 'Delete');
132
        $visitor = $this->objFromFixture(Member::class, 'Visitor');
133
134
        $this->assertTrue($object->canEdit($admin));
135
        $this->assertFalse($object->canEdit($create));
136
        $this->assertTrue($object->canEdit($edit));
137
        $this->assertFalse($object->canEdit($delete));
138
        $this->assertFalse($object->canEdit($visitor));
139
    }
140
141
    /**
142
     * Tests canDelete()
143
     */
144
    public function testCanDelete()
145
    {
146
        /** @var Job $object */
147
        $object = Injector::inst()->create(Job::class);
148
149
        $admin = $this->objFromFixture(Member::class, 'Admin');
150
        $create = $this->objFromFixture(Member::class, 'Create');
151
        $edit = $this->objFromFixture(Member::class, 'Edit');
152
        $delete = $this->objFromFixture(Member::class, 'Delete');
153
        $visitor = $this->objFromFixture(Member::class, 'Visitor');
154
155
        $this->assertTrue($object->canDelete($admin));
156
        $this->assertFalse($object->canDelete($create));
157
        $this->assertFalse($object->canDelete($edit));
158
        $this->assertTrue($object->canDelete($delete));
159
        $this->assertFalse($object->canDelete($visitor));
160
    }
161
162
    /**
163
     * Tests canView()
164
     */
165
    public function testCanView()
166
    {
167
        /** @var Job $object */
168
        $object = Injector::inst()->create(Job::class);
169
        $this->assertTrue($object->canView());
170
    }
171
}
172