Merge pull request #675 from weeman1337/feature-news-comments-migration

Introduce the NewsComments model
main
Igor Scheller 5 years ago committed by GitHub
commit aa91ab4cf7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -128,7 +128,7 @@ class CreateNewsTable extends Migration
private function copyNewToPreviousNewsTable(): void private function copyNewToPreviousNewsTable(): void
{ {
$connection = $this->schema->getConnection(); $connection = $this->schema->getConnection();
/** @var Collection[]|stdClass[] $newsRecords */ /** @var Collection|stdClass[] $newsRecords */
$newsRecords = $connection $newsRecords = $connection
->table('new_news') ->table('new_news')
->get(); ->get();

@ -0,0 +1,130 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Schema\Blueprint;
use stdClass;
/**
* This migration creates the news_comments table and copies the existing NewsComments table records to the new one.
*/
class CreateNewsCommentsTable extends Migration
{
use ChangesReferences;
use Reference;
/**
* Creates the news_comments table, copies the data and drops the NewsComments table.
*/
public function up(): void
{
$this->createNewNewsCommentsTable();
if ($this->schema->hasTable('NewsComments')) {
$this->copyPreviousToNewNewsCommentsTable();
$this->changeReferences(
'NewsComments',
'ID',
'news_comments',
'id',
'unsignedInteger'
);
$this->schema->drop('NewsComments');
}
}
/**
* Recreates the previous NewsComments table, copies back the data and drops the new news_comments table.
*/
public function down(): void
{
$this->createPreviousNewsCommentsTable();
$this->copyNewToPreviousNewsCommentsTable();
$this->changeReferences(
'news_comments',
'id',
'NewsComments',
'ID',
'unsignedInteger'
);
$this->schema->drop('news_comments');
}
/**
* @return void
*/
private function createNewNewsCommentsTable(): void
{
$this->schema->create('news_comments', function (Blueprint $table) {
$table->increments('id');
$this->references($table, 'news', 'news_id');
$table->text('text');
$this->referencesUser($table, false);
$table->timestamps();
});
}
/**
* @return void
*/
private function copyPreviousToNewNewsCommentsTable(): void
{
$connection = $this->schema->getConnection();
/** @var stdClass[] $previousNewsCommentsRecords */
$previousNewsCommentsRecords = $connection
->table('NewsComments')
->get();
foreach ($previousNewsCommentsRecords as $previousNewsComment) {
$connection->table('news_comments')->insert([
'id' => $previousNewsComment->ID,
'news_id' => $previousNewsComment->Refid,
'text' => $previousNewsComment->Text,
'user_id' => $previousNewsComment->UID,
'created_at' => $previousNewsComment->Datum,
'updated_at' => $previousNewsComment->Datum,
]);
}
}
/**
* @return void
*/
private function createPreviousNewsCommentsTable(): void
{
$this->schema->create('NewsComments', function (Blueprint $table) {
$table->increments('ID');
$this->references($table, 'news', 'Refid');
$table->dateTime('Datum');
$table->text('Text');
$this->references($table, 'users', 'UID');
});
}
/**
* @return void
*/
private function copyNewToPreviousNewsCommentsTable(): void
{
$connection = $this->schema->getConnection();
/** @var Collection|stdClass[] $newsCommentsRecords */
$newsCommentsRecords = $connection
->table('news_comments')
->get();
foreach ($newsCommentsRecords as $newsCommentRecord) {
$connection->table('NewsComments')->insert([
'ID' => $newsCommentRecord->id,
'Datum' => $newsCommentRecord->created_at,
'Refid' => $newsCommentRecord->news_id,
'Text' => $newsCommentRecord->text,
'UID' => $newsCommentRecord->user_id,
]);
}
}
}

@ -1,8 +1,6 @@
<?php <?php
use Engelsystem\Database\DB;
use Engelsystem\Models\News; use Engelsystem\Models\News;
use Engelsystem\Models\User\User;
/** /**
* @return string * @return string
@ -116,7 +114,7 @@ function display_news(News $news): string
. '<span class="glyphicon glyphicon-comment"></span> ' . '<span class="glyphicon glyphicon-comment"></span> '
. __('Comments') . ' &raquo;</a> ' . __('Comments') . ' &raquo;</a> '
. '<span class="badge">' . '<span class="badge">'
. count(DB::select('SELECT `ID` FROM `NewsComments` WHERE `Refid`=?', [$news->id])) . $news->comments()->count()
. '</span>'; . '</span>';
} }
$html .= '</div>'; $html .= '</div>';
@ -141,17 +139,10 @@ function user_news_comments()
) { ) {
if ($request->hasPostData('submit') && $request->has('text')) { if ($request->hasPostData('submit') && $request->has('text')) {
$text = $request->input('text'); $text = $request->input('text');
DB::insert(' $news->comments()->create([
INSERT INTO `NewsComments` (`Refid`, `Datum`, `Text`, `UID`) 'text' => $text,
VALUES (?, ?, ?, ?) 'user_id' => $user->id,
', ]);
[
$nid,
date('Y-m-d H:i:s'),
$text,
$user->id,
]
);
engelsystem_log('Created news_comment: ' . $text); engelsystem_log('Created news_comment: ' . $text);
$html .= success(__('Entry saved.'), true); $html .= success(__('Entry saved.'), true);
@ -159,18 +150,12 @@ function user_news_comments()
$html .= display_news($news); $html .= display_news($news);
$comments = DB::select( foreach ($news->comments as $comment) {
'SELECT * FROM `NewsComments` WHERE `Refid`=? ORDER BY \'ID\'',
[$nid]
);
foreach ($comments as $comment) {
$user_source = User::find($comment['UID']);
$html .= '<div class="panel panel-default">'; $html .= '<div class="panel panel-default">';
$html .= '<div class="panel-body">' . nl2br(htmlspecialchars($comment['Text'])) . '</div>'; $html .= '<div class="panel-body">' . nl2br(htmlspecialchars($comment->text)) . '</div>';
$html .= '<div class="panel-footer text-muted">'; $html .= '<div class="panel-footer text-muted">';
$html .= '<span class="glyphicon glyphicon-time"></span> ' . $comment['Datum'] . '&emsp;'; $html .= '<span class="glyphicon glyphicon-time"></span> ' . $comment->created_at->format('Y-m-d H:i') . '&emsp;';
$html .= User_Nick_render($user_source); $html .= User_Nick_render($comment->user);
$html .= '</div>'; $html .= '</div>';
$html .= '</div>'; $html .= '</div>';
} }

@ -6,15 +6,19 @@ namespace Engelsystem\Models;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Models\User\UsesUserModel; use Engelsystem\Models\User\UsesUserModel;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Builder as QueryBuilder;
/** /**
* @property int $id * @property int $id
* @property string $title * @property string $title
* @property string $text * @property string $text
* @property bool $is_meeting * @property bool $is_meeting
* @property Carbon|null $created_at * @property Carbon|null $created_at
* @property Carbon|null $updated_at * @property Carbon|null $updated_at
*
* @property-read Collection|NewsComment[] $comments
* *
* @method static QueryBuilder|LogEntry[] whereId($value) * @method static QueryBuilder|LogEntry[] whereId($value)
* @method static QueryBuilder|LogEntry[] whereTitle($value) * @method static QueryBuilder|LogEntry[] whereTitle($value)
@ -42,4 +46,13 @@ class News extends BaseModel
'is_meeting', 'is_meeting',
'user_id', 'user_id',
]; ];
/**
* @return HasMany
*/
public function comments(): HasMany
{
return $this->hasMany(NewsComment::class)
->orderBy('created_at');
}
} }

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Models;
use Carbon\Carbon;
use Engelsystem\Models\User\UsesUserModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Query\Builder as QueryBuilder;
/**
* This class represents a news comment.
*
* @property int $id
* @property int $news_id
* @property string $text
* @property News $news
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
*
* @method static QueryBuilder|LogEntry[] whereId($value)
* @method static QueryBuilder|LogEntry[] whereText($value)
* @method static QueryBuilder|LogEntry[] whereCreatedAt($value)
* @method static QueryBuilder|LogEntry[] whereUpdatedAt($value)
*/
class NewsComment extends BaseModel
{
use UsesUserModel;
/** @var bool Enable timestamps */
public $timestamps = true;
/** @var string[] */
protected $fillable = [
'news_id',
'text',
'user_id',
];
/**
* @return BelongsTo
*/
public function news(): BelongsTo
{
return $this->belongsTo(News::class);
}
}

