Skip to content

Commit

Permalink
馃И Test for hidden attribute feature
Browse files Browse the repository at this point in the history
  • Loading branch information
yungts97 committed Mar 15, 2022
1 parent 073cde2 commit 337c460
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
26 changes: 24 additions & 2 deletions tests/Feature/UserActivityLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Yungts97\LaravelUserActivityLog\Tests\Feature;

use Illuminate\Support\Facades\Auth;
use Yungts97\LaravelUserActivityLog\Tests\TestCase;
use Yungts97\LaravelUserActivityLog\Tests\Models\User;
use Yungts97\LaravelUserActivityLog\Tests\Models\Post;
use Yungts97\LaravelUserActivityLog\Tests\Models\User;
use Yungts97\LaravelUserActivityLog\Tests\Models\PostWithHidden;
use Yungts97\LaravelUserActivityLog\Tests\Models\PostWithoutLog;
use Illuminate\Support\Facades\Auth;

class UserActivityLogTest extends TestCase
{
Expand Down Expand Up @@ -92,4 +93,25 @@ function it_can_skip_logging()
'table_name' => 'posts',
]);
}

/** @test */
function it_can_hide_attribute_on_logging()
{
//user login
$user = User::first();
Auth::login($user);

//create a post
$newPost = new PostWithHidden(['name' => 'Post 1']);
$user->posts()->save($newPost);
$data = json_encode($newPost->makeHidden(['name'])->toArray());

//checking database doesn't have the activity log record
$this->assertDatabaseHas('logs', [
'log_type' => 'create',
'user_id' => $user->id,
'table_name' => 'posts',
'data' => $data
]);
}
}
19 changes: 19 additions & 0 deletions tests/Models/PostWithHidden.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Yungts97\LaravelUserActivityLog\Tests\Models;

class PostWithHidden extends BaseModel
{
protected $table = 'posts';

protected $guarded = [];

protected $fillable = ['id', 'name'];

public $log_hidden = ['name']; // to hide the "name" attribute on logging

public function user()
{
return $this->belongsTo(User::class);
}
}

0 comments on commit 337c460

Please sign in to comment.