Skip to content

Commit

Permalink
Merge pull request ppy#10771 from nanaya/static-provider
Browse files Browse the repository at this point in the history
Change data provider to static method
  • Loading branch information
notbakaneko authored Nov 30, 2023
2 parents e198386 + c259dc0 commit 57c0c30
Show file tree
Hide file tree
Showing 83 changed files with 241 additions and 230 deletions.
23 changes: 23 additions & 0 deletions app/Libraries/Base64Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace App\Libraries;

class Base64Url
{
public static function decode(string $value): ?string
{
return null_if_false(base64_decode(strtr($value, '-_', '+/'), true));
}

public static function encode(string $value): string
{
// url safe base64
// reference: https://datatracker.ietf.org/doc/html/rfc4648#section-5
return rtrim(strtr(base64_encode($value), '+/', '-_'), '=');
}
}
4 changes: 2 additions & 2 deletions app/Libraries/SignedRandomString.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public static function create(int $randomSize): string
$key = random_bytes($randomSize);
$hmac = static::hmac($key);

return base64url_encode($hmac.$key);
return Base64Url::encode($hmac.$key);
}

public static function isValid(string $input): bool
{
$bin = base64url_decode($input);
$bin = Base64Url::decode($input);
if ($bin === null) {
return false;
}
Expand Down
17 changes: 3 additions & 14 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

use App\Libraries\Base64Url;
use App\Libraries\LocaleMeta;
use App\Models\LoginAttempt;
use Egulias\EmailValidator\EmailValidator;
Expand Down Expand Up @@ -60,18 +61,6 @@ function background_image($url, $proxy = true)
return sprintf(' style="background-image:url(\'%s\');" ', e($url));
}

function base64url_decode(string $value): ?string
{
return null_if_false(base64_decode(strtr($value, '-_', '+/'), true));
}

function base64url_encode(string $value): string
{
// url safe base64
// reference: https://datatracker.ietf.org/doc/html/rfc4648#section-5
return rtrim(strtr(base64_encode($value), '+/', '-_'), '=');
}

