diff --git a/src/Controllers/Admin/UserGoodieController.php b/src/Controllers/Admin/UserGoodieController.php index a84cdd49..38166fb3 100644 --- a/src/Controllers/Admin/UserGoodieController.php +++ b/src/Controllers/Admin/UserGoodieController.php @@ -9,6 +9,7 @@ use Engelsystem\Config\GoodieType; use Engelsystem\Controllers\BaseController; use Engelsystem\Controllers\HasUserNotifications; use Engelsystem\Helpers\Authenticator; +use Engelsystem\Http\Exceptions\HttpNotFound; use Engelsystem\Http\Redirector; use Engelsystem\Http\Request; use Engelsystem\Http\Response; @@ -35,8 +36,16 @@ class UserGoodieController extends BaseController ) { } + private function checkActive(): void + { + if (GoodieType::from(config('goodie_type')) == GoodieType::None) { + throw new HttpNotFound(); + } + } + public function editGoodie(Request $request): Response { + $this->checkActive(); $userId = (int) $request->getAttribute('user_id'); $user = $this->user->findOrFail($userId); @@ -52,6 +61,7 @@ class UserGoodieController extends BaseController public function saveGoodie(Request $request): Response { + $this->checkActive(); $userId = (int) $request->getAttribute('user_id'); $shirtEnabled = $this->config->get('goodie_type') === GoodieType::Tshirt->value; /** @var User $user */ @@ -61,7 +71,7 @@ class UserGoodieController extends BaseController 'shirt_size' => ($shirtEnabled ? 'required' : 'optional') . '|shirt_size', 'arrived' => 'optional|checked', 'active' => 'optional|checked', - 'got_goodie' => 'optional|checked', + 'got_goodie' => 'optional|checked', ]); if ($shirtEnabled) { diff --git a/tests/Unit/Controllers/Admin/UserGoodieControllerTest.php b/tests/Unit/Controllers/Admin/UserGoodieControllerTest.php index 17299df8..32b9b622 100644 --- a/tests/Unit/Controllers/Admin/UserGoodieControllerTest.php +++ b/tests/Unit/Controllers/Admin/UserGoodieControllerTest.php @@ -7,6 +7,7 @@ namespace Engelsystem\Test\Unit\Controllers\Admin; use Engelsystem\Config\GoodieType; use Engelsystem\Controllers\Admin\UserGoodieController; use Engelsystem\Helpers\Authenticator; +use Engelsystem\Http\Exceptions\HttpNotFound; use Engelsystem\Http\Exceptions\ValidationException; use Engelsystem\Http\Redirector; use Engelsystem\Http\Validation\Validator; @@ -28,6 +29,7 @@ class UserGoodieControllerTest extends ControllerTest */ public function testIndex(): void { + $this->config->set('goodie_type', GoodieType::Tshirt->value); $request = $this->request->withAttribute('user_id', 1); /** @var Authenticator|MockObject $auth */ $auth = $this->createMock(Authenticator::class); @@ -48,6 +50,7 @@ class UserGoodieControllerTest extends ControllerTest */ public function testIndexUserNotFound(): void { + $this->config->set('goodie_type', GoodieType::Goodie->value); /** @var Authenticator|MockObject $auth */ $auth = $this->createMock(Authenticator::class); /** @var Redirector|MockObject $redirector */ @@ -60,6 +63,44 @@ class UserGoodieControllerTest extends ControllerTest $controller->editGoodie($this->request); } + /** + * @covers \Engelsystem\Controllers\Admin\UserGoodieController::editGoodie + * @covers \Engelsystem\Controllers\Admin\UserGoodieController::checkActive + */ + public function testEditShirtGoodieNone(): void + { + $this->config->set('goodie_type', GoodieType::None->value); + /** @var Authenticator|MockObject $auth */ + $auth = $this->createMock(Authenticator::class); + /** @var Redirector|MockObject $redirector */ + $redirector = $this->createMock(Redirector::class); + $user = new User(); + + $controller = new UserGoodieController($auth, $this->config, $this->log, $redirector, $this->response, $user); + + $this->expectException(HttpNotFound::class); + $controller->editGoodie($this->request); + } + + /** + * @covers \Engelsystem\Controllers\Admin\UserGoodieController::saveGoodie + * @covers \Engelsystem\Controllers\Admin\UserGoodieController::checkActive + */ + public function testSaveShirtGoodieNone(): void + { + $this->config->set('goodie_type', GoodieType::None->value); + /** @var Authenticator|MockObject $auth */ + $auth = $this->createMock(Authenticator::class); + /** @var Redirector|MockObject $redirector */ + $redirector = $this->createMock(Redirector::class); + $user = new User(); + + $controller = new UserGoodieController($auth, $this->config, $this->log, $redirector, $this->response, $user); + + $this->expectException(HttpNotFound::class); + $controller->saveGoodie($this->request); + } + /** * @todo Factor out separate tests. Isolated User, Config and permissions per test. * @covers \Engelsystem\Controllers\Admin\UserGoodieController::saveGoodie @@ -156,8 +197,8 @@ class UserGoodieControllerTest extends ControllerTest $user = User::find(1); $this->assertFalse($user->state->arrived); - // Goodie disabled - $this->config->set('goodie_type', GoodieType::None->value); + // Goodie enabled but not a shirt + $this->config->set('goodie_type', GoodieType::Goodie->value); $request = $request ->withParsedBody([ 'shirt_size' => 'XS', @@ -184,6 +225,7 @@ class UserGoodieControllerTest extends ControllerTest */ public function testSaveGoodieUserNotFound(): void { + $this->config->set('goodie_type', GoodieType::Goodie->value); /** @var Authenticator|MockObject $auth */ $auth = $this->createMock(Authenticator::class); /** @var Redirector|MockObject $redirector */