Newer
Older
David Valdez
committed
<?php
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\Core\Session\AccountInterface;
/**
* Tests the Comment Notify users preferences.
*
* @group comment_notify
*/
class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
/**
* Authenticated User.
*
* @var \Drupal\user\Entity\User
David Valdez
committed
*/
protected $authenticatedUser;
/**
* Permissions required by the module.
*
* @var array
David Valdez
committed
*/
protected $permissions = [
'post comments',
'skip comment approval',
'subscribe to comments',
];
/**
* {@inheritdoc}
*/
David Valdez
committed
protected function setUp() {
parent::setUp();
$this->authenticatedUser = $this->drupalCreateUser($this->permissions);
}
/**
* Tests that the comment notify box is displayed correctly.
*
* It should display different options depending the permissions of the user.
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
*/
public function testUserCommentNotifyBox() {
// The user hasn't the subscribe to comments permission nor the 'administer
// nodes' permission, nor has permission to create content, so it shouldn't
// see any Comment notify settings in the profile page.
$this->authenticatedUser = $this->drupalCreateUser(
[
'post comments',
'skip comment approval',
]
);
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertFalse($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
$this->drupalLogout();
// The user only has the 'subscribe to comments' permission, he should be
// able to see the Comment Notify settings box but the 'Receive content
// follow-up notification e-mails' checkbox shouldn't appear.
$this->authenticatedUser = $this->drupalCreateUser([
'post comments',
'skip comment approval',
'subscribe to comments',
]);
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
$this->assertFalse($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->drupalLogout();
// The user only has the 'administer nodes' permission, he should be
// able to see the Comment Notify settings box but the 'Comment follow-up
// notification settings' dropdown shouldn't appear.
$this->authenticatedUser = $this->drupalCreateUser([
'post comments',
'skip comment approval',
'administer nodes',
]);
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
$this->assertFalse($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->drupalLogout();
// The user only hasn't the 'administer nodes' permission nor the 'subscribe
// to comments' permission but he can create nodes of the type article, so
// he should be able to see the Comment Notify settings box with the
// 'Receive content follow-up notification e-mails' checkbox.
$this->authenticatedUser = $this->drupalCreateUser([
'post comments',
'skip comment approval',
'create article content',
]);
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
$this->assertFalse($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->drupalLogout();
// The has all the permissions, so he should be able to see all the Comment
// notify settings.
$this->authenticatedUser = $this->drupalCreateUser([
'post comments',
'skip comment approval',
'create article content',
'subscribe to comments',
]);
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->drupalLogout();
}
David Valdez
committed
/**
* Tests the Comment follow-up notification settings.
*/
public function testUserCommentPreferences() {
// Tests that the settings are present in the user profile.
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
$this->drupalLogout();
}
/**
* Tests the "Comment Follow-up notifications" options.
*/
David Valdez
committed
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
public function testsCommentFollowUpsNotifications() {
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
// Test the "No notifications" option.
$this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_DISABLED);
$this->getSession()->getPage()->pressButton(t('Save'));
$node = $this->drupalCreateNode(['type' => 'article']);
$this->drupalGet($node->toUrl()->toString());
$this->assertTrue($this->getSession()->getPage()->hasUncheckedField('Notify me when new comments are posted'));
// Test the "All comments" option.
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_NODE);
$this->getSession()->getPage()->pressButton(t('Save'));
$this->drupalGet($node->toUrl()->toString());
$this->assertTrue($this->getSession()->getPage()->hascheckedField('Notify me when new comments are posted'));
$this->assertTrue($this->getSession()->getPage()->hascheckedField('All comments'));
// Tests the "Replies to my comments" option.
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_COMMENT);
$this->getSession()->getPage()->pressButton(t('Save'));
$this->drupalGet($node->toUrl()->toString());
$this->assertTrue($this->getSession()->getPage()->hascheckedField('Notify me when new comments are posted'));
$this->assertTrue($this->getSession()->getPage()->hascheckedField('Replies to my comment'));
$this->drupalLogout();
}
/**
* Tests the Content follow-up notification settings.
*/
public function testUserNodePreferences() {
// Tests that the option not present in the user profile unless the user has
// the 'administer nodes' permission.
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertFalse($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->drupalLogout();
// Tests that the option is present in the user profile if the user has the
// 'administer nodes' permission.
$permissions = array_merge($this->permissions, ['administer nodes']);
David Valdez
committed
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
$this->authenticatedUser = $this->createUser($permissions);
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->getSession()->getPage()->checkField('Receive content follow-up notification e-mails');
$this->getSession()->getPage()->pressButton(t('Save'));
$node_notify_preference = comment_notify_get_user_node_notify_preference($this->authenticatedUser->id());
$this->assertEquals(COMMENT_NOTIFY_NODE, $node_notify_preference);
$this->drupalLogout();
// Tests that the notification is sent when the content created by the user
// receives a new comment.
$node = $this->drupalCreateNode(
[
'type' => 'article',
'uid' => $this->authenticatedUser,
]
);
// Write a comment as anonymous user.
user_role_grant_permissions(
AccountInterface::ANONYMOUS_ROLE,
[
'access comments',
'access content',
'post comments',
'skip comment approval',
'subscribe to comments',
]
);
$this->postComment(
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE]
);
// Test that the notification was sent.
$this->assertMail('to', $this->authenticatedUser->getEmail(), t('Message was sent to the user.'));
// Tests that the notification is not sent when the user hasn't selected
// the content notifications setting.
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->getSession()->getPage()->uncheckField('Receive content follow-up notification e-mails');
$this->getSession()->getPage()->pressButton(t('Save'));
$this->assertTrue($this->getSession()->getPage()->hasUncheckedField('Receive content follow-up notification e-mails'));
$this->drupalLogout();
drupal_static_reset('comment_notify_get_user_notification_setting');
$node_notify_preference = comment_notify_get_user_node_notify_preference($this->authenticatedUser->id());
$this->assertEquals(COMMENT_NOTIFY_DISABLED, $node_notify_preference);
$this->container->get('state')->set('system.test_mail_collector', []);
$node = $this->drupalCreateNode(
[
'type' => 'article',
'uid' => $this->authenticatedUser,
]
);
$this->postComment(
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE]
);
$captured_emails = $this->container->get('state')->get('system.test_mail_collector');
$this->assertEmpty($captured_emails, 'No notifications has been sent.');
}
David Valdez
committed
/**
* Tests that when a user is canceled all the notifications are deleted.
David Valdez
committed
*/
public function testUserCancelAccount() {
$cancel_method_options = [
'user_cancel_block',
'user_cancel_block_unpublish',
'user_cancel_reassign',
];
foreach ($cancel_method_options as $cancel_method_option) {
$user = $this->drupalCreateUser($this->permissions);
comment_notify_set_user_notification_setting($user->id(), COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT);
user_cancel([], $user->id(), $cancel_method_option);
$this->assertTrue(is_null(comment_notify_get_user_notification_setting($user->id())));
}
// Delete Account.
$user = $this->drupalCreateUser($this->permissions);
comment_notify_set_user_notification_setting($user->id(), COMMENT_NOTIFY_NODE, COMMENT_NOTIFY_COMMENT);
$user->delete();
$this->assertTrue(is_null(comment_notify_get_user_notification_setting($user->id())));
}