3 use BookStack\Actions\Activity;
4 use BookStack\Actions\ActivityService;
5 use BookStack\Auth\UserRepo;
6 use BookStack\Entities\Managers\TrashCan;
7 use BookStack\Entities\Page;
8 use BookStack\Entities\Repos\PageRepo;
11 class AuditLogTest extends TestCase
14 public function test_only_accessible_with_right_permissions()
16 $viewer = $this->getViewer();
17 $this->actingAs($viewer);
19 $resp = $this->get('/settings/audit');
20 $this->assertPermissionError($resp);
22 $this->giveUserPermissions($viewer, ['settings-manage']);
23 $resp = $this->get('/settings/audit');
24 $this->assertPermissionError($resp);
26 $this->giveUserPermissions($viewer, ['users-manage']);
27 $resp = $this->get('/settings/audit');
28 $resp->assertStatus(200);
29 $resp->assertSeeText('Audit Log');
32 public function test_shows_activity()
34 $admin = $this->getAdmin();
35 $this->actingAs($admin);
36 $page = Page::query()->first();
37 app(ActivityService::class)->add($page, 'page_create', $page->book->id);
38 $activity = Activity::query()->orderBy('id', 'desc')->first();
40 $resp = $this->get('settings/audit');
41 $resp->assertSeeText($page->name);
42 $resp->assertSeeText('page_create');
43 $resp->assertSeeText($activity->created_at->toDateTimeString());
44 $resp->assertElementContains('.table-user-item', $admin->name);
47 public function test_shows_name_for_deleted_items()
49 $this->actingAs( $this->getAdmin());
50 $page = Page::query()->first();
51 $pageName = $page->name;
52 app(ActivityService::class)->add($page, 'page_create', $page->book->id);
54 app(PageRepo::class)->destroy($page);
55 app(TrashCan::class)->empty();
57 $resp = $this->get('settings/audit');
58 $resp->assertSeeText('Deleted Item');
59 $resp->assertSeeText('Name: ' . $pageName);
62 public function test_shows_activity_for_deleted_users()
64 $viewer = $this->getViewer();
65 $this->actingAs($viewer);
66 $page = Page::query()->first();
67 app(ActivityService::class)->add($page, 'page_create', $page->book->id);
69 $this->actingAs($this->getAdmin());
70 app(UserRepo::class)->destroy($viewer);
72 $resp = $this->get('settings/audit');
73 $resp->assertSeeText("[ID: {$viewer->id}] Deleted User");
76 public function test_filters_by_key()
78 $this->actingAs($this->getAdmin());
79 $page = Page::query()->first();
80 app(ActivityService::class)->add($page, 'page_create', $page->book->id);
82 $resp = $this->get('settings/audit');
83 $resp->assertSeeText($page->name);
85 $resp = $this->get('settings/audit?event=page_delete');
86 $resp->assertDontSeeText($page->name);
89 public function test_date_filters()
91 $this->actingAs($this->getAdmin());
92 $page = Page::query()->first();
93 app(ActivityService::class)->add($page, 'page_create', $page->book->id);
95 $yesterday = (Carbon::now()->subDay()->format('Y-m-d'));
96 $tomorrow = (Carbon::now()->addDay()->format('Y-m-d'));
98 $resp = $this->get('settings/audit?date_from=' . $yesterday);
99 $resp->assertSeeText($page->name);
101 $resp = $this->get('settings/audit?date_from=' . $tomorrow);
102 $resp->assertDontSeeText($page->name);
104 $resp = $this->get('settings/audit?date_to=' . $tomorrow);
105 $resp->assertSeeText($page->name);
107 $resp = $this->get('settings/audit?date_to=' . $yesterday);
108 $resp->assertDontSeeText($page->name);