Skip to content

Commit 73b6491

Browse files
authored
Merge pull request #11821 from notbakaneko/feature/username-cost-change-simpler
Simpler username cost reduction
2 parents 10f260e + 4aaf280 commit 73b6491

File tree

2 files changed

+72
-51
lines changed

2 files changed

+72
-51
lines changed

app/Models/User.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,15 @@ public function getAuthPassword()
317317

318318
public function usernameChangeCost()
319319
{
320-
$tier = min($this->usernameChangeHistory()->paid()->count(), 5);
321-
322-
if ($tier > 1) {
323-
$lastChange = $this->usernameChangeHistory()->paid()->last()?->timestamp;
324-
if ($lastChange !== null) {
325-
$tier = max($tier - $lastChange->diffInYears(Carbon::now(), false), 1);
326-
}
327-
}
320+
$minTier = $this->usernameChangeHistory()->paid()->exists() ? 1 : 0;
321+
322+
$tier = max(
323+
$this->usernameChangeHistory()
324+
->paid()
325+
->where('timestamp', '>', Carbon::now()->subYears(3))
326+
->count(),
327+
$minTier,
328+
);
328329

329330
return match ($tier) {
330331
0 => 0,

tests/Models/UserTest.php

+63-43
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,6 @@ public static function dataProviderForUsernameChangeCost()
4242
];
4343
}
4444

45-
public static function dataProviderForUsernameChangeCostLastChange()
46-
{
47-
// assume there are 6 changes (max tier + 1)
48-
return [
49-
[0, 100],
50-
[1, 64],
51-
[2, 32],
52-
[3, 16],
53-
[4, 8],
54-
[5, 8],
55-
[6, 8],
56-
[10, 8],
57-
];
58-
}
59-
6045
public static function dataProviderForUsernameChangeCostType()
6146
{
6247
return [
@@ -68,14 +53,16 @@ public static function dataProviderForUsernameChangeCostType()
6853
];
6954
}
7055

71-
public static function dataProviderForUsernameChangeCostTypeLastChange()
56+
public static function dataProviderForUsernameChangeCostWindow()
7257
{
58+
// years with no name changes, cost
59+
// test is setup with name change every 6 months.
7360
return [
74-
['admin', 8],
75-
['inactive', 8],
76-
['paid', 32],
77-
['revert', 8],
78-
['support', 32],
61+
[0, 100],
62+
[1, 32],
63+
[2, 8],
64+
[3, 8],
65+
[4, 8],
7966
];
8067
}
8168

@@ -185,20 +172,50 @@ public function testUsernameChangeCost(int $changes, int $cost)
185172
$this->assertSame($cost, $user->usernameChangeCost());
186173
}
187174

188-
/**
189-
* @dataProvider dataProviderForUsernameChangeCostLastChange
190-
*/
191-
public function testUsernameChangeCostLastChange(int $years, int $cost)
175+
public function testUsernameChangeCostMultiple()
192176
{
193-
$this->travelTo(CarbonImmutable::now()->subYears($years));
177+
$user = User::factory()->create();
194178

195-
$user = User::factory()
196-
->has(UsernameChangeHistory::factory()->count(6)) // 6 = max tier + 1
197-
->create();
179+
$this->assertSame(0, $user->usernameChangeCost());
180+
181+
$user->usernameChangeHistory()->create([
182+
'timestamp' => CarbonImmutable::now(),
183+
'type' => 'paid',
184+
'username' => 'marty',
185+
]);
198186

199-
$this->travelBack();
187+
// 1 change in last 3 years
188+
$this->travelTo(CarbonImmutable::now()->addYears(3));
189+
$this->assertSame(8, $user->usernameChangeCost());
200190

201-
$this->assertSame($cost, $user->usernameChangeCost());
191+
// 0 changes in last 3 years
192+
$this->travelTo(CarbonImmutable::now()->addYears(1));
193+
$this->assertSame(8, $user->usernameChangeCost());
194+
195+
$user->usernameChangeHistory()->create([
196+
'timestamp' => CarbonImmutable::now(),
197+
'type' => 'paid',
198+
'username' => 'mcfly',
199+
]);
200+
201+
// 1 change in last 3 years
202+
$this->assertSame(8, $user->usernameChangeCost());
203+
204+
$user->usernameChangeHistory()->create([
205+
'timestamp' => CarbonImmutable::now(),
206+
'type' => 'paid',
207+
'username' => 'futuremarty',
208+
]);
209+
210+
// 2 changes in last 3 years
211+
$this->assertSame(16, $user->usernameChangeCost());
212+
213+
// 1 changes in last 3 years
214+
$this->travelTo(CarbonImmutable::now()->addYears(3));
215+
$this->assertSame(8, $user->usernameChangeCost());
216+
// 0 changes in last 3 years
217+
$this->travelTo(CarbonImmutable::now()->addYears(1));
218+
$this->assertSame(8, $user->usernameChangeCost());
202219
}
203220

204221
/**
@@ -214,22 +231,25 @@ public function testUsernameChangeCostType(string $type, int $cost)
214231
}
215232

216233
/**
217-
* This tests the correct last UsernameChangeHistory is used when applying the cost changes.
218-
*
219-
* @dataProvider dataProviderForUsernameChangeCostTypeLastChange
234+
* @dataProvider dataProviderForUsernameChangeCostWindow
220235
*/
221-
public function testUsernameChangeCostTypeLastChange(string $type, int $cost)
236+
public function testUsernameChangeCostWindow(int $years, int $cost)
222237
{
223-
$this->travelTo(CarbonImmutable::now()->subYears(1));
224-
225-
$user = User::factory()
226-
->has(UsernameChangeHistory::factory()->count(2))
227-
->create();
238+
$now = CarbonImmutable::now();
239+
$this->travelTo(CarbonImmutable::now()->subYears(3));
228240

229-
$this->travelBack();
230-
231-
UsernameChangeHistory::factory()->state(['type' => $type, 'user_id' => $user])->create();
241+
$user = User::factory()->create();
242+
while (CarbonImmutable::now()->isBefore($now)) {
243+
$user->usernameChangeHistory()->create([
244+
'timestamp' => CarbonImmutable::now(),
245+
'type' => 'paid',
246+
'username' => 'marty',
247+
]);
248+
249+
$this->travelTo(CarbonImmutable::now()->addMonths(6));
250+
}
232251

252+
$this->travelTo($now->addYears($years));
233253
$this->assertSame($cost, $user->usernameChangeCost());
234254
}
235255

0 commit comments

Comments
 (0)