function beatmap_timestamp_format($ms)
{
$s = $ms / 1000;
Expand Down Expand Up @@ -310,7 +299,7 @@ function current_locale_meta(): LocaleMeta
function cursor_decode($cursorString): ?array
{
if (is_string($cursorString) && present($cursorString)) {
$cursor = json_decode(base64_decode(strtr($cursorString, '-_', '+/'), true), true);
$cursor = json_decode(Base64Url::decode($cursorString) ?? '', true);

if (is_array($cursor)) {
return $cursor;
Expand All @@ -324,7 +313,7 @@ function cursor_encode(?array $cursor): ?string
{
return $cursor === null
? null
: base64url_encode(json_encode($cursor));
: Base64Url::encode(json_encode($cursor));
}

function cursor_for_response(?array $cursor): array
Expand Down
28 changes: 13 additions & 15 deletions phpunit.dusk.xml
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
>
<extensions>
<extension class="Tests\SeederExtension" />
<extension class="Tests\SeederExtension"/>
</extensions>
<testsuites>
<testsuite name="Browser Test Suite">
<directory suffix="Test.php">./tests/Browser</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
</phpunit>
2 changes: 1 addition & 1 deletion tests/BeatmapsetDisqualifyNotificationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function testNotificationNotSentIfNotificationOptionsNotEnabled()
}
#endregion

public function booleanDataProvider()
public static function booleanDataProvider()
{
return [
[true],
Expand Down
11 changes: 4 additions & 7 deletions tests/BroadcastNotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,9 @@ public function testSendNotificationWithOptions($details)
}
}

public function notificationJobClassesDataProvider()
public static function notificationJobClassesDataProvider()
{
$this->refreshApplication();

$path = app()->path('Jobs/Notifications');
$files = Finder::create()->files()->in($path)->sortByName();
$files = Finder::create()->files()->in(__DIR__.'/../app/Jobs/Notifications')->sortByName();
foreach ($files as $file) {
$baseName = $file->getBasename(".{$file->getExtension()}");
$classes[] = ["\\App\\Jobs\\Notifications\\{$baseName}"];
Expand All @@ -116,7 +113,7 @@ public function notificationJobClassesDataProvider()
return $classes;
}

public function notificationNamesDataProvider()
public static function notificationNamesDataProvider()
{
// TODO: move notification names to different class instead of filtering
$constants = collect((new ReflectionClass(Notification::class))->getReflectionConstants())
Expand All @@ -129,7 +126,7 @@ public function notificationNamesDataProvider()
return $constants->map(fn (ReflectionClassConstant $constant) => [$constant->getValue()])->all();
}

public function userNotificationDetailsDataProvider()
public static function userNotificationDetailsDataProvider()
{
return [
[null], // for testing defaults.
Expand Down
8 changes: 4 additions & 4 deletions tests/Browser/SanityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static function cleanup()
return;
}

(new static())->createApplication();
static::createApp();

// Tear down in reverse-order so that dependants get destroyed before their dependencies.
$nukingOrder = array_reverse(self::$scaffolding);
Expand Down Expand Up @@ -118,7 +118,7 @@ private static function createScaffolding()
return;
}

(new static())->createApplication();
static::createApp();
self::$scaffolding['country'] = Country::first() ?? Country::factory()->create();
// user to login as and to use for requests
self::$scaffolding['user'] = User::factory()->create([
Expand Down Expand Up @@ -308,7 +308,7 @@ private static function output($text)
}
}

public function routesDataProvider()
public static function routesDataProvider()
{
static $bypass = [
'__clockwork',
Expand All @@ -321,7 +321,7 @@ public function routesDataProvider()
];
static $types = ['user', 'guest'];

$this->refreshApplication();
static::createApp();
$data = [];

foreach (app()->routes->get('GET') as $uri => $route) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Commands/EsIndexScoresQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testQueueScores(callable $setUp, array|callable $params, int $ch
);
}

public function dataProviderForTestParameterValidity(): array
public static function dataProviderForTestParameterValidity(): array
{
return [
[[], false],
Expand All @@ -83,7 +83,7 @@ public function dataProviderForTestParameterValidity(): array
];
}

public function dataProviderForTestQueueScores(): array
public static function dataProviderForTestQueueScores(): array
{
$userId = 0;
$setUp = function () use ($userId) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Commands/ModdingRankCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function testRankQuotaSeparateRuleset(): void
}


public function rankDataProvider()
public static function rankDataProvider()
{
// 1 day ago isn't used because it might or might not be equal to the cutoff depending on how fast it runs.
return [
Expand All @@ -113,7 +113,7 @@ public function rankDataProvider()
];
}

public function rankHybridDataProvider()
public static function rankHybridDataProvider()
{
return [
// hybrid counts as ruleset with lowest enum value
Expand Down
2 changes: 1 addition & 1 deletion tests/Controllers/AccountControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function testUpdatePasswordWeakPassword()
->assertStatus(422);
}

public function dataProviderForUpdateCountry(): array
public static function dataProviderForUpdateCountry(): array
{
return [
['_A', '_A', true],
Expand Down
8 changes: 4 additions & 4 deletions tests/Controllers/BeatmapDiscussionsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function testPostReviewDocumentValidWithIssues()
$this->assertSame($discussionPostCount + 3, BeatmapDiscussionPost::count());
}

public function putVoteDataProvider()
public static function putVoteDataProvider()
{
return [
['graveyard', 403, 0],
Expand All @@ -211,23 +211,23 @@ public function putVoteDataProvider()
];
}

public function putVoteAgainDataProvider()
public static function putVoteAgainDataProvider()
{
return [
'voting again has no effect' => ['1', 0],
'voting 0 will remove the vote' => ['0', -1],
];
}

public function putVoteChangeToDownDataProvider()
public static function putVoteChangeToDownDataProvider()
{
return [
'bng can change to down vote' => ['bng', 200, -2],
'regular user cannot change to down vote' => [null, 403, 0],
];
}

public function putVoteDownDataProvider()
public static function putVoteDownDataProvider()
{
return [
'bng can down vote' => ['bng', 200, 1, -1],
Expand Down
2 changes: 1 addition & 1 deletion tests/Controllers/BeatmapsControllerSoloScoresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function testQuery(array $scoreKeys, array $params)
}
}

public function dataProviderForTestQuery(): array
public static function dataProviderForTestQuery(): array
{
return [
'no parameters' => [[
Expand Down
4 changes: 2 additions & 2 deletions tests/Controllers/BeatmapsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ public function testUpdateOwnerSameOwner(): void
$this->assertSame($beatmapsetEventCount, BeatmapsetEvent::count());
}

public function dataProviderForTestLookupForApi(): array
public static function dataProviderForTestLookupForApi(): array
{
return [
'checksum' => ['checksum', fn (Beatmap $b) => $b->checksum],
Expand All @@ -603,7 +603,7 @@ public function dataProviderForTestLookupForApi(): array
];
}

public function dataProviderForTestUpdateOwnerLoved(): array
public static function dataProviderForTestUpdateOwnerLoved(): array
{
return [
[Beatmapset::STATES['graveyard'], true],
Expand Down
8 changes: 4 additions & 4 deletions tests/Controllers/BeatmapsetsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,14 @@ public function testBeatmapsetUpdateTags(string $userGroupOrOwner, bool $ok): vo
$this->assertSame($expectedTags, $beatmapset->fresh()->tags);
}

public function beatmapsetStatesDataProvider()
public static function beatmapsetStatesDataProvider()
{
return array_map(function ($state) {
return [$state];
}, array_keys(Beatmapset::STATES));
}

public function dataProviderForTestBeatmapsetUpdateOffset(): array
public static function dataProviderForTestBeatmapsetUpdateOffset(): array
{
return [
['admin', true],
Expand All @@ -349,7 +349,7 @@ public function dataProviderForTestBeatmapsetUpdateOffset(): array
];
}

public function dataProviderForTestBeatmapsetUpdateTags(): array
public static function dataProviderForTestBeatmapsetUpdateTags(): array
{
return [
['admin', true],
Expand All @@ -361,7 +361,7 @@ public function dataProviderForTestBeatmapsetUpdateTags(): array
];
}

public function dataProviderForTestBeatmapsetUpdateDescriptionAsOwner(): array
public static function dataProviderForTestBeatmapsetUpdateDescriptionAsOwner(): array
{
return [
[false, null, true],
Expand Down
2 changes: 1 addition & 1 deletion tests/Controllers/Chat/ChannelsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public function testChannelLeavePublicWhenGuest() // fail

//endregion

public function dataProvider()
public static function dataProvider()
{
return [
['private', false],
Expand Down
6 changes: 3 additions & 3 deletions tests/Controllers/Chat/ChatControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public function testChatUpdatesJoinChannel()

//endregion

public function createPmWithAuthorizedGrantDataProvider()
public static function createPmWithAuthorizedGrantDataProvider()
{
return [
[['*'], 200],
Expand All @@ -297,15 +297,15 @@ public function createPmWithAuthorizedGrantDataProvider()
];
}

public function createPmWithClientCredentialsDataProvider()
public static function createPmWithClientCredentialsDataProvider()
{
return [
// TODO: need to add test that validates auth guard calls Token::validate
[['public'], 403],
];
}

public function createPmWithClientCredentialsBotGroupDataProvider()
public static function createPmWithClientCredentialsBotGroupDataProvider()
{
return [
[['chat.write', 'delegate'], 200],
Expand Down
4 changes: 2 additions & 2 deletions tests/Controllers/CommentsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public function testApiRequiresAuthentication($method, $routeName)
->assertUnauthorized();
}

public function apiRequiresAuthenticationDataProvider()
public static function apiRequiresAuthenticationDataProvider()
{
return [
['DELETE', 'comments.vote'],
Expand All @@ -270,7 +270,7 @@ public function apiRequiresAuthenticationDataProvider()
* - Whether the commentable already has a pinned comment
* - Whether pinning should be allowed
*/
public function pinPermissionsDataProvider(): array
public static function pinPermissionsDataProvider(): array
{
return [
['admin', true, true, true, true, true],
Expand Down
Loading

0 comments on commit 57c0c30

Please sign in to comment.