diff --git a/app/Http/Controllers/PlayerController.php b/app/Http/Controllers/PlayerController.php index e8c7c186..0e7a7526 100644 --- a/app/Http/Controllers/PlayerController.php +++ b/app/Http/Controllers/PlayerController.php @@ -4,10 +4,12 @@ namespace App\Http\Controllers; use View; use Event; +use Eventy; use Option; use App\Models\User; use App\Models\Player; use App\Models\Texture; +use App\Services\Rejection; use Illuminate\Http\Request; use App\Events\PlayerWasAdded; use App\Events\PlayerWasDeleted; @@ -126,6 +128,11 @@ class PlayerController extends Controller $dispatcher->dispatch('player.renaming', [$player, $newName]); + $can = Eventy::filter('can_rename_player', $player, $newName); + if ($can instanceof Rejection) { + return json($can->getReason(), 1); + } + if (! Player::where('name', $newName)->get()->isEmpty()) { return json(trans('user.player.rename.repeated'), 6); } diff --git a/app/Services/Rejection.php b/app/Services/Rejection.php new file mode 100644 index 00000000..15575961 --- /dev/null +++ b/app/Services/Rejection.php @@ -0,0 +1,34 @@ +reason = $reason; + $this->data = $data; + } + + public function getReason(): string + { + return $this->reason; + } + + public function getData($key = null, $default = null) + { + if (is_null($key)) { + return $this->data; + } + + return Arr::get($this->data, $key, $default); + } +} diff --git a/app/helpers.php b/app/helpers.php index 4bc1d556..cd497f52 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -73,13 +73,6 @@ if (! function_exists('add_filter')) { } } -if (! function_exists('apply_filters')) { - function apply_filters() - { - return call_user_func_array([app('eventy'), 'filter'], func_get_args()); - } -} - if (! function_exists('bs_footer_extra')) { function bs_footer_extra(): string { diff --git a/tests/PlayerControllerTest.php b/tests/PlayerControllerTest.php index 72077f7e..b90f5990 100644 --- a/tests/PlayerControllerTest.php +++ b/tests/PlayerControllerTest.php @@ -3,10 +3,12 @@ namespace Tests; use Event; +use Eventy; use App\Events; use App\Models\User; use App\Models\Player; use App\Models\Texture; +use App\Services\Rejection; use Illuminate\Foundation\Testing\DatabaseTransactions; class PlayerControllerTest extends TestCase @@ -187,9 +189,21 @@ class PlayerControllerTest extends TestCase ]); Event::assertDispatched('player.renaming'); + // Rejected by filter + $pid = $player->pid; + Eventy::addFilter('can_rename_player', function ($player) { + return new Rejection('rejected'); + }, 20, 1); + $name = factory(Player::class)->create()->name; + $this->postJson('/user/player/rename/'.$player->pid, ['name' => 'new']) + ->assertJson([ + 'code' => 1, + 'message' => 'rejected', + ]); + Eventy::removeAllFilters('can_rename_player'); + // Success Event::fake(); - $pid = $player->pid; $this->postJson('/user/player/rename/'.$pid, ['name' => 'new_name']) ->assertJson([ 'code' => 0, diff --git a/tests/ServicesTest/FilterTest.php b/tests/ServicesTest/FilterTest.php index febd1feb..a797fe4d 100644 --- a/tests/ServicesTest/FilterTest.php +++ b/tests/ServicesTest/FilterTest.php @@ -2,6 +2,8 @@ namespace Tests; +use Eventy; + class FilterTest extends TestCase { public function testAddFilter() @@ -21,19 +23,11 @@ class FilterTest extends TestCase }); } - public function testApplyFilters() - { - $this->mock('eventy', function ($mock) { - $mock->shouldReceive('filter')->withArgs(['my.hook', 'value'])->once(); - }); - apply_filters('my.hook', 'value'); - } - public function testIntegration() { add_filter('hook.test', function ($value) { return $value.'ed'; }); - $this->assertEquals('tested', apply_filters('hook.test', 'test')); + $this->assertEquals('tested', Eventy::filter('hook.test', 'test')); } } diff --git a/tests/ServicesTest/RejectionTest.php b/tests/ServicesTest/RejectionTest.php new file mode 100644 index 00000000..5971d21d --- /dev/null +++ b/tests/ServicesTest/RejectionTest.php @@ -0,0 +1,27 @@ +assertEquals($reason, $rejection->getReason()); + } + + public function testGetData() + { + $rejection = new Rejection('', 'data'); + $this->assertEquals('data', $rejection->getData()); + + $rejection = new Rejection('', ['a' => 'b']); + $this->assertEquals('b', $rejection->getData('a')); + $this->assertNull($rejection->getData('nope')); + $this->assertEquals('default', $rejection->getData('nope', 'default')); + $this->assertEquals(['a' => 'b'], $rejection->getData()); + } +}