2019-03-30 11:38:30 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Report;
|
|
|
|
use App\Models\Texture;
|
2019-03-30 12:53:40 +08:00
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2019-03-30 11:38:30 +08:00
|
|
|
|
|
|
|
class ReportControllerTest extends TestCase
|
|
|
|
{
|
2019-03-30 12:53:40 +08:00
|
|
|
use DatabaseTransactions;
|
2019-03-30 11:38:30 +08:00
|
|
|
|
|
|
|
public function testSubmit()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
$texture = factory(Texture::class)->create();
|
|
|
|
|
|
|
|
// Without `tid` field
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2019-03-30 11:38:30 +08:00
|
|
|
->postJson('/skinlib/report')
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('tid');
|
2019-03-30 11:38:30 +08:00
|
|
|
|
|
|
|
// Invalid texture
|
|
|
|
$this->postJson('/skinlib/report', ['tid' => $texture->tid - 1])
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('tid');
|
2019-03-30 11:38:30 +08:00
|
|
|
|
|
|
|
// Without `reason` field
|
|
|
|
$this->postJson('/skinlib/report', ['tid' => $texture->tid])
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('reason');
|
2019-03-30 11:38:30 +08:00
|
|
|
|
|
|
|
// Lack of score
|
|
|
|
$user->score = 0;
|
|
|
|
$user->save();
|
|
|
|
option(['reporter_score_modification' => -5]);
|
|
|
|
$this->postJson('/skinlib/report', ['tid' => $texture->tid, 'reason' => 'reason'])
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('skinlib.upload.lack-score'),
|
2019-03-30 11:38:30 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Success
|
|
|
|
option(['reporter_score_modification' => 5]);
|
|
|
|
$this->postJson('/skinlib/report', ['tid' => $texture->tid, 'reason' => 'reason'])
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('skinlib.report.success'),
|
2019-03-30 11:38:30 +08:00
|
|
|
]);
|
|
|
|
$user->refresh();
|
|
|
|
$this->assertEquals(5, $user->score);
|
|
|
|
$report = Report::where('reporter', $user->uid)->first();
|
|
|
|
$this->assertEquals($texture->tid, $report->tid);
|
|
|
|
$this->assertEquals($texture->uploader, $report->uploader);
|
|
|
|
$this->assertEquals('reason', $report->reason);
|
|
|
|
$this->assertEquals(Report::PENDING, $report->status);
|
|
|
|
|
|
|
|
// Prevent duplication
|
|
|
|
$this->postJson('/skinlib/report', ['tid' => $texture->tid, 'reason' => 'reason'])
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('skinlib.report.duplicate'),
|
2019-03-30 11:38:30 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTrack()
|
|
|
|
{
|
|
|
|
$user = factory(User::class)->create();
|
|
|
|
$report = new Report;
|
|
|
|
$report->tid = 1;
|
|
|
|
$report->uploader = 0;
|
|
|
|
$report->reporter = $user->uid;
|
|
|
|
$report->reason = 'test';
|
|
|
|
$report->status = Report::PENDING;
|
|
|
|
$report->save();
|
|
|
|
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($user)
|
2019-03-30 11:38:30 +08:00
|
|
|
->getJson('/user/report-list')
|
|
|
|
->assertJson([[
|
|
|
|
'tid' => 1,
|
|
|
|
'reason' => 'test',
|
|
|
|
'status' => Report::PENDING,
|
|
|
|
]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testManage()
|
|
|
|
{
|
|
|
|
$uploader = factory(User::class)->create();
|
|
|
|
$reporter = factory(User::class, 'admin')->create();
|
|
|
|
$texture = factory(Texture::class)->create(['uploader' => $uploader->uid]);
|
|
|
|
|
|
|
|
$report = new Report;
|
|
|
|
$report->tid = $texture->tid;
|
|
|
|
$report->uploader = $uploader->uid;
|
|
|
|
$report->reporter = $reporter->uid;
|
|
|
|
$report->reason = 'test';
|
|
|
|
$report->status = Report::PENDING;
|
|
|
|
$report->save();
|
|
|
|
|
2019-04-04 09:50:48 +08:00
|
|
|
$this->actingAs($reporter)
|
2019-03-30 11:38:30 +08:00
|
|
|
->getJson('/admin/report-data')
|
|
|
|
->assertJson([
|
|
|
|
'totalRecords' => 1,
|
|
|
|
'data' => [[
|
|
|
|
'tid' => $texture->tid,
|
|
|
|
'uploader' => $uploader->uid,
|
|
|
|
'reporter' => $reporter->uid,
|
|
|
|
'reason' => 'test',
|
|
|
|
'status' => Report::PENDING,
|
|
|
|
'uploaderName' => $uploader->nickname,
|
|
|
|
'reporterName' => $reporter->nickname,
|
2019-04-19 19:36:36 +08:00
|
|
|
]],
|
2019-03-30 11:38:30 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testReview()
|
|
|
|
{
|
2019-05-19 14:02:44 +08:00
|
|
|
$admin = factory(User::class, 'admin')->create();
|
2019-05-19 17:09:10 +08:00
|
|
|
$texture = factory(Texture::class)->create(['uploader' => $admin->uid]);
|
|
|
|
|
2019-03-30 11:38:30 +08:00
|
|
|
$report = new Report;
|
2019-05-19 17:09:10 +08:00
|
|
|
$report->tid = $texture->tid;
|
|
|
|
$report->uploader = $admin->uid;
|
|
|
|
$report->reporter = $admin->uid;
|
|
|
|
$report->reason = 'test';
|
|
|
|
$report->status = Report::PENDING;
|
2019-03-30 11:38:30 +08:00
|
|
|
$report->save();
|
|
|
|
$report->refresh();
|
|
|
|
|
|
|
|
// Without `id` field
|
2019-05-19 14:02:44 +08:00
|
|
|
$this->actingAs($admin)
|
2019-03-30 11:38:30 +08:00
|
|
|
->postJson('/admin/reports')
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('id');
|
2019-03-30 11:38:30 +08:00
|
|
|
|
|
|
|
// Not existed
|
|
|
|
$this->postJson('/admin/reports', ['id' => $report->id - 1])
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('id');
|
2019-03-30 11:38:30 +08:00
|
|
|
|
|
|
|
// Without `action` field
|
|
|
|
$this->postJson('/admin/reports', ['id' => $report->id])
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('action');
|
2019-03-30 11:38:30 +08:00
|
|
|
|
|
|
|
// Invalid action
|
|
|
|
$this->postJson('/admin/reports', ['id' => $report->id, 'action' => 'a'])
|
2019-04-05 08:58:00 +08:00
|
|
|
->assertJsonValidationErrors('action');
|
2019-03-30 11:38:30 +08:00
|
|
|
|
2019-05-08 12:51:48 +08:00
|
|
|
// Allow to process again
|
2019-03-30 11:38:30 +08:00
|
|
|
$this->postJson('/admin/reports', ['id' => $report->id, 'action' => 'reject'])
|
2019-05-08 12:51:48 +08:00
|
|
|
->assertJson(['code' => 0]);
|
2019-05-19 16:16:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReviewReject()
|
|
|
|
{
|
|
|
|
$uploader = factory(User::class)->create();
|
|
|
|
$reporter = factory(User::class)->create();
|
|
|
|
$admin = factory(User::class, 'admin')->create();
|
|
|
|
$texture = factory(Texture::class)->create(['uploader' => $uploader->uid]);
|
2019-03-30 11:38:30 +08:00
|
|
|
|
2019-05-19 16:16:16 +08:00
|
|
|
$report = new Report;
|
|
|
|
$report->tid = $texture->tid;
|
|
|
|
$report->uploader = $uploader->uid;
|
|
|
|
$report->reporter = $reporter->uid;
|
|
|
|
$report->reason = 'test';
|
2019-03-30 11:38:30 +08:00
|
|
|
$report->status = Report::PENDING;
|
|
|
|
$report->save();
|
2019-05-19 16:16:16 +08:00
|
|
|
$report->refresh();
|
|
|
|
|
|
|
|
// Should not cost score
|
2019-03-30 11:38:30 +08:00
|
|
|
$score = $reporter->score;
|
2019-05-19 16:16:16 +08:00
|
|
|
$this->actingAs($admin)
|
|
|
|
->postJson('/admin/reports', ['id' => $report->id, 'action' => 'reject'])
|
2019-03-30 11:38:30 +08:00
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('general.op-success'),
|
2019-04-23 19:14:41 +08:00
|
|
|
'data' => ['status' => Report::REJECTED],
|
2019-03-30 11:38:30 +08:00
|
|
|
]);
|
|
|
|
$report->refresh();
|
|
|
|
$reporter->refresh();
|
|
|
|
$this->assertEquals(Report::REJECTED, $report->status);
|
|
|
|
$this->assertEquals($score, $reporter->score);
|
|
|
|
|
2019-05-19 16:16:16 +08:00
|
|
|
// Should cost score
|
2019-03-30 11:38:30 +08:00
|
|
|
$report->status = Report::PENDING;
|
|
|
|
$report->save();
|
|
|
|
option(['reporter_score_modification' => 5]);
|
|
|
|
$score = $reporter->score;
|
|
|
|
$this->postJson('/admin/reports', ['id' => $report->id, 'action' => 'reject'])
|
2019-04-23 11:47:45 +08:00
|
|
|
->assertJson(['code' => 0]);
|
2019-03-30 11:38:30 +08:00
|
|
|
$reporter->refresh();
|
|
|
|
$this->assertEquals($score - 5, $reporter->score);
|
2019-05-19 16:16:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReviewDelete()
|
|
|
|
{
|
|
|
|
$uploader = factory(User::class)->create();
|
|
|
|
$reporter = factory(User::class)->create();
|
|
|
|
$admin = factory(User::class, 'admin')->create();
|
|
|
|
$texture = factory(Texture::class)->create(['uploader' => $uploader->uid]);
|
|
|
|
|
|
|
|
$report = new Report;
|
|
|
|
$report->tid = $texture->tid;
|
|
|
|
$report->uploader = $uploader->uid;
|
|
|
|
$report->reporter = $reporter->uid;
|
|
|
|
$report->reason = 'test';
|
|
|
|
$report->status = Report::PENDING;
|
|
|
|
$report->save();
|
|
|
|
$report->refresh();
|
2019-03-30 11:38:30 +08:00
|
|
|
|
2019-05-07 15:16:53 +08:00
|
|
|
option([
|
|
|
|
'reporter_score_modification' => -7,
|
|
|
|
'return_score' => false,
|
|
|
|
'take_back_scores_after_deletion' => false,
|
|
|
|
]);
|
2019-03-30 11:38:30 +08:00
|
|
|
$score = $reporter->score;
|
2019-05-19 16:16:16 +08:00
|
|
|
$this->actingAs($admin)
|
|
|
|
->postJson('/admin/reports', ['id' => $report->id, 'action' => 'delete'])
|
2019-03-30 11:38:30 +08:00
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('general.op-success'),
|
2019-04-23 19:14:41 +08:00
|
|
|
'data' => ['status' => Report::RESOLVED],
|
2019-03-30 11:38:30 +08:00
|
|
|
]);
|
|
|
|
$report->refresh();
|
|
|
|
$reporter->refresh();
|
|
|
|
$this->assertEquals(Report::RESOLVED, $report->status);
|
|
|
|
$this->assertNull(Texture::find($texture->tid));
|
|
|
|
$this->assertEquals($score + 7, $reporter->score);
|
2019-05-19 14:44:17 +08:00
|
|
|
}
|
2019-05-19 16:16:16 +08:00
|
|
|
|
2019-05-19 17:04:17 +08:00
|
|
|
public function testReviewDeleteNonExistentTexture()
|
|
|
|
{
|
|
|
|
$uploader = factory(User::class)->create();
|
|
|
|
$reporter = factory(User::class)->create();
|
|
|
|
$admin = factory(User::class, 'admin')->create();
|
|
|
|
$texture = factory(Texture::class)->create(['uploader' => $uploader->uid]);
|
|
|
|
|
|
|
|
$report = new Report;
|
|
|
|
$report->tid = $texture->tid;
|
|
|
|
$report->uploader = $uploader->uid;
|
|
|
|
$report->reporter = $reporter->uid;
|
|
|
|
$report->reason = 'test';
|
|
|
|
$report->status = Report::PENDING;
|
|
|
|
$report->save();
|
|
|
|
$report->refresh();
|
|
|
|
|
|
|
|
option([
|
|
|
|
'reporter_reward_score' => 6,
|
|
|
|
'reporter_score_modification' => -7,
|
|
|
|
]);
|
|
|
|
$score = $reporter->score;
|
|
|
|
$texture->delete();
|
|
|
|
$this->actingAs($admin)
|
|
|
|
->postJson('/admin/reports', ['id' => $report->id, 'action' => 'delete'])
|
|
|
|
->assertJson([
|
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('general.texture-deleted'),
|
|
|
|
'data' => ['status' => Report::RESOLVED],
|
|
|
|
]);
|
|
|
|
$report->refresh();
|
|
|
|
$this->assertEquals(Report::RESOLVED, $report->status);
|
|
|
|
$this->assertEquals($score, $reporter->score);
|
|
|
|
}
|
|
|
|
|
2019-05-19 16:16:16 +08:00
|
|
|
public function testReviewBan()
|
2019-05-19 14:44:17 +08:00
|
|
|
{
|
|
|
|
$uploader = factory(User::class)->create();
|
|
|
|
$reporter = factory(User::class)->create();
|
|
|
|
$admin = factory(User::class, 'admin')->create();
|
|
|
|
$texture = factory(Texture::class)->create(['uploader' => $uploader->uid]);
|
2019-03-30 11:38:30 +08:00
|
|
|
|
2019-05-19 14:44:17 +08:00
|
|
|
$report = new Report;
|
|
|
|
$report->tid = $texture->tid;
|
|
|
|
$report->uploader = $uploader->uid;
|
|
|
|
$report->reporter = $reporter->uid;
|
|
|
|
$report->reason = 'test';
|
2019-05-19 16:16:16 +08:00
|
|
|
$report->status = Report::PENDING;
|
2019-05-19 14:44:17 +08:00
|
|
|
$report->save();
|
|
|
|
$report->refresh();
|
2019-05-19 16:16:16 +08:00
|
|
|
|
|
|
|
// Uploader should be banned
|
2019-03-30 11:38:30 +08:00
|
|
|
option(['reporter_reward_score' => 6]);
|
|
|
|
$score = $reporter->score;
|
2019-05-19 14:49:49 +08:00
|
|
|
$this->actingAs($admin)
|
|
|
|
->postJson('/admin/reports', ['id' => $report->id, 'action' => 'ban'])
|
2019-03-30 11:38:30 +08:00
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 0,
|
|
|
|
'message' => trans('general.op-success'),
|
2019-04-23 19:14:41 +08:00
|
|
|
'data' => ['status' => Report::RESOLVED],
|
2019-03-30 11:38:30 +08:00
|
|
|
]);
|
|
|
|
$reporter->refresh();
|
2019-05-19 14:02:44 +08:00
|
|
|
$uploader->refresh();
|
2019-03-30 11:38:30 +08:00
|
|
|
$this->assertEquals(User::BANNED, $uploader->permission);
|
|
|
|
$this->assertEquals($score + 6, $reporter->score);
|
|
|
|
option(['reporter_reward_score' => 0]);
|
|
|
|
|
2019-05-19 16:16:16 +08:00
|
|
|
// Should not ban admin uploader
|
2019-03-30 11:38:30 +08:00
|
|
|
$report->refresh();
|
|
|
|
$report->status = Report::PENDING;
|
|
|
|
$report->save();
|
|
|
|
$uploader->refresh();
|
|
|
|
$uploader->permission = User::ADMIN;
|
|
|
|
$uploader->save();
|
|
|
|
$this->postJson('/admin/reports', ['id' => $report->id, 'action' => 'ban'])
|
|
|
|
->assertJson([
|
2019-04-23 11:47:45 +08:00
|
|
|
'code' => 1,
|
|
|
|
'message' => trans('admin.users.operations.no-permission'),
|
2019-03-30 11:38:30 +08:00
|
|
|
]);
|
|
|
|
$report->refresh();
|
|
|
|
$this->assertEquals(Report::PENDING, $report->status);
|
2019-05-19 16:16:16 +08:00
|
|
|
$this->assertEquals(User::ADMIN, $uploader->permission);
|
|
|
|
|
|
|
|
// Uploader has deleted its account
|
2019-05-19 14:44:17 +08:00
|
|
|
$report->uploader = -1;
|
|
|
|
$report->save();
|
|
|
|
$this->postJson('/admin/reports', ['id' => $report->id, 'action' => 'ban'])
|
|
|
|
->assertJson([
|
|
|
|
'code' => 1,
|
2019-05-19 15:09:21 +08:00
|
|
|
'message' => trans('admin.users.operations.non-existent'),
|
2019-05-19 14:44:17 +08:00
|
|
|
]);
|
2019-03-30 11:38:30 +08:00
|
|
|
}
|
|
|
|
}
|