3 use BookStack\Actions\Activity;
4 use BookStack\Actions\ActivityService;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\UserRepo;
7 use BookStack\Entities\Tools\TrashCan;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Entities\Repos\PageRepo;
12 class AuditLogTest extends TestCase
14 /** @var ActivityService */
15 protected $activityService;
17 public function setUp(): void
20 $this->activityService = app(ActivityService::class);
23 public function test_only_accessible_with_right_permissions()
25 $viewer = $this->getViewer();
26 $this->actingAs($viewer);
28 $resp = $this->get('/settings/audit');
29 $this->assertPermissionError($resp);
31 $this->giveUserPermissions($viewer, ['settings-manage']);
32 $resp = $this->get('/settings/audit');
33 $this->assertPermissionError($resp);
35 $this->giveUserPermissions($viewer, ['users-manage']);
36 $resp = $this->get('/settings/audit');
37 $resp->assertStatus(200);
38 $resp->assertSeeText('Audit Log');
41 public function test_shows_activity()
43 $admin = $this->getAdmin();
44 $this->actingAs($admin);
45 $page = Page::query()->first();
46 $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
47 $activity = Activity::query()->orderBy('id', 'desc')->first();
49 $resp = $this->get('settings/audit');
50 $resp->assertSeeText($page->name);
51 $resp->assertSeeText('page_create');
52 $resp->assertSeeText($activity->created_at->toDateTimeString());
53 $resp->assertElementContains('.table-user-item', $admin->name);
56 public function test_shows_name_for_deleted_items()
58 $this->actingAs( $this->getAdmin());
59 $page = Page::query()->first();
60 $pageName = $page->name;
61 $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
63 app(PageRepo::class)->destroy($page);
64 app(TrashCan::class)->empty();
66 $resp = $this->get('settings/audit');
67 $resp->assertSeeText('Deleted Item');
68 $resp->assertSeeText('Name: ' . $pageName);
71 public function test_shows_activity_for_deleted_users()
73 $viewer = $this->getViewer();
74 $this->actingAs($viewer);
75 $page = Page::query()->first();
76 $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
78 $this->actingAs($this->getAdmin());
79 app(UserRepo::class)->destroy($viewer);
81 $resp = $this->get('settings/audit');
82 $resp->assertSeeText("[ID: {$viewer->id}] Deleted User");
85 public function test_filters_by_key()
87 $this->actingAs($this->getAdmin());
88 $page = Page::query()->first();
89 $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
91 $resp = $this->get('settings/audit');
92 $resp->assertSeeText($page->name);
94 $resp = $this->get('settings/audit?event=page_delete');
95 $resp->assertDontSeeText($page->name);
98 public function test_date_filters()
100 $this->actingAs($this->getAdmin());
101 $page = Page::query()->first();
102 $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
104 $yesterday = (Carbon::now()->subDay()->format('Y-m-d'));
105 $tomorrow = (Carbon::now()->addDay()->format('Y-m-d'));
107 $resp = $this->get('settings/audit?date_from=' . $yesterday);
108 $resp->assertSeeText($page->name);
110 $resp = $this->get('settings/audit?date_from=' . $tomorrow);
111 $resp->assertDontSeeText($page->name);
113 $resp = $this->get('settings/audit?date_to=' . $tomorrow);
114 $resp->assertSeeText($page->name);
116 $resp = $this->get('settings/audit?date_to=' . $yesterday);
117 $resp->assertDontSeeText($page->name);