env and vendor added temporary
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\UserActivity\Tests\Unit\Listeners;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
use Vanguard\User;
|
||||
|
||||
abstract class ListenerTestCase extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected User $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->user = User::factory()->create();
|
||||
$this->be($this->user);
|
||||
}
|
||||
|
||||
protected function assertMessageLogged($msg, $user = null): void
|
||||
{
|
||||
$this->assertDatabaseHas('user_activity', [
|
||||
'user_id' => $user ? $user->id : $this->user->id,
|
||||
'ip_address' => \Request::ip(),
|
||||
'user_agent' => \Request::header('User-agent'),
|
||||
'description' => $msg,
|
||||
]);
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\UserActivity\Tests\Unit\Listeners;
|
||||
|
||||
use Vanguard\Events\Permission\Created;
|
||||
use Vanguard\Events\Permission\Deleted;
|
||||
use Vanguard\Events\Permission\Updated;
|
||||
|
||||
// Manually require the base test case to avoid issues while running automated tests
|
||||
require_once __DIR__.'/ListenerTestCase.php';
|
||||
|
||||
class PermissionEventsSubscriberTest extends \Vanguard\UserActivity\Tests\Unit\Listeners\ListenerTestCase
|
||||
{
|
||||
protected \Vanguard\Permission $perm;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->perm = \Vanguard\Permission::factory()->create();
|
||||
}
|
||||
|
||||
protected function assertMessageLogged($msg, $user = null): void
|
||||
{
|
||||
$this->assertDatabaseHas('user_activity', [
|
||||
'user_id' => $user ? $user->id : $this->user->id,
|
||||
'ip_address' => \Request::ip(),
|
||||
'user_agent' => \Request::header('User-agent'),
|
||||
'description' => $msg,
|
||||
]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onCreate()
|
||||
{
|
||||
event(new Created($this->perm));
|
||||
$this->assertMessageLogged("Created new permission called {$this->perm->display_name}.");
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onUpdate()
|
||||
{
|
||||
event(new Updated($this->perm));
|
||||
$this->assertMessageLogged("Updated the permission named {$this->perm->display_name}.");
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onDelete()
|
||||
{
|
||||
event(new Deleted($this->perm));
|
||||
$this->assertMessageLogged("Deleted permission named {$this->perm->display_name}.");
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\UserActivity\Tests\Unit\Listeners;
|
||||
|
||||
use Vanguard\Events\Role\Created;
|
||||
use Vanguard\Events\Role\Deleted;
|
||||
use Vanguard\Events\Role\PermissionsUpdated;
|
||||
use Vanguard\Events\Role\Updated;
|
||||
|
||||
class RoleEventsSubscriberTest extends ListenerTestCase
|
||||
{
|
||||
protected \Vanguard\Role $role;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->role = \Vanguard\Role::factory()->create();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onCreate()
|
||||
{
|
||||
event(new Created($this->role));
|
||||
$this->assertMessageLogged("Created new role called {$this->role->display_name}.");
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onUpdate()
|
||||
{
|
||||
event(new Updated($this->role));
|
||||
$this->assertMessageLogged("Updated role with name {$this->role->display_name}.");
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onDelete()
|
||||
{
|
||||
event(new Deleted($this->role));
|
||||
$this->assertMessageLogged("Deleted role named {$this->role->display_name}.");
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onPermissionsUpdate()
|
||||
{
|
||||
event(new PermissionsUpdated());
|
||||
$this->assertMessageLogged('Updated role permissions.');
|
||||
}
|
||||
}
|
||||
+200
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\UserActivity\Tests\Unit\Listeners;
|
||||
|
||||
use Tests\UpdatesSettings;
|
||||
|
||||
class UserEventsSubscriberTest extends ListenerTestCase
|
||||
{
|
||||
use UpdatesSettings;
|
||||
|
||||
protected \Vanguard\User $theUser;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->theUser = \Vanguard\User::factory()->create();
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onLogin()
|
||||
{
|
||||
event(new \Vanguard\Events\User\LoggedIn);
|
||||
$this->assertMessageLogged('Logged in.');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onLogout()
|
||||
{
|
||||
event(new \Vanguard\Events\User\LoggedOut());
|
||||
$this->assertMessageLogged('Logged out.');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onRegister()
|
||||
{
|
||||
$this->setSettings([
|
||||
'reg_enabled' => true,
|
||||
'reg_email_confirmation' => true,
|
||||
]);
|
||||
|
||||
$user = \Vanguard\User::factory()->create();
|
||||
|
||||
event(new \Illuminate\Auth\Events\Registered($user));
|
||||
|
||||
$this->assertMessageLogged('Created an account.', $user);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onAvatarChange()
|
||||
{
|
||||
event(new \Vanguard\Events\User\ChangedAvatar);
|
||||
$this->assertMessageLogged('Updated profile avatar.');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onProfileDetailsUpdate()
|
||||
{
|
||||
event(new \Vanguard\Events\User\UpdatedProfileDetails);
|
||||
$this->assertMessageLogged('Updated profile details.');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onDelete()
|
||||
{
|
||||
event(new \Vanguard\Events\User\Deleted($this->theUser));
|
||||
|
||||
$message = sprintf(
|
||||
'Deleted user %s.',
|
||||
$this->theUser->present()->nameOrEmail
|
||||
);
|
||||
|
||||
$this->assertMessageLogged($message);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onBan()
|
||||
{
|
||||
event(new \Vanguard\Events\User\Banned($this->theUser));
|
||||
|
||||
$message = sprintf(
|
||||
'Banned user %s.',
|
||||
$this->theUser->present()->nameOrEmail
|
||||
);
|
||||
|
||||
$this->assertMessageLogged($message);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onUpdateByAdmin()
|
||||
{
|
||||
event(new \Vanguard\Events\User\UpdatedByAdmin($this->theUser));
|
||||
|
||||
$message = sprintf(
|
||||
'Updated profile details for %s.',
|
||||
$this->theUser->present()->nameOrEmail
|
||||
);
|
||||
|
||||
$this->assertMessageLogged($message);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onCreate()
|
||||
{
|
||||
event(new \Vanguard\Events\User\Created($this->theUser));
|
||||
|
||||
$message = sprintf(
|
||||
'Created an account for user %s.',
|
||||
$this->theUser->present()->nameOrEmail
|
||||
);
|
||||
|
||||
$this->assertMessageLogged($message);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onSettingsUpdate()
|
||||
{
|
||||
event(new \Vanguard\Events\Settings\Updated);
|
||||
$this->assertMessageLogged('Updated website settings.');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onTwoFactorEnable()
|
||||
{
|
||||
event(new \Vanguard\Events\User\TwoFactorEnabled);
|
||||
$this->assertMessageLogged('Enabled Two-Factor Authentication.');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onTwoFactorDisable()
|
||||
{
|
||||
event(new \Vanguard\Events\User\TwoFactorDisabled);
|
||||
$this->assertMessageLogged('Disabled Two-Factor Authentication.');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onTwoFactorEnabledByAdmin()
|
||||
{
|
||||
event(new \Vanguard\Events\User\TwoFactorEnabledByAdmin($this->theUser));
|
||||
|
||||
$message = sprintf(
|
||||
'Enabled Two-Factor Authentication for user %s.',
|
||||
$this->theUser->present()->nameOrEmail
|
||||
);
|
||||
|
||||
$this->assertMessageLogged($message);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onTwoFactorDisabledByAdmin()
|
||||
{
|
||||
event(new \Vanguard\Events\User\TwoFactorDisabledByAdmin($this->theUser));
|
||||
|
||||
$message = sprintf(
|
||||
'Disabled Two-Factor Authentication for user %s.',
|
||||
$this->theUser->present()->nameOrEmail
|
||||
);
|
||||
|
||||
$this->assertMessageLogged($message);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onPasswordResetEmailRequest()
|
||||
{
|
||||
event(new \Vanguard\Events\User\RequestedPasswordResetEmail($this->user));
|
||||
$this->assertMessageLogged('Requested password reset email.');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onPasswordReset()
|
||||
{
|
||||
event(new \Illuminate\Auth\Events\PasswordReset($this->user));
|
||||
$this->assertMessageLogged('Reseted password using "Forgot Password" option.');
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onStartImpersonating()
|
||||
{
|
||||
$impersonated = \Vanguard\User::factory()->create([
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
]);
|
||||
|
||||
event(new \Lab404\Impersonate\Events\TakeImpersonation($this->user, $impersonated));
|
||||
|
||||
$this->assertMessageLogged("Started impersonating user John Doe (ID: {$impersonated->id})");
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function onStopImpersonating()
|
||||
{
|
||||
$impersonated = \Vanguard\User::factory()->create([
|
||||
'first_name' => 'John',
|
||||
'last_name' => 'Doe',
|
||||
]);
|
||||
|
||||
event(new \Lab404\Impersonate\Events\LeaveImpersonation($this->user, $impersonated));
|
||||
|
||||
$this->assertMessageLogged("Stopped impersonating user John Doe (ID: {$impersonated->id})");
|
||||
}
|
||||
}
|
||||
+136
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\UserActivity\Tests\Unit\Repositories\Activity;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Testing\Assert;
|
||||
use Tests\TestCase;
|
||||
use Vanguard\User;
|
||||
use Vanguard\UserActivity\Activity;
|
||||
use Vanguard\UserActivity\Repositories\Activity\EloquentActivity;
|
||||
|
||||
class EloquentActivityTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* @var EloquentActivity
|
||||
*/
|
||||
protected $repo;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->repo = app(EloquentActivity::class);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function log()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Carbon::setTestNow(Carbon::now());
|
||||
|
||||
$data = [
|
||||
'user_id' => $user->id,
|
||||
'ip_address' => '123.456.789.012',
|
||||
'user_agent' => 'foo',
|
||||
'description' => 'descriptionnnn',
|
||||
];
|
||||
|
||||
$this->repo->log($data);
|
||||
|
||||
$this->assertDatabaseHas('user_activity', $data);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function paginate_activities_for_user()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$activities = Activity::factory()->times(10)->create(['user_id' => $user->id]);
|
||||
|
||||
$result = $this->repo->paginateActivitiesForUser($user->id, 6)->toArray();
|
||||
|
||||
$this->assertEquals(6, count($result['data']));
|
||||
$this->assertEquals(10, $result['total']);
|
||||
$this->assertEquals($activities[0]->toArray(), $result['data'][0]);
|
||||
$this->assertEquals($activities[5]->toArray(), $result['data'][5]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function latest_activities_for_user()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
Carbon::setTestNow(Carbon::now()->subDay());
|
||||
$activities1 = Activity::factory()->times(5)->create(['user_id' => $user->id]);
|
||||
|
||||
Carbon::setTestNow(null);
|
||||
$activities2 = Activity::factory()->times(5)->create(['user_id' => $user->id]);
|
||||
|
||||
$result = $this->repo->getLatestActivitiesForUser($user->id, 6)->toArray();
|
||||
|
||||
$this->assertEquals(6, count($result));
|
||||
$this->assertEquals($activities2[0]->toArray(), $result[0]);
|
||||
$this->assertEquals($activities1[0]->toArray(), $result[5]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function paginate_activities()
|
||||
{
|
||||
$activities = Activity::factory()->times(10)->create();
|
||||
|
||||
$result = $this->repo->paginateActivities(6)->toArray();
|
||||
|
||||
$this->assertEquals(6, count($result['data']));
|
||||
$this->assertEquals(10, $result['total']);
|
||||
|
||||
Assert::assertArraySubset($activities[0]->toArray(), $result['data'][0]);
|
||||
Assert::assertArraySubset($activities[5]->toArray(), $result['data'][5]);
|
||||
}
|
||||
|
||||
/** @test */
|
||||
public function userActivityForPeriod()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$now = Carbon::now();
|
||||
|
||||
Carbon::setTestNow($now->copy()->subDays(15));
|
||||
Activity::factory()->times(5)->create(['user_id' => $user->id]);
|
||||
|
||||
Carbon::setTestNow($now->copy()->subDays(11));
|
||||
Activity::factory()->times(2)->create(['user_id' => $user->id]);
|
||||
|
||||
Carbon::setTestNow($now->copy()->subDays(5));
|
||||
Activity::factory()->times(3)->create(['user_id' => $user->id]);
|
||||
|
||||
Carbon::setTestNow($now->copy()->subDays(2));
|
||||
Activity::factory()->times(2)->create(['user_id' => $user->id]);
|
||||
|
||||
Carbon::setTestNow(null);
|
||||
|
||||
$result = $this->repo->userActivityForPeriod(
|
||||
$user->id,
|
||||
Carbon::now()->subWeeks(2),
|
||||
Carbon::now()
|
||||
);
|
||||
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(14)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(13)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(12)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(11)->toDateString()), 2);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(10)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(9)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(8)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(7)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(6)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(5)->toDateString()), 3);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(4)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(3)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(2)->toDateString()), 2);
|
||||
$this->assertEquals($result->get(Carbon::now()->subDays(1)->toDateString()), 0);
|
||||
$this->assertEquals($result->get(Carbon::now()->toDateString()), 0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user