2019-10-31 20:02:34 +01:00
|
|
|
<?php
|
2019-11-10 21:30:26 +01:00
|
|
|
|
2019-10-31 20:02:34 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-11-10 21:30:26 +01:00
|
|
|
namespace Engelsystem\Test\Unit\Models;
|
|
|
|
|
|
|
|
use Engelsystem\Models\News;
|
2019-10-31 20:02:34 +01:00
|
|
|
use Engelsystem\Models\User\User;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class provides tests for the News model.
|
|
|
|
*/
|
2020-05-11 19:57:25 +02:00
|
|
|
class NewsTest extends ModelTest
|
2019-10-31 20:02:34 +01:00
|
|
|
{
|
2019-11-10 21:30:26 +01:00
|
|
|
/** @var array */
|
2019-10-31 20:02:34 +01:00
|
|
|
private array $newsData;
|
|
|
|
|
|
|
|
private User $user;
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2021-06-29 00:27:57 +02:00
|
|
|
$this->user = User::factory()->create();
|
2019-10-31 20:02:34 +01:00
|
|
|
$this->newsData = [
|
|
|
|
'title' => 'test title',
|
|
|
|
'text' => 'test text',
|
|
|
|
'user_id' => $this->user->id
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that creating a News item with default values works.
|
|
|
|
*
|
2020-01-02 15:08:08 +01:00
|
|
|
* @covers \Engelsystem\Models\News
|
2019-10-31 20:02:34 +01:00
|
|
|
*/
|
|
|
|
public function testCreateDefault(): void
|
|
|
|
{
|
2019-11-10 21:30:26 +01:00
|
|
|
$news = (new News())->create($this->newsData);
|
|
|
|
$news = $news->find($news->id);
|
2019-10-31 20:02:34 +01:00
|
|
|
|
|
|
|
$this->assertSame(1, $news->id);
|
|
|
|
$this->assertSame($this->newsData['title'], $news->title);
|
|
|
|
$this->assertSame($this->newsData['text'], $news->text);
|
|
|
|
$this->assertFalse($news->is_meeting);
|
|
|
|
}
|
|
|
|
|
2020-01-02 15:08:08 +01:00
|
|
|
/**
|
|
|
|
* Tests that accessing the NewsComments of a News works.
|
|
|
|
*
|
|
|
|
* @covers \Engelsystem\Models\News::comments
|
|
|
|
*/
|
|
|
|
public function testNewsComments(): void
|
|
|
|
{
|
|
|
|
$news = (new News())->create($this->newsData);
|
|
|
|
$comment = $news->comments()->create(['text' => 'test comment', 'user_id' => $this->user->id]);
|
|
|
|
|
|
|
|
$comments = $news->comments;
|
|
|
|
$this->assertCount(1, $comments);
|
|
|
|
$this->assertEquals($comment->toArray(), $news->comments->first()->toArray());
|
|
|
|
}
|
|
|
|
|
2021-12-10 22:24:18 +01:00
|
|
|
/**
|
|
|
|
* Tests that text more tags work
|
|
|
|
*
|
|
|
|
* @covers \Engelsystem\Models\News::text
|
|
|
|
*/
|
|
|
|
public function testTextMore(): void
|
|
|
|
{
|
|
|
|
$news = new News($this->newsData);
|
|
|
|
|
|
|
|
$news->text = "Foo\n\n\nBar";
|
|
|
|
$this->assertEquals("Foo\n\n\nBar", $news->text);
|
|
|
|
$this->assertEquals("Foo\n\n\nBar", $news->text());
|
|
|
|
$this->assertEquals("Foo\n\n\nBar", $news->text(false));
|
|
|
|
|
|
|
|
$news->text = "Foo\n[more]\nBar";
|
|
|
|
$this->assertEquals("Foo\n[more]\nBar", $news->text);
|
|
|
|
$this->assertEquals("Foo\n\nBar", $news->text());
|
|
|
|
$this->assertEquals('Foo', $news->text(false));
|
|
|
|
}
|
|
|
|
|
2019-10-31 20:02:34 +01:00
|
|
|
/**
|
|
|
|
* Tests that creating a News item with all fill values works.
|
|
|
|
*
|
2020-01-02 15:08:08 +01:00
|
|
|
* @covers \Engelsystem\Models\News
|
2019-10-31 20:02:34 +01:00
|
|
|
*/
|
|
|
|
public function testCreate(): void
|
|
|
|
{
|
2019-11-10 21:30:26 +01:00
|
|
|
$news = (new News())->create(
|
2020-12-27 02:51:05 +01:00
|
|
|
$this->newsData + ['is_meeting' => true, 'is_pinned' => true]
|
2019-10-31 20:02:34 +01:00
|
|
|
);
|
2019-11-10 21:30:26 +01:00
|
|
|
$news = $news->find($news->id);
|
2019-10-31 20:02:34 +01:00
|
|
|
|
|
|
|
$this->assertSame(1, $news->id);
|
|
|
|
$this->assertSame($this->newsData['title'], $news->title);
|
|
|
|
$this->assertSame($this->newsData['text'], $news->text);
|
|
|
|
$this->assertTrue($news->is_meeting);
|
2020-12-27 02:51:05 +01:00
|
|
|
$this->assertTrue($news->is_pinned);
|
2019-10-31 20:02:34 +01:00
|
|
|
}
|
|
|
|
}
|