]> BookStack Code Mirror - bookstack/blob - tests/AuditLogTest.php
Fixed the `AddActivityIndexes` migration's `down()` method
[bookstack] / tests / AuditLogTest.php
1 <?php namespace Tests;
2
3 use BookStack\Actions\Activity;
4 use BookStack\Actions\ActivityService;
5 use BookStack\Auth\UserRepo;
6 use BookStack\Entities\Page;
7 use BookStack\Entities\Repos\PageRepo;
8 use Carbon\Carbon;
9
10 class AuditLogTest extends TestCase
11 {
12
13     public function test_only_accessible_with_right_permissions()
14     {
15         $viewer = $this->getViewer();
16         $this->actingAs($viewer);
17
18         $resp = $this->get('/settings/audit');
19         $this->assertPermissionError($resp);
20
21         $this->giveUserPermissions($viewer, ['settings-manage']);
22         $resp = $this->get('/settings/audit');
23         $this->assertPermissionError($resp);
24
25         $this->giveUserPermissions($viewer, ['users-manage']);
26         $resp = $this->get('/settings/audit');
27         $resp->assertStatus(200);
28         $resp->assertSeeText('Audit Log');
29     }
30
31     public function test_shows_activity()
32     {
33         $admin = $this->getAdmin();
34         $this->actingAs($admin);
35         $page = Page::query()->first();
36         app(ActivityService::class)->add($page, 'page_create', $page->book->id);
37         $activity = Activity::query()->orderBy('id', 'desc')->first();
38
39         $resp = $this->get('settings/audit');
40         $resp->assertSeeText($page->name);
41         $resp->assertSeeText('page_create');
42         $resp->assertSeeText($activity->created_at->toDateTimeString());
43         $resp->assertElementContains('.audit-log-user', $admin->name);
44     }
45
46     public function test_shows_name_for_deleted_items()
47     {
48         $this->actingAs( $this->getAdmin());
49         $page = Page::query()->first();
50         $pageName = $page->name;
51         app(ActivityService::class)->add($page, 'page_create', $page->book->id);
52
53         app(PageRepo::class)->destroy($page);
54
55         $resp = $this->get('settings/audit');
56         $resp->assertSeeText('Deleted Item');
57         $resp->assertSeeText('Name: ' . $pageName);
58     }
59
60     public function test_shows_activity_for_deleted_users()
61     {
62         $viewer = $this->getViewer();
63         $this->actingAs($viewer);
64         $page = Page::query()->first();
65         app(ActivityService::class)->add($page, 'page_create', $page->book->id);
66
67         $this->actingAs($this->getAdmin());
68         app(UserRepo::class)->destroy($viewer);
69
70         $resp = $this->get('settings/audit');
71         $resp->assertSeeText("[ID: {$viewer->id}] Deleted User");
72     }
73
74     public function test_filters_by_key()
75     {
76         $this->actingAs($this->getAdmin());
77         $page = Page::query()->first();
78         app(ActivityService::class)->add($page, 'page_create', $page->book->id);
79
80         $resp = $this->get('settings/audit');
81         $resp->assertSeeText($page->name);
82
83         $resp = $this->get('settings/audit?event=page_delete');
84         $resp->assertDontSeeText($page->name);
85     }
86
87     public function test_date_filters()
88     {
89         $this->actingAs($this->getAdmin());
90         $page = Page::query()->first();
91         app(ActivityService::class)->add($page, 'page_create', $page->book->id);
92
93         $yesterday = (Carbon::now()->subDay()->format('Y-m-d'));
94         $tomorrow = (Carbon::now()->addDay()->format('Y-m-d'));
95
96         $resp = $this->get('settings/audit?date_from=' . $yesterday);
97         $resp->assertSeeText($page->name);
98
99         $resp = $this->get('settings/audit?date_from=' . $tomorrow);
100         $resp->assertDontSeeText($page->name);
101
102         $resp = $this->get('settings/audit?date_to=' . $tomorrow);
103         $resp->assertSeeText($page->name);
104
105         $resp = $this->get('settings/audit?date_to=' . $yesterday);
106         $resp->assertDontSeeText($page->name);
107     }
108
109 }