-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathCommentsTest.php
216 lines (157 loc) · 5.43 KB
/
CommentsTest.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace BeyondCode\Comments\Tests;
use BeyondCode\Comments\Events\CommentAdded;
use BeyondCode\Comments\Events\CommentDeleted;
use BeyondCode\Comments\Tests\Models\ApprovedUser;
use BeyondCode\Comments\Tests\Models\Post;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Event;
class CommentsTest extends TestCase
{
/** @test */
public function users_without_commentator_interface_do_not_get_approved()
{
$post = Post::create([
'title' => 'Some post',
]);
$post->comment('this is a comment');
$comment = $post->comments()->first();
$this->assertFalse($comment->is_approved);
}
/** @test */
public function models_can_store_comments()
{
$post = Post::create([
'title' => 'Some post',
]);
$post->comment('this is a comment');
$post->comment('this is a different comment');
$this->assertCount(2, $post->comments);
$this->assertSame('this is a comment', $post->comments[0]->comment);
$this->assertSame('this is a different comment', $post->comments[1]->comment);
}
/** @test */
public function comments_without_users_have_no_relation()
{
$post = Post::create([
'title' => 'Some post',
]);
$comment = $post->comment('this is a comment');
$this->assertNull($comment->commentator);
$this->assertNull($comment->user_id);
}
/** @test */
public function comments_can_be_posted_as_authenticated_users()
{
$user = User::first();
auth()->login($user);
$post = Post::create([
'title' => 'Some post',
]);
$comment = $post->comment('this is a comment');
$this->assertSame($user->toArray(), $comment->commentator->toArray());
}
/** @test */
public function comments_can_be_posted_as_different_users()
{
$user = User::first();
$post = Post::create([
'title' => 'Some post',
]);
$comment = $post->commentAsUser($user, 'this is a comment');
$this->assertSame($user->toArray(), $comment->commentator->toArray());
}
/** @test */
public function comments_can_be_approved()
{
$user = User::first();
$post = Post::create([
'title' => 'Some post',
]);
$comment = $post->comment('this is a comment');
$this->assertFalse($comment->is_approved);
$comment->approve();
$this->assertTrue($comment->is_approved);
}
/** @test */
public function comments_resolve_the_commented_model()
{
$user = User::first();
$post = Post::create([
'title' => 'Some post',
]);
$comment = $post->comment('this is a comment');
$this->assertSame($comment->commentable->id, $post->id);
$this->assertSame($comment->commentable->title, $post->title);
}
/** @test */
public function users_can_be_auto_approved()
{
$user = ApprovedUser::first();
$post = Post::create([
'title' => 'Some post',
]);
$comment = $post->commentAsUser($user, 'this is a comment');
$this->assertTrue($comment->is_approved);
}
/** @test */
public function comments_have_an_approved_scope()
{
$user = ApprovedUser::first();
$post = Post::create([
'title' => 'Some post',
]);
$post->comment('this comment is not approved');
$post->commentAsUser($user, 'this comment is approved');
$this->assertCount(2, $post->comments);
$this->assertCount(1, $post->comments()->approved()->get());
$this->assertSame('this comment is approved', $post->comments()->approved()->first()->comment);
}
/** @test */
public function comments_are_deleted_when_posts_are_deleted()
{
$post = Post::create([
'title' => 'Some post',
]);
$comment = $post->comment('this comment will be deleted');
$post->delete();
$this->assertFalse($comment->exists());
}
/** @test */
public function replies_are_deleted_when_post_comments_are_deleted()
{
config(['comments.delete_replies_along_comments' => true]);
$post = Post::create([
'title' => 'Some post',
]);
$comment = $post->comment('this comment will be deleted');
$reply = $comment->comment('this comment will be deleted too');
$comment->delete();
$this->assertFalse($reply->exists());
}
/** @test */
public function comment_added_event_is_dispatched_when_comment_is_created()
{
Event::fake([CommentAdded::class]);
$post = Post::create([
'title' => 'Some post',
]);
$comment = $post->comment('this comment is added');
Event::assertDispatched(CommentAdded::class, function ($event) use ($comment) {
return $event->comment->is($comment);
});
}
/** @test */
public function comment_deleted_event_is_dispatched_when_comment_is_deleted()
{
Event::fake([CommentDeleted::class]);
$post = Post::create([
'title' => 'Some post',
]);
$comment = $post->comment('this comment is added');
$comment->delete();
Event::assertDispatched(CommentDeleted::class, function ($event) use ($comment) {
return $event->comment->is($comment);
});
}
}