2021-09-24 20:28:51 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-09-24 20:28:51 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Controllers\Admin;
|
|
|
|
|
2023-03-05 03:00:38 +01:00
|
|
|
use Engelsystem\Config\GoodieType;
|
2024-04-10 18:29:12 +02:00
|
|
|
use Engelsystem\Controllers\Admin\UserGoodieController;
|
2021-09-24 20:28:51 +02:00
|
|
|
use Engelsystem\Helpers\Authenticator;
|
2024-04-15 22:33:17 +02:00
|
|
|
use Engelsystem\Http\Exceptions\HttpNotFound;
|
2023-05-03 17:51:43 +02:00
|
|
|
use Engelsystem\Http\Exceptions\ValidationException;
|
2021-09-24 20:28:51 +02:00
|
|
|
use Engelsystem\Http\Redirector;
|
|
|
|
use Engelsystem\Http\Validation\Validator;
|
|
|
|
use Engelsystem\Models\User\PersonalData;
|
|
|
|
use Engelsystem\Models\User\State;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
use Engelsystem\Test\Unit\Controllers\ControllerTest;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
class UserGoodieControllerTest extends ControllerTest
|
2021-09-24 20:28:51 +02:00
|
|
|
{
|
|
|
|
use HasDatabase;
|
|
|
|
|
|
|
|
/**
|
2024-04-10 18:29:12 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Admin\UserGoodieController::editGoodie
|
|
|
|
* @covers \Engelsystem\Controllers\Admin\UserGoodieController::__construct
|
2021-09-24 20:28:51 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testIndex(): void
|
2021-09-24 20:28:51 +02:00
|
|
|
{
|
2024-04-15 22:33:17 +02:00
|
|
|
$this->config->set('goodie_type', GoodieType::Tshirt->value);
|
2022-12-11 18:01:34 +01:00
|
|
|
$request = $this->request->withAttribute('user_id', 1);
|
2021-09-24 20:28:51 +02:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
|
|
|
/** @var Redirector|MockObject $redirector */
|
|
|
|
$redirector = $this->createMock(Redirector::class);
|
|
|
|
$user = new User();
|
|
|
|
User::factory()->create();
|
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
$this->setExpects($this->response, 'withView', ['admin/user/edit-goodie.twig'], $this->response);
|
2021-09-24 20:28:51 +02:00
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller = new UserGoodieController($auth, $this->config, $this->log, $redirector, $this->response, $user);
|
2021-09-24 20:28:51 +02:00
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller->editGoodie($request);
|
2021-09-24 20:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-04-10 18:29:12 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Admin\UserGoodieController::editGoodie
|
2021-09-24 20:28:51 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testIndexUserNotFound(): void
|
2021-09-24 20:28:51 +02:00
|
|
|
{
|
2024-04-15 22:33:17 +02:00
|
|
|
$this->config->set('goodie_type', GoodieType::Goodie->value);
|
2021-09-24 20:28:51 +02:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
|
|
|
/** @var Redirector|MockObject $redirector */
|
|
|
|
$redirector = $this->createMock(Redirector::class);
|
|
|
|
$user = new User();
|
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller = new UserGoodieController($auth, $this->config, $this->log, $redirector, $this->response, $user);
|
2021-09-24 20:28:51 +02:00
|
|
|
|
|
|
|
$this->expectException(ModelNotFoundException::class);
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller->editGoodie($this->request);
|
2021-09-24 20:28:51 +02:00
|
|
|
}
|
|
|
|
|
2024-04-15 22:33:17 +02:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
2021-09-24 20:28:51 +02:00
|
|
|
/**
|
2023-05-03 17:51:43 +02:00
|
|
|
* @todo Factor out separate tests. Isolated User, Config and permissions per test.
|
2024-04-10 18:29:12 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Admin\UserGoodieController::saveGoodie
|
2021-09-24 20:28:51 +02:00
|
|
|
*/
|
2024-04-10 18:29:12 +02:00
|
|
|
public function testSaveGoodie(): void
|
2021-09-24 20:28:51 +02:00
|
|
|
{
|
2023-03-05 03:00:38 +01:00
|
|
|
$this->config->set('goodie_type', GoodieType::Tshirt->value);
|
2021-09-24 20:28:51 +02:00
|
|
|
$request = $this->request
|
2022-12-11 18:01:34 +01:00
|
|
|
->withAttribute('user_id', 1)
|
2021-09-24 20:28:51 +02:00
|
|
|
->withParsedBody([
|
|
|
|
'shirt_size' => 'S',
|
|
|
|
]);
|
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
2023-01-27 21:01:23 +01:00
|
|
|
$this->config->set('tshirt_sizes', ['S' => 'Small', 'XS' => 'Extra Small']);
|
2021-09-24 20:28:51 +02:00
|
|
|
/** @var Redirector|MockObject $redirector */
|
|
|
|
$redirector = $this->createMock(Redirector::class);
|
|
|
|
User::factory()
|
|
|
|
->has(State::factory())
|
|
|
|
->has(PersonalData::factory())
|
|
|
|
->create();
|
|
|
|
|
|
|
|
$auth
|
2023-05-03 17:51:43 +02:00
|
|
|
->expects($this->exactly(5))
|
2021-09-24 20:28:51 +02:00
|
|
|
->method('can')
|
|
|
|
->with('admin_arrive')
|
2023-05-03 17:51:43 +02:00
|
|
|
->willReturnOnConsecutiveCalls(true, true, false, false, true);
|
|
|
|
$this->setExpects($redirector, 'back', null, $this->response, $this->exactly(5));
|
2021-09-24 20:28:51 +02:00
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller = new UserGoodieController(
|
2021-09-24 20:28:51 +02:00
|
|
|
$auth,
|
|
|
|
$this->config,
|
|
|
|
$this->log,
|
|
|
|
$redirector,
|
|
|
|
$this->response,
|
|
|
|
new User()
|
|
|
|
);
|
|
|
|
$controller->setValidator(new Validator());
|
|
|
|
|
|
|
|
// Set shirt size
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller->saveGoodie($request);
|
2021-09-24 20:28:51 +02:00
|
|
|
|
|
|
|
$this->assertHasNotification('user.edit.success');
|
2024-04-10 18:29:12 +02:00
|
|
|
$this->assertTrue($this->log->hasInfoThatContains('Updated user goodie state'));
|
2021-09-24 20:28:51 +02:00
|
|
|
|
|
|
|
$user = User::find(1);
|
|
|
|
$this->assertEquals('S', $user->personalData->shirt_size);
|
|
|
|
$this->assertFalse($user->state->arrived);
|
|
|
|
$this->assertFalse($user->state->active);
|
2024-04-10 18:29:12 +02:00
|
|
|
$this->assertFalse($user->state->got_goodie);
|
2021-09-24 20:28:51 +02:00
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
// Set active, arrived and got_goodie
|
2021-09-24 20:28:51 +02:00
|
|
|
$request = $request
|
|
|
|
->withParsedBody([
|
|
|
|
'shirt_size' => 'S',
|
|
|
|
'arrived' => '1',
|
|
|
|
'active' => '1',
|
2024-04-10 18:29:12 +02:00
|
|
|
'got_goodie' => '1',
|
2021-09-24 20:28:51 +02:00
|
|
|
]);
|
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller->saveGoodie($request);
|
2021-09-24 20:28:51 +02:00
|
|
|
|
|
|
|
$user = User::find(1);
|
|
|
|
$this->assertTrue($user->state->active);
|
|
|
|
$this->assertTrue($user->state->arrived);
|
2024-04-10 18:29:12 +02:00
|
|
|
$this->assertTrue($user->state->got_goodie);
|
2021-09-24 20:28:51 +02:00
|
|
|
|
|
|
|
// Shirt size not available
|
|
|
|
$request = $request
|
|
|
|
->withParsedBody([
|
|
|
|
'shirt_size' => 'L',
|
|
|
|
]);
|
|
|
|
|
2023-05-03 17:51:43 +02:00
|
|
|
try {
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller->saveGoodie($request);
|
2023-05-03 17:51:43 +02:00
|
|
|
self::fail('Expected exception was not raised');
|
2024-03-25 00:03:39 +01:00
|
|
|
} catch (ValidationException) {
|
2023-05-03 17:51:43 +02:00
|
|
|
// ignore
|
|
|
|
}
|
2021-09-24 20:28:51 +02:00
|
|
|
$user = User::find(1);
|
|
|
|
$this->assertEquals('S', $user->personalData->shirt_size);
|
|
|
|
|
|
|
|
// Not allowed changing arrived
|
|
|
|
$request = $request
|
|
|
|
->withParsedBody([
|
|
|
|
'shirt_size' => 'S',
|
|
|
|
'arrived' => '1',
|
|
|
|
]);
|
|
|
|
|
2023-05-03 17:51:43 +02:00
|
|
|
$user->state->arrived = false;
|
|
|
|
$user->state->save();
|
2021-09-24 20:28:51 +02:00
|
|
|
$this->assertFalse($user->state->arrived);
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller->saveGoodie($request);
|
2021-09-24 20:28:51 +02:00
|
|
|
$user = User::find(1);
|
|
|
|
$this->assertFalse($user->state->arrived);
|
2023-01-27 21:01:23 +01:00
|
|
|
|
2024-04-15 22:33:17 +02:00
|
|
|
// Goodie enabled but not a shirt
|
|
|
|
$this->config->set('goodie_type', GoodieType::Goodie->value);
|
2023-03-05 03:00:38 +01:00
|
|
|
$request = $request
|
|
|
|
->withParsedBody([
|
|
|
|
'shirt_size' => 'XS',
|
|
|
|
]);
|
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller->saveGoodie($request);
|
2023-03-05 03:00:38 +01:00
|
|
|
$user = User::find(1);
|
|
|
|
$this->assertEquals('S', $user->personalData->shirt_size);
|
|
|
|
|
|
|
|
// Shirt enabled
|
|
|
|
$this->config->set('goodie_type', GoodieType::Tshirt->value);
|
2023-01-27 21:01:23 +01:00
|
|
|
$request = $request
|
|
|
|
->withParsedBody([
|
|
|
|
'shirt_size' => 'XS',
|
|
|
|
]);
|
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller->saveGoodie($request);
|
2023-01-27 21:01:23 +01:00
|
|
|
$user = User::find(1);
|
|
|
|
$this->assertEquals('XS', $user->personalData->shirt_size);
|
2021-09-24 20:28:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-04-10 18:29:12 +02:00
|
|
|
* @covers \Engelsystem\Controllers\Admin\UserGoodieController::saveGoodie
|
2021-09-24 20:28:51 +02:00
|
|
|
*/
|
2024-04-10 18:29:12 +02:00
|
|
|
public function testSaveGoodieUserNotFound(): void
|
2021-09-24 20:28:51 +02:00
|
|
|
{
|
2024-04-15 22:33:17 +02:00
|
|
|
$this->config->set('goodie_type', GoodieType::Goodie->value);
|
2021-09-24 20:28:51 +02:00
|
|
|
/** @var Authenticator|MockObject $auth */
|
|
|
|
$auth = $this->createMock(Authenticator::class);
|
|
|
|
/** @var Redirector|MockObject $redirector */
|
|
|
|
$redirector = $this->createMock(Redirector::class);
|
|
|
|
$user = new User();
|
|
|
|
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller = new UserGoodieController($auth, $this->config, $this->log, $redirector, $this->response, $user);
|
2021-09-24 20:28:51 +02:00
|
|
|
|
|
|
|
$this->expectException(ModelNotFoundException::class);
|
2024-04-10 18:29:12 +02:00
|
|
|
$controller->editGoodie($this->request);
|
2021-09-24 20:28:51 +02:00
|
|
|
}
|
|
|
|
}
|