Commit ce005fdb authored by bvipul's avatar bvipul

Completed Unit test for Pages and added created by relationship in Page with User

parent 130e2c18
......@@ -32,17 +32,17 @@ class PagesTableController extends Controller
{
return Datatables::of($this->pages->getForDataTable())
->escapeColumns(['title'])
->addColumn('status', function ($pages) {
return $pages->status_label;
->addColumn('status', function ($page) {
return $page->status_label;
})
->addColumn('created_at', function ($pages) {
return Carbon::parse($pages->created_at)->toDateString();
->addColumn('created_at', function ($page) {
return $page->created_at->toDateString();
})
->addColumn('updated_at', function ($pages) {
return Carbon::parse($pages->updated_at)->toDateString();
->addColumn('created_by', function ($page) {
return $page->created_by;
})
->addColumn('actions', function ($pages) {
return $pages->action_buttons;
->addColumn('actions', function ($page) {
return $page->action_buttons;
})
->make(true);
}
......
......@@ -4,13 +4,15 @@ namespace App\Models\Page;
use App\Models\BaseModel;
use App\Models\ModelTrait;
use App\Models\Page\Traits\Attribute\PageAttribute;
use App\Models\Page\Traits\PageRelationship;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Page\Traits\Attribute\PageAttribute;
class Page extends BaseModel
{
use ModelTrait,
SoftDeletes,
PageRelationship,
PageAttribute {
// PageAttribute::getEditButtonAttribute insteadof ModelTrait;
}
......@@ -29,6 +31,17 @@ class Page extends BaseModel
*/
protected $guarded = ['id'];
/**
* The default values for attributes
*
* @var array
*/
protected $attributes = [
'created_by' => 1
];
protected $with = ['owner'];
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
......
......@@ -13,8 +13,8 @@ trait PageAttribute
public function getActionButtonsAttribute()
{
return '<div class="btn-group action-btn">
'.$this->getEditButtonAttribute('edit-cms-pages', 'admin.pages.edit').'
'.$this->getDeleteButtonAttribute('delete-cms-pages', 'admin.pages.destroy').'
'.$this->getEditButtonAttribute('edit-page', 'admin.pages.edit').'
'.$this->getDeleteButtonAttribute('delete-page', 'admin.pages.destroy').'
</div>';
}
......
<?php
namespace App\Models\Page\Traits;
use App\Models\Access\User\User;
trait PageRelationship
{
public function owner()
{
return $this->belongsTo(User::class, 'created_by');
}
}
\ No newline at end of file
......@@ -25,12 +25,14 @@ class PagesRepository extends BaseRepository
public function getForDataTable()
{
return $this->query()
->leftjoin(config('access.users_table'), config('access.users_table').'.id', '=', config('module.pages.table').'.created_by')
->select([
config('module.pages.table').'.id',
config('module.pages.table').'.title',
config('module.pages.table').'.status',
config('module.pages.table').'.created_at',
config('module.pages.table').'.updated_at',
config('access.users_table').'.first_name as created_by'
]);
}
......@@ -47,7 +49,7 @@ class PagesRepository extends BaseRepository
throw new GeneralException(trans('exceptions.backend.pages.already_exists'));
}
//Making extra fields
// Making extra fields
$input['page_slug'] = str_slug($input['title']);
$input['status'] = isset($input['status']) ? 1 : 0;
$input['created_by'] = auth()->id();
......@@ -75,7 +77,7 @@ class PagesRepository extends BaseRepository
throw new GeneralException(trans('exceptions.backend.pages.already_exists'));
}
//Making extra fields
// Making extra fields
$input['page_slug'] = str_slug($input['title']);
$input['status'] = isset($input['status']) ? 1 : 0;
$input['updated_by'] = access()->user()->id;
......
......@@ -126,6 +126,7 @@ return [
'status' => 'Status',
'createdat' => 'Created At',
'updatedat' => 'Updated At',
'createdby' => 'Created By',
'all' => 'All',
],
],
......
......@@ -24,7 +24,7 @@
<th>{{ trans('labels.backend.pages.table.title') }}</th>
<th>{{ trans('labels.backend.pages.table.status') }}</th>
<th>{{ trans('labels.backend.pages.table.createdat') }}</th>
<th>{{ trans('labels.backend.pages.table.updatedat') }}</th>
<th>{{ trans('labels.backend.pages.table.createdby') }}</th>
<th>{{ trans('labels.general.actions') }}</th>
</tr>
</thead>
......@@ -77,10 +77,10 @@
{data: 'title', name: '{{config('module.pages.table')}}.title'},
{data: 'status', name: '{{config('module.pages.table')}}.status'},
{data: 'created_at', name: '{{config('module.pages.table')}}.created_at'},
{data: 'updated_at', name: '{{config('module.pages.table')}}.updated_at'},
{data: 'created_by', name: '{{config('access.users_table')}}.first_name'},
{data: 'actions', name: 'actions', searchable: false, sortable: false}
],
order: [[3, "asc"]],
order: [[1, "asc"]],
searchDelay: 500,
dom: 'lBfrtip',
buttons: {
......
<?php
namespace Tests\Unit;
use Tests\TestCase;
use App\Models\Page\Page;
use App\Models\Access\User\User;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class PageTest extends TestCase
{
/** @test */
public function it_has_an_owner()
{
$this->actingAs($this->admin);
$page = create(Page::class);
$this->assertInstanceOf(User::class, $page->owner);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment