Files
ops-Gazelle/tests/phpunit/BonusTest.php
2025-08-27 16:44:44 +02:00

323 lines
13 KiB
PHP

<?php
namespace Gazelle;
use PHPUnit\Framework\TestCase;
use GazelleUnitTest\Helper;
use Gazelle\Enum\UserStatus;
class BonusTest extends TestCase {
protected array $userList;
protected array $tgroupList;
public function tearDown(): void {
if (isset($this->tgroupList)) {
foreach ($this->tgroupList as $tgroup) {
Helper::removeTGroup($tgroup, current($this->userList));
}
}
if (isset($this->userList)) {
foreach ($this->userList as $user) {
$user->remove();
}
}
}
public function testBonus(): void {
$this->userList['giver'] = Helper::makeUser('bonusg.' . randomString(6), 'bonus', true);
$this->userList['receiver'] = Helper::makeUser('bonusr.' . randomString(6), 'bonus', true);
$startingPoints = 10000;
$giver = new User\Bonus($this->userList['giver']);
$this->assertEquals(0.0, $giver->hourlyRate(), 'bonus-per-hour');
$this->assertEquals(0, $giver->user()->bonusPointsTotal(), 'bonus-points-initial');
$this->assertEquals(0, $giver->user()->tokenCount(), 'bonus-fltokens-intial');
$this->assertCount(0, $giver->history(10, 0), 'bonus-history-initial');
$this->assertEquals(['nr' => 0, 'total' => 0], $giver->summary(), 'bonus-summary-initial');
$this->assertEquals(
[
'total_torrents' => 0,
'total_size' => 0,
'hourly_points' => 0.0,
'daily_points' => 0.0,
'weekly_points' => 0.0,
'monthly_points' => 0.0,
'yearly_points' => 0.0,
'points_per_gb' => 0.0,
],
$giver->userTotals(),
'bonus-accrual-initial'
);
$giver->setPoints($startingPoints);
$this->assertEquals($startingPoints, $giver->user()->bonusPointsTotal(), 'bonus-set-points');
$manager = new Manager\Bonus();
$manager->flushPriceCache();
$itemList = $manager->itemList();
$this->assertArrayHasKey('token-1', $itemList, 'item-token-1');
$token = $giver->item('token-1');
$this->assertArrayHasKey('Price', $token, 'item-price-1');
$price = $token['Price'];
$this->assertEquals($price, $giver->effectivePrice('token-1'), 'item-price-token-1');
// buy a token
$this->assertTrue($giver->purchaseToken('token-1'), 'item-purchase-token-1');
$this->assertEquals(1, $giver->user()->tokenCount(), 'bonus-fltokens-bought');
$this->assertEquals($startingPoints - $price, $giver->user()->bonusPointsTotal(), 'bonus-spent-points');
$history = $giver->history(10, 0);
$this->assertEquals('1 Freeleech Token', $history[0]['Title'], 'bonus-history-title');
// buy a seedbox
$this->assertTrue($giver->unlockSeedbox(), 'item-purchase-seedbox');
$this->assertTrue($giver->user()->hasAttr('feature-seedbox'), 'giver-has-seedbox');
$this->assertCount(2, $giver->history(10, 0), 'bonus-history-new');
// not enough point to buy a fifty
$this->assertFalse($giver->purchaseToken('token-50'), 'item-purchase-token-50');
$giver->addPoints(
(float)($giver->item('other-1')['Price'])
+ (float)($giver->item('other-3')['Price'])
);
$this->assertEquals(
$giver->item('other-3')['Amount'],
$giver->purchaseTokenOther($this->userList['receiver'], 'other-3', 'phpunit gift'),
'item-purchase-other-50'
);
$other = $giver->otherList();
$this->assertEquals('other-1', $other[0]['Label'], 'item-all-I-can-give');
// buy file count feature
$giver->addPoints(
(float)($giver->item('file-count')['Price'])
);
$this->assertTrue($giver->purchaseFeatureFilecount(), 'item-purchase-file-count');
$this->assertTrue($giver->user()->hasAttr('feature-seedbox'), 'giver-has-file-count');
$this->assertEquals(
$giver->item('token-1')['Price'] /** @phpstan-ignore-line it is an int, get over it */
+ $giver->item('other-3')['Price']
+ $giver->item('seedbox')['Price']
+ $giver->item('file-count')['Price'],
$giver->pointsSpent(),
'bonus-points-spent'
);
$latest = $giver->otherLatest($this->userList['receiver']);
$this->assertEquals('50 Freeleech Tokens to Other', $latest['title'], 'item-given');
$giver->addPoints($giver->item('title-bb-n')['Price']);
$this->assertTrue($giver->purchaseTitle('title-bb-n', '[b]i got u[/b]'), 'item-title-no-bb');
$this->assertEquals('i got u', $giver->user()->title(), 'item-user-has-title-no-bb');
$giver->addPoints($giver->item('title-bb-y')['Price']);
$this->assertTrue($giver->purchaseTitle('title-bb-y', '[b]i got u[/b]'), 'item-title-yes-bb');
$this->assertEquals('<strong>i got u</strong>', $giver->user()->title(), 'item-user-has-title-yes-bb');
$giver->addPoints($giver->item('collage-1')['Price']);
$this->assertTrue($giver->purchaseCollage('collage-1'), 'item-purchase-collage');
$history = $giver->history(10, 0);
$this->assertCount(7, $history, 'bonus-history-final');
$this->assertEquals(
[
'nr' => 7,
'total' => $giver->item('token-1')['Price'] /** @phpstan-ignore-line */
+ $giver->item('other-3')['Price']
+ $giver->item('seedbox')['Price']
+ $giver->item('file-count')['Price']
+ $giver->item('collage-1')['Price']
+ $giver->item('title-bb-y')['Price']
+ $giver->item('title-bb-n')['Price']
],
$giver->summary(),
'bonus-summary-initial'
);
$this->assertTrue($giver->removePoints(1.125), 'bonus-taketh-away');
}
public function testBonusPool(): void {
global $Cache;
$Cache->delete_value("bonus_pool");
$manager = new Manager\Bonus();
$this->assertEquals(
[],
$manager->openPoolList(),
'bonus-open-pool',
);
}
public function testAddPoints(): void {
$this->userList = [
Helper::makeUser('bonusadd.' . randomString(6), 'bonus', enable: false),
Helper::makeUser('bonusadd.' . randomString(6), 'bonus', enable: true),
];
// back to the future
DB::DB()->prepared_query("
INSERT INTO user_last_access (user_id, last_access) values (?, ?)
", $this->userList[1]->id, date('Y-m-d H:i:s', time() + 10)
);
$manager = new Manager\Bonus();
$this->assertEquals(
1,
// but not too far
$manager->addActivePoints(23456, date('Y-m-d H:i:s', time() + 5)),
'bonus-add-active',
);
$this->assertEquals(
0,
$manager->addMultiPoints(789, []),
'bonus-add-no-multi-points',
);
$this->assertEquals(
2,
$manager->addMultiPoints(
12345,
array_map(fn ($u) => $u->id, $this->userList),
),
'bonus-add-multi-points',
);
$this->assertEquals(
0,
$manager->addUploadPoints(369, date('Y-m-d H:i:s', time() + 1)),
'bonus-add-upload-points',
);
$this->assertEquals(
12345,
$this->userList[0]->flush()->bonusPointsTotal(),
'bonus-added-user-0',
);
$this->tgroupList[] = Helper::makeTGroupMusic(
name: 'bonus add ' . randomString(10),
artistName: [[ARTIST_MAIN], ['phpunit bonus add ' . randomString(12)]],
tagName: ['hard.bop'],
user: $this->userList[1],
);
$torrent = Helper::makeTorrentMusic(
tgroup: $this->tgroupList[0],
user: $this->userList[1],
title: 'phpunit bonus add ' . randomString(10),
);
Helper::generateTorrentSeed($torrent, $this->userList[1]);
$this->assertGreaterThan(
0,
$manager->addSeedPoints(24680),
'bonus-add-seed-points',
);
$this->assertEquals(
12345 + 23456 + 24680,
$this->userList[1]->flush()->bonusPointsTotal(),
'bonus-added-user-1',
);
$this->userList[1]->toggleAttr('no-fl-gifts', true);
$this->assertGreaterThan(
0,
$manager->addGlobalPoints(7531),
'bonus-add-global-points',
);
$this->assertEquals(
12345 + 23456 + 24680,
$this->userList[1]->flush()->bonusPointsTotal(),
'bonus-added-no-global',
);
foreach ($this->userList as $u) {
new User\Bonus($u)->setPoints(0.0);
}
}
public function testUploadReward(): void {
$this->userList[] = Helper::makeUser('bonusup.' . randomString(6), 'bonus');
$this->tgroupList[] = Helper::makeTGroupMusic(
name: 'bonus ' . randomString(10),
artistName: [[ARTIST_MAIN], ['phpunit bonus ' . randomString(12)]],
tagName: ['bop'],
user: $this->userList[0],
);
$reward = new BonusUploadReward();
$torrent = Helper::makeTorrentMusic(
tgroup: $this->tgroupList[0],
user: $this->userList[0],
title: 'phpunit bonus ' . randomString(10),
media: 'Vinyl',
format: 'FLAC',
encoding: '24bit Lossless',
);
$this->assertEquals(400, $reward->reward($torrent), 'bonus-reward-perfect-flac');
$torrent = Helper::makeTorrentMusic(
tgroup: $this->tgroupList[0],
user: $this->userList[0],
title: 'phpunit bonus ' . randomString(10),
media: 'CD',
format: 'FLAC',
encoding: 'Lossless',
);
$this->assertEquals(30, $reward->reward($torrent), 'bonus-reward-flac');
$torrent = Helper::makeTorrentMusic(
tgroup: $this->tgroupList[0],
user: $this->userList[0],
title: 'phpunit bonus ' . randomString(10),
media: 'Vinyl',
format: 'MP3',
encoding: '320',
);
$this->assertEquals(30, $reward->reward($torrent), 'bonus-reward-mp3');
$this->tgroupList[] = Helper::makeTGroupEBook('phpunit ebook title');
$torrent = Helper::makeTorrentEBook(
tgroup: $this->tgroupList[1],
user: $this->userList[0],
description: 'phpunit bonus ebook ' . randomString(10),
);
// If the following test fails, look in bonus_upload_reward with:
// select * from bonus_upload_reward where id_category = 3 order by lower(valid);
// You may have to close out the current valid range with something like
// update bonus_upload_reward set valid = tstzrange(lower(valid), now()) where now() <@ valid and id_category = 3;
// insert into bonus_upload_reward (id_category, high, standard, low, valid) values (3, 10, 10, 10, tstzrange(now(), 'infinity'));
$this->assertEquals(10, $reward->reward($torrent), 'bonus-reward-other');
$this->assertGreaterThan(
0,
$reward->modifyCategory(
CATEGORY_EBOOK,
['high' => 330, 'standard' => 220, 'low' => 110],
),
'bonus-reward-modify'
);
$torrent->setField('created', date('Y-m-d H:i:s', time() + 1))->modify();
$this->assertEquals(220, $reward->reward($torrent->flush()), 'bonus-reward-new-other');
$reward->modifyCategory(
CATEGORY_EBOOK,
['high' => 10, 'standard' => 10, 'low' => 10],
);
}
public function testStats(): void {
$eco = new Stats\Economic();
$eco->flush();
$total = $eco->bonusTotal();
$stranded = $eco->bonusStrandedTotal();
$this->userList['bonus'] = Helper::makeUser('bonusstat.' . randomString(6), 'bonus', true);
$bonus = new User\Bonus($this->userList['bonus']);
$bonus->addPoints(98765);
$eco->flush();
$this->assertEquals(98765 + $total, $eco->bonusTotal(), 'bonus-total-points');
$this->assertEquals($stranded, $eco->bonusStrandedTotal(), 'bonus-total-stranded-points');
$this->userList['bonus']->setField('Enabled', UserStatus::disabled->value)->modify();
$eco->flush();
$this->assertEquals(98765 + $stranded, $eco->bonusStrandedTotal(), 'bonus-total-disabled-stranded-points');
}
}