From c83e637ff92c05e23f34c8d64a0c46a94a0d0dc5 Mon Sep 17 00:00:00 2001
From: Andrew Nicols <andrew@nicols.co.uk>
Date: Thu, 23 Sep 2021 13:43:47 +0800
Subject: [PATCH] MDL-72621 admin: Add environment check for admin deprecation

---
 admin/environment.xml         |  5 +++++
 config-dist.php               |  5 ++++-
 lang/en/admin.php             |  1 +
 lib/tests/upgradelib_test.php | 38 ++++++++++++++++++++++++++++++++++-
 lib/upgradelib.php            | 25 +++++++++++++++++++++++
 5 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/admin/environment.xml b/admin/environment.xml
index 6faa7ded027..55a1aee8825 100644
--- a/admin/environment.xml
+++ b/admin/environment.xml
@@ -3724,6 +3724,11 @@
       </CUSTOM_CHECK>
       <CUSTOM_CHECK file="lib/upgradelib.php" function="check_max_input_vars" level="optional">
       </CUSTOM_CHECK>
+      <CUSTOM_CHECK file="lib/upgradelib.php" function="check_admin_dir_usage" level="optional">
+        <FEEDBACK>
+          <ON_CHECK message="iscustomadminwarnings" />
+        </FEEDBACK>
+      </CUSTOM_CHECK>
     </CUSTOM_CHECKS>
   </MOODLE>
 </COMPATIBILITY_MATRIX>
diff --git a/config-dist.php b/config-dist.php
index 317fd9543ca..70268dd05da 100644
--- a/config-dist.php
+++ b/config-dist.php
@@ -166,8 +166,11 @@ $CFG->directorypermissions = 02777;
 
 
 //=========================================================================
-// 5. DIRECTORY LOCATION  (most people can just ignore this setting)
+// 5. ADMIN DIRECTORY LOCATION  (deprecated)
 //=========================================================================
+// Please note: Support from this feature has been deprecated and it will be
+// removed after Moodle 4.2.
+//
 // A very few webhosts use /admin as a special URL for you to access a
 // control panel or something.  Unfortunately this conflicts with the
 // standard location for the Moodle admin pages.  You can work around this
diff --git a/lang/en/admin.php b/lang/en/admin.php
index e7f472c56d6..90243b03e31 100644
--- a/lang/en/admin.php
+++ b/lang/en/admin.php
@@ -714,6 +714,7 @@ $string['iplookupinfo'] = 'By default Moodle uses the free online NetGeo (The In
 It is recommended to install local copy of free GeoLite2 City database from MaxMind.<br />
 IP address location is displayed on simple map or using Google Maps. Please note that you need to have a Google account and apply for free Google Maps API key to enable interactive maps.';
 $string['iplookupmaxmindnote'] = 'This product includes GeoLite2 data created by MaxMind, available from <a href="https://www.maxmind.com">https://www.maxmind.com</a>.';
+$string['iscustomadminwarnings'] = 'It has been detected that your site is using a custom admin directory. This feature is no longer supported and will be removed after Moodle 4.2.';
 $string['ishttpswarning'] = 'It has been detected that your site is not secured using HTTPS. It is strongly recommended to migrate your site to HTTPS for increased security and improved integration with other systems.';
 $string['keeptagnamecase'] = 'Keep tag name casing';
 $string['lang'] = 'Default language';
diff --git a/lib/tests/upgradelib_test.php b/lib/tests/upgradelib_test.php
index b9769af0d04..e6bc221f1c7 100644
--- a/lib/tests/upgradelib_test.php
+++ b/lib/tests/upgradelib_test.php
@@ -33,7 +33,7 @@ require_once($CFG->dirroot . '/calendar/tests/helpers.php');
 /**
  * Tests various classes and functions in upgradelib.php library.
  */
-class core_upgradelib_testcase extends advanced_testcase {
+class upgradelib_test extends advanced_testcase {
 
     /**
      * Test the {@link upgrade_stale_php_files_present() function
@@ -1490,4 +1490,40 @@ class core_upgradelib_testcase extends advanced_testcase {
         // Since group override events do not set userid, these events should not be flagged to be fixed.
         $this->assertEquals(0, $groupoverrideinfo->bad);
     }
+
+    /**
+     * Test the admin_dir_usage check with no admin setting specified.
+     */
+    public function test_admin_dir_usage_not_set(): void {
+        $result = new environment_results("custom_checks");
+
+        $this->assertNull(check_admin_dir_usage($result));
+    }
+
+    /**
+     * Test the admin_dir_usage check with the default admin setting specified.
+     */
+    public function test_admin_dir_usage_is_default(): void {
+        global $CFG;
+
+        $CFG->admin = 'admin';
+
+        $result = new environment_results("custom_checks");
+        $this->assertNull(check_admin_dir_usage($result));
+    }
+
+    /**
+     * Test the admin_dir_usage check with a custom admin setting specified.
+     */
+    public function test_admin_dir_usage_non_standard(): void {
+        global $CFG;
+
+        $this->resetAfterTest(true);
+        $CFG->admin = 'notadmin';
+
+        $result = new environment_results("custom_checks");
+        $this->assertInstanceOf(environment_results::class, check_admin_dir_usage($result));
+        $this->assertEquals('admin_dir_usage', $result->getInfo());
+        $this->assertFalse($result->getStatus());
+    }
 }
diff --git a/lib/upgradelib.php b/lib/upgradelib.php
index de8742ee329..67f9a695091 100644
--- a/lib/upgradelib.php
+++ b/lib/upgradelib.php
@@ -2701,3 +2701,28 @@ function check_max_input_vars(environment_results $result) {
     }
     return null;
 }
+
+/**
+ * Check whether the admin directory has been configured and warn if so.
+ *
+ * The admin directory has been deprecated since Moodle 4.0.
+ *
+ * @param environment_results $result
+ * @return null|environment_results
+ */
+function check_admin_dir_usage(environment_results $result): ?environment_results {
+    global $CFG;
+
+    if (empty($CFG->admin)) {
+        return null;
+    }
+
+    if ($CFG->admin === 'admin') {
+        return null;
+    }
+
+    $result->setInfo('admin_dir_usage');
+    $result->setStatus(false);
+
+    return $result;
+}