From 3b477434652853b59f652c8a92f122e88b9492fe Mon Sep 17 00:00:00 2001
From: lucashedding <lucashedding@1463982.no-reply.drupal.org>
Date: Mon, 15 Apr 2019 09:05:59 -0500
Subject: [PATCH] Issue #3046858 by heddn: Add admin / config page to
 enable/disable PSA

---
 automatic_updates.info.yml                    |  1 +
 automatic_updates.links.menu.yml              |  5 ++
 automatic_updates.routing.yml                 |  9 +++
 config/install/automatic_updates.settings.yml |  1 +
 config/schema/automatic_updates.schema.yml    |  3 +
 src/Form/AdminForm.php                        | 55 +++++++++++++++++++
 src/Services/AutomaticUpdatesPsa.php          |  4 ++
 tests/src/Functional/AutomaticUpdatesTest.php | 10 ++++
 8 files changed, 88 insertions(+)
 create mode 100644 automatic_updates.links.menu.yml
 create mode 100644 automatic_updates.routing.yml
 create mode 100644 src/Form/AdminForm.php

diff --git a/automatic_updates.info.yml b/automatic_updates.info.yml
index 3ba06a9eb1..3bcad2cfac 100644
--- a/automatic_updates.info.yml
+++ b/automatic_updates.info.yml
@@ -3,3 +3,4 @@ type: module
 description: 'Drupal Automatic Updates'
 core: 8.x
 package: 'Security'
+configure: automatic_updates.admin_form
diff --git a/automatic_updates.links.menu.yml b/automatic_updates.links.menu.yml
new file mode 100644
index 0000000000..572208f91e
--- /dev/null
+++ b/automatic_updates.links.menu.yml
@@ -0,0 +1,5 @@
+automatic_updates.admin_form:
+  title: 'Automatic Updates'
+  route_name: automatic_updates.admin_form
+  description: 'Configure automatic updates'
+  parent: system.admin_config_system
diff --git a/automatic_updates.routing.yml b/automatic_updates.routing.yml
new file mode 100644
index 0000000000..18c08689d0
--- /dev/null
+++ b/automatic_updates.routing.yml
@@ -0,0 +1,9 @@
+automatic_updates.admin_form:
+  path: '/admin/config/automatic_updates'
+  defaults:
+    _form: '\Drupal\automatic_updates\Form\AdminForm'
+    _title: 'Automatic Updates'
+  requirements:
+    _permission: 'administer software updates'
+  options:
+    _admin_route: TRUE
diff --git a/config/install/automatic_updates.settings.yml b/config/install/automatic_updates.settings.yml
index 2dc7865e65..085f065b0f 100644
--- a/config/install/automatic_updates.settings.yml
+++ b/config/install/automatic_updates.settings.yml
@@ -2,3 +2,4 @@
 # TODO:  Update to correct end point once it is available. See
 # https://www.drupal.org/project/automatic_updates/issues/3045273
 psa_endpoint: 'http://localhost/automatic_updates/test-json'
+enable_psa: true
diff --git a/config/schema/automatic_updates.schema.yml b/config/schema/automatic_updates.schema.yml
index 467d316c33..ca294031e2 100644
--- a/config/schema/automatic_updates.schema.yml
+++ b/config/schema/automatic_updates.schema.yml
@@ -5,3 +5,6 @@ automatic_updates.settings:
     psa_endpoint:
       type: string
       label: 'Endpoint URI for PSAs'
+    enable_psa:
+      type: boolean
+      label: 'Enable PSA notices'
diff --git a/src/Form/AdminForm.php b/src/Form/AdminForm.php
new file mode 100644
index 0000000000..78217e8bc7
--- /dev/null
+++ b/src/Form/AdminForm.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace Drupal\automatic_updates\Form;
+
+use Drupal\Core\Form\ConfigFormBase;
+use Drupal\Core\Form\FormStateInterface;
+
+/**
+ * Administration form for automatic updates.
+ */
+class AdminForm extends ConfigFormBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function getEditableConfigNames() {
+    return [
+      'automatic_updates.settings',
+    ];
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getFormId() {
+    return 'automatic_updates_admin_form';
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function buildForm(array $form, FormStateInterface $form_state) {
+    $config = $this->config('automatic_updates.settings');
+    $form['enable_psa'] = [
+      '#type' => 'checkbox',
+      '#title' => $this->t('Enable messaging of public service alerts (PSAs)'),
+      '#default_value' => $config->get('enable_psa'),
+    ];
+    return parent::buildForm($form, $form_state);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function submitForm(array &$form, FormStateInterface $form_state) {
+    parent::submitForm($form, $form_state);
+    $form_state->cleanValues();
+    $config = $this->config('automatic_updates.settings');
+    foreach ($form_state->getValues() as $key => $value) {
+      $config->set($key, $value);
+    }
+    $config->save();
+  }
+
+}
diff --git a/src/Services/AutomaticUpdatesPsa.php b/src/Services/AutomaticUpdatesPsa.php
index 9b31b744ad..bc33939f52 100644
--- a/src/Services/AutomaticUpdatesPsa.php
+++ b/src/Services/AutomaticUpdatesPsa.php
@@ -114,6 +114,10 @@ class AutomaticUpdatesPsa implements AutomaticUpdatesPsaInterface {
   public function getPublicServiceMessages() {
     $messages = [];
 
+    if (!$this->config->get('enable_psa')) {
+      return $messages;
+    }
+
     if ($cache = $this->cache->get('automatic_updates_psa')) {
       $response = $cache->data;
     }
diff --git a/tests/src/Functional/AutomaticUpdatesTest.php b/tests/src/Functional/AutomaticUpdatesTest.php
index 5cfca1d981..807851dfcc 100644
--- a/tests/src/Functional/AutomaticUpdatesTest.php
+++ b/tests/src/Functional/AutomaticUpdatesTest.php
@@ -68,6 +68,16 @@ class AutomaticUpdatesTest extends BrowserTestBase {
     drupal_flush_all_caches();
     $this->drupalGet(Url::fromRoute('system.admin'));
     $this->assertSession()->pageTextContains('Drupal PSA endpoint http://localhost/automatic_updates/test-json-denied is unreachable.');
+
+    // Test disabling PSAs.
+    $end_point = $this->buildUrl(Url::fromRoute('test_automatic_updates.json_test_controller'));
+    $this->config('automatic_updates.settings')
+      ->set('psa_endpoint', $end_point)
+      ->set('enable_psa', FALSE)
+      ->save();
+    drupal_flush_all_caches();
+    $this->drupalGet(Url::fromRoute('system.admin'));
+    $this->assertSession()->pageTextNotContains('Drupal Core PSA: Critical Release - PSA-2019-02-19');
   }
 
 }
-- 
GitLab