blessing-skin-server/tests/PlayerControllerTest.php

301 lines
10 KiB
PHP
Raw Normal View History

2017-11-14 23:25:04 +08:00
<?php
2018-08-17 15:25:08 +08:00
namespace Tests;
2019-03-22 21:40:12 +08:00
use Event;
2017-11-14 23:25:04 +08:00
use App\Events;
use App\Models\User;
use App\Models\Player;
use App\Models\Texture;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class PlayerControllerTest extends TestCase
{
use DatabaseTransactions;
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2017-11-14 23:25:04 +08:00
{
parent::setUp();
2019-02-27 23:44:50 +08:00
$this->actAs('normal');
2017-11-14 23:25:04 +08:00
}
public function testIndex()
{
$this->get('/user/player?pid=5')->assertViewIs('user.player');
2017-11-14 23:25:04 +08:00
}
2018-07-30 15:45:08 +08:00
public function testListAll()
{
$user = factory(User::class)->create();
$player = factory(Player::class)->create(['uid' => $user->uid]);
2018-07-30 15:45:08 +08:00
$this->actingAs($user)
->get('/user/player/list')
2019-04-23 19:14:41 +08:00
->assertJson(['data' => [
2018-07-30 15:45:08 +08:00
[
'pid' => $player->pid,
2019-03-13 13:16:51 +08:00
'name' => $player->name,
],
2019-04-23 19:14:41 +08:00
]]);
2018-07-30 15:45:08 +08:00
}
2017-11-14 23:25:04 +08:00
public function testAdd()
{
// Without player name
2019-04-23 23:03:12 +08:00
$this->postJson('/user/player/add')->assertJsonValidationErrors('name');
2017-11-14 23:25:04 +08:00
// Only A-Za-z0-9_ are allowed
option(['player_name_rule' => 'official']);
2018-07-13 16:14:49 +08:00
$this->postJson(
'/user/player/add',
2019-04-23 23:03:12 +08:00
['name' => '角色名']
)->assertJsonValidationErrors('name');
// Custom player name rule (regexp)
option(['player_name_rule' => 'custom']);
option(['custom_player_name_regexp' => '/^([0-9]+)$/']);
2018-07-13 16:14:49 +08:00
$this->postJson(
'/user/player/add',
2019-04-23 23:03:12 +08:00
['name' => 'yjsnpi']
)->assertJsonValidationErrors('name');
2017-11-14 23:25:04 +08:00
// Lack of score
option(['player_name_rule' => 'official']);
2017-11-14 23:25:04 +08:00
$user = factory(User::class)->create(['score' => 0]);
2019-04-04 09:50:48 +08:00
$this->actingAs($user)->postJson(
'/user/player/add',
2019-04-23 23:03:12 +08:00
['name' => 'no_score']
2018-07-13 16:14:49 +08:00
)->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 7,
'message' => trans('user.player.add.lack-score'),
]);
2017-11-14 23:25:04 +08:00
$this->expectsEvents(Events\CheckPlayerExists::class);
// Allowed to use CJK characters
option(['player_name_rule' => 'cjk']);
2017-11-14 23:25:04 +08:00
$user = factory(User::class)->create();
$score = $user->score;
2019-04-04 09:50:48 +08:00
$this->actingAs($user)->postJson('/user/player/add', [
2019-04-23 23:03:12 +08:00
'name' => '角色名',
2018-07-13 16:14:49 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('user.player.add.success', ['name' => '角色名']),
2017-11-14 23:25:04 +08:00
]);
$this->expectsEvents(Events\PlayerWillBeAdded::class);
$this->expectsEvents(Events\PlayerWasAdded::class);
2019-03-13 13:16:51 +08:00
$player = Player::where('name', '角色名')->first();
2017-11-14 23:25:04 +08:00
$this->assertNotNull($player);
$this->assertEquals($user->uid, $player->uid);
2019-03-13 13:16:51 +08:00
$this->assertEquals('角色名', $player->name);
2017-11-14 23:25:04 +08:00
$this->assertEquals(
$score - option('score_per_player'),
User::find($user->uid)->score
);
// Add a existed player
2019-04-23 23:03:12 +08:00
$this->postJson('/user/player/add', ['name' => '角色名'])
2018-07-13 16:14:49 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 6,
'message' => trans('user.player.add.repeated'),
2017-11-14 23:25:04 +08:00
]);
2019-03-22 21:40:12 +08:00
// Single player
option(['single_player' => true]);
2019-04-23 23:03:12 +08:00
$this->postJson('/user/player/add', ['name' => 'abc'])
2019-03-22 21:40:12 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('user.player.add.single'),
2019-03-22 21:40:12 +08:00
]);
2017-11-14 23:25:04 +08:00
}
public function testDelete()
{
$user = factory(User::class)->create();
$player = factory(Player::class)->create(['uid' => $user->uid]);
$score = $user->score;
2017-11-14 23:25:04 +08:00
$this->expectsEvents(Events\PlayerWillBeDeleted::class);
$this->actingAs($user)
2019-04-24 13:10:03 +08:00
->postJson('/user/player/delete/'.$player->pid)
2018-07-13 16:14:49 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('user.player.delete.success', ['name' => $player->name]),
2017-11-14 23:25:04 +08:00
]);
$this->assertNull(Player::find($player->pid));
$this->expectsEvents(Events\PlayerWasDeleted::class);
$this->assertEquals(
$score + option('score_per_player'),
2017-11-14 23:25:04 +08:00
User::find($user->uid)->score
);
// No returning score
option(['return_score' => false]);
$player = factory(Player::class)->create();
2018-07-16 10:22:19 +08:00
$user = $player->user;
$this->actingAs($user)
2019-04-24 13:10:03 +08:00
->postJson('/user/player/delete/'.$player->pid)
2018-07-13 16:14:49 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('user.player.delete.success', ['name' => $player->name]),
2017-11-14 23:25:04 +08:00
]);
$this->assertEquals(
$user->score,
User::find($user->uid)->score
);
2019-03-22 21:40:12 +08:00
// Single player
option(['single_player' => true]);
$player = factory(Player::class)->create(['uid' => $user->uid]);
$this->actingAs($user)
2019-04-24 13:10:03 +08:00
->postJson('/user/player/delete/'.$player->pid)
2019-03-22 21:40:12 +08:00
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('user.player.delete.single'),
2019-03-22 21:40:12 +08:00
]);
$this->assertNotNull(Player::find($player->pid));
2017-11-14 23:25:04 +08:00
}
public function testRename()
{
2019-03-23 00:20:28 +08:00
Event::fake();
2017-11-14 23:25:04 +08:00
$player = factory(Player::class)->create();
2018-07-16 10:22:19 +08:00
$user = $player->user;
2017-11-14 23:25:04 +08:00
// Without new player name
2019-04-04 09:50:48 +08:00
$this->actingAs($user)
2019-04-24 13:10:03 +08:00
->postJson('/user/player/rename/'.$player->pid)
->assertJsonValidationErrors('name');
2017-11-14 23:25:04 +08:00
// Only A-Za-z0-9_ are allowed
option(['player_name_rule' => 'official']);
2019-04-24 13:10:03 +08:00
$this->postJson('/user/player/rename/'.$player->pid, ['name' => '角色名'])
->assertJsonValidationErrors('name');
2017-11-14 23:25:04 +08:00
// Other invalid characters
option(['player_name_rule' => 'cjk']);
2019-04-24 13:10:03 +08:00
$this->postJson('/user/player/rename/'.$player->pid, ['name' => '\\'])
->assertJsonValidationErrors('name');
2017-11-14 23:25:04 +08:00
// Use a duplicated player name
2019-03-13 13:16:51 +08:00
$name = factory(Player::class)->create()->name;
2019-04-24 13:10:03 +08:00
$this->postJson('/user/player/rename/'.$player->pid, ['name' => $name])
->assertJson([
'code' => 6,
'message' => trans('user.player.rename.repeated'),
]);
2017-11-14 23:25:04 +08:00
// Success
2019-04-24 13:10:03 +08:00
$this->postJson('/user/player/rename/'.$player->pid, ['name' => 'new_name'])
->assertJson([
'code' => 0,
'message' => trans(
'user.player.rename.success',
['old' => $player->name, 'new' => 'new_name']
),
]);
2019-03-23 00:20:28 +08:00
Event::assertDispatched(Events\PlayerProfileUpdated::class);
2019-03-22 21:40:12 +08:00
// Single player
option(['single_player' => true]);
2019-04-24 13:10:03 +08:00
$this->postJson('/user/player/rename/'.$player->pid, ['name' => 'abc'])
->assertJson(['code' => 0]);
2019-03-22 21:40:12 +08:00
$this->assertEquals('abc', $player->user->nickname);
2017-11-14 23:25:04 +08:00
}
public function testSetTexture()
{
$player = factory(Player::class)->create();
2018-07-16 10:22:19 +08:00
$user = $player->user;
$skin = factory(Texture::class)->create();
2017-11-14 23:25:04 +08:00
$cape = factory(Texture::class, 'cape')->create();
// Set a not-existed texture
2019-04-04 09:50:48 +08:00
$this->actingAs($user)
2019-04-24 13:10:03 +08:00
->postJson('/user/player/set/'.$player->pid, ['skin' => -1])
->assertJson([
'code' => 1,
'message' => trans('skinlib.non-existent'),
2017-11-14 23:25:04 +08:00
]);
// Set for "skin" type
2019-04-24 13:10:03 +08:00
$this->postJson('/user/player/set/'.$player->pid, ['skin' => $skin->tid])
->assertJson([
'code' => 0,
'message' => trans('user.player.set.success', ['name' => $player->name]),
]);
$this->assertEquals($skin->tid, Player::find($player->pid)->tid_skin);
2017-11-14 23:25:04 +08:00
2017-11-24 23:05:49 +08:00
// Set for "cape" type
2019-04-24 13:10:03 +08:00
$this->postJson('/user/player/set/'.$player->pid, ['cape' => $cape->tid])
->assertJson([
'code' => 0,
'message' => trans('user.player.set.success', ['name' => $player->name]),
]);
2017-11-14 23:25:04 +08:00
$this->assertEquals($cape->tid, Player::find($player->pid)->tid_cape);
}
public function testClearTexture()
{
2019-03-23 00:20:28 +08:00
Event::fake();
2017-11-14 23:25:04 +08:00
$player = factory(Player::class)->create();
2018-07-16 10:22:19 +08:00
$user = $player->user;
2017-11-14 23:25:04 +08:00
2019-03-23 00:20:28 +08:00
$player->tid_skin = 1;
$player->tid_cape = 2;
$player->save();
$player->refresh();
2017-11-14 23:25:04 +08:00
2019-04-04 09:50:48 +08:00
$this->actingAs($user)
2019-04-24 13:10:03 +08:00
->postJson('/user/player/texture/clear/'.$player->pid, [
'skin' => 1, // "1" stands for "true"
2017-11-14 23:25:04 +08:00
'cape' => 1,
'nope' => 1, // Invalid texture type is acceptable
2018-07-13 16:14:49 +08:00
])->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('user.player.clear.success', ['name' => $player->name]),
2017-11-14 23:25:04 +08:00
]);
$this->assertEquals(0, Player::find($player->pid)->tid_skin);
2017-11-14 23:25:04 +08:00
$this->assertEquals(0, Player::find($player->pid)->tid_cape);
2019-03-23 00:20:28 +08:00
Event::assertDispatched(Events\PlayerProfileUpdated::class);
2019-04-24 13:10:03 +08:00
$this->postJson('/user/player/texture/clear/'.$player->pid, ['type' => ['skin']])
->assertJson(['code' => 0]);
2017-11-14 23:25:04 +08:00
}
2019-03-22 21:40:12 +08:00
public function testBind()
{
Event::fake();
option(['single_player' => true]);
$user = factory(User::class)->create();
$this->actingAs($user)
->postJson('/user/player/bind')
->assertJsonValidationErrors('player');
2019-03-22 21:40:12 +08:00
$this->postJson('/user/player/bind', ['player' => 'abc'])
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => trans('user.player.bind.success'),
2019-03-22 21:40:12 +08:00
]);
Event::assertDispatched(Events\CheckPlayerExists::class);
Event::assertDispatched(Events\PlayerWillBeAdded::class);
Event::assertDispatched(Events\PlayerWasAdded::class);
$player = Player::where('name', 'abc')->first();
$this->assertNotNull($player);
$this->assertEquals($user->uid, $player->uid);
$this->assertEquals('abc', $player->name);
$user->refresh();
$this->assertEquals('abc', $user->nickname);
$player2 = factory(Player::class)->create();
$player3 = factory(Player::class)->create(['uid' => $user->uid]);
$this->postJson('/user/player/bind', ['player' => $player2->name])
->assertJson([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('user.player.rename.repeated'),
2019-03-22 21:40:12 +08:00
]);
$this->postJson('/user/player/bind', ['player' => $player->name])
2019-04-23 11:47:45 +08:00
->assertJson(['code' => 0]);
2019-03-22 21:40:12 +08:00
$this->assertNull(Player::where('name', $player3->name)->first());
}
2017-11-14 23:25:04 +08:00
}