vendor and env first commit

This commit is contained in:
2025-03-28 08:52:46 +01:00
parent f8388bc81b
commit 8f26283832
10976 changed files with 1349952 additions and 2 deletions
@@ -0,0 +1,134 @@
<?php
namespace Vanguard\UserActivity\Tests\Feature\Api;
use Facades\Tests\Setup\UserFactory;
use Tests\Feature\ApiTestCase;
use Vanguard\User;
use Vanguard\UserActivity\Activity;
use Vanguard\UserActivity\Http\Resources\ActivityResource;
class ActivityTest extends ApiTestCase
{
/** @test */
public function unauthenticated()
{
$this->getJson('/api/activity')->assertStatus(401);
}
/** @test */
public function get_activities_without_permission()
{
$user = User::factory()->create();
$this->actingAs($user, self::API_GUARD)
->getJson('/api/activity')
->assertStatus(403);
}
/** @test */
public function paginate_activities()
{
$user = $this->getUser();
$user2 = User::factory()->create();
$activities = Activity::factory()->times(25)->create(['user_id' => $user->id]);
Activity::factory()->times(10)->create(['user_id' => $user2->id]);
$response = $this->actingAs($user, self::API_GUARD)->getJson('/api/activity');
$transformed = ActivityResource::collection($activities->take(20))->resolve();
$this->assertEquals($response->json('data'), $transformed);
$response->assertJson([
'meta' => [
'current_page' => 1,
'from' => 1,
'to' => 20,
'last_page' => 2,
'path' => url('api/activity'),
'total' => 35,
'per_page' => 20,
],
]);
}
/** @test */
public function paginate_activities_with_search_param()
{
$user = $this->getUser();
$set1 = Activity::factory()->times(10)->create([
'user_id' => $user->id,
'description' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
]);
$set2 = Activity::factory()->times(5)->create([
'user_id' => $user->id,
'description' => 'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris...',
]);
$transformed = ActivityResource::collection($set2)->resolve();
$response = $this->actingAs($user, self::API_GUARD)
->getJson('/api/activity?filter[description]=minim&per_page=10&sort=created_at')
->assertOk();
$this->assertEquals($response->json('data'), $transformed);
$response->assertJson([
'meta' => [
'current_page' => 1,
'from' => 1,
'to' => 5,
'last_page' => 1,
'total' => 5,
'per_page' => 10,
'path' => url('api/activity'),
],
]);
}
/** @test */
public function paginate_activities_with_more_records_per_page_than_allowed()
{
$this->actingAs($this->getUser(), self::API_GUARD)
->getJson('/api/activity?per_page=140')
->assertStatus(422);
}
/** @test */
public function paginate_activities_for_user()
{
$user = UserFactory::user()->withPermissions('users.activity')->create();
$this->be($user, self::API_GUARD);
$activities = Activity::factory()->times(25)->create(['user_id' => $user->id]);
$response = $this->getJson("/api/activity?filters[user]={$user->id}");
$transformed = ActivityResource::collection($activities->take(20))->resolve();
$this->assertEquals($response->json('data'), $transformed);
$response->assertJson([
'meta' => [
'current_page' => 1,
'from' => 1,
'to' => 20,
'last_page' => 2,
'path' => url('api/activity'),
'total' => 25,
'per_page' => 20,
],
]);
}
/**
* @return mixed
*/
private function getUser()
{
return UserFactory::user()->withPermissions('users.activity')->create();
}
}
@@ -0,0 +1,35 @@
<?php
namespace Vanguard\UserActivity\Tests\Feature\Api;
use Carbon\Carbon;
use Tests\Feature\ApiTestCase;
use Vanguard\User;
use Vanguard\UserActivity\Activity;
use Vanguard\UserActivity\Repositories\Activity\ActivityRepository;
class StatsTest extends ApiTestCase
{
/** @test */
public function non_admin_users_cannot_get_user_stats()
{
$user = User::factory()->create();
Carbon::setTestNow(Carbon::now()->subWeek());
Activity::factory()->times(5)->create(['user_id' => $user->id]);
Carbon::setTestNow(null);
Activity::factory()->times(5)->create(['user_id' => $user->id]);
$response = $this->actingAs($user, self::API_GUARD)->getJson('/api/stats/activity');
$expected = app(ActivityRepository::class)->userActivityForPeriod(
$user->id,
Carbon::now()->subWeek(2),
Carbon::now()
)->toArray();
$response->assertOk()
->assertJson($expected);
}
}
@@ -0,0 +1,72 @@
<?php
namespace Vanguard\UserActivity\Tests\Feature\Web;
use Carbon\Carbon;
use Facades\Tests\Setup\UserFactory;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use Vanguard\UserActivity\Logger;
class ActivityTest extends TestCase
{
use RefreshDatabase;
public $logger;
protected function setUp(): void
{
parent::setUp();
$this->logger = app(Logger::class);
$this->artisan('db:seed');
}
/** @test */
public function display_all_activities()
{
$this->withoutMiddleware();
$user1 = UserFactory::create();
$user2 = UserFactory::create();
Carbon::setTestNow(Carbon::now());
$this->be($user1);
$this->logger->log('foo');
$this->be($user2);
$this->logger->log('bar');
$this->get('activity')
->assertSee('foo')
->assertSee('bar');
}
/** @test */
public function display_activities_for_a_specific_user()
{
$user = UserFactory::admin()->create();
$this->be($user);
$this->logger->log('foo');
$this->get("activity/user/{$user->id}/log")
->assertSee('foo');
}
/** @test */
public function search_activities()
{
$this->withoutMiddleware();
$user = UserFactory::create();
$this->be($user);
$this->logger->log('foo');
$this->logger->log('barrr');
$this->get('activity?search=foo')
->assertSee('foo')
->assertDontSee('barrr');
}
}
@@ -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,
]);
}
}
@@ -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}.");
}
}
@@ -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.');
}
}
@@ -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})");
}
}
@@ -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);
}
}