2017-11-14 23:25:04 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use App\Events;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Player;
|
|
|
|
use App\Models\Texture;
|
|
|
|
use Illuminate\Foundation\Testing\WithoutMiddleware;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
|
|
|
|
|
|
class PlayerControllerTest extends TestCase
|
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
return $this->actAs('normal');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIndex()
|
|
|
|
{
|
2018-07-13 16:14:49 +08:00
|
|
|
$this->get('/user/player?pid=5')
|
2017-11-14 23:25:04 +08:00
|
|
|
->assertViewHas('players')
|
|
|
|
->assertViewHas('user');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAdd()
|
|
|
|
{
|
|
|
|
// Without player name
|
2018-07-13 16:14:49 +08:00
|
|
|
$this->postJson('/user/player/add', [], ['X-Requested-With' => 'XMLHttpRequest'])
|
|
|
|
->assertJson([
|
2017-11-14 23:25:04 +08:00
|
|
|
'errno' => 1,
|
|
|
|
'msg' => trans('validation.required', ['attribute' => 'Player Name'])
|
|
|
|
]);
|
|
|
|
|
2018-06-19 11:51:34 +08:00
|
|
|
// Only A-Za-z0-9_ are allowed
|
|
|
|
option(['player_name_rule' => 'official']);
|
2018-07-13 16:14:49 +08:00
|
|
|
$this->postJson(
|
2018-06-19 11:51:34 +08:00
|
|
|
'/user/player/add',
|
|
|
|
['player_name' => '角色名'],
|
|
|
|
['X-Requested-With' => 'XMLHttpRequest']
|
2018-07-13 16:14:49 +08:00
|
|
|
)->assertJson([
|
2018-06-19 11:51:34 +08:00
|
|
|
'errno' => 1,
|
|
|
|
'msg' => trans('validation.player_name', ['attribute' => '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(
|
2018-06-19 11:51:34 +08:00
|
|
|
'/user/player/add',
|
|
|
|
['player_name' => 'yjsnpi'],
|
|
|
|
['X-Requested-With' => 'XMLHttpRequest']
|
2018-07-13 16:14:49 +08:00
|
|
|
)->assertJson([
|
2018-06-19 11:51:34 +08:00
|
|
|
'errno' => 1,
|
|
|
|
'msg' => trans('validation.player_name', ['attribute' => 'Player Name'])
|
|
|
|
]);
|
2017-11-14 23:25:04 +08:00
|
|
|
|
|
|
|
// Lack of score
|
2018-06-19 11:51:34 +08:00
|
|
|
option(['player_name_rule' => 'official']);
|
2017-11-14 23:25:04 +08:00
|
|
|
$user = factory(User::class)->create(['score' => 0]);
|
2018-07-13 16:14:49 +08:00
|
|
|
$this->actAs($user)->postJson(
|
2018-06-19 11:51:34 +08:00
|
|
|
'/user/player/add',
|
|
|
|
['player_name' => 'no_score'],
|
|
|
|
['X-Requested-With' => 'XMLHttpRequest']
|
2018-07-13 16:14:49 +08:00
|
|
|
)->assertJson([
|
2018-06-19 11:51:34 +08:00
|
|
|
'errno' => 7,
|
|
|
|
'msg' => trans('user.player.add.lack-score')
|
|
|
|
]);
|
2017-11-14 23:25:04 +08:00
|
|
|
$this->expectsEvents(Events\CheckPlayerExists::class);
|
|
|
|
|
2018-06-19 11:51:34 +08:00
|
|
|
// 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;
|
2018-07-13 16:14:49 +08:00
|
|
|
$this->actAs($user)->postJson('/user/player/add', [
|
2017-11-14 23:25:04 +08:00
|
|
|
'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' => '角色名'])
|
|
|
|
]);
|
|
|
|
$this->expectsEvents(Events\PlayerWillBeAdded::class);
|
|
|
|
$this->expectsEvents(Events\PlayerWasAdded::class);
|
|
|
|
$player = Player::where('player_name', '角色名')->first();
|
|
|
|
$this->assertNotNull($player);
|
|
|
|
$this->assertEquals($user->uid, $player->uid);
|
|
|
|
$this->assertEquals('角色名', $player->player_name);
|
|
|
|
$this->assertEquals('default', $player->preference);
|
|
|
|
$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')
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDelete()
|
|
|
|
{
|
2018-07-20 14:42:43 +08:00
|
|
|
$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);
|
2018-07-20 14:42:43 +08:00
|
|
|
$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,
|
|
|
|
'msg' => trans('user.player.delete.success', ['name' => $player->player_name])
|
|
|
|
]);
|
|
|
|
$this->assertNull(Player::find($player->pid));
|
|
|
|
$this->expectsEvents(Events\PlayerWasDeleted::class);
|
|
|
|
$this->assertEquals(
|
2018-07-20 14:42:43 +08:00
|
|
|
$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;
|
2018-07-20 14:42:43 +08:00
|
|
|
$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,
|
|
|
|
'msg' => trans('user.player.delete.success', ['name' => $player->player_name])
|
|
|
|
]);
|
|
|
|
$this->assertEquals(
|
|
|
|
$user->score,
|
|
|
|
User::find($user->uid)->score
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
$this->actAs($user)
|
2018-07-13 16:14:49 +08:00
|
|
|
->postJson('/user/player/rename', [
|
2017-11-14 23:25:04 +08:00
|
|
|
'pid' => $player->pid,
|
|
|
|
], [
|
|
|
|
'X-Requested-With' => 'XMLHttpRequest'
|
2018-07-13 16:14:49 +08:00
|
|
|
])->assertJson([
|
2017-11-14 23:25:04 +08:00
|
|
|
'errno' => 1,
|
|
|
|
'msg' => trans('validation.required', ['attribute' => 'Player Name'])
|
|
|
|
]);
|
|
|
|
|
2018-06-19 11:51:34 +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/rename',[
|
2017-11-14 23:25:04 +08:00
|
|
|
'pid' => $player->pid,
|
|
|
|
'new_player_name' => '角色名'
|
|
|
|
], [
|
|
|
|
'X-Requested-With' => 'XMLHttpRequest'
|
2018-07-13 16:14:49 +08:00
|
|
|
])->assertJson([
|
2017-11-14 23:25:04 +08:00
|
|
|
'errno' => 1,
|
2018-06-19 11:51:34 +08:00
|
|
|
'msg' => trans('validation.player_name', ['attribute' => 'Player Name'])
|
2017-11-14 23:25:04 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Other invalid characters
|
2018-06-19 11:51:34 +08:00
|
|
|
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' => '\\'
|
|
|
|
], [
|
|
|
|
'X-Requested-With' => 'XMLHttpRequest'
|
2018-07-13 16:14:49 +08:00
|
|
|
])->assertJson([
|
2017-11-14 23:25:04 +08:00
|
|
|
'errno' => 1,
|
2018-06-19 11:51:34 +08:00
|
|
|
'msg' => trans('validation.player_name', ['attribute' => 'Player Name'])
|
2017-11-14 23:25:04 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
// Use a duplicated player name
|
|
|
|
$name = factory(Player::class)->create()->player_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')
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Success
|
|
|
|
$this->expectsEvents(Events\PlayerProfileUpdated::class);
|
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',
|
|
|
|
['old' => $player->player_name, 'new' => 'new_name']
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetTexture()
|
|
|
|
{
|
|
|
|
$player = factory(Player::class)->create();
|
2018-07-16 10:22:19 +08:00
|
|
|
$user = $player->user;
|
2017-11-14 23:25:04 +08:00
|
|
|
$steve = factory(Texture::class)->create();
|
|
|
|
$alex = factory(Texture::class, 'alex')->create();
|
|
|
|
$cape = factory(Texture::class, 'cape')->create();
|
|
|
|
|
|
|
|
// Set a not-existed texture
|
|
|
|
$this->actAs($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' => ['steve' => -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-24 23:05:49 +08:00
|
|
|
// Set for "steve" 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' => ['steve' => $steve->tid]
|
2018-07-13 16:14:49 +08:00
|
|
|
])->assertJson([
|
2017-11-14 23:25:04 +08:00
|
|
|
'errno' => 0,
|
|
|
|
'msg' => trans('user.player.set.success', ['name' => $player->player_name])
|
|
|
|
]);
|
|
|
|
$this->expectsEvents(Events\PlayerProfileUpdated::class);
|
|
|
|
$this->assertEquals($steve->tid, Player::find($player->pid)->tid_steve);
|
|
|
|
|
2017-11-24 23:05:49 +08:00
|
|
|
// Set for "alex" 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' => ['alex' => $alex->tid]
|
2018-07-13 16:14:49 +08:00
|
|
|
])->assertJson([
|
2017-11-14 23:25:04 +08:00
|
|
|
'errno' => 0,
|
|
|
|
'msg' => trans('user.player.set.success', ['name' => $player->player_name])
|
|
|
|
]);
|
|
|
|
$this->assertEquals($alex->tid, Player::find($player->pid)->tid_alex);
|
|
|
|
|
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,
|
|
|
|
'msg' => trans('user.player.set.success', ['name' => $player->player_name])
|
|
|
|
]);
|
|
|
|
$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' => $steve->tid] // TID must be valid
|
2018-07-13 16:14:49 +08:00
|
|
|
])->assertJson([
|
2017-11-14 23:25:04 +08:00
|
|
|
'errno' => 0,
|
|
|
|
'msg' => trans('user.player.set.success', ['name' => $player->player_name])
|
|
|
|
]);
|
2017-11-24 23:05:49 +08:00
|
|
|
|
|
|
|
// Should be OK if texture type does not match
|
2018-07-13 16:14:49 +08:00
|
|
|
$this->postJson('/user/player/set', [
|
2017-11-24 23:05:49 +08:00
|
|
|
'pid' => $player->pid,
|
|
|
|
'tid' => [
|
|
|
|
'steve' => $alex->tid,
|
|
|
|
'alex' => $cape->tid,
|
|
|
|
'cape' => $steve->tid
|
|
|
|
]
|
2018-07-13 16:14:49 +08:00
|
|
|
])->assertJson([
|
2017-11-24 23:05:49 +08:00
|
|
|
'errno' => 0,
|
|
|
|
'msg' => trans('user.player.set.success', ['name' => $player->player_name])
|
|
|
|
]);
|
|
|
|
$player = Player::find($player->pid);
|
|
|
|
$this->assertEquals($steve->tid, $player->tid_steve);
|
|
|
|
$this->assertEquals($alex->tid, $player->tid_alex);
|
|
|
|
$this->assertEquals($cape->tid, $player->tid_cape);
|
2017-11-14 23:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClearTexture()
|
|
|
|
{
|
|
|
|
$player = factory(Player::class)->create();
|
2018-07-16 10:22:19 +08:00
|
|
|
$user = $player->user;
|
2017-11-14 23:25:04 +08:00
|
|
|
|
|
|
|
$player->setTexture([
|
|
|
|
'tid_steve' => 1,
|
|
|
|
'tid_alex' => 2,
|
|
|
|
'tid_cape' => 3
|
|
|
|
]);
|
|
|
|
$player = Player::find($player->pid);
|
|
|
|
|
|
|
|
$this->expectsEvents(Events\PlayerProfileUpdated::class);
|
|
|
|
$this->actAs($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,
|
|
|
|
'steve' => 1, // "1" stands for "true"
|
|
|
|
'alex' => 1,
|
|
|
|
'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,
|
|
|
|
'msg' => trans('user.player.clear.success', ['name' => $player->player_name])
|
|
|
|
]);
|
|
|
|
$this->assertEquals(0, Player::find($player->pid)->tid_steve);
|
|
|
|
$this->assertEquals(0, Player::find($player->pid)->tid_alex);
|
|
|
|
$this->assertEquals(0, Player::find($player->pid)->tid_cape);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetPreference()
|
|
|
|
{
|
|
|
|
// Without `preference` field
|
|
|
|
$player = factory(Player::class)->create();
|
2018-07-16 10:22:19 +08:00
|
|
|
$this->actAs($player->user)
|
2018-07-13 16:14:49 +08:00
|
|
|
->postJson('/user/player/preference', [
|
2017-11-15 14:00:11 +08:00
|
|
|
'pid' => $player->pid
|
|
|
|
], [
|
|
|
|
'X-Requested-With' => 'XMLHttpRequest'
|
2018-07-13 16:14:49 +08:00
|
|
|
])->assertJson([
|
2017-11-15 14:00:11 +08:00
|
|
|
'errno' => 1,
|
|
|
|
'msg' => trans('validation.required', ['attribute' => 'preference'])
|
|
|
|
]);
|
2017-11-14 23:25:04 +08:00
|
|
|
|
|
|
|
// value of `preference` is invalid
|
2018-07-13 16:14:49 +08:00
|
|
|
$this->postJson('/user/player/preference', [
|
2017-11-14 23:25:04 +08:00
|
|
|
'pid' => $player->pid,
|
|
|
|
'preference' => 'steve'
|
|
|
|
], [
|
|
|
|
'X-Requested-With' => 'XMLHttpRequest'
|
2018-07-13 16:14:49 +08:00
|
|
|
])->assertJson([
|
2017-11-14 23:25:04 +08:00
|
|
|
'errno' => 1,
|
|
|
|
'msg' => trans('validation.preference', ['attribute' => 'preference'])
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Success
|
|
|
|
$this->expectsEvents(Events\PlayerProfileUpdated::class);
|
2018-07-13 16:14:49 +08:00
|
|
|
$this->postJson('/user/player/preference', [
|
2017-11-14 23:25:04 +08:00
|
|
|
'pid' => $player->pid,
|
|
|
|
'preference' => 'slim'
|
|
|
|
], [
|
|
|
|
'X-Requested-With' => 'XMLHttpRequest'
|
2018-07-13 16:14:49 +08:00
|
|
|
])->assertJson([
|
2017-11-14 23:25:04 +08:00
|
|
|
'errno' => 0,
|
|
|
|
'msg' => trans(
|
|
|
|
'user.player.preference.success',
|
|
|
|
['name' => $player->player_name, 'preference' => 'slim']
|
|
|
|
)
|
|
|
|
]);
|
|
|
|
$this->assertEquals('slim', Player::find($player->pid)->preference);
|
|
|
|
}
|
|
|
|
}
|