-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathNewPluginAvailableTest.php
More file actions
140 lines (104 loc) · 4.93 KB
/
NewPluginAvailableTest.php
File metadata and controls
140 lines (104 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
namespace Tests\Feature\Notifications;
use App\Models\Plugin;
use App\Models\User;
use App\Notifications\NewPluginAvailable;
use App\Services\PluginSyncService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Tests\TestCase;
class NewPluginAvailableTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->mock(PluginSyncService::class, function ($mock): void {
$mock->shouldReceive('sync')->andReturn(true);
});
}
public function test_notification_is_sent_to_opted_in_users_on_first_approval(): void
{
Notification::fake();
$author = User::factory()->create();
$optedIn = User::factory()->create(['receives_new_plugin_notifications' => true]);
$optedOut = User::factory()->create(['receives_new_plugin_notifications' => false]);
$plugin = Plugin::factory()->pending()->for($author)->create();
$admin = User::factory()->create();
$plugin->approve($admin->id);
Notification::assertSentTo($optedIn, NewPluginAvailable::class);
Notification::assertNotSentTo($optedOut, NewPluginAvailable::class);
Notification::assertNotSentTo($author, NewPluginAvailable::class);
}
public function test_notification_is_not_sent_on_re_approval(): void
{
Notification::fake();
$author = User::factory()->create();
$optedIn = User::factory()->create(['receives_new_plugin_notifications' => true]);
$plugin = Plugin::factory()->pending()->for($author)->create([
'approved_at' => now()->subDay(),
]);
$admin = User::factory()->create();
$plugin->approve($admin->id);
Notification::assertNotSentTo($optedIn, NewPluginAvailable::class);
}
public function test_via_returns_empty_array_when_user_opted_out(): void
{
$user = User::factory()->create(['receives_new_plugin_notifications' => false]);
$plugin = Plugin::factory()->for($user)->create();
$notification = new NewPluginAvailable($plugin);
$this->assertEmpty($notification->via($user));
}
public function test_via_returns_mail_and_database_when_user_opted_in(): void
{
$user = User::factory()->create(['receives_new_plugin_notifications' => true]);
$plugin = Plugin::factory()->for($user)->create();
$notification = new NewPluginAvailable($plugin);
$this->assertEquals(['mail', 'database'], $notification->via($user));
}
public function test_mail_contains_plugin_name(): void
{
$user = User::factory()->create();
$plugin = Plugin::factory()->for($user)->create(['name' => 'acme/awesome-plugin']);
$notification = new NewPluginAvailable($plugin);
$mail = $notification->toMail($user);
$this->assertStringContainsString('acme/awesome-plugin', $mail->subject);
}
public function test_mail_action_links_to_plugin_page(): void
{
$user = User::factory()->create();
$plugin = Plugin::factory()->for($user)->create(['name' => 'acme/awesome-plugin']);
$notification = new NewPluginAvailable($plugin);
$mail = $notification->toMail($user);
$this->assertEquals(route('plugins.show', ['vendor' => 'acme', 'package' => 'awesome-plugin']), $mail->actionUrl);
}
public function test_database_notification_contains_plugin_data(): void
{
$user = User::factory()->create();
$plugin = Plugin::factory()->for($user)->create(['name' => 'acme/awesome-plugin']);
$notification = new NewPluginAvailable($plugin);
$data = $notification->toArray($user);
$this->assertEquals($plugin->id, $data['plugin_id']);
$this->assertEquals('acme/awesome-plugin', $data['plugin_name']);
$this->assertStringContainsString('acme/awesome-plugin', $data['title']);
$this->assertEquals(route('plugins.show', ['vendor' => 'acme', 'package' => 'awesome-plugin']), $data['action_url']);
$this->assertEquals('View Plugin', $data['action_label']);
}
public function test_mail_contains_notification_preferences_link(): void
{
$user = User::factory()->create();
$plugin = Plugin::factory()->for($user)->create();
$notification = new NewPluginAvailable($plugin);
$mail = $notification->toMail($user);
$expectedUrl = route('customer.settings', ['tab' => 'notifications']);
$found = collect($mail->introLines)->concat($mail->outroLines)->contains(function ($line) use ($expectedUrl) {
return str_contains($line, $expectedUrl);
});
$this->assertTrue($found, 'Mail should contain a link to the notification preferences page.');
}
public function test_new_users_receive_new_plugin_notifications_by_default(): void
{
$user = User::factory()->create();
$this->assertTrue($user->receives_new_plugin_notifications);
}
}