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 ...@@ -32,17 +32,17 @@ class PagesTableController extends Controller
{ {
return Datatables::of($this->pages->getForDataTable()) return Datatables::of($this->pages->getForDataTable())
->escapeColumns(['title']) ->escapeColumns(['title'])
->addColumn('status', function ($pages) { ->addColumn('status', function ($page) {
return $pages->status_label; return $page->status_label;
}) })
->addColumn('created_at', function ($pages) { ->addColumn('created_at', function ($page) {
return Carbon::parse($pages->created_at)->toDateString(); return $page->created_at->toDateString();
}) })
->addColumn('updated_at', function ($pages) { ->addColumn('created_by', function ($page) {
return Carbon::parse($pages->updated_at)->toDateString(); return $page->created_by;
}) })
->addColumn('actions', function ($pages) { ->addColumn('actions', function ($page) {
return $pages->action_buttons; return $page->action_buttons;
}) })
->make(true); ->make(true);
} }
......
...@@ -4,13 +4,15 @@ namespace App\Models\Page; ...@@ -4,13 +4,15 @@ namespace App\Models\Page;
use App\Models\BaseModel; use App\Models\BaseModel;
use App\Models\ModelTrait; use App\Models\ModelTrait;
use App\Models\Page\Traits\Attribute\PageAttribute; use App\Models\Page\Traits\PageRelationship;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use App\Models\Page\Traits\Attribute\PageAttribute;
class Page extends BaseModel class Page extends BaseModel
{ {
use ModelTrait, use ModelTrait,
SoftDeletes, SoftDeletes,
PageRelationship,
PageAttribute { PageAttribute {
// PageAttribute::getEditButtonAttribute insteadof ModelTrait; // PageAttribute::getEditButtonAttribute insteadof ModelTrait;
} }
...@@ -29,6 +31,17 @@ class Page extends BaseModel ...@@ -29,6 +31,17 @@ class Page extends BaseModel
*/ */
protected $guarded = ['id']; protected $guarded = ['id'];
/**
* The default values for attributes
*
* @var array
*/
protected $attributes = [
'created_by' => 1
];
protected $with = ['owner'];
public function __construct(array $attributes = []) public function __construct(array $attributes = [])
{ {
parent::__construct($attributes); parent::__construct($attributes);
......
...@@ -13,8 +13,8 @@ trait PageAttribute ...@@ -13,8 +13,8 @@ trait PageAttribute
public function getActionButtonsAttribute() public function getActionButtonsAttribute()
{ {
return '<div class="btn-group action-btn"> return '<div class="btn-group action-btn">
'.$this->getEditButtonAttribute('edit-cms-pages', 'admin.pages.edit').' '.$this->getEditButtonAttribute('edit-page', 'admin.pages.edit').'
'.$this->getDeleteButtonAttribute('delete-cms-pages', 'admin.pages.destroy').' '.$this->getDeleteButtonAttribute('delete-page', 'admin.pages.destroy').'
</div>'; </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 ...@@ -25,12 +25,14 @@ class PagesRepository extends BaseRepository
public function getForDataTable() public function getForDataTable()
{ {
return $this->query() return $this->query()
->leftjoin(config('access.users_table'), config('access.users_table').'.id', '=', config('module.pages.table').'.created_by')
->select([ ->select([
config('module.pages.table').'.id', config('module.pages.table').'.id',
config('module.pages.table').'.title', config('module.pages.table').'.title',
config('module.pages.table').'.status', config('module.pages.table').'.status',
config('module.pages.table').'.created_at', config('module.pages.table').'.created_at',
config('module.pages.table').'.updated_at', config('module.pages.table').'.updated_at',
config('access.users_table').'.first_name as created_by'
]); ]);
} }
...@@ -47,7 +49,7 @@ class PagesRepository extends BaseRepository ...@@ -47,7 +49,7 @@ class PagesRepository extends BaseRepository
throw new GeneralException(trans('exceptions.backend.pages.already_exists')); throw new GeneralException(trans('exceptions.backend.pages.already_exists'));
} }
//Making extra fields // Making extra fields
$input['page_slug'] = str_slug($input['title']); $input['page_slug'] = str_slug($input['title']);
$input['status'] = isset($input['status']) ? 1 : 0; $input['status'] = isset($input['status']) ? 1 : 0;
$input['created_by'] = auth()->id(); $input['created_by'] = auth()->id();
...@@ -75,7 +77,7 @@ class PagesRepository extends BaseRepository ...@@ -75,7 +77,7 @@ class PagesRepository extends BaseRepository
throw new GeneralException(trans('exceptions.backend.pages.already_exists')); throw new GeneralException(trans('exceptions.backend.pages.already_exists'));
} }
//Making extra fields // Making extra fields
$input['page_slug'] = str_slug($input['title']); $input['page_slug'] = str_slug($input['title']);
$input['status'] = isset($input['status']) ? 1 : 0; $input['status'] = isset($input['status']) ? 1 : 0;
$input['updated_by'] = access()->user()->id; $input['updated_by'] = access()->user()->id;
......
...@@ -126,6 +126,7 @@ return [ ...@@ -126,6 +126,7 @@ return [
'status' => 'Status', 'status' => 'Status',
'createdat' => 'Created At', 'createdat' => 'Created At',
'updatedat' => 'Updated At', 'updatedat' => 'Updated At',
'createdby' => 'Created By',
'all' => 'All', 'all' => 'All',
], ],
], ],
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
<th>{{ trans('labels.backend.pages.table.title') }}</th> <th>{{ trans('labels.backend.pages.table.title') }}</th>
<th>{{ trans('labels.backend.pages.table.status') }}</th> <th>{{ trans('labels.backend.pages.table.status') }}</th>
<th>{{ trans('labels.backend.pages.table.createdat') }}</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> <th>{{ trans('labels.general.actions') }}</th>
</tr> </tr>
</thead> </thead>
...@@ -77,10 +77,10 @@ ...@@ -77,10 +77,10 @@
{data: 'title', name: '{{config('module.pages.table')}}.title'}, {data: 'title', name: '{{config('module.pages.table')}}.title'},
{data: 'status', name: '{{config('module.pages.table')}}.status'}, {data: 'status', name: '{{config('module.pages.table')}}.status'},
{data: 'created_at', name: '{{config('module.pages.table')}}.created_at'}, {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} {data: 'actions', name: 'actions', searchable: false, sortable: false}
], ],
order: [[3, "asc"]], order: [[1, "asc"]],
searchDelay: 500, searchDelay: 500,
dom: 'lBfrtip', dom: 'lBfrtip',
buttons: { 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