diff --git a/config/schema/entity_notify.schema.yml b/config/schema/entity_notify.schema.yml index 9421ca26cfa29d88afebd7885989967409dc24a2..496718dc48708a79b7cbf0c45f7dcfa1be3ef83e 100644 --- a/config/schema/entity_notify.schema.yml +++ b/config/schema/entity_notify.schema.yml @@ -2,6 +2,9 @@ entity_notify.settings: type: config_object label: 'Entity notify settings' mapping: + enable: + label: 'Enable the notifications' + type: boolean enabled_target_entity_types: label: 'Target entity types' type: sequence diff --git a/entity_notify.install b/entity_notify.install index f1d3dca390bd1c2489acaa29239b6fb152149984..d4a938cd70ace16b630b6b4469e09f45cc382684 100644 --- a/entity_notify.install +++ b/entity_notify.install @@ -74,3 +74,13 @@ function entity_notify_update_10001(): void { } } } + +/** + * Add "enabled" config. + */ +function entity_notify_update_10002(): void { + $config_factory = \Drupal::configFactory(); + $config = $config_factory->getEditable('entity_notify.settings'); + $config->set('enable', TRUE); + $config->save(); +} diff --git a/entity_notify.module b/entity_notify.module index aefbd9a6269d8ece997b287381f7b993ac607eb1..a489ae0462c05c56ad414937476daaa68f92a4a5 100644 --- a/entity_notify.module +++ b/entity_notify.module @@ -65,131 +65,134 @@ function _entity_notify_send_mail($to, $params) { function _entity_notify_event(EntityInterface $entity, string $event) { $config = \Drupal::config('entity_notify.settings'); $entities = $config->get('enabled_target_entity_types'); + $enable = $config->get('enable'); + + if ($enable) { + if (($entity->getEntityTypeId() === 'node') || + ($entity->getEntityTypeId() === 'comment') || + ($entities && in_array($entity->getEntityTypeId(), $entities, TRUE))) { + if ($entity->getEntityTypeId() === 'comment') { + $url = $entity->permalink()->setOption('absolute', TRUE)->toString(); + } + else { + $url = $entity->toUrl('canonical', ['absolute' => TRUE])->toString(); + } - if (($entity->getEntityTypeId() === 'node') || - ($entity->getEntityTypeId() === 'comment') || - ($entities && in_array($entity->getEntityTypeId(), $entities, TRUE))) { - if ($entity->getEntityTypeId() === 'comment') { - $url = $entity->permalink()->setOption('absolute', TRUE)->toString(); - } - else { - $url = $entity->toUrl('canonical', ['absolute' => TRUE])->toString(); - } - - $params = [ - 'url' => $url, - 'event' => $event, - ]; - - $send_to_admin = $config->get('entity_notify_admin'); - $send_to_roles = $config->get('entity_notify_roles'); - $maillist = $config->get('entity_notify_maillist'); - $send_to_telegram = $config->get('entity_notify_telegram'); - $telegram_bottoken = $config->get('entity_notify_telegram_bottoken'); - $telegram_chatids = $config->get('entity_notify_telegram_chatids'); - $telegram_proxy = $config->get('entity_notify_telegram_proxy'); - $telegram_proxy_server = $config->get('entity_notify_telegram_proxy_server'); - $telegram_proxy_login = $config->get('entity_notify_telegram_proxy_login'); - $telegram_proxy_password = $config->get('entity_notify_telegram_proxy_password'); - - if ($entity->getEntityTypeId() === 'node') { - $node_bundle = $entity->bundle(); - if ($node_bundle) { - $node_type = NodeType::load($node_bundle); - if ($node_type) { - $send_to_admin = $node_type->getThirdPartySetting('entity_notify', 'admin', FALSE); - $send_to_roles = $node_type->getThirdPartySetting('entity_notify', 'roles'); - $maillist = $node_type->getThirdPartySetting('entity_notify', 'maillist', ''); - $send_to_telegram = $node_type->getThirdPartySetting('entity_notify', 'telegram'); - $telegram_bottoken = $node_type->getThirdPartySetting('entity_notify', 'telegram_bottoken'); - $telegram_chatids = $node_type->getThirdPartySetting('entity_notify', 'telegram_chatids'); - $telegram_proxy = $node_type->getThirdPartySetting('entity_notify', 'telegram_proxy'); - $telegram_proxy_server = $node_type->getThirdPartySetting('entity_notify', 'telegram_proxy_server'); - $telegram_proxy_login = $node_type->getThirdPartySetting('entity_notify', 'telegram_proxy_login'); - $telegram_proxy_password = $node_type->getThirdPartySetting('entity_notify', 'telegram_proxy_password'); + $params = [ + 'url' => $url, + 'event' => $event, + ]; + + $send_to_admin = $config->get('entity_notify_admin'); + $send_to_roles = $config->get('entity_notify_roles'); + $maillist = $config->get('entity_notify_maillist'); + $send_to_telegram = $config->get('entity_notify_telegram'); + $telegram_bottoken = $config->get('entity_notify_telegram_bottoken'); + $telegram_chatids = $config->get('entity_notify_telegram_chatids'); + $telegram_proxy = $config->get('entity_notify_telegram_proxy'); + $telegram_proxy_server = $config->get('entity_notify_telegram_proxy_server'); + $telegram_proxy_login = $config->get('entity_notify_telegram_proxy_login'); + $telegram_proxy_password = $config->get('entity_notify_telegram_proxy_password'); + + if ($entity->getEntityTypeId() === 'node') { + $node_bundle = $entity->bundle(); + if ($node_bundle) { + $node_type = NodeType::load($node_bundle); + if ($node_type) { + $send_to_admin = $node_type->getThirdPartySetting('entity_notify', 'admin', FALSE); + $send_to_roles = $node_type->getThirdPartySetting('entity_notify', 'roles'); + $maillist = $node_type->getThirdPartySetting('entity_notify', 'maillist', ''); + $send_to_telegram = $node_type->getThirdPartySetting('entity_notify', 'telegram'); + $telegram_bottoken = $node_type->getThirdPartySetting('entity_notify', 'telegram_bottoken'); + $telegram_chatids = $node_type->getThirdPartySetting('entity_notify', 'telegram_chatids'); + $telegram_proxy = $node_type->getThirdPartySetting('entity_notify', 'telegram_proxy'); + $telegram_proxy_server = $node_type->getThirdPartySetting('entity_notify', 'telegram_proxy_server'); + $telegram_proxy_login = $node_type->getThirdPartySetting('entity_notify', 'telegram_proxy_login'); + $telegram_proxy_password = $node_type->getThirdPartySetting('entity_notify', 'telegram_proxy_password'); + } } } - } - if ($entity->getEntityTypeId() === 'comment') { - $comment_bundle = $entity->bundle(); - if ($comment_bundle) { - $comment_type = CommentType::load($comment_bundle); - if ($comment_type) { - $send_to_admin = $comment_type->getThirdPartySetting('entity_notify', 'admin', FALSE); - $send_to_roles = $comment_type->getThirdPartySetting('entity_notify', 'roles'); - $maillist = $comment_type->getThirdPartySetting('entity_notify', 'maillist', ''); - $send_to_telegram = $comment_type->getThirdPartySetting('entity_notify', 'telegram'); - $telegram_bottoken = $comment_type->getThirdPartySetting('entity_notify', 'telegram_bottoken'); - $telegram_chatids = $comment_type->getThirdPartySetting('entity_notify', 'telegram_chatids'); - $telegram_proxy = $comment_type->getThirdPartySetting('entity_notify', 'telegram_proxy'); - $telegram_proxy_server = $comment_type->getThirdPartySetting('entity_notify', 'telegram_proxy_server'); - $telegram_proxy_login = $comment_type->getThirdPartySetting('entity_notify', 'telegram_proxy_login'); - $telegram_proxy_password = $comment_type->getThirdPartySetting('entity_notify', 'telegram_proxy_password'); + if ($entity->getEntityTypeId() === 'comment') { + $comment_bundle = $entity->bundle(); + if ($comment_bundle) { + $comment_type = CommentType::load($comment_bundle); + if ($comment_type) { + $send_to_admin = $comment_type->getThirdPartySetting('entity_notify', 'admin', FALSE); + $send_to_roles = $comment_type->getThirdPartySetting('entity_notify', 'roles'); + $maillist = $comment_type->getThirdPartySetting('entity_notify', 'maillist', ''); + $send_to_telegram = $comment_type->getThirdPartySetting('entity_notify', 'telegram'); + $telegram_bottoken = $comment_type->getThirdPartySetting('entity_notify', 'telegram_bottoken'); + $telegram_chatids = $comment_type->getThirdPartySetting('entity_notify', 'telegram_chatids'); + $telegram_proxy = $comment_type->getThirdPartySetting('entity_notify', 'telegram_proxy'); + $telegram_proxy_server = $comment_type->getThirdPartySetting('entity_notify', 'telegram_proxy_server'); + $telegram_proxy_login = $comment_type->getThirdPartySetting('entity_notify', 'telegram_proxy_login'); + $telegram_proxy_password = $comment_type->getThirdPartySetting('entity_notify', 'telegram_proxy_password'); + } } } - } - // Send to admin. - if ($send_to_admin) { - $account = User::load(1); - if ($account) { - $to = $account->getEmail(); - if ($to) { - _entity_notify_send_mail($to, $params); + // Send to admin. + if ($send_to_admin) { + $account = User::load(1); + if ($account) { + $to = $account->getEmail(); + if ($to) { + _entity_notify_send_mail($to, $params); + } } } - } - // Send to users with roles. - if ($send_to_roles) { - foreach ($send_to_roles as $user_role) { - if ($user_role !== 0) { - $ids = \Drupal::entityQuery('user') - ->condition('status', 1) - ->condition('roles', $user_role) - ->accessCheck(FALSE) - ->execute(); - $users = User::loadMultiple($ids); - if (!empty(array_filter($users))) { - foreach ($users as $user) { - $to = $user->getEmail(); - if ($to) { - _entity_notify_send_mail($to, $params); + // Send to users with roles. + if ($send_to_roles) { + foreach ($send_to_roles as $user_role) { + if ($user_role !== 0) { + $ids = \Drupal::entityQuery('user') + ->condition('status', 1) + ->condition('roles', $user_role) + ->accessCheck(FALSE) + ->execute(); + $users = User::loadMultiple($ids); + if (!empty(array_filter($users))) { + foreach ($users as $user) { + $to = $user->getEmail(); + if ($to) { + _entity_notify_send_mail($to, $params); + } } } } } } - } - // Send to non-registered users. - if (!empty($maillist)) { - $mails = explode(',', $maillist); - foreach ($mails as $to) { - _entity_notify_send_mail($to, $params); + // Send to non-registered users. + if (!empty($maillist)) { + $mails = explode(',', $maillist); + foreach ($mails as $to) { + _entity_notify_send_mail($to, $params); + } } - } - // Send message to telegram. - if ($send_to_telegram) { - $chat_ids = explode(',', $telegram_chatids); - $text = t("New entity event (@event) on @siteName\n", [ - '@siteName' => \Drupal::config('system.site')->get('name'), - '@event' => $event, - ]); - $text .= t('Link to the entity: @linkToEntity', [ - '@linkToEntity' => $params['url'], - ]); - foreach ($chat_ids as $chat_id) { - if ($telegram_proxy) { - $result = \Drupal::service('telegram_api.service')->sendToTelegramBot($text, $telegram_bottoken, $chat_id, TRUE, $telegram_proxy_server, $telegram_proxy_login, $telegram_proxy_password); - } - else { - $result = \Drupal::service('telegram_api.service')->sendToTelegramBot($text, $telegram_bottoken, $chat_id); - } - if ($result !== TRUE) { - \Drupal::logger('entity_notify')->error($result); + // Send message to telegram. + if ($send_to_telegram) { + $chat_ids = explode(',', $telegram_chatids); + $text = t("New entity event (@event) on @siteName\n", [ + '@siteName' => \Drupal::config('system.site')->get('name'), + '@event' => $event, + ]); + $text .= t('Link to the entity: @linkToEntity', [ + '@linkToEntity' => $params['url'], + ]); + foreach ($chat_ids as $chat_id) { + if ($telegram_proxy) { + $result = \Drupal::service('telegram_api.service')->sendToTelegramBot($text, $telegram_bottoken, $chat_id, TRUE, $telegram_proxy_server, $telegram_proxy_login, $telegram_proxy_password); + } + else { + $result = \Drupal::service('telegram_api.service')->sendToTelegramBot($text, $telegram_bottoken, $chat_id); + } + if ($result !== TRUE) { + \Drupal::logger('entity_notify')->error($result); + } } } } diff --git a/src/Form/EntityNotifySettingsForm.php b/src/Form/EntityNotifySettingsForm.php index e6f104381805dbbd5e63d7c7551355f4adf22c65..600d49cc89d97053c7c54925a700f82a4d8473d4 100644 --- a/src/Form/EntityNotifySettingsForm.php +++ b/src/Form/EntityNotifySettingsForm.php @@ -72,6 +72,13 @@ class EntityNotifySettingsForm extends ConfigFormBase { public function buildForm(array $form, FormStateInterface $form_state) { $config = $this->config('entity_notify.settings'); + $form['enable'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Enable the notifications'), + '#description' => $this->t('You can disable this on local if you use Config Split'), + '#default_value' => $config->get('enable'), + ]; + $all_entity_types = $this->entityTypeManager->getDefinitions(); $content_entity_types = []; /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_type_options */ @@ -113,6 +120,7 @@ class EntityNotifySettingsForm extends ConfigFormBase { */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->config('entity_notify.settings') + ->set('enable', $form_state->getValue('enable')) ->set('enabled_target_entity_types', array_values(array_filter($form_state->getValue('enabled_target_entity_types')['entity_types']))) ->set('entity_notify_admin', $form_state->getValue('entity_notify_admin')) ->set('entity_notify_roles', array_values(array_filter($form_state->getValue('entity_notify_roles')))) diff --git a/tests/src/Functional/EntityNotifyCommentNotificationsTest.php b/tests/src/Functional/EntityNotifyCommentNotificationsTest.php index 1f5b1ba3803be227ba5d93fc1af4048c37c25a45..28fce4e48a8544467192e33e02b849e9698bcd8c 100644 --- a/tests/src/Functional/EntityNotifyCommentNotificationsTest.php +++ b/tests/src/Functional/EntityNotifyCommentNotificationsTest.php @@ -66,6 +66,10 @@ class EntityNotifyCommentNotificationsTest extends BrowserTestBase { public function setUp(): void { parent::setUp(); + $config = $this->config('entity_notify.settings'); + $config->set('enable', TRUE); + $config->save(); + $node_type = 'article'; $this->createContentType(['type' => $node_type]); $this->addDefaultCommentField('node', $node_type); @@ -233,4 +237,81 @@ class EntityNotifyCommentNotificationsTest extends BrowserTestBase { $this->assertCount(6, $captured_emails, 'Three emails were captured.'); } + /** + * Tests disabled notification. + */ + public function testDisabledNotify() { + $node = $this->createNode([ + 'type' => 'article', + ]); + + $this->drupalLogin($this->adminUser); + $this->drupalGet($this->settingsRoute); + $this->assertSession()->statusCodeEquals(200); + + $edit = [ + 'entity_notify_enable' => '1', + 'entity_notify_admin' => '1', + ]; + $this->submitForm($edit, 'Save'); + $this->assertSession()->pageTextContains('has been updated'); + + $captured_emails = $this->drupalGetMails(); + $this->assertCount(0, $captured_emails, 'The captured emails queue is empty.'); + + $comment = Comment::create([ + 'entity_id' => $node->id(), + 'entity_type' => 'node', + 'field_name' => 'comment', + 'pid' => 0, + 'uid' => 0, + 'status' => CommentInterface::PUBLISHED, + 'subject' => $this->randomMachineName(), + 'hostname' => '127.0.0.1', + 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, + 'comment_body' => [['value' => $this->randomMachineName()]], + ]); + $comment->save(); + + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(1, $captured_emails, 'One email was captured.'); + + $comment->setHomepage('http://example.com/')->save(); + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(2, $captured_emails, 'Two emails were captured.'); + + $comment->delete(); + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(3, $captured_emails, 'Three emails were captured.'); + + $this->drupalGet(Url::fromRoute('entity_notify.settings')); + $this->assertSession()->statusCodeEquals(200); + + $edit = [ + 'enable' => '0', + ]; + $this->submitForm($edit, 'Save'); + $this->assertSession()->pageTextContains('have been saved'); + + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(3, $captured_emails, 'Three emails were captured.'); + + $comment = Comment::create([ + 'entity_id' => $node->id(), + 'entity_type' => 'node', + 'field_name' => 'comment', + 'pid' => 0, + 'uid' => 0, + 'status' => CommentInterface::PUBLISHED, + 'subject' => $this->randomMachineName(), + 'hostname' => '127.0.0.1', + 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, + 'comment_body' => [['value' => $this->randomMachineName()]], + ]); + $comment->save(); + + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(3, $captured_emails, 'Three emails were captured.'); + } + } diff --git a/tests/src/Functional/EntityNotifyNodeNotificationsTest.php b/tests/src/Functional/EntityNotifyNodeNotificationsTest.php index c734eaf2eb9b30310f9e5b4369168816e4b8c75f..78403b46ff4d000026fa85d1e1765a3586c834ad 100644 --- a/tests/src/Functional/EntityNotifyNodeNotificationsTest.php +++ b/tests/src/Functional/EntityNotifyNodeNotificationsTest.php @@ -59,6 +59,10 @@ class EntityNotifyNodeNotificationsTest extends BrowserTestBase { public function setUp(): void { parent::setUp(); + $config = $this->config('entity_notify.settings'); + $config->set('enable', TRUE); + $config->save(); + $node_type = 'article'; $this->createContentType(['type' => $node_type]); $this->settingsRoute = Url::fromRoute('entity.node_type.edit_form', ['node_type' => $node_type]); @@ -184,4 +188,57 @@ class EntityNotifyNodeNotificationsTest extends BrowserTestBase { $this->assertCount(6, $captured_emails, 'Three emails were captured.'); } + /** + * Tests disabled notification. + */ + public function testDisabledNotify() { + $this->drupalLogin($this->adminUser); + $this->drupalGet($this->settingsRoute); + $this->assertSession()->statusCodeEquals(200); + + $edit = [ + 'entity_notify_enable' => '1', + 'entity_notify_admin' => '1', + ]; + $this->submitForm($edit, 'Save'); + $this->assertSession()->pageTextContains('has been updated'); + + $captured_emails = $this->drupalGetMails(); + $this->assertCount(0, $captured_emails, 'The captured emails queue is empty.'); + + $node = $this->createNode([ + 'type' => 'article', + ]); + + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(1, $captured_emails, 'One email was captured.'); + + $node->setTitle('New name')->save(); + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(2, $captured_emails, 'Two emails were captured.'); + + $node->delete(); + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(3, $captured_emails, 'Three emails were captured.'); + + $this->drupalGet(Url::fromRoute('entity_notify.settings')); + $this->assertSession()->statusCodeEquals(200); + + $edit = [ + 'enable' => '0', + ]; + $this->submitForm($edit, 'Save'); + $this->assertSession()->pageTextContains('have been saved'); + + $captured_emails = $this->drupalGetMails(); + $this->assertCount(3, $captured_emails, 'Three emails were captured.'); + + $this->createNode([ + 'type' => 'article', + ]); + + $captured_emails = $this->drupalGetMails(['key' => 'entity_notify_new_event']); + $this->assertCount(3, $captured_emails, 'Three emails were captured.'); + } + } diff --git a/tests/src/Functional/EntityNotifyOtherEntitiesNotificationsTest.php b/tests/src/Functional/EntityNotifyOtherEntitiesNotificationsTest.php index a4864391ab116bb79c44852f3d96dc84bba8c049..e89d14c0edd9dbc0df34e9deef100fbed4d87251 100644 --- a/tests/src/Functional/EntityNotifyOtherEntitiesNotificationsTest.php +++ b/tests/src/Functional/EntityNotifyOtherEntitiesNotificationsTest.php @@ -67,6 +67,10 @@ class EntityNotifyOtherEntitiesNotificationsTest extends BrowserTestBase { public function setUp(): void { parent::setUp(); + $config = $this->config('entity_notify.settings'); + $config->set('enable', TRUE); + $config->save(); + $this->settingsRoute = Url::fromRoute('entity_notify.settings'); $this->drupalCreateRole([], 'llamalovers', 'Llama Lovers'); diff --git a/tests/src/Functional/EntityNotifySettingsFormTest.php b/tests/src/Functional/EntityNotifySettingsFormTest.php index b33bd616e7113cba16f6830b25aebb6925a47d6f..fb60a3f5c57a1f1b0c2d3892bc8e79d916e2f011 100644 --- a/tests/src/Functional/EntityNotifySettingsFormTest.php +++ b/tests/src/Functional/EntityNotifySettingsFormTest.php @@ -72,6 +72,7 @@ class EntityNotifySettingsFormTest extends BrowserTestBase { $telegram_bot_token = $this->randomMachineName(); $telegram_chat_id = $this->randomMachineName(); $edit = [ + 'enable' => '0', 'enabled_target_entity_types[entity_types][taxonomy_term]' => 'taxonomy_term', 'entity_notify_admin' => '1', 'entity_notify_roles[llamalovers]' => 'llamalovers', @@ -86,6 +87,7 @@ class EntityNotifySettingsFormTest extends BrowserTestBase { 'entity_notify_telegram_proxy_password' => 'password', ]; $expected_values = [ + 'enable' => FALSE, 'enabled_target_entity_types' => ['taxonomy_term'], 'entity_notify_admin' => TRUE, 'entity_notify_roles' => ['llamalovers', 'catcuddlers'],