From 4196d19e5536deaeff835670a13f9e91f25d3762 Mon Sep 17 00:00:00 2001
From: lucashedding <lucashedding@1463982.no-reply.drupal.org>
Date: Wed, 5 Jun 2019 15:02:17 -0500
Subject: [PATCH] Issue #3046858 by heddn, tatarbj: Add admin / config page to
 enable/disable PSA

---
 AutomaticUpdatesPsa.php      |  6 +++++-
 automatic_updates.admin.inc  | 21 +++++++++++++++++++++
 automatic_updates.install    |  3 ++-
 automatic_updates.module     | 19 +++++++++++++++++++
 tests/automatic_updates.test | 17 ++++++++++++++---
 5 files changed, 61 insertions(+), 5 deletions(-)
 create mode 100644 automatic_updates.admin.inc

diff --git a/AutomaticUpdatesPsa.php b/AutomaticUpdatesPsa.php
index e057bc6482..1e266b436f 100644
--- a/AutomaticUpdatesPsa.php
+++ b/AutomaticUpdatesPsa.php
@@ -23,6 +23,10 @@ class AutomaticUpdatesPsa {
   public static function getPublicServiceMessages() {
     $messages = array();
 
+    if (!variable_get('automatic_updates_enable_psa')) {
+      return $messages;
+    }
+
     if ($cache = cache_get('automatic_updates_psa')) {
       $response = $cache->data;
     }
@@ -31,7 +35,7 @@ class AutomaticUpdatesPsa {
       $response = drupal_http_request($psa_endpoint);
       if (isset($response->code) && ($response->code == 200)) {
         // Set response in cache for 12 hours.
-        cache_set('automatic_updates_psa', $response, 'cache', REQUEST_TIME + 3600 * 12);
+        cache_set('automatic_updates_psa', $response, 'cache', variable_get('automatic_updates_check_frequency', REQUEST_TIME + 3600 * 12));
       }
       else {
         watchdog('automatic_updates', 'Drupal PSA endpoint %url is unreachable.', array('%url' => $psa_endpoint), WATCHDOG_ERROR);
diff --git a/automatic_updates.admin.inc b/automatic_updates.admin.inc
new file mode 100644
index 0000000000..3c799364a5
--- /dev/null
+++ b/automatic_updates.admin.inc
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * @file
+ * Administration functions for Automatic Updates module.
+ */
+
+/**
+ * Form callback for administrator interface.
+ */
+function automatic_updates_admin_form($form, &$form_state) {
+  $form['description'] = [
+    '#markup' => '<p>' . t('Public service announcements are compared against the entire code for the site, not just installed extensions.') . '</p>',
+  ];
+  $form['automatic_updates_enable_psa'] = [
+    '#type' => 'checkbox',
+    '#title' => t('Show public service announcements on administrative pages.'),
+    '#default_value' => variable_get('automatic_updates_enable_psa', TRUE),
+  ];
+  return system_settings_form($form);
+}
diff --git a/automatic_updates.install b/automatic_updates.install
index a7bc635bcd..094c3420fc 100644
--- a/automatic_updates.install
+++ b/automatic_updates.install
@@ -9,5 +9,6 @@
  * Implements hook_uninstall().
  */
 function automatic_updates_uninstall() {
-  variable_del('psa_endpoint_psa_endpoint');
+  variable_del('automatic_updates_psa_endpoint');
+  variable_del('automatic_updates_enable_psa');
 }
diff --git a/automatic_updates.module b/automatic_updates.module
index ddb99e477f..ba4c3dc694 100644
--- a/automatic_updates.module
+++ b/automatic_updates.module
@@ -31,3 +31,22 @@ function automatic_updates_init() {
     }
   }
 }
+
+/**
+ * Implements hook_menu().
+ */
+function automatic_updates_menu() {
+  $items = [];
+
+  $items['admin/config/system/automatic_updates'] = [
+    'title' => 'Automatic Updates',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => ['automatic_updates_admin_form'],
+    'file' => 'automatic_updates.admin.inc',
+    'file path' => drupal_get_path('module', 'automatic_updates'),
+    'access arguments' => ['administer software updates'],
+    'tab parent' => 'admin/config/system',
+  ];
+
+  return $items;
+}
diff --git a/tests/automatic_updates.test b/tests/automatic_updates.test
index 982eaec692..be89b049ac 100644
--- a/tests/automatic_updates.test
+++ b/tests/automatic_updates.test
@@ -27,24 +27,35 @@ class AutomaticUpdatesTestCase extends DrupalWebTestCase {
   public function setUp() {
     parent::setUp(array('automatic_updates', 'automatic_updates_test'));
     // Create a user with permission to view the actions administration pages.
-    $user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages'));
+    $user = $this->drupalCreateUser(array(
+      'access administration pages',
+      'administer site configuration',
+      'administer software updates',
+    ));
     $this->drupalLogin($user);
     $psa_endpoint = $this->getAbsoluteUrl('automatic_updates/test-json');
     variable_set('automatic_updates_psa_endpoint', $psa_endpoint);
+    variable_set('automatic_updates_enable_psa', TRUE);
   }
 
   /**
    * Test automatic updates.
    */
   public function testAutomaticUpdates() {
+    // Test PSAs.
     $this->drupalGet('admin');
-
     $this->assertText('Drupal Core PSA: Critical Release - PSA-2019-02-19');
     $this->assertNoText('Drupal Core PSA: Critical Release - PSA-Really Old');
     $this->assertNoText('Node - Moderately critical - Access bypass - SA-CONTRIB-2019');
     $this->assertText('Drupal Contrib Project PSA: Standard - Moderately critical - Access bypass - SA-CONTRIB-2019');
     $this->assertText('Drupal Contrib Project PSA: Seven - Moderately critical - Access bypass - SA-CONTRIB-2019');
 
+    // Test disabling PSAs.
+    variable_set('automatic_updates_enable_psa', FALSE);
+    $this->drupalGet('admin');
+    $this->assertNoText('Drupal Core PSA: Critical Release - PSA-2019-02-19');
+    variable_set('automatic_updates_enable_psa', TRUE);
+
     // Test cache.
     $psa_endpoint = $this->getAbsoluteUrl('automatic_updates/test-json-denied');
     variable_set('automatic_updates_psa_endpoint', $psa_endpoint);
@@ -54,7 +65,7 @@ class AutomaticUpdatesTestCase extends DrupalWebTestCase {
     // Test transmit errors with JSON endpoint.
     drupal_flush_all_caches();
     $this->drupalGet('admin');
-    $this->assertText('Drupal PSA endpoint http://localhost/automatic_updates/test-json-denied is unreachable.');
+    $this->assertText('automatic_updates/test-json-denied is unreachable.');
   }
 
 }
-- 
GitLab