Commit 0546cd5f authored by gnuget's avatar gnuget Committed by gnuget

Issue #2950876 by drupalgin, gnuget: Fixed coding standards

parent a2f7604d
This diff is collapsed.
<?php
/**
* @file
* comment_notify.install.
* Comment_notify.install.
*/
/**
......@@ -16,10 +17,10 @@ function comment_notify_install() {
// Mix in a random string to all values.
$salt = uniqid(mt_rand(), TRUE);
if (db_driver() == 'pgsql') {
$comments_select->addExpression("MD5(:salt || c.mail || COALESCE(u.mail, u.init) || c.uid || c.name || c.entity_id || c.hostname || c.cid)", 'notify_hash', array(':salt' => $salt));
$comments_select->addExpression("MD5(:salt || c.mail || COALESCE(u.mail, u.init) || c.uid || c.name || c.entity_id || c.hostname || c.cid)", 'notify_hash', [':salt' => $salt]);
}
else {
$comments_select->addExpression("MD5(CONCAT_WS('', :salt, c.mail, COALESCE(u.mail, u.init), c.uid, c.name, c.entity_id, c.hostname, c.cid))", 'notify_hash', array(':salt' => $salt));
$comments_select->addExpression("MD5(CONCAT_WS('', :salt, c.mail, COALESCE(u.mail, u.init), c.uid, c.name, c.entity_id, c.hostname, c.cid))", 'notify_hash', [':salt' => $salt]);
}
db_insert('comment_notify')->from($comments_select)->execute();
......@@ -31,64 +32,72 @@ function comment_notify_install() {
* Implements hook_schema().
*/
function comment_notify_schema() {
$schema['comment_notify'] = array(
'description' => t('Stores information about which commenters on the site have subscriped to followup emails.'),
'fields' => array(
'cid' => array(
$schema['comment_notify'] = [
'description' => 'Stores information about which commenters on the site have subscriped to followup emails.',
'fields' => [
'cid' => [
'type' => 'int',
'unsigned' => TRUE,
'description' => 'The comment id from {comments}.cid',
'not null' => TRUE,
'disp-width' => '11'),
'notify' => array(
'disp-width' => '11',
],
'notify' => [
'type' => 'int',
'description' => 'An integer indicating the type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.',
'size' => 'tiny',
'not null' => TRUE,
'disp-width' => '11'),
'notify_hash' => array(
'disp-width' => '11',
],
'notify_hash' => [
'type' => 'varchar',
'description' => 'A hash of unique information about the commenter. Used for unsubscribing users.',
'length' => '128',
'not null' => TRUE,
'default' => ''),
'notified' => array(
'default' => '',
],
'notified' => [
'type' => 'int',
'description' => 'A boolean indicator for whether or not a notification for the comment has been sent: 1 means yes, 0 means no.',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11'),
),
'primary key' => array('cid'),
'indexes' => array(
'notify_hash' => array('notify_hash')),
);
$schema['comment_notify_user_settings'] = array(
'fields' => array(
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'description' => 'The user id from {users}.cid',
'not null' => TRUE,
'disp-width' => '11'),
'node_notify' => array(
'type' => 'int',
'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11'),
'comment_notify' => array(
'type' => 'int',
'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11'),
),
'primary key' => array('uid'),
);
'disp-width' => '11',
],
],
'primary key' => ['cid'],
'indexes' => [
'notify_hash' => ['notify_hash'],
],
];
$schema['comment_notify_user_settings'] = [
'fields' => [
'uid' => [
'type' => 'int',
'unsigned' => TRUE,
'description' => 'The user id from {users}.cid',
'not null' => TRUE,
'disp-width' => '11',
],
'node_notify' => [
'type' => 'int',
'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11',
],
'comment_notify' => [
'type' => 'int',
'description' => 'An integer indicating the default type of subscription: 0 means not subscribed, 1 means subscribed to all comments, and 2 means only subscribed to replies of this comment.',
'size' => 'tiny',
'not null' => TRUE,
'default' => 0,
'disp-width' => '11',
],
],
'primary key' => ['uid'],
];
return $schema;
}
......@@ -9,31 +9,35 @@
* Field handler.
*/
class CommentNotifyMigrationHandler extends MigrateDestinationHandler {
/**
* {@inheritdoc}
*/
public function __construct() {
$this->registerTypes(array('comment'));
$this->registerTypes(['comment']);
}
/**
* Make the destination field visible.
*/
public function fields() {
return array(
return [
'notify' => t('Comment Notify: Whether to send notifications for this comment'),
'notified' => t('Comment Notify: Whether notifications have been sent for this comment'),
'notify_hash' => t('Comment Notify: Hash representing this notification'),
);
];
}
/**
* Implements MigrateDestinationHandler::prepare().
*
* @param $comment
* The comment object being prepared for saving.
* The comment object being prepared for saving.
* @param $row
* Raw source data for the migration - ignored.
* Raw source data for the migration - ignored.
*/
public function prepare($comment, $row) {
// By default, set notifications off
// By default, set notifications off.
if (!isset($comment->notify)) {
$comment->notify = 0;
}
......@@ -46,23 +50,24 @@ class CommentNotifyMigrationHandler extends MigrateDestinationHandler {
* Implements MigrateDestinationHandler::complete().
*
* @param $comment
* The comment object taht was just saved.
* The comment object taht was just saved.
* @param $row
* Raw source data for the migration - ignored.
* Raw source data for the migration - ignored.
*/
public function complete($comment, $row) {
if (!isset($comment->notified) || $comment->notified) {
comment_notify_mark_comment_as_notified($comment);
}
}
}
/*
* Implementats hook_migrate_api().
/**
* Implements hook_migrate_api().
*/
function comment_notify_migrate_api() {
$api = array(
$api = [
'api' => 2,
);
];
return $api;
}
This diff is collapsed.
......@@ -4,6 +4,7 @@
* @file
* Builds placeholder replacement tokens for comment_notify.module.
*/
use Drupal\Core\Render\BubbleableMetadata;
/**
......@@ -11,18 +12,18 @@ use Drupal\Core\Render\BubbleableMetadata;
*/
function comment_notify_token_info() {
// Comment tokens.
$info['tokens']['comment']['unsubscribe-url'] = array(
$info['tokens']['comment']['unsubscribe-url'] = [
'name' => t('Unsubscribe URL'),
'description' => t('The URL to disable notifications for the comment.'),
'type' => 'url',
);
];
// Comment subscriber token type (extends the comment token type).
$info['types']['comment-subscribed'] = array(
$info['types']['comment-subscribed'] = [
'name' => t('Subscribed comment'),
'description' => t('Tokens related to a comment that is subscribed to new comments.'),
'type' => 'comment',
);
];
return $info;
}
......@@ -30,8 +31,8 @@ function comment_notify_token_info() {
/**
* Implements hook_tokens().
*/
function comment_notify_tokens($type, $tokens, array $data = array(), array $options = array(), BubbleableMetadata $bubbleable_metadata) {
$url_options = array('absolute' => TRUE);
function comment_notify_tokens($type, $tokens, array $data = [], array $options = [], BubbleableMetadata $bubbleable_metadata) {
$url_options = ['absolute' => TRUE];
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
......@@ -41,7 +42,7 @@ function comment_notify_tokens($type, $tokens, array $data = array(), array $opt
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
$replacements = [];
if ($type == 'comment' && !empty($data['comment'])) {
$comment = $data['comment'];
......@@ -58,13 +59,13 @@ function comment_notify_tokens($type, $tokens, array $data = array(), array $opt
// [comment:unsubscribe-url:*] chained token replacements.
if (($unsubscribe_url_tokens = \Drupal::token()->findWithPrefix($tokens, 'unsubscribe-url')) && $unsubscribe_url = comment_notify_get_unsubscribe_url($comment)) {
$replacements += \Drupal::token()->generate('url', $unsubscribe_url_tokens, array('path' => $unsubscribe_url), $options, $bubbleable_metadata);
$replacements += \Drupal::token()->generate('url', $unsubscribe_url_tokens, ['path' => $unsubscribe_url], $options, $bubbleable_metadata);
}
}
// Comment subscriber tokens (pass through to comment token replacement).
if ($type == 'comment-subscribed' && !empty($data['comment-subscribed'])) {
$replacements += \Drupal::token()->generate('comment', $tokens, array('comment' => $data['comment-subscribed']), $options, $bubbleable_metadata);
$replacements += \Drupal::token()->generate('comment', $tokens, ['comment' => $data['comment-subscribed']], $options, $bubbleable_metadata);
}
return $replacements;
......
......@@ -15,7 +15,8 @@ class CommentNotifyController extends ControllerBase {
* @param string $hash
* A hash identifying the notification entry to disable.
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
* @return array
* A renderable array.
*/
public function disable($hash) {
module_load_include('inc', 'comment_notify', 'comment_notify');
......
......@@ -29,6 +29,9 @@ class CommentNotifySettings extends ConfigFormBase {
return ['comment_notify.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('comment_notify.settings');
......@@ -150,6 +153,9 @@ class CommentNotifySettings extends ConfigFormBase {
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
if (!array_filter($form_state->getValue('available_alerts'))) {
$form_state->setErrorByName('available_alerts', 'You must enable at least one subscription mode.');
......@@ -164,8 +170,20 @@ class CommentNotifySettings extends ConfigFormBase {
->set('node_types', array_keys(array_filter($form_state->getValue('node_types'))))
->set('available_alerts', $form_state->getValue('available_alerts'))
->set('enable_default', $form_state->getValue('enable_default'))
->set('mail_templates.watcher.body', $form_state->getValue(['mail_templates', 'watcher', 'body']))
->set('mail_templates.entity_author.body', $form_state->getValue(['mail_templates', 'entity_author', 'body']))
->set('mail_templates.watcher.body', $form_state->getValue(
[
'mail_templates',
'watcher',
'body',
]
))
->set('mail_templates.entity_author.body', $form_state->getValue(
[
'mail_templates',
'entity_author',
'body',
]
))
->save();
parent::submitForm($form, $form_state);
}
......
......@@ -4,7 +4,6 @@ namespace Drupal\comment_notify\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
/**
* Unsubscribe form for Comment Notify.
......@@ -44,7 +43,7 @@ class CommentNotifyUnsubscribe extends FormBase {
module_load_include('inc', 'comment_notify', 'comment_notify');
$email = trim($form_state->getValue(['email']));
$comments = comment_notify_unsubscribe_by_email($email);
// Update the admin about the state of this comment notification subscription.
// Update the admin about the state of the subscription.
if ($comments == 0) {
drupal_set_message($this->t("There were no active comment notifications for that email."));
}
......
......@@ -11,6 +11,9 @@ use Drupal\Core\Session\AccountInterface;
*/
class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
......@@ -28,8 +31,7 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
}
/**
* Tests that the mail must be required if an anonymous user wants
* to receive notifications.
* Tests that the mail is required for anonymous users.
*/
public function testMail() {
/** @var \Drupal\node\Entity\Node $node */
......@@ -146,18 +148,6 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
);
$captured_emails = $this->container->get('state')->get('system.test_mail_collector');
$this->assertEmpty($captured_emails, 'No notifications has been sent.');
// @TODO Move this test to the settings form.
// \Drupal::configFactory()->getEditable('comment_notify.settings')->set('available_alerts', [1 => FALSE, 2 => TRUE])->save();
// $subscribe_0 = ['notify' => TRUE];
// $contact_0 = ['mail' => $this->getRandomEmailAddress()];
// $anonymous_comment_0 = $this->postComment($node->toUrl()->toString(), $this->randomMachineName(), $this->randomMachineName(), $subscribe_0, $contact_0);
//
// // Confirm that the notification is saved.
// $result = comment_notify_get_notification_type($anonymous_comment_0['id']);
// $this->assertEquals($result, 2, 'Notify selection option 0 is saved properly.');
}
}
......@@ -19,7 +19,7 @@ abstract class CommentNotifyTestBase extends BrowserTestBase {
/**
* Admin User.
*
* @var \Drupal\user\Entity\User $adminUser
* @var \Drupal\user\Entity\User
*/
protected $adminUser;
......@@ -64,8 +64,8 @@ abstract class CommentNotifyTestBase extends BrowserTestBase {
/**
* Post comment.
*
* @param \Drupal\node\NodeInterface $node
* Node to post comment on.
* @param string $url
* The url where the comment will be submitted.
* @param string $subject
* Comment subject.
* @param string $comment
......@@ -77,9 +77,9 @@ abstract class CommentNotifyTestBase extends BrowserTestBase {
* array of values to set contact info.
*
* @return array|bool
* return an array with the comment or false if the post comment fails.
* return an array with the comment or false if the post comment fails.
*/
protected function postComment($url, $subject, $comment, $notify, $contact = NULL) {
protected function postComment($url, $subject, $comment, array $notify, $contact = NULL) {
$edit = [];
$edit['subject[0][value]'] = $subject;
$edit['comment_body[0][value]'] = $comment;
......@@ -94,7 +94,7 @@ abstract class CommentNotifyTestBase extends BrowserTestBase {
$this->drupalPostForm($url, $edit, t('Save'));
$match = array();
$match = [];
// Get comment ID.
preg_match('/#comment-([^"]+)/', $this->getURL(), $match);
......@@ -119,12 +119,12 @@ abstract class CommentNotifyTestBase extends BrowserTestBase {
* Checks current page for specified comment.
*
* @param object $comment
* Comment object.
* Comment object.
* @param bool $reply
* The comment is a reply to another comment.
* The comment is a reply to another comment.
*
* @return bool
* Comment found.
* Comment found.
*/
protected function commentExists($comment, $reply = FALSE) {
if ($comment && is_object($comment)) {
......@@ -151,7 +151,7 @@ abstract class CommentNotifyTestBase extends BrowserTestBase {
/**
* Returns a randomly generated valid email address.
*
* @return string.
* @return string
* A random email.
*/
public function getRandomEmailAddress() {
......
......@@ -14,12 +14,14 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
/**
* Authenticated User.
*
* @var \Drupal\user\Entity\User $authenticatedUser
* @var \Drupal\user\Entity\User
*/
protected $authenticatedUser;
/**
* Permissions required by the module.
*
* @var array
*/
protected $permissions = [
'post comments',
......@@ -27,14 +29,18 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
'subscribe to comments',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->authenticatedUser = $this->drupalCreateUser($this->permissions);
}
/**
* The User's comment notify box should display different options depending
* the permissions of the user.
* Tests that the comment notify box is displayed correctly.
*
* It should display different options depending the permissions of the user.
*/
public function testUserCommentNotifyBox() {
// The user hasn't the subscribe to comments permission nor the 'administer
......@@ -125,6 +131,9 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
$this->drupalLogout();
}
/**
* Tests the "Comment Follow-up notifications" options.
*/
public function testsCommentFollowUpsNotifications() {
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
......@@ -171,7 +180,7 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
// 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']);
$permissions = array_merge($this->permissions, ['administer nodes']);
$this->authenticatedUser = $this->createUser($permissions);
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
......@@ -243,8 +252,7 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
}
/**
* Test that when the user account is canceled or deleted all the settings
* related with the CommentNotify module are deleted.
* Tests that when a user is canceled all the notifications are deleted.
*/
public function testUserCancelAccount() {
$cancel_method_options = [
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment