2018-10-06 14:15:54 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-19 20:41:38 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Models\User;
|
2018-10-06 14:15:54 +02:00
|
|
|
|
2020-09-12 19:45:25 +02:00
|
|
|
use Carbon\Carbon;
|
2019-04-24 10:45:00 +02:00
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2023-01-18 13:02:11 +01:00
|
|
|
use Engelsystem\Config\Config;
|
2022-12-03 00:57:04 +01:00
|
|
|
use Engelsystem\Models\AngelType;
|
2019-11-10 21:30:26 +01:00
|
|
|
use Engelsystem\Models\BaseModel;
|
2022-11-06 12:41:52 +01:00
|
|
|
use Engelsystem\Models\Group;
|
2019-11-10 21:30:26 +01:00
|
|
|
use Engelsystem\Models\News;
|
2020-01-02 15:08:08 +01:00
|
|
|
use Engelsystem\Models\NewsComment;
|
2020-11-15 18:47:30 +01:00
|
|
|
use Engelsystem\Models\OAuth;
|
2022-11-06 12:41:52 +01:00
|
|
|
use Engelsystem\Models\Privilege;
|
2019-12-07 21:14:08 +01:00
|
|
|
use Engelsystem\Models\Question;
|
2023-09-17 15:03:56 +02:00
|
|
|
use Engelsystem\Models\Session;
|
2023-01-03 22:19:03 +01:00
|
|
|
use Engelsystem\Models\Shifts\Shift;
|
2023-01-18 13:02:11 +01:00
|
|
|
use Engelsystem\Models\Shifts\ShiftEntry;
|
2018-10-06 14:15:54 +02:00
|
|
|
use Engelsystem\Models\User\Contact;
|
|
|
|
use Engelsystem\Models\User\HasUserModel;
|
2021-12-19 18:38:42 +01:00
|
|
|
use Engelsystem\Models\User\License;
|
2018-10-06 14:15:54 +02:00
|
|
|
use Engelsystem\Models\User\PersonalData;
|
|
|
|
use Engelsystem\Models\User\Settings;
|
|
|
|
use Engelsystem\Models\User\State;
|
|
|
|
use Engelsystem\Models\User\User;
|
2022-12-03 00:57:04 +01:00
|
|
|
use Engelsystem\Models\UserAngelType;
|
2020-09-12 19:45:25 +02:00
|
|
|
use Engelsystem\Models\Worklog;
|
2020-05-11 19:57:25 +02:00
|
|
|
use Engelsystem\Test\Unit\Models\ModelTest;
|
2019-11-06 13:16:00 +01:00
|
|
|
use Exception;
|
2022-11-06 12:41:52 +01:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2019-12-07 21:14:08 +01:00
|
|
|
use Illuminate\Support\Str;
|
2018-10-06 14:15:54 +02:00
|
|
|
|
2020-05-11 19:57:25 +02:00
|
|
|
class UserTest extends ModelTest
|
2018-10-06 14:15:54 +02:00
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
use ArraySubsetAsserts;
|
2018-10-06 14:15:54 +02:00
|
|
|
|
2019-11-10 21:30:26 +01:00
|
|
|
/** @var string[] */
|
2022-12-15 19:50:56 +01:00
|
|
|
protected array $data = [
|
2018-10-06 14:15:54 +02:00
|
|
|
'name' => 'lorem',
|
|
|
|
'password' => '',
|
|
|
|
'email' => 'foo@bar.batz',
|
|
|
|
'api_key' => '',
|
|
|
|
];
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
public function hasOneRelationsProvider(): array
|
2018-10-06 14:15:54 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
Contact::class,
|
|
|
|
'contact',
|
|
|
|
[
|
2018-12-22 00:12:46 +01:00
|
|
|
'dect' => '1234567',
|
2018-10-06 14:15:54 +02:00
|
|
|
'email' => 'foo@bar.batz',
|
|
|
|
'mobile' => '1234/12341234',
|
2019-04-24 11:01:37 +02:00
|
|
|
],
|
2018-10-06 14:15:54 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
PersonalData::class,
|
|
|
|
'personalData',
|
|
|
|
[
|
2019-04-24 11:01:37 +02:00
|
|
|
'first_name' => 'Foo',
|
|
|
|
],
|
2018-10-06 14:15:54 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
Settings::class,
|
|
|
|
'settings',
|
|
|
|
[
|
|
|
|
'language' => 'de_DE',
|
|
|
|
'theme' => 4,
|
2019-04-24 11:01:37 +02:00
|
|
|
],
|
2018-10-06 14:15:54 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
State::class,
|
|
|
|
'state',
|
|
|
|
[
|
|
|
|
'force_active' => true,
|
2019-04-24 11:01:37 +02:00
|
|
|
],
|
2018-10-06 14:15:54 +02:00
|
|
|
],
|
2021-12-19 18:38:42 +01:00
|
|
|
[
|
|
|
|
License::class,
|
|
|
|
'license',
|
|
|
|
[
|
|
|
|
'has_car' => true,
|
|
|
|
'drive_car' => true,
|
|
|
|
],
|
|
|
|
],
|
2018-10-06 14:15:54 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-11-06 12:41:52 +01:00
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function hasManyRelationsProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'news' => [
|
|
|
|
News::class,
|
|
|
|
'news',
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'title' => 'Hey hoo',
|
|
|
|
'text' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.',
|
|
|
|
'is_meeting' => false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'title' => 'Huuhuuu',
|
|
|
|
'text' => 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.',
|
|
|
|
'is_meeting' => true,
|
|
|
|
],
|
2023-01-03 22:19:03 +01:00
|
|
|
],
|
|
|
|
],
|
2022-11-06 12:41:52 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function belongsToManyRelationsProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'groups' => [
|
|
|
|
Group::class,
|
|
|
|
'groups',
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'name' => 'Lorem',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'name' => 'Ipsum',
|
2023-02-05 18:03:00 +01:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2022-11-06 12:41:52 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2018-10-06 14:15:54 +02:00
|
|
|
/**
|
2023-01-03 22:19:03 +01:00
|
|
|
* @covers \Engelsystem\Models\User\User::contact
|
|
|
|
* @covers \Engelsystem\Models\User\User::license
|
|
|
|
* @covers \Engelsystem\Models\User\User::personalData
|
|
|
|
* @covers \Engelsystem\Models\User\User::settings
|
|
|
|
* @covers \Engelsystem\Models\User\User::state
|
2018-10-06 14:15:54 +02:00
|
|
|
*
|
|
|
|
* @dataProvider hasOneRelationsProvider
|
|
|
|
*
|
2019-11-06 13:16:00 +01:00
|
|
|
* @throws Exception
|
2018-10-06 14:15:54 +02:00
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testHasOneRelations(string $class, string $name, array $data): void
|
2018-10-06 14:15:54 +02:00
|
|
|
{
|
|
|
|
$user = new User($this->data);
|
|
|
|
$user->save();
|
|
|
|
|
2022-11-06 12:41:52 +01:00
|
|
|
/** @var HasUserModel $instance */
|
|
|
|
$instance = new $class($data);
|
|
|
|
$instance->user()
|
2018-10-06 14:15:54 +02:00
|
|
|
->associate($user)
|
|
|
|
->save();
|
|
|
|
|
2022-12-25 11:59:45 +01:00
|
|
|
$this->assertArraySubset($data, (array) $user->{$name}->attributesToArray());
|
2018-10-06 14:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-10 21:30:26 +01:00
|
|
|
* @covers \Engelsystem\Models\User\User::news()
|
2019-10-31 20:02:34 +01:00
|
|
|
*
|
|
|
|
* @dataProvider hasManyRelationsProvider
|
|
|
|
*
|
2022-11-06 12:41:52 +01:00
|
|
|
* @param string $class Class name of the related models
|
|
|
|
* @param string $name Name of the accessor for the related models
|
2019-11-10 21:30:26 +01:00
|
|
|
* @param array $modelData List of the related models
|
2018-10-06 14:15:54 +02:00
|
|
|
*/
|
2019-11-10 21:30:26 +01:00
|
|
|
public function testHasManyRelations(string $class, string $name, array $modelData): void
|
2018-10-06 14:15:54 +02:00
|
|
|
{
|
2019-10-31 20:02:34 +01:00
|
|
|
$user = new User($this->data);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$relatedModelIds = [];
|
|
|
|
|
2019-11-10 21:30:26 +01:00
|
|
|
foreach ($modelData as $data) {
|
|
|
|
/** @var BaseModel $model */
|
2019-11-10 23:26:23 +01:00
|
|
|
$model = $this->app->make($class);
|
2019-11-10 21:30:26 +01:00
|
|
|
$stored = $model->create($data + ['user_id' => $user->id]);
|
2019-10-31 20:02:34 +01:00
|
|
|
$relatedModelIds[] = $stored->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($relatedModelIds, $user->{$name}->modelKeys());
|
|
|
|
}
|
|
|
|
|
2022-11-06 12:41:52 +01:00
|
|
|
|
2019-10-31 20:02:34 +01:00
|
|
|
/**
|
2022-11-06 12:41:52 +01:00
|
|
|
* @covers \Engelsystem\Models\User\User::groups
|
|
|
|
*
|
|
|
|
* @dataProvider belongsToManyRelationsProvider
|
|
|
|
*
|
|
|
|
* @param string $class Class name of the related models
|
|
|
|
* @param string $name Name of the accessor for the related models
|
|
|
|
* @param array $modelData List of the related models
|
2019-10-31 20:02:34 +01:00
|
|
|
*/
|
2022-11-06 12:41:52 +01:00
|
|
|
public function testBelongsToManyRelations(string $class, string $name, array $modelData): void
|
2019-10-31 20:02:34 +01:00
|
|
|
{
|
2022-11-06 12:41:52 +01:00
|
|
|
$user = new User($this->data);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$relatedModelIds = [];
|
|
|
|
|
|
|
|
foreach ($modelData as $data) {
|
|
|
|
/** @var BaseModel $model */
|
|
|
|
$model = $this->app->make($class);
|
|
|
|
$stored = $model->create($data);
|
|
|
|
$stored->users()->attach($user);
|
|
|
|
$relatedModelIds[] = $stored->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($relatedModelIds, $user->{$name}->modelKeys());
|
|
|
|
}
|
|
|
|
|
2023-01-18 13:02:11 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::isFreeloader
|
|
|
|
*/
|
|
|
|
public function testIsFreeloader(): void
|
|
|
|
{
|
|
|
|
$this->app->instance('config', new Config([
|
|
|
|
'max_freeloadable_shifts' => 2,
|
|
|
|
]));
|
|
|
|
|
|
|
|
$user = new User($this->data);
|
|
|
|
$user->save();
|
|
|
|
$this->assertFalse($user->isFreeloader());
|
|
|
|
|
2023-09-25 17:36:43 +02:00
|
|
|
ShiftEntry::factory()->create(['user_id' => $user->id, 'freeloaded' => false]);
|
2023-01-18 13:02:11 +01:00
|
|
|
ShiftEntry::factory()->create(['user_id' => $user->id, 'freeloaded' => true]);
|
|
|
|
$this->assertFalse($user->isFreeloader());
|
|
|
|
|
|
|
|
ShiftEntry::factory()->create(['user_id' => $user->id, 'freeloaded' => true]);
|
|
|
|
$this->assertTrue($user->isFreeloader());
|
|
|
|
|
|
|
|
ShiftEntry::factory()->create(['user_id' => $user->id, 'freeloaded' => true]);
|
|
|
|
$this->assertTrue($user->isFreeloader());
|
|
|
|
}
|
|
|
|
|
2022-12-03 00:57:04 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::userAngelTypes
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testUserAngelTypes(): void
|
2022-12-03 00:57:04 +01:00
|
|
|
{
|
|
|
|
AngelType::factory(2)->create();
|
|
|
|
$angelType1 = AngelType::factory()->create();
|
|
|
|
AngelType::factory(1)->create();
|
|
|
|
$angelType2 = AngelType::factory()->create();
|
|
|
|
|
|
|
|
$user = new User($this->data);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$user->userAngelTypes()->attach($angelType1);
|
|
|
|
$user->userAngelTypes()->attach($angelType2);
|
|
|
|
|
|
|
|
/** @var UserAngelType $userAngelType */
|
|
|
|
$userAngelType = UserAngelType::find(1);
|
|
|
|
$this->assertEquals($user->id, $userAngelType->user->id);
|
|
|
|
|
|
|
|
$angeltypes = $user->userAngelTypes;
|
|
|
|
$this->assertCount(2, $angeltypes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::isAngelTypeSupporter
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testIsAngelTypeSupporter(): void
|
2022-12-03 00:57:04 +01:00
|
|
|
{
|
|
|
|
/** @var AngelType $angelType1 */
|
|
|
|
$angelType1 = AngelType::factory()->create();
|
|
|
|
/** @var AngelType $angelType2 */
|
|
|
|
$angelType2 = AngelType::factory()->create();
|
|
|
|
|
|
|
|
$user = new User($this->data);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
$user->userAngelTypes()->attach($angelType1, ['supporter' => true]);
|
|
|
|
$user->userAngelTypes()->attach($angelType2);
|
|
|
|
|
|
|
|
$this->assertTrue($user->isAngelTypeSupporter($angelType1));
|
|
|
|
$this->assertFalse($user->isAngelTypeSupporter($angelType2));
|
|
|
|
}
|
|
|
|
|
2022-11-06 12:41:52 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::privileges
|
|
|
|
* @covers \Engelsystem\Models\User\User::getPrivilegesAttribute
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function testPrivileges(): void
|
2022-11-06 12:41:52 +01:00
|
|
|
{
|
|
|
|
$user = new User($this->data);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
/** @var Group $group1 */
|
|
|
|
$group1 = Group::factory()->create();
|
|
|
|
/** @var Group $group2 */
|
|
|
|
$group2 = Group::factory()->create();
|
|
|
|
/** @var Group $group3 */
|
|
|
|
$group3 = Group::factory()->create();
|
|
|
|
/** @var Privilege $privilege1 */
|
|
|
|
$privilege1 = Privilege::factory()->create();
|
|
|
|
/** @var Privilege $privilege2 */
|
|
|
|
$privilege2 = Privilege::factory()->create();
|
|
|
|
/** @var Privilege $privilege3 */
|
|
|
|
$privilege3 = Privilege::factory()->create();
|
|
|
|
/** @var Privilege $privilege4 */
|
|
|
|
$privilege4 = Privilege::factory()->create();
|
|
|
|
|
|
|
|
$user->groups()->attach($group1);
|
|
|
|
$user->groups()->attach($group2);
|
|
|
|
|
|
|
|
$group1->privileges()->attach($privilege1);
|
|
|
|
$group1->privileges()->attach($privilege2);
|
|
|
|
|
|
|
|
$group2->privileges()->attach($privilege2);
|
|
|
|
$group2->privileges()->attach($privilege3);
|
|
|
|
|
|
|
|
$group3->privileges()->attach($privilege3);
|
|
|
|
$group3->privileges()->attach($privilege4);
|
|
|
|
|
|
|
|
/** @var User $createdUser */
|
|
|
|
$createdUser = User::first();
|
|
|
|
$this->assertInstanceOf(Builder::class, $createdUser->privileges());
|
|
|
|
|
|
|
|
$privileges = $createdUser->privileges->pluck('name');
|
|
|
|
$this->assertCount(3, $privileges);
|
|
|
|
$this->assertContains($privilege1->name, $privileges);
|
|
|
|
$this->assertContains($privilege2->name, $privileges);
|
|
|
|
$this->assertContains($privilege3->name, $privileges);
|
2018-10-06 14:15:54 +02:00
|
|
|
}
|
2019-11-10 21:30:26 +01:00
|
|
|
|
2020-01-02 15:08:08 +01:00
|
|
|
/**
|
|
|
|
* Tests that accessing the NewsComments of an User works.
|
|
|
|
*
|
|
|
|
* @covers \Engelsystem\Models\User\User::newsComments
|
|
|
|
*/
|
|
|
|
public function testNewsComments(): void
|
|
|
|
{
|
|
|
|
($user = new User($this->data))->save();
|
|
|
|
$newsComment = NewsComment::create(['news_id' => 0, 'text' => 'test comment', 'user_id' => $user->id]);
|
|
|
|
$comments = $user->newsComments;
|
|
|
|
|
|
|
|
$this->assertCount(1, $comments);
|
|
|
|
$comment = $comments->first();
|
|
|
|
$this->assertSame($newsComment->id, $comment->id);
|
|
|
|
}
|
|
|
|
|
2020-11-15 18:47:30 +01:00
|
|
|
/**
|
|
|
|
* Tests that accessing OAuth of an User works
|
|
|
|
*
|
|
|
|
* @covers \Engelsystem\Models\User\User::oauth
|
|
|
|
*/
|
|
|
|
public function testOauth(): void
|
|
|
|
{
|
|
|
|
($user = new User($this->data))->save();
|
|
|
|
(new OAuth(['provider' => 'test', 'identifier' => 'LoremIpsumDolor123']))
|
|
|
|
->user()
|
|
|
|
->associate($user)
|
|
|
|
->save();
|
|
|
|
|
|
|
|
$oauth = $user->oauth;
|
|
|
|
|
|
|
|
$this->assertCount(1, $oauth);
|
|
|
|
}
|
|
|
|
|
2023-01-18 13:02:11 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::shiftEntries
|
|
|
|
*/
|
|
|
|
public function testShiftEntries(): void
|
|
|
|
{
|
|
|
|
$user = new User($this->data);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
ShiftEntry::factory(2)->create(['user_id' => $user->id]);
|
|
|
|
|
|
|
|
$this->assertCount(2, $user->shiftEntries);
|
|
|
|
}
|
|
|
|
|
2023-09-17 15:03:56 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::sessions
|
|
|
|
*/
|
|
|
|
public function testSessions(): void
|
|
|
|
{
|
|
|
|
$user = new User($this->data);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
Session::factory(2)->create();
|
|
|
|
Session::factory(3)->create(['user_id' => $user->id]);
|
|
|
|
Session::factory(2)->create();
|
|
|
|
Session::factory(4)->create(['user_id' => $user->id]);
|
|
|
|
|
|
|
|
$this->assertCount(7, $user->sessions);
|
|
|
|
}
|
|
|
|
|
2020-09-12 19:45:25 +02:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::worklogs
|
|
|
|
*/
|
|
|
|
public function testWorklogs(): void
|
|
|
|
{
|
|
|
|
($user = new User($this->data))->save();
|
|
|
|
$worklogEntry = Worklog::create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'creator_id' => $user->id,
|
|
|
|
'hours' => 1,
|
|
|
|
'comment' => '',
|
|
|
|
'worked_at' => Carbon::now(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$worklogs = $user->worklogs;
|
|
|
|
$this->assertCount(1, $worklogs);
|
|
|
|
$worklog = $worklogs->first();
|
|
|
|
$this->assertSame($worklogEntry->id, $worklog->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::worklogsCreated
|
|
|
|
*/
|
|
|
|
public function testWorklogsCreated(): void
|
|
|
|
{
|
|
|
|
($user = new User($this->data))->save();
|
|
|
|
$worklogEntry = Worklog::create([
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'creator_id' => $user->id,
|
|
|
|
'hours' => 1,
|
|
|
|
'comment' => '',
|
|
|
|
'worked_at' => Carbon::now(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$worklogs = $user->worklogsCreated;
|
|
|
|
$this->assertCount(1, $worklogs);
|
|
|
|
$worklog = $worklogs->first();
|
|
|
|
$this->assertSame($worklogEntry->id, $worklog->id);
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:14:08 +01:00
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::questionsAsked
|
|
|
|
*/
|
|
|
|
public function testQuestionsAsked(): void
|
|
|
|
{
|
|
|
|
($user = new User($this->data))->save();
|
|
|
|
($user2 = new User(array_merge($this->data, ['name' => 'dolor', 'email' => 'dolor@bar.batz'])))->save();
|
|
|
|
|
|
|
|
($question1 = new Question(['user_id' => $user->id, 'text' => Str::random()]))->save();
|
|
|
|
($question2 = new Question(['user_id' => $user->id, 'text' => Str::random()]))->save();
|
|
|
|
// create some questions asked by user 2 to test the correct assignment
|
|
|
|
(new Question(['user_id' => $user2->id, 'text' => Str::random()]))->save();
|
|
|
|
(new Question(['user_id' => $user2->id, 'text' => Str::random()]))->save();
|
|
|
|
|
|
|
|
$questionIds = $user->questionsAsked()->pluck('id')->toArray();
|
|
|
|
$this->assertCount(2, $questionIds);
|
|
|
|
$this->assertContains($question1->id, $questionIds);
|
|
|
|
$this->assertContains($question2->id, $questionIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::questionsAnswered
|
|
|
|
*/
|
|
|
|
public function testQuestionsAnswered(): void
|
|
|
|
{
|
|
|
|
($user = new User($this->data))->save();
|
|
|
|
($user2 = new User(array_merge($this->data, ['name' => 'dolor', 'email' => 'dolor@bar.batz'])))->save();
|
|
|
|
|
|
|
|
$questionData = ['user_id' => $user->id, 'text' => Str::random()];
|
|
|
|
$answerData = ['answerer_id' => $user2->id, 'answer' => Str::random()];
|
|
|
|
($question1 = new Question(array_merge($questionData, $answerData)))->save();
|
|
|
|
($question2 = new Question(array_merge($questionData, $answerData)))->save();
|
|
|
|
// Create some questions asked by user 2 to test the correct assignment
|
|
|
|
(new Question(array_merge($questionData, $answerData, ['answerer_id' => $user->id])))->save();
|
|
|
|
(new Question($questionData))->save();
|
|
|
|
(new Question($questionData))->save();
|
|
|
|
|
|
|
|
$answers = $user2->questionsAnswered()->pluck('id')->toArray();
|
|
|
|
$this->assertCount(2, $answers);
|
|
|
|
$this->assertContains($question1->id, $answers);
|
|
|
|
$this->assertContains($question2->id, $answers);
|
|
|
|
}
|
2023-01-03 22:19:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::shiftsCreated
|
|
|
|
* @covers \Engelsystem\Models\User\User::shiftsUpdated
|
|
|
|
*/
|
|
|
|
public function testShiftsCreatedAndUpdated(): void
|
|
|
|
{
|
|
|
|
($user1 = new User($this->data))->save();
|
|
|
|
($user2 = new User(array_merge($this->data, ['name' => 'foo', 'email' => 'someone@bar.batz'])))->save();
|
|
|
|
|
|
|
|
Shift::factory()->create();
|
|
|
|
Shift::factory()->create(['created_by' => $user1->id]);
|
|
|
|
Shift::factory()->create(['created_by' => $user2->id, 'updated_by' => $user1->id]);
|
|
|
|
Shift::factory()->create(['created_by' => $user1->id, 'updated_by' => $user2->id]);
|
|
|
|
Shift::factory()->create(['created_by' => $user2->id, 'updated_by' => $user2->id]);
|
|
|
|
Shift::factory()->create(['created_by' => $user1->id]);
|
|
|
|
|
|
|
|
$user1 = User::find(1);
|
|
|
|
$this->assertCount(3, $user1->shiftsCreated);
|
|
|
|
$this->assertCount(1, $user1->shiftsUpdated);
|
|
|
|
|
|
|
|
$user2 = User::find(2);
|
|
|
|
$this->assertCount(2, $user2->shiftsCreated);
|
|
|
|
$this->assertCount(2, $user2->shiftsUpdated);
|
|
|
|
}
|
2022-07-10 21:12:10 +02:00
|
|
|
|
|
|
|
public function getDisplayNameAttributeProvider(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['lorem'],
|
|
|
|
['lorem', ' '],
|
|
|
|
['lorem', null, ' '],
|
|
|
|
['lorem', ' ', ' '],
|
|
|
|
['Test', 'Test', ' '],
|
|
|
|
['Tester', ' ', 'Tester'],
|
|
|
|
['Foo', 'Foo'],
|
|
|
|
['Bar', null, 'Bar'],
|
|
|
|
['Foo Bar', 'Foo', 'Bar'],
|
|
|
|
['Some name', ' Some', ' name'],
|
|
|
|
['Another Surname', ' Another ', ' Surname '],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::getDisplayNameAttribute
|
|
|
|
* @dataProvider getDisplayNameAttributeProvider
|
|
|
|
*/
|
|
|
|
public function testGetDisplayNameAttribute(
|
|
|
|
string $expected,
|
|
|
|
?string $firstName = null,
|
|
|
|
?string $lastName = null
|
|
|
|
): void {
|
|
|
|
$this->app->instance('config', new Config());
|
|
|
|
|
|
|
|
($user1 = new User($this->data))->save();
|
|
|
|
$user1->personalData->first_name = $firstName;
|
|
|
|
$user1->personalData->last_name = $lastName;
|
|
|
|
|
|
|
|
$this->assertEquals('lorem', $user1->displayName);
|
|
|
|
|
|
|
|
config(['display_full_name' => true]);
|
|
|
|
$this->assertEquals($expected, $user1->displayName);
|
|
|
|
}
|
2018-10-06 14:15:54 +02:00
|
|
|
}
|