blessing-skin-server/tests/ClosetControllerTest.php

295 lines
9.4 KiB
PHP
Raw Normal View History

2017-11-04 17:18:01 +08:00
<?php
2018-08-17 15:25:08 +08:00
namespace Tests;
2017-11-04 17:18:01 +08:00
use App\Models\User;
use App\Models\Texture;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ClosetControllerTest extends TestCase
{
use DatabaseTransactions;
/**
* @var User
*/
private $user;
2019-02-27 23:44:50 +08:00
protected function setUp(): void
2017-11-04 17:18:01 +08:00
{
parent::setUp();
$this->user = factory(User::class)->create();
2019-02-27 23:44:50 +08:00
$this->actAs($this->user);
2017-11-04 17:18:01 +08:00
}
public function testIndex()
{
$this->get('/user/closet')->assertViewIs('user.closet');
2017-11-04 17:18:01 +08:00
}
public function testGetClosetData()
{
2017-11-06 11:07:24 +08:00
$textures = factory(Texture::class, 10)->create();
2019-03-14 23:55:49 +08:00
$textures->each(function ($t) {
$this->user->closet()->attach($t->tid, ['item_name' => $t->name]);
2017-11-04 17:18:01 +08:00
});
// Use default query parameters
2018-07-13 15:51:17 +08:00
$this->getJson('/user/closet-data')
->assertJsonStructure([
2017-11-05 16:54:01 +08:00
'category',
'total_pages',
2019-03-14 23:55:49 +08:00
'items' => [['tid', 'name', 'type']],
2017-11-04 17:18:01 +08:00
]);
2017-11-06 11:07:24 +08:00
// Responsive
2018-07-13 15:51:17 +08:00
$result = $this->json('get', '/user/closet-data?perPage=0')->json();
$this->assertCount(6, $result['items']);
$result = $this->json('get', '/user/closet-data?perPage=8')->json();
$this->assertCount(8, $result['items']);
$result = $this->json('get', '/user/closet-data?perPage=8&page=2')->json();
$this->assertCount(2, $result['items']);
2017-11-06 11:07:24 +08:00
2017-11-04 17:18:01 +08:00
// Get capes
$cape = factory(Texture::class, 'cape')->create();
2019-03-14 23:55:49 +08:00
$this->user->closet()->attach($cape->tid, ['item_name' => 'custom_name']);
2018-07-13 15:51:17 +08:00
$this->getJson('/user/closet-data?category=cape')
->assertJson([
2017-11-04 17:18:01 +08:00
'category' => 'cape',
'total_pages' => 1,
2017-11-05 16:54:01 +08:00
'items' => [[
'tid' => $cape->tid,
'name' => 'custom_name',
'type' => 'cape',
]],
2017-11-04 17:18:01 +08:00
]);
// Search by keyword
$random = $textures->random();
2018-07-13 15:51:17 +08:00
$this->getJson('/user/closet-data?q='.$random->name)
->assertJson([
2017-11-04 17:18:01 +08:00
'category' => 'skin',
'total_pages' => 1,
2017-11-05 16:54:01 +08:00
'items' => [[
'tid' => $random->tid,
'name' => $random->name,
'type' => $random->type,
]],
2017-11-04 17:18:01 +08:00
]);
}
public function testAdd()
{
2019-03-20 23:28:04 +08:00
$uploader = factory(User::class)->create(['score' => 0]);
$texture = factory(Texture::class)->create(['uploader' => $uploader->uid]);
2019-03-14 23:55:49 +08:00
$likes = $texture->likes;
2017-11-04 17:18:01 +08:00
$name = 'my';
option(['score_per_closet_item' => 10]);
// Missing `tid` field
2019-03-14 00:30:53 +08:00
$this->postJson('/user/closet/add')
2018-07-13 15:51:17 +08:00
->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid']),
2017-11-04 17:18:01 +08:00
]);
// `tid` is not a integer
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
2019-03-14 00:30:53 +08:00
['tid' => 'string']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid']),
2017-11-04 17:18:01 +08:00
]);
// Missing `name` field
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
2019-03-14 00:30:53 +08:00
['tid' => 0]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'name']),
2017-11-04 17:18:01 +08:00
]);
// `name` field has special characters
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
2019-03-14 00:30:53 +08:00
['tid' => 0, 'name' => '\\']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'name']),
2017-11-04 17:18:01 +08:00
]);
// The user doesn't have enough score to add a texture
$this->user->setScore(0);
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => $texture->tid, 'name' => $name]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 7,
'msg' => trans('user.closet.add.lack-score'),
2017-11-04 17:18:01 +08:00
]);
// Add a not-existed texture
$this->user->setScore(100);
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => -1, 'name' => 'my']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('user.closet.add.not-found'),
2017-11-04 17:18:01 +08:00
]);
// Add a texture successfully
2019-03-20 23:28:04 +08:00
option(['score_award_per_like' => 5]);
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => $texture->tid, 'name' => $name]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 0,
'msg' => trans('user.closet.add.success', ['name' => $name]),
2017-11-04 17:18:01 +08:00
]);
2019-03-14 23:55:49 +08:00
$this->assertEquals($likes + 1, Texture::find($texture->tid)->likes);
2017-11-04 17:18:01 +08:00
$this->user = User::find($this->user->uid);
$this->assertEquals(90, $this->user->score);
2019-03-14 23:55:49 +08:00
$this->assertEquals(1, $this->user->closet()->count());
2019-03-20 23:28:04 +08:00
$uploader->refresh();
$this->assertEquals(5, $uploader->score);
2017-11-04 17:18:01 +08:00
// If the texture is duplicated, should be warned
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/add',
['tid' => $texture->tid, 'name' => $name]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('user.closet.add.repeated'),
2017-11-04 17:18:01 +08:00
]);
}
public function testRename()
{
$texture = factory(Texture::class)->create();
$name = 'new';
// Missing `tid` field
2019-03-14 00:30:53 +08:00
$this->postJson('/user/closet/rename')
2018-07-13 15:51:17 +08:00
->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid']),
2017-11-04 17:18:01 +08:00
]);
// `tid` is not a integer
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/rename',
2019-03-14 00:30:53 +08:00
['tid' => 'string']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid']),
2017-11-04 17:18:01 +08:00
]);
// Missing `new_name` field
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/rename',
2019-03-14 00:30:53 +08:00
['tid' => 0]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'new name']),
2017-11-04 17:18:01 +08:00
]);
// `new_name` field has special characters
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/rename',
2019-03-14 00:30:53 +08:00
['tid' => 0, 'new_name' => '\\']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.no_special_chars', ['attribute' => 'new name']),
2017-11-04 17:18:01 +08:00
]);
// Rename a not-existed texture
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/rename',
['tid' => -1, 'new_name' => $name]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('user.closet.remove.non-existent'),
2017-11-04 17:18:01 +08:00
]);
// Rename a closet item successfully
2019-03-14 23:55:49 +08:00
$this->user->closet()->attach($texture->tid, ['item_name' => 'name']);
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/rename',
['tid' => $texture->tid, 'new_name' => $name]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 0,
'msg' => trans('user.closet.rename.success', ['name' => 'new']),
2017-11-04 17:18:01 +08:00
]);
2019-03-14 23:55:49 +08:00
$this->assertEquals(1, $this->user->closet()->where('item_name', 'new')->count());
2017-11-04 17:18:01 +08:00
}
public function testRemove()
{
2019-03-20 23:28:04 +08:00
$uploader = factory(User::class)->create(['score' => 5]);
$texture = factory(Texture::class)->create(['uploader' => $uploader->uid]);
2019-03-14 23:55:49 +08:00
$likes = $texture->likes;
2017-11-04 17:18:01 +08:00
// Missing `tid` field
2019-03-14 00:30:53 +08:00
$this->postJson('/user/closet/remove')
2018-07-13 15:51:17 +08:00
->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.required', ['attribute' => 'tid']),
2017-11-04 17:18:01 +08:00
]);
// `tid` is not a integer
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/remove',
2019-03-14 00:30:53 +08:00
['tid' => 'string']
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('validation.integer', ['attribute' => 'tid']),
2017-11-04 17:18:01 +08:00
]);
// Rename a not-existed texture
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/remove',
['tid' => -1]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 1,
'msg' => trans('user.closet.remove.non-existent'),
2017-11-04 17:18:01 +08:00
]);
// Should return score if `return_score` is true
2019-03-20 23:28:04 +08:00
option(['score_award_per_like' => 5]);
2019-03-14 23:55:49 +08:00
$this->user->closet()->attach($texture->tid, ['item_name' => 'name']);
2017-11-04 17:18:01 +08:00
$score = $this->user->score;
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/remove',
['tid' => $texture->tid]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 0,
'msg' => trans('user.closet.remove.success'),
2017-11-04 17:18:01 +08:00
]);
2019-03-14 23:55:49 +08:00
$this->assertEquals($likes, Texture::find($texture->tid)->likes);
2017-11-04 17:18:01 +08:00
$this->assertEquals($score + option('score_per_closet_item'), $this->user->score);
2019-03-14 23:55:49 +08:00
$this->assertEquals(0, $this->user->closet()->count());
2019-03-20 23:28:04 +08:00
$uploader->refresh();
$this->assertEquals(0, $uploader->score);
2017-11-04 17:18:01 +08:00
$texture = Texture::find($texture->tid);
2019-03-14 23:55:49 +08:00
$likes = $texture->likes;
2017-11-04 17:18:01 +08:00
// Should not return score if `return_score` is false
option(['return_score' => false]);
2019-03-14 23:55:49 +08:00
$this->user->closet()->attach($texture->tid, ['item_name' => 'name']);
2017-11-04 17:18:01 +08:00
$score = $this->user->score;
2018-07-13 15:51:17 +08:00
$this->postJson(
2017-11-04 17:18:01 +08:00
'/user/closet/remove',
['tid' => $texture->tid]
2018-07-13 15:51:17 +08:00
)->assertJson([
2017-11-04 17:18:01 +08:00
'errno' => 0,
'msg' => trans('user.closet.remove.success'),
2017-11-04 17:18:01 +08:00
]);
2019-03-14 23:55:49 +08:00
$this->assertEquals($likes, Texture::find($texture->tid)->likes);
2017-11-04 17:18:01 +08:00
$this->assertEquals($score, $this->user->score);
2019-03-14 23:55:49 +08:00
$this->assertEquals(0, $this->user->closet()->count());
2017-11-04 17:18:01 +08:00
}
}