@ -5,6 +5,8 @@ namespace Engelsystem\Models\User;
use Carbon\Carbon; use Carbon\Carbon;
use Engelsystem\Models\BaseModel; use Engelsystem\Models\BaseModel;
use Engelsystem\Models\News; use Engelsystem\Models\News;
use Engelsystem\Models\NewsComment;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Query\Builder as QueryBuilder; use Illuminate\Database\Query\Builder as QueryBuilder;
@ -23,6 +25,7 @@ use Illuminate\Database\Query\Builder as QueryBuilder;
* @property-read QueryBuilder|PersonalData $personalData * @property-read QueryBuilder|PersonalData $personalData
* @property-read QueryBuilder|Settings $settings * @property-read QueryBuilder|Settings $settings
* @property-read QueryBuilder|State $state * @property-read QueryBuilder|State $state
* @property-read Collection|NewsComment[] $newsComments
* *
* @method static QueryBuilder|User whereId($value) * @method static QueryBuilder|User whereId($value)
* @method static QueryBuilder|User[] whereName($value) * @method static QueryBuilder|User[] whereName($value)
@ -105,4 +108,12 @@ class User extends BaseModel
{ {
return $this->hasMany(News::class); return $this->hasMany(News::class);
} }
/**
* @return HasMany
*/
public function newsComments(): HasMany
{
return $this->hasMany(NewsComment::class);
}
} }

