blessing-skin-server/tests/ClosetControllerTest.php

240 lines
8.1 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-04-04 09:50:48 +08:00
$this->actingAs($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([
2019-04-23 19:14:41 +08:00
'data' => [
'category',
'total_pages',
'items' => [['tid', 'name', 'type']],
2019-05-19 13:49:44 +08:00
],
2017-11-04 17:18:01 +08:00
]);
2017-11-06 11:07:24 +08:00
// Responsive
2019-04-23 19:14:41 +08:00
$result = $this->json('get', '/user/closet-data?perPage=0')->json()['data'];
2018-07-13 15:51:17 +08:00
$this->assertCount(6, $result['items']);
2019-04-23 19:14:41 +08:00
$result = $this->json('get', '/user/closet-data?perPage=8')->json()['data'];
2018-07-13 15:51:17 +08:00
$this->assertCount(8, $result['items']);
2019-04-23 19:14:41 +08:00
$result = $this->json('get', '/user/closet-data?perPage=8&page=2')->json()['data'];
2018-07-13 15:51:17 +08:00
$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')
2019-04-23 19:14:41 +08:00
->assertJson(['data' => [
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',
]],
2019-04-23 19:14:41 +08:00
]]);
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)
2019-04-23 19:14:41 +08:00
->assertJson(['data' => [
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,
]],
2019-04-23 19:14:41 +08:00
]]);
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
$this->postJson('/user/closet/add')->assertJsonValidationErrors('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']
)->assertJsonValidationErrors('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]
)->assertJsonValidationErrors('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' => '\\']
)->assertJsonValidationErrors('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([
2019-04-23 11:47:45 +08:00
'code' => 7,
'message' => 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([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('user.closet.add.not-found'),
2017-11-04 17:18:01 +08:00
]);
2019-04-28 11:38:38 +08:00
// Texture is private
2019-03-20 23:28:04 +08:00
option(['score_award_per_like' => 5]);
2019-04-28 11:38:38 +08:00
$privateTexture = factory(Texture::class)->create([
'public' => false,
'uploader' => $uploader->uid + 1,
]);
$this->postJson(
'/user/closet/add',
['tid' => $privateTexture->tid, 'name' => $name]
)->assertJson([
'code' => 1,
'message' => trans('skinlib.show.private'),
]);
// Add a texture successfully
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([
2019-04-23 11:47:45 +08:00
'code' => 0,
'message' => 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([
2019-04-23 11:47:45 +08:00
'code' => 1,
'message' => trans('user.closet.add.repeated'),
2017-11-04 17:18:01 +08:00
]);
}
public function testRename()
{
$texture = factory(Texture::class)->create();
$name = 'new';
2019-04-28 11:38:38 +08:00
// Missing `name` field
$this->postJson('/user/closet/rename/0')->assertJsonValidationErrors('name');
2017-11-04 17:18:01 +08:00
// `new_name` field has special characters
2019-04-28 11:38:38 +08:00
$this->postJson('/user/closet/rename/0', ['name' => '\\'])
->assertJsonValidationErrors('name');
2017-11-04 17:18:01 +08:00
// Rename a not-existed texture
2019-04-28 11:38:38 +08:00
$this->postJson('/user/closet/rename/-1', ['name' => $name])
->assertJson([
'code' => 1,
'message' => 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']);
2019-04-28 11:38:38 +08:00
$this->postJson('/user/closet/rename/'.$texture->tid, ['name' => $name])
->assertJson([
'code' => 0,
'message' => trans('user.closet.rename.success', ['name' => $name]),
]);
$this->assertEquals(1, $this->user->closet()->where('item_name', $name)->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
// Rename a not-existed texture
2019-04-28 11:38:38 +08:00
$this->postJson('/user/closet/remove/-1')
->assertJson([
'code' => 1,
'message' => 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;
2019-04-28 11:38:38 +08:00
$this->postJson('/user/closet/remove/'.$texture->tid)
->assertJson([
'code' => 0,
'message' => trans('user.closet.remove.success'),
]);
$this->assertEquals($likes - 1, 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;
2019-04-28 11:38:38 +08:00
$this->postJson('/user/closet/remove/'.$texture->tid)->assertJson(['code' => 0]);
$this->assertEquals($likes - 1, 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
}
}