blessing-skin-server/tests/PlayerControllerTest.php

355 lines
12 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')
->assertJson([
[
'pid' => $player->pid,
2019-03-13 13:16:51 +08:00
'name' => $player->name,
],
2018-07-30 15:45:08 +08:00
]);
}
2017-11-14 23:25:04 +08:00
public function testAdd()
{
// Without player name
2019-03-14 00:30:53 +08:00
$this->postJson('/user/player/add')
2018-07-13 16:14:49 +08:00
->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => trans('validation.attributes.player_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-03-14 00:30:53 +08:00
['player_name' => '角色名']
2018-07-13 16:14:49 +08:00
)->assertJson([
'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => trans('validation.attributes.player_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-03-14 00:30:53 +08:00
['player_name' => 'yjsnpi']
2018-07-13 16:14:49 +08:00
)->assertJson([
'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => trans('validation.attributes.player_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-03-14 00:30:53 +08:00
['player_name' => 'no_score']
2018-07-13 16:14:49 +08:00
)->assertJson([
'errno' => 7,
'msg' => 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', [
'player_name' => '角色名',
2018-07-13 16:14:49 +08:00
])->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 0,
'msg' => 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
2018-07-13 16:14:49 +08:00
$this->postJson('/user/player/add', ['player_name' => '角色名'])
->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 6,
'msg' => 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]);
$this->postJson('/user/player/add', ['player_name' => 'abc'])
->assertJson([
'errno' => 1,
'msg' => trans('user.player.add.single'),
]);
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)
2018-07-13 16:14:49 +08:00
->postJson('/user/player/delete', ['pid' => $player->pid])
->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 0,
2019-03-13 13:16:51 +08:00
'msg' => 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)
2018-07-13 16:14:49 +08:00
->postJson('/user/player/delete', ['pid' => $player->pid])
->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 0,
2019-03-13 13:16:51 +08:00
'msg' => 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)
->postJson('/user/player/delete', ['pid' => $player->pid])
->assertJson([
'errno' => 1,
'msg' => trans('user.player.delete.single'),
]);
$this->assertNotNull(Player::find($player->pid));
2017-11-14 23:25:04 +08:00
}
public function testShow()
{
$player = factory(Player::class)->create(['last_modified' => '2017-11-11 22:51:00']);
$this->get('/user/player/show?pid='.$player->pid)
2018-07-13 16:14:49 +08:00
->assertJson($player->toArray());
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)
2018-07-13 16:14:49 +08:00
->postJson('/user/player/rename', [
2017-11-14 23:25:04 +08:00
'pid' => $player->pid,
2019-03-14 00:30:53 +08:00
])
->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => trans('validation.attributes.player_name')]),
2017-11-14 23:25:04 +08:00
]);
// Only A-Za-z0-9_ are allowed
option(['player_name_rule' => 'official']);
$this->postJson('/user/player/rename', [
2017-11-14 23:25:04 +08:00
'pid' => $player->pid,
'new_player_name' => '角色名',
2018-07-13 16:14:49 +08:00
])->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => trans('validation.attributes.player_name')]),
2017-11-14 23:25:04 +08:00
]);
// Other invalid characters
option(['player_name_rule' => 'cjk']);
2018-07-13 16:14:49 +08:00
$this->postJson('/user/player/rename', [
2017-11-14 23:25:04 +08:00
'pid' => $player->pid,
'new_player_name' => '\\',
2019-03-14 00:30:53 +08:00
])
->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 1,
'msg' => trans('validation.player_name', ['attribute' => trans('validation.attributes.player_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;
2018-07-13 16:14:49 +08:00
$this->postJson('/user/player/rename', [
2017-11-14 23:25:04 +08:00
'pid' => $player->pid,
'new_player_name' => $name,
2018-07-13 16:14:49 +08:00
])->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 6,
'msg' => trans('user.player.rename.repeated'),
2017-11-14 23:25:04 +08:00
]);
// Success
2018-07-13 16:14:49 +08:00
$this->postJson('/user/player/rename', [
2017-11-14 23:25:04 +08:00
'pid' => $player->pid,
'new_player_name' => 'new_name',
2018-07-13 16:14:49 +08:00
])->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 0,
'msg' => trans(
'user.player.rename.success',
2019-03-13 13:16:51 +08:00
['old' => $player->name, 'new' => 'new_name']
),
2017-11-14 23:25:04 +08:00
]);
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]);
$this->postJson('/user/player/rename', [
'pid' => $player->pid,
'new_player_name' => 'abc',
])->assertJson(['errno' => 0]);
$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)
2018-07-13 16:14:49 +08:00
->postJson('/user/player/set', [
2017-11-14 23:25:04 +08:00
'pid' => $player->pid,
'tid' => ['skin' => -1],
2018-07-13 16:14:49 +08:00
])->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 6,
'msg' => trans('skinlib.un-existent'),
2017-11-14 23:25:04 +08:00
]);
// Set for "skin" type
2018-07-13 16:14:49 +08:00
$this->postJson('/user/player/set', [
2017-11-14 23:25:04 +08:00
'pid' => $player->pid,
'tid' => ['skin' => $skin->tid],
2018-07-13 16:14:49 +08:00
])->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 0,
2019-03-13 13:16:51 +08:00
'msg' => trans('user.player.set.success', ['name' => $player->name]),
2017-11-14 23:25:04 +08:00
]);
$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
2018-07-13 16:14:49 +08:00
$this->postJson('/user/player/set', [
2017-11-14 23:25:04 +08:00
'pid' => $player->pid,
'tid' => ['cape' => $cape->tid],
2018-07-13 16:14:49 +08:00
])->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 0,
2019-03-13 13:16:51 +08:00
'msg' => 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);
// Invalid texture type is acceptable
2018-07-13 16:14:49 +08:00
$this->postJson('/user/player/set', [
2017-11-14 23:25:04 +08:00
'pid' => $player->pid,
'tid' => ['nope' => $skin->tid], // TID must be valid
2018-07-13 16:14:49 +08:00
])->assertJson([
2017-11-14 23:25:04 +08:00
'errno' => 0,
2019-03-13 13:16:51 +08:00
'msg' => trans('user.player.set.success', ['name' => $player->name]),
2017-11-14 23:25:04 +08:00
]);
}
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)
2018-07-13 16:14:49 +08:00
->postJson('/user/player/texture/clear', [
2017-11-14 23:25:04 +08:00
'pid' => $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([
2017-11-14 23:25:04 +08:00
'errno' => 0,
2019-03-13 13:16:51 +08:00
'msg' => 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);
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();
2019-04-04 09:50:48 +08:00
$this->actingAs($user)->postJson('/user/player/bind')
2019-03-22 21:40:12 +08:00
->assertJson([
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'player']),
]);
$this->postJson('/user/player/bind', ['player' => 'abc'])
->assertJson([
'errno' => 0,
'msg' => trans('user.player.bind.success')
]);
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([
'errno' => 1,
'msg' => trans('user.player.rename.repeated')
]);
$this->postJson('/user/player/bind', ['player' => $player->name])
->assertJson(['errno' => 0]);
$this->assertNull(Player::where('name', $player3->name)->first());
}
2017-11-14 23:25:04 +08:00
}