@ -0,0 +1,126 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Test\Unit\Models;
use Engelsystem\Models\News;
use Engelsystem\Models\NewsComment;
use Engelsystem\Models\User\User;
use Engelsystem\Test\Unit\HasDatabase;
use Engelsystem\Test\Unit\TestCase;
/**
* This class provides tests for the NewsComments model.
*/
class NewsCommentsTest extends TestCase
{
use HasDatabase;
/** @var User */
private $user;
/** @var News */
private $news;
/** @var array */
private $newsCommentData;
/**
* Sets up some test objects and test data.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();
$this->initDatabase();
$this->user = User::create([
'name' => 'lorem',
'password' => '',
'email' => 'lorem@example.com',
'api_key' => '',
]);
$this->news = News::create([
'title' => 'test title',
'text' => 'test text',
'user_id' => $this->user->id,
]);
$this->newsCommentData = [
'news_id' => $this->news->id,
'text' => 'test comment',
'user_id' => $this->user->id,
];
}
/**
* Tests that a NewsComment can be created and loaded.
*
* @return void
*/
public function testCreate(): void
{
$createdNewsComment = NewsComment::create($this->newsCommentData);
$newsComment = NewsComment::find($createdNewsComment->id);
$this->assertInstanceOf(NewsComment::class, $newsComment);
$this->assertEquals($this->newsCommentData['news_id'], $newsComment->news_id);
$this->assertSame($this->newsCommentData['text'], $newsComment->text);
$this->assertEquals($this->newsCommentData['user_id'], $newsComment->user_id);
}
/**
* Tests that accessing the User of a NewsComment works.
*
* @return void
*/
public function testUser(): void
{
$newsComment = NewsComment::create($this->newsCommentData);
$this->assertInstanceOf(User::class, $newsComment->user);
$this->assertSame($this->user->id, $newsComment->user->id);
}
/**
* Tests that accessing the News of a NewsComment works.
*
* @return void
*/
public function testNews(): void
{
$newsComment = NewsComment::create($this->newsCommentData);
$this->assertInstanceOf(News::class, $newsComment->news);
$this->assertSame($this->news->id, $newsComment->news->id);
}
/**
* Tests that accessing the NewsComments of a News works.
*
* @return void
*/
public function testNewsComments(): void
{
$newsComment = NewsComment::create($this->newsCommentData);
$comments = $this->news->comments;
$this->assertCount(1, $comments);
$comment = $comments->first();
$this->assertSame($newsComment->id, $comment->id);
}
/**
* Tests that accessing the NewsComments of an User works.
*
* @return void
*/
public function testUserNewsComments(): void
{
$newsComment = NewsComment::create($this->newsCommentData);
$comments = $this->user->newsComments;
$this->assertCount(1, $comments);
$comment = $comments->first();
$this->assertSame($newsComment->id, $comment->id);
}
}
Loading…
Cancel
Save