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;
7
8use App\Models\Beatmap;
9use App\Models\User;
10
11class RouteTest extends TestCase
12{
13 /**
14 * Test the homepage don't error.
15 *
16 * @return void
17 */
18 public function testHomeRoutes()
19 {
20 $this->assertGetRoutes(['/']);
21 }
22
23 /**
24 * Test the download page doesn't error.
25 *
26 * @return void
27 */
28 public function testDownloadRoutes()
29 {
30 $this->assertGetRoutes(['/home/download']);
31 }
32
33 /**
34 * Test the changelog page doesn't error.
35 *
36 * @return void
37 */
38 public function testChangelogRoutes()
39 {
40 $this->assertGetRoutes(['/home/changelog']);
41 }
42
43 /**
44 * Test the help page doesnt error.
45 *
46 * @return void
47 */
48 public function testWikiRoutes()
49 {
50 $this->assertGetRoutes(['/wiki']);
51 }
52
53 /**
54 * Test the ranking pages don't error.
55 *
56 * @return void
57 */
58 public function testRankingRoutes()
59 {
60 $rankingTypes = ['performance', 'score', 'country'];
61
62 foreach (Beatmap::MODES as $mode => $enum) {
63 foreach ($rankingTypes as $type) {
64 $this->assertGetRoutes(["/rankings/{$mode}/{$type}"]);
65 }
66 }
67 }
68
69 /**
70 * Test the redirects for the ranking pages.
71 *
72 * @return void
73 */
74 public function testRankingRedirects()
75 {
76 foreach (Beatmap::MODES as $mode => $enum) {
77 $this->assertRedirect(["/rankings/{$mode}"]);
78 }
79
80 $this->assertRedirect(['/rankings/']);
81 }
82
83 /**
84 * Test the profile page doesn't error.
85 *
86 * @return void
87 */
88 public function testProfileRoutes()
89 {
90 $user = User::factory()->create();
91
92 $this->assertGetRoutes(["/u/{$user->getKey()}", "/u/{$user->username}"]);
93 }
94
95 /**
96 * Test a given set of GET routes.
97 *
98 * @return void
99 */
100 protected function assertGetRoutes(array $routes = [])
101 {
102 $this->assertCustomRoute($routes, 'GET');
103 }
104
105 /**
106 * Test the download page doesn't error.
107 *
108 * @return void
109 */
110 protected function assertCustomRoute(array $routes, $method)
111 {
112 foreach ($routes as $route) {
113 $response = $this->call($method, $route);
114 $this->assertTrue($response->isOK() || $response->isRedirect());
115 }
116 }
117
118 /**
119 * Asserts that the given routes perform redirects.
120 *
121 * @return void
122 */
123 protected function assertRedirect(array $routes, $method = 'GET')
124 {
125 foreach ($routes as $route) {
126 $response = $this->call($method, $route);
127 $this->assertTrue($response->isRedirect());
128 }
129 }
130}