1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Controllers;
7
8use App\Models\Contest;
9use App\Models\User;
10use Carbon\Carbon;
11use Illuminate\Http\Testing\File;
12use Tests\TestCase;
13
14class ContestEntriesControllerTest extends TestCase
15{
16 private Contest $contest;
17 private User $user;
18
19 public function testStoreEntryWithWrongFileExtension()
20 {
21 $this->prepareForStore();
22 $file = File::fake()->create('entry.osz');
23
24 $this
25 ->actingAsVerified($this->user)
26 ->postJson(route('contest-entries.store'), ['entry' => $file, 'contest_id' => $this->contest->id])
27 ->assertStatus(422)
28 ->assertJson(['error' => 'Files for this contest must have one of the following extensions: jpg, jpeg, png']);
29 }
30
31 public function testStoreEntryExceedingMaxFileSize()
32 {
33 $this->prepareForStore();
34 $file = File::fake()->create('test.png', 8 * 1024 * 1024 * 2);
35
36 $this
37 ->actingAsVerified($this->user)
38 ->postJson(route('contest-entries.store'), ['entry' => $file, 'contest_id' => $this->contest->id])
39 ->assertStatus(413)
40 ->assertJson(['error' => 'File exceeds max size']);
41 }
42
43 public function testStoreEntryWithWrongForcedDimensions()
44 {
45 $this->prepareForStore();
46 $file = File::image('entry.png', 1600, 900);
47
48 $this
49 ->actingAsVerified($this->user)
50 ->postJson(route('contest-entries.store'), ['entry' => $file, 'contest_id' => $this->contest->id])
51 ->assertStatus(422)
52 ->assertJson(['error' => 'Images for this contest must be 1920x1080']);
53 }
54
55 public function testStoreEntryWithCorrectRequirements()
56 {
57 $this->prepareForStore();
58 $file = File::image('test.png', 1920, 1080);
59
60 $this
61 ->actingAsVerified($this->user)
62 ->postJson(route('contest-entries.store'), ['entry' => $file, 'contest_id' => $this->contest->id])
63 ->assertStatus(200);
64 }
65
66 private function prepareForStore()
67 {
68 $this->user = User::factory()->create();
69 $this->contest = Contest::create([
70 'name' => 'Test',
71 'description_enter' => 'This is just a test!',
72 'description_voting' => 'This is just a test!',
73 'entry_starts_at' => Carbon::now(),
74 'entry_ends_at' => Carbon::now()->addDays(),
75 'extra_options' => ['forced_width' => 1920, 'forced_height' => 1080],
76 'header_url' => 'https://assets.ppy.sh/contests/154/header.jpg',
77 'max_entries' => 1,
78 'max_votes' => 1,
79 'show_votes' => true,
80 'type' => 'art',
81 'visible' => true,
82 'voting_ends_at' => Carbon::now()->addDays(2),
83 'voting_starts_at' => Carbon::now()->addDays(3),
84 ]);
85 }
86}