]> BookStack Code Mirror - bookstack/blob - tests/AuditLogTest.php
Filtered scripts in custom HTML head for exports
[bookstack] / tests / AuditLogTest.php
1 <?php namespace Tests;
2
3 use BookStack\Actions\Activity;
4 use BookStack\Actions\ActivityService;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\UserRepo;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Tools\TrashCan;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Repos\PageRepo;
11 use Carbon\Carbon;
12
13 class AuditLogTest extends TestCase
14 {
15     /** @var ActivityService  */
16     protected $activityService;
17
18     public function setUp(): void
19     {
20         parent::setUp();
21         $this->activityService = app(ActivityService::class);
22     }
23
24     public function test_only_accessible_with_right_permissions()
25     {
26         $viewer = $this->getViewer();
27         $this->actingAs($viewer);
28
29         $resp = $this->get('/settings/audit');
30         $this->assertPermissionError($resp);
31
32         $this->giveUserPermissions($viewer, ['settings-manage']);
33         $resp = $this->get('/settings/audit');
34         $this->assertPermissionError($resp);
35
36         $this->giveUserPermissions($viewer, ['users-manage']);
37         $resp = $this->get('/settings/audit');
38         $resp->assertStatus(200);
39         $resp->assertSeeText('Audit Log');
40     }
41
42     public function test_shows_activity()
43     {
44         $admin = $this->getAdmin();
45         $this->actingAs($admin);
46         $page = Page::query()->first();
47         $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
48         $activity = Activity::query()->orderBy('id', 'desc')->first();
49
50         $resp = $this->get('settings/audit');
51         $resp->assertSeeText($page->name);
52         $resp->assertSeeText('page_create');
53         $resp->assertSeeText($activity->created_at->toDateTimeString());
54         $resp->assertElementContains('.table-user-item', $admin->name);
55     }
56
57     public function test_shows_name_for_deleted_items()
58     {
59         $this->actingAs( $this->getAdmin());
60         $page = Page::query()->first();
61         $pageName = $page->name;
62         $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
63
64         app(PageRepo::class)->destroy($page);
65         app(TrashCan::class)->empty();
66
67         $resp = $this->get('settings/audit');
68         $resp->assertSeeText('Deleted Item');
69         $resp->assertSeeText('Name: ' . $pageName);
70     }
71
72     public function test_shows_activity_for_deleted_users()
73     {
74         $viewer = $this->getViewer();
75         $this->actingAs($viewer);
76         $page = Page::query()->first();
77         $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
78
79         $this->actingAs($this->getAdmin());
80         app(UserRepo::class)->destroy($viewer);
81
82         $resp = $this->get('settings/audit');
83         $resp->assertSeeText("[ID: {$viewer->id}] Deleted User");
84     }
85
86     public function test_filters_by_key()
87     {
88         $this->actingAs($this->getAdmin());
89         $page = Page::query()->first();
90         $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
91
92         $resp = $this->get('settings/audit');
93         $resp->assertSeeText($page->name);
94
95         $resp = $this->get('settings/audit?event=page_delete');
96         $resp->assertDontSeeText($page->name);
97     }
98
99     public function test_date_filters()
100     {
101         $this->actingAs($this->getAdmin());
102         $page = Page::query()->first();
103         $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
104
105         $yesterday = (Carbon::now()->subDay()->format('Y-m-d'));
106         $tomorrow = (Carbon::now()->addDay()->format('Y-m-d'));
107
108         $resp = $this->get('settings/audit?date_from=' . $yesterday);
109         $resp->assertSeeText($page->name);
110
111         $resp = $this->get('settings/audit?date_from=' . $tomorrow);
112         $resp->assertDontSeeText($page->name);
113
114         $resp = $this->get('settings/audit?date_to=' . $tomorrow);
115         $resp->assertSeeText($page->name);
116
117         $resp = $this->get('settings/audit?date_to=' . $yesterday);
118         $resp->assertDontSeeText($page->name);
119     }
120
121     public function test_user_filter()
122     {
123         $admin = $this->getAdmin();
124         $editor = $this->getEditor();
125         $this->actingAs($admin);
126         $page = Page::query()->first();
127         $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
128
129         $this->actingAs($editor);
130         $chapter = Chapter::query()->first();
131         $this->activityService->addForEntity($chapter, ActivityType::CHAPTER_UPDATE);
132
133         $resp = $this->actingAs($admin)->get('settings/audit?user=' . $admin->id);
134         $resp->assertSeeText($page->name);
135         $resp->assertDontSeeText($chapter->name);
136
137         $resp = $this->actingAs($admin)->get('settings/audit?user=' . $editor->id);
138         $resp->assertSeeText($chapter->name);
139         $resp->assertDontSeeText($page->name);
140
141     }
142
143 }