2018-10-06 14:15:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Test\Unit\Models;
|
|
|
|
|
2019-04-24 10:45:00 +02:00
|
|
|
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
|
2019-11-10 21:30:26 +01:00
|
|
|
use Engelsystem\Models\BaseModel;
|
|
|
|
use Engelsystem\Models\News;
|
2020-01-02 15:08:08 +01:00
|
|
|
use Engelsystem\Models\NewsComment;
|
2019-12-07 21:14:08 +01:00
|
|
|
use Engelsystem\Models\Question;
|
2018-10-06 14:15:54 +02:00
|
|
|
use Engelsystem\Models\User\Contact;
|
|
|
|
use Engelsystem\Models\User\HasUserModel;
|
|
|
|
use Engelsystem\Models\User\PersonalData;
|
|
|
|
use Engelsystem\Models\User\Settings;
|
|
|
|
use Engelsystem\Models\User\State;
|
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
use Engelsystem\Test\Unit\HasDatabase;
|
2019-10-08 13:57:50 +02:00
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
2019-11-06 13:16:00 +01:00
|
|
|
use Exception;
|
2019-12-07 21:14:08 +01:00
|
|
|
use Illuminate\Support\Str;
|
2018-10-06 14:15:54 +02:00
|
|
|
|
|
|
|
class UserTest extends TestCase
|
|
|
|
{
|
2019-04-24 10:45:00 +02:00
|
|
|
use ArraySubsetAsserts;
|
2018-10-06 14:15:54 +02:00
|
|
|
use HasDatabase;
|
|
|
|
|
2019-11-10 21:30:26 +01:00
|
|
|
/** @var string[] */
|
2018-10-06 14:15:54 +02:00
|
|
|
protected $data = [
|
|
|
|
'name' => 'lorem',
|
|
|
|
'password' => '',
|
|
|
|
'email' => 'foo@bar.batz',
|
|
|
|
'api_key' => '',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function hasOneRelationsProvider()
|
|
|
|
{
|
|
|
|
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
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Models\User\User::contact
|
|
|
|
* @covers \Engelsystem\Models\User\User::personalData
|
|
|
|
* @covers \Engelsystem\Models\User\User::settings
|
|
|
|
* @covers \Engelsystem\Models\User\User::state
|
|
|
|
*
|
|
|
|
* @dataProvider hasOneRelationsProvider
|
|
|
|
*
|
|
|
|
* @param string $class
|
|
|
|
* @param string $name
|
|
|
|
* @param array $data
|
2019-11-06 13:16:00 +01:00
|
|
|
* @throws Exception
|
2018-10-06 14:15:54 +02:00
|
|
|
*/
|
|
|
|
public function testHasOneRelations($class, $name, $data)
|
|
|
|
{
|
|
|
|
$user = new User($this->data);
|
|
|
|
$user->save();
|
|
|
|
|
|
|
|
/** @var HasUserModel $contact */
|
|
|
|
$contact = new $class($data);
|
|
|
|
$contact->user()
|
|
|
|
->associate($user)
|
|
|
|
->save();
|
|
|
|
|
|
|
|
$this->assertArraySubset($data, (array)$user->{$name}->attributesToArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-10 21:30:26 +01:00
|
|
|
* @covers \Engelsystem\Models\User\User::news()
|
2019-10-31 20:02:34 +01:00
|
|
|
*
|
|
|
|
* @dataProvider hasManyRelationsProvider
|
|
|
|
*
|
2019-11-10 21:30:26 +01:00
|
|
|
* @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
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-10 21:30:26 +01:00
|
|
|
* @return array[]
|
2019-10-31 20:02:34 +01:00
|
|
|
*/
|
|
|
|
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,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
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);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-11-10 21:30:26 +01:00
|
|
|
/**
|
|
|
|
* Prepare test
|
|
|
|
*/
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->initDatabase();
|
|
|
|
}
|
2018-10-06 14:15:54 +02:00
|
|
|
}
|