Passed
Push — master ( 2c3721...94fb22 )
by Grant
09:10 queued 10s
created

JobControllerTest::testManagerCreate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 59
rs 9.2
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Tests\Unit;
4
5
use Tests\TestCase;
6
use Illuminate\Foundation\Testing\WithFaker;
7
use Illuminate\Foundation\Testing\RefreshDatabase;
8
use Illuminate\Support\Facades\Lang;
9
use Illuminate\Support\Facades\Mail;
10
11
use Jenssegers\Date\Date;
12
13
use App\Models\Applicant;
14
use App\Models\Lookup\Department;
15
use App\Models\JobPoster;
16
use App\Models\Lookup\LanguageRequirement;
17
use App\Models\Manager;
18
use App\Models\Lookup\Province;
19
use App\Models\Lookup\SecurityClearance;
20
use App\Mail\JobPosterReviewRequested;
21
22
class JobControllerTest extends TestCase
23
{
24
    use RefreshDatabase;
25
26
    /**
27
     * Run parent setup and provide reusable factories.
28
     *
29
     * @return void
30
     */
31
    protected function setUp() : void
32
    {
33
        parent::setUp();
34
35
        $this->faker = \Faker\Factory::create();
36
        $this->faker_fr = \Faker\Factory::create('fr_FR');
37
38
        $this->manager = factory(Manager::class)->create();
39
        $this->jobPoster = factory(JobPoster::class)
40
        ->create([
41
            'manager_id' => $this->manager->id
42
        ]);
43
44
        $this->otherManager = factory(Manager::class)->create();
45
        $this->otherJobPoster = factory(JobPoster::class)
46
        ->create([
47
            'manager_id' => $this->otherManager->id
48
        ]);
49
50
        $this->publishedJob = factory(JobPoster::class)->states('published')->create();
51
    }
52
53
    /**
54
     * Generate an array with all the data that would be submitted through a completed edit/create job form.
55
     *
56
     * @return array
57
     */
58
    private function generateEditJobFormData() : array
59
    {
60
        $jobForm = [
61
        'term_qty' => $this->faker->numberBetween(1, 4),
62
        'salary_min' => $this->faker->numberBetween(60000, 80000),
63
        'salary_max' => $this->faker->numberBetween(80000, 100000),
64
        'noc' => $this->faker->numberBetween(1, 9999),
65
        'classification' => $this->faker->regexify('[A-Z]{2}-0[1-5]'),
66
        'manager_id' => $this->manager->id,
67
        'published' => false,
68
        'remote_work_allowed' => $this->faker->boolean(50),
69
        'open_date' => $this->faker->date('Y-m-d', strtotime('+1 day')),
70
        'open_time' => $this->faker->time(),
71
        'close_date' => $this->faker->date('Y-m-d', strtotime('+2 weeks')),
72
        'close_time' => $this->faker->time(),
73
        'start_date_time' => $this->faker->date('Y-m-d', strtotime('+2 weeks')) . ' ' . $this->faker->time(),
74
        'security_clearance' => SecurityClearance::inRandomOrder()->first()->id,
75
        'language_requirement' => LanguageRequirement::inRandomOrder()->first()->id,
76
        'department' => Department::inRandomOrder()->first()->id,
77
        'province' => Province::inRandomOrder()->first()->id,
78
        'city' => $this->faker->city,
79
        'title' => [
80
            'en' => $this->faker->word,
81
            'fr' => $this->faker_fr->word
82
        ],
83
        'impact' => [
84
            'en' => $this->faker->paragraphs(
85
                2,
86
                true
87
            ),
88
            'fr' => $this->faker_fr->paragraphs(
89
                2,
90
                true
91
            )
92
        ],
93
        'branch' => [
94
            'en' => $this->faker->word,
95
            'fr' => $this->faker_fr->word
96
        ],
97
        'division' => [
98
            'en' => $this->faker->word,
99
            'fr' => $this->faker_fr->word
100
        ],
101
        'education' => [
102
            'en' => $this->faker->sentence(),
103
            'fr' => $this->faker_fr->sentence()
104
        ],
105
        'submit' => '',
106
        ];
107
        return $jobForm;
108
    }
109
110
    /**
111
     * Ensure an unauthorized user receives the correct job poster view.
112
     *
113
     * @return void
114
     */
115
    public function testGuestSingleView() : void
116
    {
117
        $response = $this->get('jobs/' . $this->publishedJob->id);
118
        $response->assertStatus(200);
119
        $response->assertSee(e(Lang::get('applicant/job_post')['apply']['login_link_title']));
120
    }
121
122
    /**
123
     * Ensure an authorized applicant receives the correct job poster view.
124
     *
125
     * @return void
126
     */
127
    public function testApplicantSingleView() : void
128
    {
129
        $applicant = factory(Applicant::class)->create();
130
131
        $response = $this->actingAs($applicant->user)
132
        ->get('jobs/' . $this->publishedJob->id);
133
        $response->assertStatus(200);
134
        $response->assertSee(e(Lang::get('applicant/job_post')['apply']['apply_link_title']));
135
    }
136
137
    /**
138
     * Ensure a manager can view their index page.
139
     *
140
     * @return void
141
     */
142
    public function testManagerIndexView() : void
143
    {
144
        $response = $this->actingAs($this->manager->user)
145
        ->get('manager/jobs');
146
        $response->assertStatus(200);
147
148
        $response->assertSee(e($this->jobPoster->title));
149
        $response->assertDontSeeText(e($this->otherJobPoster->title));
150
    }
151
152
    /**
153
     * Ensure a Job Poster can be submitted for review.
154
     *
155
     * @return void
156
     */
157
    public function testSubmitForReview() : void
158
    {
159
        Mail::fake();
160
161
        $jobPoster = $this->jobPoster;
162
163
        $response = $this->followingRedirects()
164
        ->actingAs($this->manager->user)
165
        ->post("manager/jobs/$jobPoster->id/review");
166
167
        $response->assertStatus(200);
168
169
        $jobPoster->refresh();
170
171
        $this->assertInstanceOf(Date::class, $jobPoster->review_requested_at);
172
173
        Mail::assertSent(JobPosterReviewRequested::class, function ($mail) use ($jobPoster) {
174
            return $mail->jobPoster->id === $jobPoster->id;
175
        });
176
    }
177
178
    /**
179
     * Ensure a manager can view the create Job Poster form.
180
     *
181
     * @return void
182
     */
183
    public function testManagerCreateView() : void
184
    {
185
        $response = $this->actingAs($this->manager->user)
186
        ->get('manager/jobs/create');
187
        $response->assertStatus(200);
188
189
        $response->assertSee(e(Lang::get('manager/job_create')['title']));
190
        $response->assertViewIs('manager.job_create');
191
192
        $response->assertSee(e(Lang::get('manager/job_create', [], 'en')['questions']['00']));
193
        $response->assertSee(e(Lang::get('manager/job_create', [], 'fr')['questions']['00']));
194
    }
195
196
    /**
197
     * Ensure a manager can create a Job Poster.
198
     *
199
     * @return void
200
     */
201
    public function testManagerCreate() : void
202
    {
203
        $newJob = $this->generateEditJobFormData();
204
205
        $dbValues = array_slice($newJob, 0, 8);
206
207
        $response = $this->followingRedirects()
208
        ->actingAs($this->manager->user)
209
        ->post('manager/jobs/', $newJob);
210
        $response->assertStatus(200);
211
        $response->assertViewIs('applicant.job_post');
212
        $this->assertDatabaseHas('job_posters', $dbValues);
213
        $response->assertSee(e(Lang::get('applicant/job_post')['apply']['edit_link_title']));
214
    }
215
216
    /**
217
     * Ensure a manager can edit an unpublished Job Poster they created.
218
     *
219
     * @return void
220
     */
221
    public function testManagerEditView() : void
222
    {
223
        $response = $this->actingAs($this->manager->user)
224
        ->get('manager/jobs/' . $this->jobPoster->id . '/edit');
225
226
        $response->assertStatus(200);
227
        $response->assertViewIs('manager.job_create');
228
        // Check for a handful of properties
229
        $response->assertSee(e($this->jobPoster->city));
230
        $response->assertSee(e($this->jobPoster->education));
231
        $response->assertSee(e($this->jobPoster->title));
232
        $response->assertSee(e($this->jobPoster->impact));
233
        $response->assertSee(e($this->jobPoster->branch));
234
        $response->assertSee(e($this->jobPoster->division));
235
        $response->assertSee(e($this->jobPoster->education));
236
    }
237
238
    /**
239
     * Ensure a manager cannot edit a published Job Poster they created.
240
     *
241
     * @return void
242
     */
243
    public function testManagerCanNotEditViewPublished() : void
244
    {
245
        $response = $this->actingAs($this->manager->user)
246
        ->get('manager/jobs/' . $this->publishedJob->id . '/edit');
247
248
        $response->assertStatus(500);
249
    }
250
251
    /**
252
     * Adding a 'published' field to the create job form data should affect the created job.
253
     *
254
     * @return void
255
     */
256
    public function testManagerCannotPublishJobThroughEditView() : void
257
    {
258
        $newJob = $this->generateEditJobFormData();
259
        $newJob['published'] = true;
260
261
        $dbValues = array_slice($newJob, 0, 8);
262
        $dbValues['published'] = false;
263
264
        $response = $this->followingRedirects()
265
        ->actingAs($this->manager->user)
266
        ->post('manager/jobs/', $newJob);
267
        $response->assertStatus(200);
268
        $this->assertDatabaseHas('job_posters', $dbValues);
269
    }
270
}
271