]> BookStack Code Mirror - bookstack/blob - tests/AuditLogTest.php
fix image delete confirm text
[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\Tools\TrashCan;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Entities\Repos\PageRepo;
10 use Carbon\Carbon;
11
12 class AuditLogTest extends TestCase
13 {
14     /** @var ActivityService  */
15     protected $activityService;
16
17     public function setUp(): void
18     {
19         parent::setUp();
20         $this->activityService = app(ActivityService::class);
21     }
22
23     public function test_only_accessible_with_right_permissions()
24     {
25         $viewer = $this->getViewer();
26         $this->actingAs($viewer);
27
28         $resp = $this->get('/settings/audit');
29         $this->assertPermissionError($resp);
30
31         $this->giveUserPermissions($viewer, ['settings-manage']);
32         $resp = $this->get('/settings/audit');
33         $this->assertPermissionError($resp);
34
35         $this->giveUserPermissions($viewer, ['users-manage']);
36         $resp = $this->get('/settings/audit');
37         $resp->assertStatus(200);
38         $resp->assertSeeText('Audit Log');
39     }
40
41     public function test_shows_activity()
42     {
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();
48
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);
54     }
55
56     public function test_shows_name_for_deleted_items()
57     {
58         $this->actingAs( $this->getAdmin());
59         $page = Page::query()->first();
60         $pageName = $page->name;
61         $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
62
63         app(PageRepo::class)->destroy($page);
64         app(TrashCan::class)->empty();
65
66         $resp = $this->get('settings/audit');
67         $resp->assertSeeText('Deleted Item');
68         $resp->assertSeeText('Name: ' . $pageName);
69     }
70
71     public function test_shows_activity_for_deleted_users()
72     {
73         $viewer = $this->getViewer();
74         $this->actingAs($viewer);
75         $page = Page::query()->first();
76         $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
77
78         $this->actingAs($this->getAdmin());
79         app(UserRepo::class)->destroy($viewer);
80
81         $resp = $this->get('settings/audit');
82         $resp->assertSeeText("[ID: {$viewer->id}] Deleted User");
83     }
84
85     public function test_filters_by_key()
86     {
87         $this->actingAs($this->getAdmin());
88         $page = Page::query()->first();
89         $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
90
91         $resp = $this->get('settings/audit');
92         $resp->assertSeeText($page->name);
93
94         $resp = $this->get('settings/audit?event=page_delete');
95         $resp->assertDontSeeText($page->name);
96     }
97
98     public function test_date_filters()
99     {
100         $this->actingAs($this->getAdmin());
101         $page = Page::query()->first();
102         $this->activityService->addForEntity($page, ActivityType::PAGE_CREATE);
103
104         $yesterday = (Carbon::now()->subDay()->format('Y-m-d'));
105         $tomorrow = (Carbon::now()->addDay()->format('Y-m-d'));
106
107         $resp = $this->get('settings/audit?date_from=' . $yesterday);
108         $resp->assertSeeText($page->name);
109
110         $resp = $this->get('settings/audit?date_from=' . $tomorrow);
111         $resp->assertDontSeeText($page->name);
112
113         $resp = $this->get('settings/audit?date_to=' . $tomorrow);
114         $resp->assertSeeText($page->name);
115
116         $resp = $this->get('settings/audit?date_to=' . $yesterday);
117         $resp->assertDontSeeText($page->name);
118     }
119
120 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.