mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-16 21:41:31 +02:00
Compare commits
251 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f45918a277 | ||
|
|
478d0fc5c1 | ||
|
|
ca4a202752 | ||
|
|
25b9e553a5 | ||
|
|
5cc215d819 | ||
|
|
e8c00be2dd | ||
|
|
ca213e4c04 | ||
|
|
eac1575f51 | ||
|
|
cff0f3fbe3 | ||
|
|
ffb9e27456 | ||
|
|
f842e2baa4 | ||
|
|
f3d91cb044 | ||
|
|
a63ffed1bc | ||
|
|
c99d8f12a0 | ||
|
|
d3b5ed73aa | ||
|
|
03362c084c | ||
|
|
51db933862 | ||
|
|
11fbe64c46 | ||
|
|
0114db359b | ||
|
|
aedf7a5a17 | ||
|
|
3f79129774 | ||
|
|
43d82333ca | ||
|
|
0dfede6084 | ||
|
|
7f55f5d403 | ||
|
|
2a55769a86 | ||
|
|
68959e336d | ||
|
|
386a623bb3 | ||
|
|
e01fa3797e | ||
|
|
5aac578c98 | ||
|
|
329e92445c | ||
|
|
07d4fc89b8 | ||
|
|
a40e198d3e | ||
|
|
c5485849d5 | ||
|
|
cba55cdc69 | ||
|
|
bfa9569e3b | ||
|
|
abcb087a58 | ||
|
|
97b118593d | ||
|
|
dae83f45ba | ||
|
|
c9b5a9ad92 | ||
|
|
541b2d4407 | ||
|
|
e281b59eb4 | ||
|
|
b90d17d84a | ||
|
|
24678ab31e | ||
|
|
94afadac9d | ||
|
|
57b5a229c0 | ||
|
|
bf7e2e5a97 | ||
|
|
3f64f702c4 | ||
|
|
cf0969c933 | ||
|
|
4aa5976ea8 | ||
|
|
143862e25a | ||
|
|
bed817cd5e | ||
|
|
755dcf8152 | ||
|
|
5cebaf9227 | ||
|
|
a8aa5c49c5 | ||
|
|
85bcca8d38 | ||
|
|
3b87330bb1 | ||
|
|
1627bced86 | ||
|
|
89e2621bf6 | ||
|
|
9473db8c6e | ||
|
|
b9cbf015ac | ||
|
|
42d4744534 | ||
|
|
b95fb97af5 | ||
|
|
bbde2f74d3 | ||
|
|
0791b39744 | ||
|
|
21fe888862 | ||
|
|
12f43447b7 | ||
|
|
5465e5fe40 | ||
|
|
8eb86bce44 | ||
|
|
fe18931334 | ||
|
|
0344500671 | ||
|
|
1f08a44a1a | ||
|
|
c64a33c345 | ||
|
|
afc96d347d | ||
|
|
59fbdee57b | ||
|
|
2b554967d9 | ||
|
|
5bf0496c79 | ||
|
|
015300d098 | ||
|
|
ddfb17f5f8 | ||
|
|
a98029f41e | ||
|
|
6dc2227034 | ||
|
|
c628cd8218 | ||
|
|
c47d102326 | ||
|
|
1f5927c394 | ||
|
|
6e8aa6d007 | ||
|
|
3181522f63 | ||
|
|
690d90f3bc | ||
|
|
94fc6fb540 | ||
|
|
43ff1f9301 | ||
|
|
34a716226e | ||
|
|
6af9317240 | ||
|
|
eea4029f57 | ||
|
|
f95fd64871 | ||
|
|
cf8c88aefc | ||
|
|
c0fa33a559 | ||
|
|
7c9bb96f1a | ||
|
|
45987b9259 | ||
|
|
17463f0d3e | ||
|
|
2aec03911a | ||
|
|
a262467d58 | ||
|
|
207223fc2d | ||
|
|
b40fb047e2 | ||
|
|
4bda39434d | ||
|
|
50511d1e46 | ||
|
|
baa7138a01 | ||
|
|
802acbd26c | ||
|
|
9c2b529831 | ||
|
|
e1d7d2d3f4 | ||
|
|
1f03af1968 | ||
|
|
b29e7e914b | ||
|
|
6255ebb311 | ||
|
|
18d5864cac | ||
|
|
0a34a368d6 | ||
|
|
84ad9759f4 | ||
|
|
bfc3c2317b | ||
|
|
5fef56cb2f | ||
|
|
c55b9271c4 | ||
|
|
77ce63e4f9 | ||
|
|
47154e545f | ||
|
|
2d59580d36 | ||
|
|
49ea2a142e | ||
|
|
a6bb7f4bc2 | ||
|
|
f40f52bdc3 | ||
|
|
07a8a41ad4 | ||
|
|
19bc18512d | ||
|
|
116467429e | ||
|
|
def6e997a9 | ||
|
|
c53a629bc8 | ||
|
|
8f7d38f355 | ||
|
|
e58f7b73b3 | ||
|
|
f77c85355c | ||
|
|
56ac8aa19b | ||
|
|
c590eadb4f | ||
|
|
7cd10ab05c | ||
|
|
ca6c48d0f3 | ||
|
|
1cd0b7c365 | ||
|
|
ce93854b92 | ||
|
|
aab2af04e8 | ||
|
|
42babf617b | ||
|
|
90bc928754 | ||
|
|
e22c393681 | ||
|
|
e4dc17aae4 | ||
|
|
5fa3ba8034 | ||
|
|
6113b48866 | ||
|
|
01bdacc90b | ||
|
|
0efb9acbd1 | ||
|
|
2b249ac036 | ||
|
|
d9b29d2335 | ||
|
|
c4f9945c21 | ||
|
|
6f65ff9199 | ||
|
|
a62be02438 | ||
|
|
32412c2580 | ||
|
|
5a53bf04a1 | ||
|
|
b2e8100c19 | ||
|
|
bf242d9e65 | ||
|
|
981452ac27 | ||
|
|
73f283847a | ||
|
|
afc81edfde | ||
|
|
9ed0b74112 | ||
|
|
22b38c2838 | ||
|
|
0e6a8ccabb | ||
|
|
3b1b1ea0e1 | ||
|
|
e6a63f02e6 | ||
|
|
56126cd431 | ||
|
|
777e466a0a | ||
|
|
4467bea1cf | ||
|
|
77b63bce75 | ||
|
|
bac83b143a | ||
|
|
7d52bb9695 | ||
|
|
c3a2faef08 | ||
|
|
51f6f5ef5c | ||
|
|
75d872564b | ||
|
|
98b3bb38f6 | ||
|
|
b435a3f0a7 | ||
|
|
b428805c0e | ||
|
|
a722ca6965 | ||
|
|
2535d6fa4c | ||
|
|
cd21fb977b | ||
|
|
a71abed5b4 | ||
|
|
2cecb87317 | ||
|
|
b6f5db001a | ||
|
|
aadd7eaeee | ||
|
|
7ad82886d7 | ||
|
|
26baaf9993 | ||
|
|
a361ae4008 | ||
|
|
7a1fa1f91b | ||
|
|
ae85c94364 | ||
|
|
2af15f2136 | ||
|
|
076321db60 | ||
|
|
c724e1abe4 | ||
|
|
d438027a18 | ||
|
|
587bd3a311 | ||
|
|
0137811f2b | ||
|
|
a906f993f0 | ||
|
|
10223da351 | ||
|
|
1e7d035a9b | ||
|
|
d709f39122 | ||
|
|
acda7557d1 | ||
|
|
ecaee161bb | ||
|
|
de958edc9f | ||
|
|
20258deb20 | ||
|
|
d3816960f0 | ||
|
|
2a6cf91743 | ||
|
|
6e960e8814 | ||
|
|
e56cdf4ed3 | ||
|
|
34c34603b6 | ||
|
|
905ffa34be | ||
|
|
f60244f62c | ||
|
|
7c77d2652f | ||
|
|
a2224e28da | ||
|
|
02524a4028 | ||
|
|
40012e84ae | ||
|
|
a56c67284f | ||
|
|
816114bd87 | ||
|
|
0658733a86 | ||
|
|
9c7eb1b681 | ||
|
|
de1c2cab8a | ||
|
|
92ad2f5db5 | ||
|
|
a3304d0bfe | ||
|
|
36cd0517b9 | ||
|
|
dec4c7bf53 | ||
|
|
e94d9c94ea | ||
|
|
030fec75e1 | ||
|
|
cf85a04ddc | ||
|
|
c820bb1c0b | ||
|
|
e1b6279437 | ||
|
|
846879d2d6 | ||
|
|
38f838d84e | ||
|
|
df649c8133 | ||
|
|
8f5e5dbb50 | ||
|
|
f4962f22a8 | ||
|
|
1ec50dadb2 | ||
|
|
54045d5ce1 | ||
|
|
e84534f7b6 | ||
|
|
ceb9e065b9 | ||
|
|
3639883d30 | ||
|
|
6c69d46ebe | ||
|
|
6cea3f52c2 | ||
|
|
49bdb687c3 | ||
|
|
c175333f88 | ||
|
|
3e5446d8d1 | ||
|
|
6c489ad20e | ||
|
|
a33e80b4aa | ||
|
|
fa0641ec47 | ||
|
|
20df09d3e9 | ||
|
|
ff59dac023 | ||
|
|
5aaf0ddeda | ||
|
|
b3917b7b63 | ||
|
|
882f6a1afc | ||
|
|
07e26e312c | ||
|
|
92f7ec0541 | ||
|
|
84b35f01fa |
12
.github/workflows/ci.yml
vendored
Normal file
12
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
# .github/workflows/ci.yml
|
||||
name: ci
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
ci:
|
||||
uses: catalyst/catalyst-moodle-workflows/.github/workflows/ci.yml@main
|
||||
secrets:
|
||||
moodle_org_token: ${{ secrets.MOODLE_ORG_TOKEN }}
|
||||
with:
|
||||
disable_behat: true
|
||||
55
.travis.yml
55
.travis.yml
@@ -1,55 +0,0 @@
|
||||
language: php
|
||||
|
||||
notifications:
|
||||
email:
|
||||
recipients:
|
||||
- daniel.roperto@catalyst-au.net
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.composer/cache
|
||||
|
||||
addons:
|
||||
postgresql: "9.3"
|
||||
|
||||
php:
|
||||
- 7.0
|
||||
|
||||
env:
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_30_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_31_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_32_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=MOODLE_33_STABLE
|
||||
- DB=pgsql MOODLE_BRANCH=master
|
||||
- DB=mysqli MOODLE_BRANCH=master
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- php: 5.5
|
||||
env: DB=pgsql MOODLE_BRANCH=MOODLE_30_STABLE
|
||||
- php: 5.5
|
||||
env: DB=mysqli MOODLE_BRANCH=MOODLE_30_STABLE
|
||||
- php: 7.1
|
||||
env: DB=pgsql MOODLE_BRANCH=master
|
||||
|
||||
|
||||
before_install:
|
||||
- phpenv config-rm xdebug.ini
|
||||
- cd ../..
|
||||
- composer selfupdate
|
||||
- composer create-project -n --no-dev moodlerooms/moodle-plugin-ci ci ^1
|
||||
- export PATH="$(cd ci/bin; pwd):$(cd ci/vendor/bin; pwd):$PATH"
|
||||
|
||||
install:
|
||||
- moodle-plugin-ci install -vvv
|
||||
|
||||
script:
|
||||
- moodle-plugin-ci validate
|
||||
- moodle-plugin-ci phplint
|
||||
- moodle-plugin-ci phpcpd
|
||||
- moodle-plugin-ci phpmd
|
||||
- moodle-plugin-ci codechecker
|
||||
- moodle-plugin-ci csslint
|
||||
- moodle-plugin-ci shifter
|
||||
- moodle-plugin-ci jshint
|
||||
- moodle-plugin-ci phpunit
|
||||
@@ -59,6 +59,9 @@ Basic Usage
|
||||
|
||||
_Anyone who tries to use Moodle now will receive a maintenance message._
|
||||
|
||||
**Note:** If the outage starts with the option `Auto start maintenance mode` checked, the maint mode won't exit automatically.
|
||||
It will need to be disabled through command line.
|
||||
|
||||
1) Perform the maintenance activities.
|
||||
|
||||
At this point it is safe to perform the required maintenance as no one can use the system, not even admins.
|
||||
|
||||
111
README.md
111
README.md
@@ -1,17 +1,21 @@
|
||||
<a href="https://travis-ci.org/catalyst/moodle-auth_outage">
|
||||
<img src="https://travis-ci.org/catalyst/moodle-auth_outage.svg?branch=master">
|
||||
</a>
|
||||
[](https://github.com/catalyst/moodle-auth_outage/actions/workflows/ci.yml?branch=MOODLE_39_STABLE)
|
||||
|
||||
# Moodle Outage manager plugin
|
||||
|
||||
* [What is this?](#what-is-this)
|
||||
* [Moodle Requirements](#moodle-requirements)
|
||||
* [Screenshots](#screenshots)
|
||||
* [Installation](#installation)
|
||||
* [How to use](#how-to-use)
|
||||
* [Quick Guide](#quick-guide)
|
||||
* [Why is it an auth plugin?](#why-it-is-an-auth-plugin)
|
||||
* [Feedback and issues](#feedback-and-issues)
|
||||
- [Moodle Outage manager plugin](#moodle-outage-manager-plugin)
|
||||
- [What is this?](#what-is-this)
|
||||
- [Moodle Requirements](#moodle-requirements)
|
||||
- [Branches](#branches)
|
||||
- [Screenshots](#screenshots)
|
||||
- [Installation](#installation)
|
||||
- [Theme configuration](#theme-configuration)
|
||||
- [Custom Theme Additional SCSS](#custom-theme-additional-scss)
|
||||
- [How to use](#how-to-use)
|
||||
- [Quick Guide](#quick-guide)
|
||||
- [Why it is an auth plugin?](#why-it-is-an-auth-plugin)
|
||||
- [Tester restriction options](#tester-restriction-options)
|
||||
- [IP restriction](#ip-restriction)
|
||||
- [Access key](#access-key)
|
||||
- [Feedback and issues](#feedback-and-issues)
|
||||
|
||||
What is this?
|
||||
-------------
|
||||
@@ -25,10 +29,7 @@ this plugin creates the concept of graduated outages where at predefined times b
|
||||
an outage and after, different levels of warning and access can be provided to students
|
||||
and testers letting them know what is about to happen and why.
|
||||
|
||||
<img alt="Default" src="https://cdn.rawgit.com/catalyst/moodle-auth_outage/master/docs/default.svg" width="520">
|
||||
|
||||
<img alt="Manager" src="https://cdn.rawgit.com/catalyst/moodle-auth_outage/master/docs/outage.svg" width="520">
|
||||
|
||||

|
||||
|
||||
Moodle Requirements
|
||||
-------------------
|
||||
@@ -39,6 +40,14 @@ If you have an older version of Moodle you can still make it work but you will
|
||||
need to manually add one extra plugin, please check:
|
||||
* https://github.com/catalyst/moodle-local_outage
|
||||
|
||||
Branches
|
||||
--------
|
||||
| Moodle version | Totara | Branch | PHP |
|
||||
| ------------------ | --------------- | ---------------- | ---- |
|
||||
| Moodle 3.9+ | Totara 13+ | MOODLE_39_STABLE | 7.2+ |
|
||||
| Moodle 3.3 to 3.8 | Totara 11 to 12 | MOODLE_38_STABLE | 7.1+ |
|
||||
| Moodle 2.7 to 3.2 | | MOODLE_32_STABLE | 5.5+ |
|
||||
| | Totara up to 10 | TOTARA_10 | 5.5+ |
|
||||
|
||||
Screenshots
|
||||
-----------
|
||||
@@ -83,6 +92,62 @@ if (file_exists(__DIR__.'/auth/outage/bootstrap.php')) {
|
||||
}
|
||||
```
|
||||
|
||||
Theme configuration
|
||||
-------------------
|
||||
|
||||
This plugin must work gracefully with your theme, but every theme can be different so it's impossible to get this right out of the box (other than with the default moodle theme Boost).
|
||||
|
||||
There is an admin setting which allows you to add or override and css to fix css issues. Typically these include properly pushing the page down when the outage notification bar is visible, including making this work with fixed headers and when the hamburger menu is open / closed and at different responsive breakpoints.
|
||||
|
||||
This can be found at:
|
||||
|
||||
`Dashboard / Site administration / Plugins / Authentication / Outage manager / Settings`
|
||||
|
||||
|
||||
|
||||
Custom Theme Additional SCSS
|
||||
-------------------
|
||||
|
||||
Custom themes generally do not have the same `$navbar-height` variable set to 80px (MOODLE), therefore custom themes will not calculate the change in navbar height with page elements that calculate the navbar total height.
|
||||
|
||||
Add the following SCSS For Moodle 3.11+
|
||||
|
||||
```
|
||||
body.auth_outage {
|
||||
#page-wrapper {
|
||||
#nav-drawer {
|
||||
top: $navbar-height + 100px;
|
||||
height: calc(100% - (#{$navbar-height} + 100px));
|
||||
}
|
||||
#page {
|
||||
margin-top: $navbar-height + 100px;
|
||||
}
|
||||
}
|
||||
[data-region=right-hand-drawer].drawer {
|
||||
top: $navbar-height + 100px;
|
||||
height: calc(100% - (#{$navbar-height} + 100px));
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Totara is a little different with version 13+ and no variables are used to set the `totaraNav` height
|
||||
|
||||
Add the following CSS For Totara 13+
|
||||
|
||||
```
|
||||
.totaraNav {
|
||||
margin-top: 100px;
|
||||
}
|
||||
.local_envbar .totaraNav {
|
||||
margin-top: 50px;
|
||||
}
|
||||
body.auth_outage #page {
|
||||
margin-top: 0;
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
How to use
|
||||
----------
|
||||
|
||||
@@ -99,7 +164,7 @@ Creates a new outage.
|
||||
-c, --clone clone another outage except for the start time.
|
||||
-a, --autostart must be Y or N, sets if the outage automatically triggers maintenance mode.
|
||||
-w, --warn how many seconds before it starts to display a warning.
|
||||
-s, --start in how many seconds should this outage start. Required.
|
||||
-s, --start in how many seconds should this outage start or unix time to start outage. Required.
|
||||
-d, --duration how many seconds should the outage last.
|
||||
-t, --title the title of the outage.
|
||||
-e, --description the description of the outage.
|
||||
@@ -118,6 +183,18 @@ Why it is an auth plugin?
|
||||
|
||||
One of the graduated stages this plugin introduces is a 'tester only' mode which disables login for most normal users. This is conceptually similar to the maintenance mode but enables testers to login and confirm the state after an upgrade without needing full admin privileges.
|
||||
|
||||
Tester restriction options
|
||||
------------
|
||||
Two options are available to restrict the site to only let testers in during the tester phase.
|
||||
Note: these restrictions build on each other; If both are enabled, users must meet both criteria to be allowed in.
|
||||
|
||||
## IP restriction
|
||||
Only allow users from a certain IP or range of ips to enter.
|
||||
## Access key
|
||||
Users provide an access key in the URL params on first page load, which is then stored as a cookie for 24 hours. If the access key matches the one setup for the outage, they are allowed in.
|
||||
## Using IP restriction with access key
|
||||
Users will be allowed if they are from the configured allowed ips OR if they provide the correct access key.
|
||||
|
||||
|
||||
Feedback and issues
|
||||
-------------------
|
||||
|
||||
8
auth.php
8
auth.php
@@ -58,7 +58,6 @@ class auth_plugin_outage extends auth_plugin_base {
|
||||
/**
|
||||
* Always returns false (password wrong or user does not exist).
|
||||
*
|
||||
* @inheritdoc PHPMD will not warn about unused parameters if overriding.
|
||||
* @param string $username Not used in this plugin.
|
||||
* @param string $password Not used in this plugin.
|
||||
* @return bool False
|
||||
@@ -66,11 +65,4 @@ class auth_plugin_outage extends auth_plugin_base {
|
||||
public function user_login($username, $password) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Login page hook.
|
||||
*/
|
||||
public function loginpage_hook() {
|
||||
outagelib::inject();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* This file should run before config.php requires '/lib/setup.php'.
|
||||
*
|
||||
@@ -29,16 +30,16 @@
|
||||
* @var stdClass $CFG
|
||||
*/
|
||||
|
||||
// This call is required by Moodle, but this script should have been called by config.php anyway.
|
||||
require_once(__DIR__.'/../../config.php');
|
||||
|
||||
define('MOODLE_INTERNAL', true);
|
||||
defined('MOODLE_INTERNAL') || die(); // Make sniffer happy.
|
||||
//
|
||||
// We need the CFG->dataroot, if not set yet this script is called too early in config.php file.
|
||||
if (!isset($CFG->dataroot)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1) Make sure we replace the configurations for behat as we have not ran 'lib/setup.php' yet.
|
||||
if (!empty($CFG->behat_wwwroot) or !empty($CFG->behat_dataroot) or !empty($CFG->behat_prefix)) {
|
||||
if (!empty($CFG->behat_wwwroot) || !empty($CFG->behat_dataroot) || !empty($CFG->behat_prefix)) {
|
||||
require_once(__DIR__.'/../../lib/behat/lib.php');
|
||||
behat_update_vars_for_process();
|
||||
if (behat_is_test_site()) {
|
||||
@@ -67,11 +68,24 @@ if (is_callable('auth_outage_bootstrap_callback')) {
|
||||
}
|
||||
|
||||
// 3) Check for allowed scripts or IPs during outages.
|
||||
if (!empty($_SERVER['REQUEST_URI'])) {
|
||||
$rooturl = parse_url($CFG->wwwroot);
|
||||
$path = '';
|
||||
if (array_key_exists('path', $rooturl) && !empty($rooturl['path'])) {
|
||||
$path = $rooturl['path'];
|
||||
}
|
||||
$url = $path.'/auth/outage/info.php';
|
||||
$outageinfo = strpos($_SERVER['REQUEST_URI'], $url) === 0 ? true : false;
|
||||
}
|
||||
|
||||
$allowed = !file_exists($CFG->dataroot.'/climaintenance.php') // Not in maintenance mode.
|
||||
|| (defined('ABORT_AFTER_CONFIG') && ABORT_AFTER_CONFIG) // Only config requested.
|
||||
|| (defined('CLI_SCRIPT') && CLI_SCRIPT); // Allow CLI scripts.
|
||||
|| (defined('CLI_SCRIPT') && CLI_SCRIPT) // Allow CLI scripts.
|
||||
|| $outageinfo // Allow outage info requests.
|
||||
|| (defined('NO_AUTH_OUTAGE') && NO_AUTH_OUTAGE); // Allow any page should not be blocked by maintenance mode.
|
||||
if (!$allowed) {
|
||||
// Call the climaintenance.php which will check for allowed IPs.
|
||||
// Call the climaintenance.php which will check for the conditions
|
||||
// that have been baked into it from the frontend (ip, accesskey, etc...).
|
||||
$CFG->dirroot = dirname(dirname(dirname(__FILE__))); // It is not defined yet but the script below needs it.
|
||||
require($CFG->dataroot.'/climaintenance.php'); // This call may terminate the script here or not.
|
||||
}
|
||||
|
||||
@@ -25,6 +25,12 @@
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
|
||||
define('NO_MOODLE_COOKIES', true);
|
||||
// @codingStandardsIgnoreStart
|
||||
header('Cache-Control: public, max-age=10,s-maxage=10');
|
||||
// @codingStandardsIgnoreEnd
|
||||
define('NO_AUTH_OUTAGE', true);
|
||||
|
||||
require_once(__DIR__.'/../../config.php');
|
||||
|
||||
$active = outagedb::get_active();
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* calendar class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\calendar;
|
||||
|
||||
use auth_outage\local\outage;
|
||||
@@ -65,7 +56,7 @@ class calendar {
|
||||
debugging('Cannot update calendar entry for outage #'.$outage->id.', event not found. Creating it...');
|
||||
self::create($outage);
|
||||
} else {
|
||||
$event->update(self::create_data($outage));
|
||||
$event->update(self::create_data($outage), false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -98,6 +89,7 @@ class calendar {
|
||||
'userid' => 0,
|
||||
'modulename' => '',
|
||||
'instance' => $outage->id,
|
||||
'component' => 'auth_outage',
|
||||
'eventtype' => 'auth_outage',
|
||||
'timestart' => $outage->starttime,
|
||||
'visible' => true,
|
||||
|
||||
@@ -14,18 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* outagedb class.
|
||||
*
|
||||
* The DB Context to manipulate Outages.
|
||||
* It will also commit changes to the calendar as you change outages.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\dml;
|
||||
|
||||
use auth_outage\calendar\calendar;
|
||||
@@ -117,25 +105,40 @@ class outagedb {
|
||||
$outage->createdby = $USER->id;
|
||||
// Then create it, log it and adjust its id.
|
||||
$outage->id = $DB->insert_record('auth_outage', $outage, true);
|
||||
outage_created::create(
|
||||
['objectid' => $outage->id, 'other' => (array)$outage]
|
||||
)->trigger();
|
||||
|
||||
$other = (array) $outage;
|
||||
$other['title'] = $outage->get_title();
|
||||
$event = outage_created::create([
|
||||
'objectid' => $outage->id,
|
||||
'other' => $other,
|
||||
]);
|
||||
$event->add_record_snapshot('auth_outage', (object)(array) $outage);
|
||||
$event->trigger();
|
||||
|
||||
// Create calendar entry.
|
||||
calendar::create($outage);
|
||||
} else {
|
||||
|
||||
$other = (array) $outage;
|
||||
$other['title'] = $outage->get_title();
|
||||
$event = outage_updated::create([
|
||||
'objectid' => $outage->id,
|
||||
'other' => $other,
|
||||
]);
|
||||
|
||||
$event->add_record_snapshot('auth_outage', (object)(array) $outage);
|
||||
$event->trigger();
|
||||
|
||||
// Remove the createdby field so it does not get updated.
|
||||
unset($outage->createdby);
|
||||
$DB->update_record('auth_outage', $outage);
|
||||
// Log it.
|
||||
outage_updated::create(
|
||||
['objectid' => $outage->id, 'other' => (array)$outage]
|
||||
)->trigger();
|
||||
|
||||
// Update calendar entry.
|
||||
calendar::update($outage);
|
||||
}
|
||||
|
||||
// Trigger outages modified events.
|
||||
outagelib::prepare_next_outage();
|
||||
outagelib::prepare_next_outage(true);
|
||||
|
||||
// All done, return the id.
|
||||
return $outage->id;
|
||||
@@ -156,7 +159,16 @@ class outagedb {
|
||||
|
||||
// Log it.
|
||||
$previous = $DB->get_record('auth_outage', ['id' => $id], '*', MUST_EXIST);
|
||||
$event = outage_deleted::create(['objectid' => $id, 'other' => (array)$previous]);
|
||||
|
||||
$outage = new outage($previous);
|
||||
|
||||
$other = (array) $outage;
|
||||
$other['title'] = $outage->get_title();
|
||||
$event = outage_deleted::create([
|
||||
'objectid' => $id,
|
||||
'other' => $other,
|
||||
]);
|
||||
|
||||
$event->add_record_snapshot('auth_outage', $previous);
|
||||
$event->trigger();
|
||||
|
||||
@@ -187,21 +199,20 @@ class outagedb {
|
||||
throw new coding_exception('$time must be null or a positive int.', $time);
|
||||
}
|
||||
|
||||
$select = ':datetime2 <= stoptime AND (finished IS NULL OR :datetime3 <= finished)'; // End condition.
|
||||
$select = "(warntime <= :datetime1 AND (${select}))"; // Full select part.
|
||||
$data = $DB->get_records_select(
|
||||
'auth_outage',
|
||||
$select,
|
||||
['datetime1' => $time, 'datetime2' => $time, 'datetime3' => $time],
|
||||
'starttime ASC, stoptime DESC, title ASC',
|
||||
'*',
|
||||
0,
|
||||
1
|
||||
);
|
||||
// Get cached outage, or null.
|
||||
$outageinfo = get_config('moodle', 'auth_outage_active_outage');
|
||||
|
||||
// Not using $DB->get_record_select instead because there is no 'limit' parameter.
|
||||
// Allowing multiple records still raises an internal error.
|
||||
return (count($data) == 0) ? null : new outage(array_shift($data));
|
||||
if (!$outageinfo) {
|
||||
return null;
|
||||
} else {
|
||||
$outagecache = new outage(json_decode($outageinfo));
|
||||
}
|
||||
|
||||
if ($outagecache && $outagecache->warntime <= $time && $outagecache->stoptime >= $time
|
||||
&& (!$outagecache->finished || $outagecache->finished >= $time)) {
|
||||
return $outagecache;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,22 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Toutage_created class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\event;
|
||||
|
||||
use core\event\base;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* outage_created class.
|
||||
*
|
||||
@@ -45,8 +34,7 @@ class outage_created extends base {
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with the id '{$this->userid}' created a new outage title '{$this->other['title']}' ".
|
||||
" with id '{$this->other['id']}'.";
|
||||
return "The user with the id '{$this->userid}' created outage {$this->other['id']} '{$this->other['title']}'";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,7 +42,7 @@ class outage_created extends base {
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new moodle_url('/auth/outage/list.php#auth_outage_id_'.$this->other['id']);
|
||||
return new moodle_url('/auth/outage/edit.php', ['edit' => $this->other['id']]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,21 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* outage_deleted class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
namespace auth_outage\event;
|
||||
|
||||
use core\event\base;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* outage_deleted class.
|
||||
*
|
||||
@@ -44,8 +34,7 @@ class outage_deleted extends base {
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with the id '{$this->userid}' deleted the outage titled '{$this->other['title']}' ".
|
||||
"with id '{$this->other['id']}'.";
|
||||
return "The user with the id '{$this->userid}' deleted outage {$this->other['id']} '{$this->other['title']}'";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,7 +42,7 @@ class outage_deleted extends base {
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new moodle_url('/auth/outage/list.php#auth_outage_id_'.$this->other['id']);
|
||||
return new moodle_url('/auth/outage/manage.php');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,22 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* outage_updated class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\event;
|
||||
|
||||
use core\event\base;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* outage_updated class.
|
||||
*
|
||||
@@ -45,8 +34,7 @@ class outage_updated extends base {
|
||||
* @return string
|
||||
*/
|
||||
public function get_description() {
|
||||
return "The user with the id '{$this->userid}' updated the outage title '{$this->other['title']}' ".
|
||||
"with id '{$this->other['id']}'.";
|
||||
return "The user with the id '{$this->userid}' updated outage {$this->other['id']} '{$this->other['title']}'";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -54,7 +42,7 @@ class outage_updated extends base {
|
||||
* @return moodle_url
|
||||
*/
|
||||
public function get_url() {
|
||||
return new moodle_url('/auth/outage/list.php');
|
||||
return new moodle_url('/auth/outage/edit.php', ['edit' => $this->other['id']]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,23 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* delete class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\form\outage;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir.'/formslib.php');
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* delete class.
|
||||
*
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* edit class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\form\outage;
|
||||
|
||||
use auth_outage\local\outage;
|
||||
@@ -82,6 +73,15 @@ class edit extends moodleform {
|
||||
$mform->addHelpButton('description', 'description', 'auth_outage');
|
||||
|
||||
$mform->addElement('static', 'usagehints', '', get_string('textplaceholdershint', 'auth_outage'));
|
||||
$mform->addElement('static', 'warningreenablemaintenancemode', '');
|
||||
|
||||
$mform->addElement('advcheckbox', 'useaccesskey', get_string('useaccesskey', 'auth_outage'),
|
||||
get_string('useaccesskey:desc', 'auth_outage'), 0);
|
||||
|
||||
$mform->addElement('text', 'accesskey', get_string('accesskey', 'auth_outage'));
|
||||
$mform->setType('accesskey', PARAM_TEXT);
|
||||
$mform->disabledIf('accesskey', 'useaccesskey');
|
||||
$mform->addHelpButton('accesskey', 'accesskey', 'auth_outage');
|
||||
|
||||
$this->add_action_buttons();
|
||||
}
|
||||
@@ -136,6 +136,7 @@ class edit extends moodleform {
|
||||
'warntime' => $data->starttime - $data->warningduration,
|
||||
'title' => $data->title,
|
||||
'description' => $data->description['text'],
|
||||
'accesskey' => $data->useaccesskey ? $data->accesskey : null,
|
||||
];
|
||||
return new outage($outagedata);
|
||||
}
|
||||
@@ -146,6 +147,9 @@ class edit extends moodleform {
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function set_data($outage) {
|
||||
global $OUTPUT, $CFG;
|
||||
$mform = $this->_form;
|
||||
|
||||
// Cannot change method signature, check type.
|
||||
if ($outage instanceof outage) {
|
||||
$this->_form->setDefaults([
|
||||
@@ -156,7 +160,24 @@ class edit extends moodleform {
|
||||
'warningduration' => $outage->get_warning_duration(),
|
||||
'title' => $outage->title,
|
||||
'description' => ['text' => $outage->description, 'format' => '1'],
|
||||
'accesskey' => $outage->accesskey,
|
||||
'useaccesskey' => !empty($outage->accesskey),
|
||||
]);
|
||||
|
||||
// If the default_autostart is configured in config, then force autostart to be the default value.
|
||||
if (array_key_exists('auth_outage', $CFG->forced_plugin_settings)
|
||||
&& array_key_exists('default_autostart', $CFG->forced_plugin_settings['auth_outage'])) {
|
||||
$this->_form->setDefaults([
|
||||
'autostart' => $CFG->forced_plugin_settings['auth_outage']['default_autostart'],
|
||||
]);
|
||||
$mform->freeze('autostart');
|
||||
}
|
||||
|
||||
if (!empty($outage->id) && $outage->autostart && $outage->starttime < time() && $outage->stoptime > time()) {
|
||||
$warning = $mform->getElement('warningreenablemaintenancemode');
|
||||
$warning->setValue($OUTPUT->notification(get_string('warningreenablemaintenancemode', 'auth_outage'),
|
||||
'notifywarning'));
|
||||
}
|
||||
} else {
|
||||
throw new coding_exception('$outage must be an outage object.', $outage);
|
||||
}
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* finish class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\form\outage;
|
||||
|
||||
use moodleform;
|
||||
|
||||
41
classes/hook_callbacks.php
Normal file
41
classes/hook_callbacks.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace auth_outage;
|
||||
|
||||
use auth_outage\local\outagelib;
|
||||
use core\hook\output\before_standard_top_of_body_html_generation;
|
||||
|
||||
/**
|
||||
* Hook callbacks for auth_outage.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Benjamin Walker (benjaminwalker@catalyst-au.net)
|
||||
* @copyright 2024 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class hook_callbacks {
|
||||
|
||||
/**
|
||||
* Inject the warning bar into the page if there is currently an outage.
|
||||
*
|
||||
* @param before_standard_top_of_body_html_generation $hook
|
||||
*/
|
||||
public static function before_standard_top_of_body_html_generation(before_standard_top_of_body_html_generation $hook): void {
|
||||
// Get code to inject.
|
||||
$hook->add_html(outagelib::get_inject_code());
|
||||
}
|
||||
}
|
||||
@@ -14,21 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* cli_exception class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local\cli;
|
||||
|
||||
use Exception;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* cli_exception class.
|
||||
*
|
||||
@@ -87,9 +76,9 @@ class cli_exception extends Exception {
|
||||
* cliexception constructor.
|
||||
* @param string $message An explanation of the exception.
|
||||
* @param int $code Exit code to be used.
|
||||
* @param Exception $previous Another exception as reference or null.
|
||||
* @param Exception|null $previous Another exception as reference or null.
|
||||
*/
|
||||
public function __construct($message, $code = 1, Exception $previous = null) {
|
||||
public function __construct($message, $code = 1, ?Exception $previous = null) {
|
||||
parent::__construct('*ERROR* '.$message, $code, $previous = null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,23 +14,12 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* clibase class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local\cli;
|
||||
|
||||
use auth_outage\local\outagelib;
|
||||
use coding_exception;
|
||||
use core\session\manager;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* clibase class.
|
||||
*
|
||||
@@ -52,10 +41,10 @@ abstract class clibase {
|
||||
|
||||
/**
|
||||
* clibase constructor.
|
||||
* @param array $options The parameters to use.
|
||||
* @param array|null $options The parameters to use.
|
||||
* @throws cli_exception
|
||||
*/
|
||||
public function __construct(array $options = null) {
|
||||
public function __construct(?array $options = null) {
|
||||
global $CFG;
|
||||
require_once($CFG->libdir.'/clilib.php');
|
||||
|
||||
@@ -106,25 +95,25 @@ abstract class clibase {
|
||||
* Generates all options (parameters) available for the CLI command.
|
||||
* @return mixed[] Options.
|
||||
*/
|
||||
public abstract function generate_options();
|
||||
abstract public function generate_options();
|
||||
|
||||
/**
|
||||
* Generate all short forms for the available options.
|
||||
* @return string[] Short form options.
|
||||
*/
|
||||
public abstract function generate_shortcuts();
|
||||
abstract public function generate_shortcuts();
|
||||
|
||||
/**
|
||||
* Executes the CLI script.
|
||||
*/
|
||||
public abstract function execute();
|
||||
abstract public function execute();
|
||||
|
||||
/**
|
||||
* Change session to admin user.
|
||||
*/
|
||||
protected function become_admin_user() {
|
||||
global $DB;
|
||||
$user = $DB->get_record('user', ['id' => 2]);
|
||||
$user = get_admin();
|
||||
unset($user->description);
|
||||
unset($user->access);
|
||||
unset($user->preference);
|
||||
@@ -147,5 +136,6 @@ abstract class clibase {
|
||||
$long = '--'.$long;
|
||||
printf(" %-4s %-20s %s\n", $short, $long, $text);
|
||||
}
|
||||
printf("\n%s\n\n", get_string('cli'.$cliname.'examples', 'auth_outage'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,23 +14,12 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* create class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local\cli;
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
use coding_exception;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* create class.
|
||||
*
|
||||
@@ -170,7 +159,9 @@ class create extends clibase {
|
||||
$this->become_admin_user();
|
||||
|
||||
// Create the outage.
|
||||
$start = $this->time + $options['start'];
|
||||
// If time is above 1500000000 then it must be a unix time timestamp, otherwise they are trying to create
|
||||
// an outage 47 years in advance.
|
||||
$start = $options['start'] > 1500000000 ? $options['start'] : $this->time + $options['start'];
|
||||
$outage = new outage([
|
||||
'autostart' => $options['autostart'],
|
||||
'warntime' => $start - $options['warn'],
|
||||
@@ -198,7 +189,7 @@ class create extends clibase {
|
||||
private function clone_defaults() {
|
||||
$id = $this->options['clone'];
|
||||
if (!is_number($id) || ($id <= 0)) {
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => 'clone']),
|
||||
throw new cli_exception(get_string('clierrorinvalidvaluenotid', 'auth_outage', ['param' => 'clone']),
|
||||
cli_exception::ERROR_PARAMETER_INVALID);
|
||||
}
|
||||
|
||||
@@ -243,12 +234,12 @@ class create extends clibase {
|
||||
*/
|
||||
private function merge_options_check_parameters_int_nonnegative($option, $param) {
|
||||
if (!is_number($option)) {
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]),
|
||||
throw new cli_exception(get_string('clierrorinvalidvaluenotnumber', 'auth_outage', ['param' => $param]),
|
||||
cli_exception::ERROR_PARAMETER_INVALID);
|
||||
}
|
||||
$option = (int)$option;
|
||||
if ($option < 0) {
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]),
|
||||
throw new cli_exception(get_string('clierrorinvalidvaluenegativenumber', 'auth_outage', ['param' => $param]),
|
||||
cli_exception::ERROR_PARAMETER_INVALID);
|
||||
}
|
||||
return $option;
|
||||
@@ -263,12 +254,12 @@ class create extends clibase {
|
||||
*/
|
||||
private function merge_options_check_parameters_string_nonempty($option, $param) {
|
||||
if (!is_string($option)) {
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]),
|
||||
throw new cli_exception(get_string('clierrorinvalidvaluenotstring', 'auth_outage', ['param' => $param]),
|
||||
cli_exception::ERROR_PARAMETER_INVALID);
|
||||
}
|
||||
$option = trim($option);
|
||||
if (strlen($option) == 0) {
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]),
|
||||
throw new cli_exception(get_string('clierrorinvalidvalueemptystring', 'auth_outage', ['param' => $param]),
|
||||
cli_exception::ERROR_PARAMETER_INVALID);
|
||||
}
|
||||
return $option;
|
||||
@@ -296,7 +287,7 @@ class create extends clibase {
|
||||
}
|
||||
}
|
||||
|
||||
throw new cli_exception(get_string('clierrorinvalidvalue', 'auth_outage', ['param' => $param]),
|
||||
throw new cli_exception(get_string('clierrorinvalidvaluenotbool', 'auth_outage', ['param' => $param]),
|
||||
cli_exception::ERROR_PARAMETER_INVALID);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,22 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* finish class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local\cli;
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* finish class.
|
||||
*
|
||||
|
||||
@@ -14,22 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* waitforit class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local\cli;
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* waitforit class.
|
||||
*
|
||||
@@ -131,7 +120,7 @@ class waitforit extends clibase {
|
||||
return;
|
||||
}
|
||||
|
||||
$time = strftime('%F %T %Z');
|
||||
$time = date('Y-m-d H:i:s T', time());
|
||||
printf("[%s] %s\n", $time, $message);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* infopage class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local\controllers;
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
@@ -32,8 +23,6 @@ use coding_exception;
|
||||
use context_system;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* infopage class.
|
||||
*
|
||||
@@ -49,19 +38,31 @@ class infopage {
|
||||
private $outage;
|
||||
|
||||
/**
|
||||
* infopage_controller constructor.
|
||||
* @param array $params Parameters to use or null to get from Moodle API (request).
|
||||
* @var bool|null Defines if the page is generated for a static outage page.
|
||||
*/
|
||||
public function __construct(array $params = null) {
|
||||
private $static;
|
||||
|
||||
/**
|
||||
* infopage_controller constructor.
|
||||
* @param array|null $params Parameters to use or null to get from Moodle API (request).
|
||||
*/
|
||||
public function __construct(?array $params = null) {
|
||||
global $CFG;
|
||||
// Enable SVG support here to make sure all SVG files
|
||||
// used in the current theme are served properly.
|
||||
$CFG->svgicons = true;
|
||||
|
||||
if (is_null($params)) {
|
||||
$params = [
|
||||
'id' => optional_param('id', null, PARAM_INT),
|
||||
'outage' => null,
|
||||
'static' => optional_param('static', false, PARAM_BOOL),
|
||||
];
|
||||
} else {
|
||||
$defaults = [
|
||||
'id' => null,
|
||||
'outage' => null,
|
||||
'static' => false,
|
||||
];
|
||||
$params = array_merge($defaults, $params);
|
||||
}
|
||||
@@ -88,7 +89,6 @@ class infopage {
|
||||
/**
|
||||
* Generates and outputs the HTML for the info page.
|
||||
* @uses redirect
|
||||
* @SuppressWarnings(PHPMD.UnusedLocalVariable) $viewbag is used inside 'require'.
|
||||
*/
|
||||
public function output() {
|
||||
global $PAGE, $CFG, $OUTPUT;
|
||||
@@ -97,13 +97,17 @@ class infopage {
|
||||
redirect(new moodle_url('/'));
|
||||
}
|
||||
|
||||
// If it's not static outage page, then check access, then redirect if not allowed.
|
||||
if (!$this->static && !has_capability('auth/outage:viewinfo', context_system::instance())) {
|
||||
redirect(new moodle_url('/'));
|
||||
}
|
||||
$PAGE->set_context(context_system::instance());
|
||||
$PAGE->set_title($this->outage->get_title());
|
||||
$PAGE->set_heading($this->outage->get_title());
|
||||
$PAGE->set_url(new moodle_url('/auth/outage/info.php'));
|
||||
|
||||
// No hooks injecting into this page, do it manually.
|
||||
outagelib::inject();
|
||||
echo outagelib::get_inject_code();
|
||||
|
||||
echo $OUTPUT->header();
|
||||
$viewbag = [
|
||||
@@ -139,5 +143,6 @@ class infopage {
|
||||
}
|
||||
|
||||
$this->outage = $params['outage'];
|
||||
$this->static = $params['static'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,23 +14,12 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* maintenance_static_page class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local\controllers;
|
||||
|
||||
use auth_outage\local\outage;
|
||||
use coding_exception;
|
||||
use DOMDocument;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* maintenance_static_page class.
|
||||
*
|
||||
@@ -57,11 +46,11 @@ class maintenance_static_page {
|
||||
|
||||
if (is_null($outage)) {
|
||||
$html = null;
|
||||
} else if (PHPUNIT_TEST) {
|
||||
} else if (PHPUNIT_TEST || defined('BEHAT_SITE_RUNNING')) {
|
||||
$html = '<html></html>';
|
||||
} else {
|
||||
$data = maintenance_static_page_io::file_get_data(
|
||||
$CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&id='.$outage->id);
|
||||
$CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&static=1&id='.$outage->id);
|
||||
$html = $data['contents'];
|
||||
}
|
||||
|
||||
@@ -122,6 +111,7 @@ class maintenance_static_page {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets generator io.
|
||||
* @return maintenance_static_page_io
|
||||
*/
|
||||
public function get_io() {
|
||||
|
||||
@@ -14,24 +14,15 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* maintenance_static_page_generator class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local\controllers;
|
||||
|
||||
use auth_outage\local\outagelib;
|
||||
use coding_exception;
|
||||
use core_php_time_limit;
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use invalid_state_exception;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
use moodle_url;
|
||||
|
||||
/**
|
||||
* maintenance_static_page_generator class.
|
||||
@@ -42,6 +33,15 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class maintenance_static_page_generator {
|
||||
/** PATTERN
|
||||
* The pattern should match the attribute values that
|
||||
* go as 'url(xxxxx)', but make sure 'url(data:xxxxx)' is not
|
||||
* rewritten. Must be case insensitive to match 'URL(xxxxx)'.
|
||||
* It should be possible to specify other background attributes as
|
||||
* 'background: color url(xxxxx) no-repeat'.
|
||||
*/
|
||||
protected const PATTERN = '/url\s*\(\s*[\'"]?(?![\'"]?data:)([^\s\'"]+)[\'"]?\s*\)/i';
|
||||
|
||||
/** @var DOMDocument */
|
||||
protected $dom;
|
||||
|
||||
@@ -74,6 +74,10 @@ class maintenance_static_page_generator {
|
||||
$this->io->cleanup();
|
||||
|
||||
if (!is_null($this->dom)) {
|
||||
|
||||
// This can take a while to process using repeated curls.
|
||||
core_php_time_limit::raise();
|
||||
|
||||
$this->io->create_resources_path();
|
||||
|
||||
$this->remove_script_tags();
|
||||
@@ -82,6 +86,7 @@ class maintenance_static_page_generator {
|
||||
$this->update_link_favicon();
|
||||
$this->update_images();
|
||||
$this->remove_configured_css_selectors();
|
||||
$this->update_inline_background_images();
|
||||
|
||||
$html = $this->dom->saveHTML();
|
||||
if (trim($html) == '') {
|
||||
@@ -94,6 +99,7 @@ class maintenance_static_page_generator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets maintenance_static_page_io.
|
||||
* @return maintenance_static_page_io
|
||||
*/
|
||||
public function get_io() {
|
||||
@@ -127,7 +133,7 @@ class maintenance_static_page_generator {
|
||||
continue;
|
||||
}
|
||||
$saved = $this->io->save_url_file($href);
|
||||
if (is_null($saved['url'])) {
|
||||
if (empty($saved['url'])) {
|
||||
$url = $href; // Skipped, use original URL.
|
||||
} else {
|
||||
$this->update_link_stylesheet_parse($saved['file'], dirname($href));
|
||||
@@ -137,6 +143,29 @@ class maintenance_static_page_generator {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all URLs from file content using regular expressions.
|
||||
*
|
||||
* @param string $contents Content of the file
|
||||
* @return array Array of all matches in multi-dimensional array
|
||||
*/
|
||||
public function get_urls_from_stylesheet($contents) {
|
||||
preg_match_all('#url\([\'"]?(?!data:)([^\'"\)]+)#', $contents, $matches);
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a URL from inline style using regular expressions.
|
||||
*
|
||||
* @param string $style Content of the style attribute
|
||||
* @return array Array containing match
|
||||
*/
|
||||
public function get_url_from_inline_style($style) {
|
||||
preg_match(self::PATTERN, $style, $match);
|
||||
return $match;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Checks for urls inside filename.
|
||||
*
|
||||
@@ -147,15 +176,15 @@ class maintenance_static_page_generator {
|
||||
global $CFG;
|
||||
|
||||
$contents = file_get_contents($filename);
|
||||
if (!preg_match_all('#url\([\'"]?([^\'"\)]+)#', $contents, $matches)) {
|
||||
return;
|
||||
}
|
||||
$matches = $this->get_urls_from_stylesheet($contents);
|
||||
|
||||
foreach ($matches[1] as $originalurl) {
|
||||
// Allow incomplete URLs in CSS, assume it is from moodle root.
|
||||
if (maintenance_static_page_io::is_url($originalurl)) {
|
||||
$fullurl = $originalurl;
|
||||
} else if ($originalurl[0] == '/') {
|
||||
$fullurl = $CFG->wwwroot.$originalurl;
|
||||
$rooturl = parse_url($CFG->wwwroot);
|
||||
$fullurl = $rooturl['scheme'].'://'.$rooturl['host'].$originalurl;
|
||||
} else {
|
||||
$fullurl = $baseref.'/'.$originalurl;
|
||||
}
|
||||
@@ -179,10 +208,12 @@ class maintenance_static_page_generator {
|
||||
foreach ($links as $link) {
|
||||
$rel = $link->getAttribute("rel");
|
||||
$href = $link->getAttribute("href");
|
||||
if (($rel != 'shortcut icon') || ($href == '')) {
|
||||
continue;
|
||||
if (($rel == 'shortcut icon') && ($href != '')) {
|
||||
if (!maintenance_static_page_io::is_url($href)) {
|
||||
$href = (string) new moodle_url($href);
|
||||
}
|
||||
$link->setAttribute('href', $this->io->generate_file_url($href)); // Works for most image formats.
|
||||
}
|
||||
$link->setAttribute('href', $this->io->generate_file_url($href)); // Works for most image formats.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,10 +225,37 @@ class maintenance_static_page_generator {
|
||||
|
||||
foreach ($links as $link) {
|
||||
$src = $link->getAttribute("src");
|
||||
if ($src == '') {
|
||||
continue;
|
||||
if ($src != '') {
|
||||
if (!maintenance_static_page_io::is_url($src)) {
|
||||
$src = (string) new moodle_url($src);
|
||||
}
|
||||
$link->setAttribute('src', $this->io->generate_file_url($src)); // Works for most image formats.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch and fixes all inline background images.
|
||||
*/
|
||||
private function update_inline_background_images() {
|
||||
global $CFG;
|
||||
$xpath = new \DOMXPath($this->dom);
|
||||
$elements = $xpath->query("//*[contains(@style,'background')]");
|
||||
|
||||
foreach ($elements as $element) {
|
||||
$style = $element->getAttribute("style");
|
||||
$matches = $this->get_url_from_inline_style($style);
|
||||
if (isset($matches[1])) {
|
||||
// Allow incomplete URLs in style, assume it is from moodle root.
|
||||
if (maintenance_static_page_io::is_url($matches[1])) {
|
||||
$fullurl = $matches[1];
|
||||
} else {
|
||||
$fullurl = (string) new moodle_url($matches[1]);
|
||||
}
|
||||
$newurl = $this->io->generate_file_url($fullurl);
|
||||
$updated = preg_replace(self::PATTERN, ' url('.$newurl.') ', $style);
|
||||
$element->setAttribute('style', $updated);
|
||||
}
|
||||
$link->setAttribute('src', $this->io->generate_file_url($src)); // Works for most image formats.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,7 +294,7 @@ class maintenance_static_page_generator {
|
||||
/**
|
||||
* Fetches all elements based on the given selector.
|
||||
*
|
||||
* @param $selector
|
||||
* @param string $selector element selector
|
||||
*
|
||||
* @return DOMElement[]
|
||||
*/
|
||||
@@ -254,7 +312,7 @@ class maintenance_static_page_generator {
|
||||
/**
|
||||
* Fetch all elements which contains the given class.
|
||||
*
|
||||
* @param $class
|
||||
* @param string $class element class
|
||||
*
|
||||
* @return DOMElement[]
|
||||
*/
|
||||
@@ -270,6 +328,9 @@ class maintenance_static_page_generator {
|
||||
return $matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds meta refresh to head element.
|
||||
*/
|
||||
private function add_meta_refresh() {
|
||||
$meta = $this->dom->createElement('meta');
|
||||
$meta->setAttribute('http-equiv', 'refresh');
|
||||
@@ -282,6 +343,7 @@ class maintenance_static_page_generator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets refresh time.
|
||||
* @return int
|
||||
*/
|
||||
public function get_refresh_time() {
|
||||
@@ -289,6 +351,7 @@ class maintenance_static_page_generator {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets refresh time.
|
||||
* @param int $refreshtime
|
||||
*/
|
||||
public function set_refresh_time($refreshtime) {
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* maintenance_static_page_io class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local\controllers;
|
||||
|
||||
use auth_outage\local\outagelib;
|
||||
@@ -31,8 +22,6 @@ use finfo;
|
||||
use invalid_parameter_exception;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* maintenance_static_page_io class.
|
||||
*
|
||||
@@ -44,11 +33,13 @@ defined('MOODLE_INTERNAL') || die();
|
||||
class maintenance_static_page_io {
|
||||
/**
|
||||
* Checks if the given string starts with "http://" or "https://".
|
||||
* @param $url
|
||||
* Also checks for "//" at the start of image, which setting_file_url still uses.
|
||||
*
|
||||
* @param string $url url string for check
|
||||
* @return bool
|
||||
*/
|
||||
public static function is_url($url) {
|
||||
return (bool)preg_match('#^http(s)?://#', $url);
|
||||
return ((bool) preg_match('#^http(s)?://#', $url) || (bool) preg_match('#^//#', $url));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,6 +71,7 @@ class maintenance_static_page_io {
|
||||
protected $preview = false;
|
||||
|
||||
/**
|
||||
* Sets preview
|
||||
* @param boolean $preview
|
||||
*/
|
||||
public function set_preview($preview) {
|
||||
@@ -213,7 +205,7 @@ class maintenance_static_page_io {
|
||||
/**
|
||||
* Saves the content of the URL into a file, returning the local filename.
|
||||
* @param string $url Input URL.
|
||||
* @return string|null Output filename or null if skipped.
|
||||
* @return array|null Output an array with the filename and url or null if skipped.
|
||||
*/
|
||||
public function save_url_file($url) {
|
||||
global $CFG;
|
||||
|
||||
@@ -14,22 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* outage class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local;
|
||||
|
||||
use coding_exception;
|
||||
use stdClass;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* outage class.
|
||||
*
|
||||
@@ -119,6 +108,11 @@ class outage {
|
||||
*/
|
||||
public $lastmodified = null;
|
||||
|
||||
/**
|
||||
* @var string|null access key, or null if not enabled.
|
||||
*/
|
||||
public $accesskey = null;
|
||||
|
||||
/**
|
||||
* outage constructor.
|
||||
* @param stdClass|array|null $data The data for the outage.
|
||||
@@ -281,7 +275,17 @@ class outage {
|
||||
*/
|
||||
private function adjust_field_types() {
|
||||
// Adjust int fields.
|
||||
$fs = ['createdby', 'id', 'lastmodified', 'modifiedby', 'starttime', 'stoptime', 'warntime', 'finished'];
|
||||
$fs = [
|
||||
'id',
|
||||
'starttime',
|
||||
'stoptime',
|
||||
'warntime',
|
||||
'finished',
|
||||
'createdby',
|
||||
'lastmodified',
|
||||
'modifiedby',
|
||||
];
|
||||
|
||||
foreach ($fs as $f) {
|
||||
$this->$f = ($this->$f === null) ? null : (int)$this->$f;
|
||||
}
|
||||
@@ -289,4 +293,12 @@ class outage {
|
||||
// Adjust bool fields.
|
||||
$this->autostart = ($this->autostart === null) ? null : (bool)$this->autostart;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return json encoded outage.
|
||||
* @return string Json string.
|
||||
*/
|
||||
public function __toString() {
|
||||
return json_encode(get_object_vars($this));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,28 +14,18 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* outagelib class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\local;
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\controllers\maintenance_static_page;
|
||||
use auth_outage\output\renderer;
|
||||
use coding_exception;
|
||||
use curl;
|
||||
use Exception;
|
||||
use file_exception;
|
||||
use invalid_parameter_exception;
|
||||
use stdClass;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once(__DIR__.'/../../lib.php');
|
||||
|
||||
/**
|
||||
@@ -47,39 +37,72 @@ require_once(__DIR__.'/../../lib.php');
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class outagelib {
|
||||
|
||||
/** Outage start. */
|
||||
const OUTAGE_START = '<!-- OUTAGESTART -->';
|
||||
|
||||
/** Outage end. */
|
||||
const OUTAGE_END = '<!-- OUTAGEEND -->';
|
||||
|
||||
/**
|
||||
* @var bool Flags in the injection function was already called.
|
||||
*/
|
||||
private static $injectcalled = false;
|
||||
|
||||
/**
|
||||
* Fetches page.
|
||||
* @param string $file file to be fetched
|
||||
*/
|
||||
public static function fetch_page($file) {
|
||||
$curl = curl_init();
|
||||
curl_setopt($curl, CURLOPT_URL, $file);
|
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 1); // It is localhost, time to connect is enough.
|
||||
curl_setopt($curl, CURLOPT_TIMEOUT, 5); // It is localhost, time to fetch index is enough.
|
||||
$contents = curl_exec($curl);
|
||||
$mime = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
|
||||
curl_close($curl);
|
||||
global $CFG;
|
||||
require_once($CFG->libdir . '/filelib.php');
|
||||
|
||||
$curl = new curl(['ignoresecurity' => true]);
|
||||
$contents = $curl->get($file);
|
||||
$info = $curl->get_info();
|
||||
if (!empty($info['content_type'])) {
|
||||
$mime = $info['content_type'];
|
||||
} else {
|
||||
$mime = '';
|
||||
}
|
||||
return compact('contents', 'mime');
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls inject even if it was already called before.
|
||||
* Resets inject called to allow the code to be regenerated.
|
||||
*/
|
||||
public static function reinject() {
|
||||
public static function reset_injectcalled() {
|
||||
self::$injectcalled = false;
|
||||
self::inject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Will check for ongoing or warning outages and will attach the message bar as required.
|
||||
* Given a time, usually now, when is the next outage window?
|
||||
* @param int $time time for next window
|
||||
*/
|
||||
public static function inject() {
|
||||
global $CFG;
|
||||
public static function get_next_window($time = null) {
|
||||
|
||||
$config = self::get_config();
|
||||
|
||||
if (!$time) {
|
||||
$time = time();
|
||||
}
|
||||
|
||||
$default = $config->default_time;
|
||||
if ($default) {
|
||||
// First try natural language parsing.
|
||||
$time = strtotime($default, $time);
|
||||
}
|
||||
return $time;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Will check for ongoing or warning outages and will return the message bar as required.
|
||||
*
|
||||
* @return string|void CSS and HTML for the warning bar if it should be displayed
|
||||
*/
|
||||
public static function get_inject_code() {
|
||||
global $PAGE;
|
||||
// Ensure we do not kill the whole website in case of an error.
|
||||
try {
|
||||
// Check if we should inject the code.
|
||||
@@ -109,8 +132,8 @@ class outagelib {
|
||||
}
|
||||
|
||||
// There is a previewing or active outage.
|
||||
$CFG->additionalhtmltopofbody = renderer::get()->render_warningbar($active, $time, false, $preview).
|
||||
$CFG->additionalhtmltopofbody;
|
||||
$renderer = $PAGE->get_renderer('auth_outage');
|
||||
return $renderer->render_warningbar($active, $time, false, $preview);
|
||||
} catch (Exception $e) {
|
||||
debugging('Exception occured while injecting our code: '.$e->getMessage());
|
||||
debugging($e->getTraceAsString(), DEBUG_DEVELOPER);
|
||||
@@ -134,7 +157,12 @@ class outagelib {
|
||||
}
|
||||
}
|
||||
|
||||
return (object)array_merge(self::get_config_defaults(), $config);
|
||||
$config = array_merge(self::get_config_defaults(), $config);
|
||||
// Combine allowed IPs config values together.
|
||||
if (isset($config['allowedips_forced'])) {
|
||||
$config['allowedips'] = trim($config['allowedips'] . "\n" . $config['allowedips_forced']);
|
||||
}
|
||||
return (object)$config;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,6 +173,7 @@ class outagelib {
|
||||
return [
|
||||
'allowedips' => '',
|
||||
'css' => '',
|
||||
'default_time' => '',
|
||||
'default_autostart' => '0',
|
||||
'default_duration' => (string)(60 * 60),
|
||||
'default_warning_duration' => (string)(60 * 60),
|
||||
@@ -156,18 +185,34 @@ class outagelib {
|
||||
|
||||
/**
|
||||
* Executed when outages are modified (created, updated or deleted).
|
||||
*
|
||||
* @param bool $reenablemaint should we re-enable maintenance mode for ongoing outage
|
||||
* @throws coding_exception
|
||||
* @throws file_exception
|
||||
*/
|
||||
public static function prepare_next_outage() {
|
||||
public static function prepare_next_outage($reenablemaint = false) {
|
||||
// If there is an ongoing outage, prepare it instead.
|
||||
$outage = outagedb::get_ongoing();
|
||||
if (is_null($outage)) {
|
||||
$outage = outagedb::get_next_starting();
|
||||
$ongoingoutage = false;
|
||||
} else {
|
||||
$ongoingoutage = true;
|
||||
}
|
||||
|
||||
// Set json formatted outage string to cache.
|
||||
set_config('auth_outage_active_outage', (string)$outage);
|
||||
|
||||
maintenance_static_page::create_from_outage($outage)->generate();
|
||||
self::update_climaintenance_code($outage);
|
||||
self::update_maintenance_later($outage);
|
||||
if (!$ongoingoutage || $reenablemaint || is_null($outage)) {
|
||||
self::update_maintenance_later($outage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if wwwroot accessible.
|
||||
*/
|
||||
private static function check_wwwroot_accessible() {
|
||||
global $CFG;
|
||||
$result = self::fetch_page($CFG->wwwroot);
|
||||
@@ -190,6 +235,7 @@ class outagelib {
|
||||
unset_config('maintenance_message');
|
||||
}
|
||||
set_config('maintenance_later', $outage->starttime);
|
||||
self::maintenance_config_log($outage);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,7 +266,7 @@ class outagelib {
|
||||
|
||||
// Used to test the try block in case of errors.
|
||||
if (PHPUNIT_TEST && optional_param('auth_outage_break_code', false, PARAM_INT)) {
|
||||
(new stdClass())->invalidfield; // Triggers an exception.
|
||||
throw new coding_exception('Exception triggered');
|
||||
}
|
||||
|
||||
// Nothing preventing the injection.
|
||||
@@ -233,53 +279,95 @@ class outagelib {
|
||||
* @param int $starttime Outage start time.
|
||||
* @param int $stoptime Outage stop time.
|
||||
* @param string $allowedips List of IPs allowed.
|
||||
* @param string|null $accesskey access key, or null if no access key set.
|
||||
*
|
||||
* @return string
|
||||
* @throws invalid_parameter_exception
|
||||
*/
|
||||
public static function create_climaintenancephp_code($starttime, $stoptime, $allowedips) {
|
||||
public static function create_climaintenancephp_code($starttime, $stoptime, $allowedips, $accesskey = null) {
|
||||
global $CFG;
|
||||
if (!is_int($starttime) || !is_int($stoptime)) {
|
||||
throw new invalid_parameter_exception('Make sure $startime and $stoptime are integers.');
|
||||
}
|
||||
if (!is_string($allowedips) || (trim($allowedips) == '')) {
|
||||
throw new invalid_parameter_exception('$allowedips must be a valid string.');
|
||||
}
|
||||
// I know Moodle validation would clean up this field, but just in case, let's ensure no
|
||||
// single-quotes (and double for the sake of it) are present otherwise it would break the code.
|
||||
$allowedips = str_replace('\'"', '', $allowedips);
|
||||
$allowedips = addslashes($allowedips);
|
||||
|
||||
$cookiesecure = is_moodle_cookie_secure();
|
||||
|
||||
// Since Moodle 4.3 cookiehttponly is default to true and this CFG is not set.
|
||||
// so if not set, default to true.
|
||||
$cookiehttponly = isset($CFG->cookiehttponly) ? (bool) $CFG->cookiehttponly : true;
|
||||
|
||||
$code = <<<'EOT'
|
||||
<?php
|
||||
if ((time() >= {{STARTTIME}}) && (time() < {{STOPTIME}})) {
|
||||
define('MOODLE_INTERNAL', true);
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
define('MOODLE_INTERNAL', true);
|
||||
}
|
||||
require_once($CFG->dirroot.'/lib/moodlelib.php');
|
||||
if (!remoteip_in_list('{{ALLOWEDIPS}}')) {
|
||||
header($_SERVER['SERVER_PROTOCOL'] . ' 503 Moodle under maintenance');
|
||||
header('Status: 503 Moodle under maintenance');
|
||||
header('Retry-After: 300');
|
||||
header('Content-type: text/html; charset=utf-8');
|
||||
header('X-UA-Compatible: IE=edge');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
header('Accept-Ranges: none');
|
||||
if ((defined('AJAX_SCRIPT') && AJAX_SCRIPT) || (defined('WS_SERVER') && WS_SERVER)) {
|
||||
if (file_exists($CFG->dirroot.'/lib/classes/ip_utils.php')) {
|
||||
require_once($CFG->dirroot.'/lib/classes/ip_utils.php');
|
||||
}
|
||||
// Put access key as a cookie if given. This stops the need to put it as a url param on every request.
|
||||
$urlaccesskey = optional_param('accesskey', null, PARAM_TEXT);
|
||||
$isphpunit = defined('PHPUNIT_TEST');
|
||||
|
||||
if (!empty($urlaccesskey) && !$isphpunit) {
|
||||
setcookie('auth_outage_accesskey', $urlaccesskey, time() + 86400, '/', '', {{COOKIESECURE}}, {{COOKIEHTTPONLY}});
|
||||
}
|
||||
|
||||
// Use url access key if given, else the cookie, else null.
|
||||
$useraccesskey = $urlaccesskey ?: $_COOKIE['auth_outage_accesskey'] ?? null;
|
||||
|
||||
$ipblocked = !remoteip_in_list('{{ALLOWEDIPS}}');
|
||||
$accesskeyblocked = $useraccesskey != '{{ACCESSKEY}}';
|
||||
$allowed = ({{USEACCESSKEY}} && !$accesskeyblocked) || ({{USEALLOWEDIPS}} && !$ipblocked);
|
||||
|
||||
if (!$allowed) {
|
||||
if (!$isphpunit) {
|
||||
header($_SERVER['SERVER_PROTOCOL'] . ' 503 Moodle under maintenance');
|
||||
header('Status: 503 Moodle under maintenance');
|
||||
header('Retry-After: 300');
|
||||
header('Content-type: text/html; charset=utf-8');
|
||||
header('X-UA-Compatible: IE=edge');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
header('Accept-Ranges: none');
|
||||
header('X-Moodle-Maintenance: manager');
|
||||
}
|
||||
|
||||
if (!$isphpunit && ((defined('AJAX_SCRIPT') && AJAX_SCRIPT) || (defined('WS_SERVER') && WS_SERVER))) {
|
||||
exit(0);
|
||||
}
|
||||
echo '<!-- Blocked by ip, your ip: '.getremoteaddr('n/a').' -->';
|
||||
if (file_exists($CFG->dataroot.'/climaintenance.template.html')) {
|
||||
require($CFG->dataroot.'/climaintenance.template.html');
|
||||
exit(0);
|
||||
|
||||
if ({{USEALLOWEDIPS}} && $ipblocked) {
|
||||
echo '<!-- Blocked by ip, your ip: '.getremoteaddr('n/a').' -->';
|
||||
}
|
||||
|
||||
if ({{USEACCESSKEY}} && $accesskeyblocked) {
|
||||
echo '<!-- Blocked by missing or incorrect access key, access key given: '. $useraccesskey .' -->';
|
||||
}
|
||||
|
||||
if (!$isphpunit) {
|
||||
if (file_exists($CFG->dataroot.'/climaintenance.template.html')) {
|
||||
require($CFG->dataroot.'/climaintenance.template.html');
|
||||
exit(0);
|
||||
}
|
||||
// The file above should always exist, but just in case...
|
||||
die('We are currently under maintentance, please try again later.');
|
||||
}
|
||||
// The file above should always exist, but just in case...
|
||||
die('We are currently under maintentance, please try again later.');
|
||||
}
|
||||
}
|
||||
EOT;
|
||||
$search = ['{{STARTTIME}}', '{{STOPTIME}}', '{{ALLOWEDIPS}}', '{{YOURIP}}'];
|
||||
$replace = [$starttime, $stoptime, $allowedips, getremoteaddr('n/a')];
|
||||
$search = ['{{STARTTIME}}', '{{STOPTIME}}', '{{USEALLOWEDIPS}}', '{{ALLOWEDIPS}}', '{{USEACCESSKEY}}', '{{ACCESSKEY}}',
|
||||
'{{YOURIP}}', '{{COOKIESECURE}}', '{{COOKIEHTTPONLY}}'];
|
||||
// Note that var_export is required because (string) false == '', not 'false'.
|
||||
$replace = [$starttime, $stoptime, var_export(!empty($allowedips), true), $allowedips, var_export(!empty($accesskey), true),
|
||||
$accesskey, getremoteaddr('n/a'), var_export($cookiesecure, true), var_export($cookiehttponly, true)];
|
||||
return str_replace($search, $replace, $code);
|
||||
}
|
||||
|
||||
@@ -301,13 +389,15 @@ EOT;
|
||||
|
||||
$config = self::get_config();
|
||||
$allowedips = trim($config->allowedips);
|
||||
$accesskey = $outage->accesskey ?? null;
|
||||
|
||||
if (is_null($outage) || ($allowedips == '')) {
|
||||
// If no outage, or allowed ips is null and access key is null (i.e. no blocking required).
|
||||
if (is_null($outage) || ($allowedips == '' && empty($accesskey))) {
|
||||
if (file_exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
} else {
|
||||
$code = self::create_climaintenancephp_code($outage->starttime, $outage->stoptime, $allowedips);
|
||||
$code = self::create_climaintenancephp_code($outage->starttime, $outage->stoptime, $allowedips, $accesskey);
|
||||
|
||||
$dir = dirname($file);
|
||||
if (!file_exists($dir) || !is_dir($dir)) {
|
||||
@@ -321,16 +411,14 @@ EOT;
|
||||
* Generates a warning message in case the plugin is not active and configured.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @internal stdClass $CFG
|
||||
* @internal bootstrap_renderer $OUTPUT
|
||||
*/
|
||||
public static function generate_plugin_configuration_warning() {
|
||||
global $CFG, $OUTPUT;
|
||||
global $CFG, $OUTPUT, $PAGE;
|
||||
|
||||
$message = [];
|
||||
|
||||
if (!isset($CFG->auth_outage_bootstrap_loaded) || !$CFG->auth_outage_bootstrap_loaded) {
|
||||
if (trim(self::get_config()->allowedips) != ''
|
||||
&& (!isset($CFG->auth_outage_bootstrap_loaded) || !$CFG->auth_outage_bootstrap_loaded)) {
|
||||
$message[] = get_string('configurationwarning', 'auth_outage');
|
||||
}
|
||||
|
||||
@@ -338,8 +426,10 @@ EOT;
|
||||
$message[] = get_string('configurationdisabled', 'auth_outage');
|
||||
}
|
||||
|
||||
if (!self::check_wwwroot_accessible()) {
|
||||
$message[] = get_string('configurationinaccessiblewwwroot', 'auth_outage', ['wwwroot' => $CFG->wwwroot]);
|
||||
if ($PAGE->pagetype == "admin-setting-auth_outage" || $PAGE->pagetype == "admin-setting-authsettingoutage") {
|
||||
if (!self::check_wwwroot_accessible()) {
|
||||
$message[] = get_string('configurationinaccessiblewwwroot', 'auth_outage', ['wwwroot' => $CFG->wwwroot]);
|
||||
}
|
||||
}
|
||||
|
||||
if (count($message) == 0) {
|
||||
@@ -354,4 +444,18 @@ EOT;
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logging for maintenance mode configuration.
|
||||
*
|
||||
* @param outage|null $outage Outage or null if no scheduled outage.
|
||||
*/
|
||||
private static function maintenance_config_log(outage $outage) {
|
||||
mtrace(get_string('logformaintmodeconfig', 'auth_outage'));
|
||||
$timezone = ' (Timezone ' . \core_date::get_server_timezone_object()->getName() . ')';
|
||||
mtrace('... updated at ' . date('H:i:s'));
|
||||
$time = date("Y-m-d H:i:s", $outage->starttime);
|
||||
mtrace("... enable maintenance mode at $time $timezone");
|
||||
mtrace(get_string('logformaintmodeconfigcomplete', 'auth_outage'));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* base_table class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <danielroperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\output\manage;
|
||||
|
||||
use auth_outage\local\outage;
|
||||
@@ -31,7 +22,6 @@ use html_writer;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir.'/tablelib.php');
|
||||
|
||||
/**
|
||||
@@ -48,6 +38,10 @@ class base_table extends flexible_table {
|
||||
*/
|
||||
private static $autoid = 0;
|
||||
|
||||
/**
|
||||
* Creates start time string.
|
||||
* @param string $starttime start time
|
||||
*/
|
||||
protected static function create_starttime_string($starttime) {
|
||||
$absolute = userdate($starttime, get_string('datetimeformat', 'auth_outage'));
|
||||
$relative = $starttime - time();
|
||||
@@ -83,12 +77,7 @@ class base_table extends flexible_table {
|
||||
// View button.
|
||||
$buttons .= html_writer::link(
|
||||
new moodle_url('/auth/outage/info.php', ['id' => $outage->id]),
|
||||
html_writer::empty_tag('img', [
|
||||
'src' => $OUTPUT->pix_url('t/preview'),
|
||||
'alt' => get_string('view'),
|
||||
'class' => 'iconsmall',
|
||||
|
||||
]),
|
||||
$OUTPUT->pix_icon('t/preview', get_string('view'), 'moodle', array('class' => 'iconsmall')),
|
||||
[
|
||||
'title' => get_string('view'),
|
||||
'target' => '_blank',
|
||||
@@ -99,11 +88,7 @@ class base_table extends flexible_table {
|
||||
if ($editdelete) {
|
||||
$buttons .= html_writer::link(
|
||||
new moodle_url('/auth/outage/edit.php', ['edit' => $outage->id]),
|
||||
html_writer::empty_tag('img', [
|
||||
'src' => $OUTPUT->pix_url('t/edit'),
|
||||
'alt' => get_string('edit'),
|
||||
'class' => 'iconsmall',
|
||||
]),
|
||||
$OUTPUT->pix_icon('t/edit', get_string('edit'), 'moodle', array('class' => 'iconsmall')),
|
||||
['title' => get_string('edit')]
|
||||
);
|
||||
}
|
||||
@@ -111,12 +96,7 @@ class base_table extends flexible_table {
|
||||
// Clone button.
|
||||
$buttons .= html_writer::link(
|
||||
new moodle_url('/auth/outage/edit.php', ['clone' => $outage->id]),
|
||||
html_writer::empty_tag('img', [
|
||||
'src' => $OUTPUT->pix_url('t/copy'),
|
||||
'alt' => get_string('clone', 'auth_outage'),
|
||||
'class' => 'iconsmall',
|
||||
|
||||
]),
|
||||
$OUTPUT->pix_icon('t/copy', get_string('clone', 'auth_outage'), 'moodle', array('class' => 'iconsmall')),
|
||||
['title' => get_string('clone', 'auth_outage')]
|
||||
);
|
||||
|
||||
@@ -124,11 +104,7 @@ class base_table extends flexible_table {
|
||||
if ($outage->is_ongoing()) {
|
||||
$buttons .= html_writer::link(
|
||||
new moodle_url('/auth/outage/finish.php', ['id' => $outage->id]),
|
||||
html_writer::empty_tag('img', [
|
||||
'src' => $OUTPUT->pix_url('t/check'),
|
||||
'alt' => get_string('finish', 'auth_outage'),
|
||||
'class' => 'iconsmall',
|
||||
]),
|
||||
$OUTPUT->pix_icon('t/check', get_string('finish', 'auth_outage'), 'moodle', array('class' => 'iconsmall')),
|
||||
['title' => get_string('finish', 'auth_outage')]
|
||||
);
|
||||
}
|
||||
@@ -137,11 +113,7 @@ class base_table extends flexible_table {
|
||||
if ($editdelete) {
|
||||
$buttons .= html_writer::link(
|
||||
new moodle_url('/auth/outage/delete.php', ['id' => $outage->id]),
|
||||
html_writer::empty_tag('img', [
|
||||
'src' => $OUTPUT->pix_url('t/delete'),
|
||||
'alt' => get_string('delete'),
|
||||
'class' => 'iconsmall',
|
||||
]),
|
||||
$OUTPUT->pix_icon('t/delete', get_string('delete'), 'moodle', array('class' => 'iconsmall')),
|
||||
['title' => get_string('delete')]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,21 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* history_table class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <danielroperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\output\manage;
|
||||
|
||||
use auth_outage\local\outage;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir.'/tablelib.php');
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,15 +14,6 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* planned_table class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <danielroperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\output\manage;
|
||||
|
||||
use auth_outage\local\outage;
|
||||
@@ -30,7 +21,6 @@ use html_writer;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir.'/tablelib.php');
|
||||
|
||||
/**
|
||||
@@ -68,7 +58,7 @@ class planned_table extends base_table {
|
||||
public function show_data(array $outages) {
|
||||
foreach ($outages as $outage) {
|
||||
$title = html_writer::link(
|
||||
new moodle_url('/auth/outage/edit.php', ['id' => $outage->id]),
|
||||
new moodle_url('/auth/outage/edit.php', ['edit' => $outage->id]),
|
||||
$outage->get_title(),
|
||||
['title' => get_string('edit')]
|
||||
);
|
||||
|
||||
@@ -14,26 +14,16 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* auth_outage_renderer class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\output;
|
||||
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\outagelib;
|
||||
use coding_exception;
|
||||
use core_user;
|
||||
use html_writer;
|
||||
use moodle_url;
|
||||
use plugin_renderer_base;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* auth_outage_renderer class.
|
||||
*
|
||||
@@ -43,15 +33,6 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class renderer extends plugin_renderer_base {
|
||||
/**
|
||||
* Returns the outage renderer.
|
||||
* @return renderer The outage renderer.
|
||||
*/
|
||||
public static function get() {
|
||||
global $PAGE;
|
||||
return $PAGE->get_renderer('auth_outage');
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the view in a separate scope to avoid conflicts with variable names.
|
||||
* @param string $view View PHP file.
|
||||
@@ -74,6 +55,13 @@ class renderer extends plugin_renderer_base {
|
||||
* @return string The rendered view code.
|
||||
*/
|
||||
public function render_view($view, $viewbag = []) {
|
||||
|
||||
// Do not render if the request are from some layouts.
|
||||
$excludelayout = ['embedded', 'popup', 'secure', 'maintenance'];
|
||||
if (in_array($this->page->pagelayout, $excludelayout)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
ob_start();
|
||||
$this->output_view($view, $viewbag);
|
||||
$html = ob_get_contents();
|
||||
@@ -153,8 +141,6 @@ class renderer extends plugin_renderer_base {
|
||||
* @return string The formatted HTML.
|
||||
*/
|
||||
private function renderoutage(outage $outage, $buttons) {
|
||||
global $OUTPUT;
|
||||
|
||||
if ($outage->createdby == 0) {
|
||||
$created = get_string('na', 'auth_outage');
|
||||
} else {
|
||||
@@ -178,14 +164,14 @@ class renderer extends plugin_renderer_base {
|
||||
$url = new moodle_url('/auth/outage/edit.php', ['edit' => $outage->id]);
|
||||
$img = html_writer::empty_tag(
|
||||
'img',
|
||||
['src' => $OUTPUT->pix_url('t/edit'), 'alt' => get_string('edit'), 'class' => 'iconsmall']
|
||||
['src' => $this->output->image_url('t/edit'), 'alt' => get_string('edit'), 'class' => 'iconsmall']
|
||||
);
|
||||
$linkedit = html_writer::link($url, $img, ['title' => get_string('edit')]);
|
||||
|
||||
$url = new moodle_url('/auth/outage/delete.php', ['id' => $outage->id]);
|
||||
$img = html_writer::empty_tag(
|
||||
'img',
|
||||
['src' => $OUTPUT->pix_url('t/delete'), 'alt' => get_string('delete'), 'class' => 'iconsmall']
|
||||
['src' => $this->output->image_url('t/delete'), 'alt' => get_string('delete'), 'class' => 'iconsmall']
|
||||
);
|
||||
$linkdelete = html_writer::link($url, $img, ['title' => get_string('delete')]);
|
||||
|
||||
@@ -196,35 +182,38 @@ class renderer extends plugin_renderer_base {
|
||||
$finished = userdate($finished, get_string('datetimeformat', 'auth_outage'));
|
||||
}
|
||||
|
||||
return html_writer::div(
|
||||
$start = outagelib::OUTAGE_START;
|
||||
$end = outagelib::OUTAGE_END;
|
||||
$outagehtml = html_writer::div(
|
||||
html_writer::tag('blockquote',
|
||||
html_writer::div(html_writer::tag('b', $outage->get_title(), ['data-id' => $outage->id])).
|
||||
html_writer::div(html_writer::tag('i', $outage->get_description())).
|
||||
html_writer::div(
|
||||
html_writer::tag('b', get_string('tableheaderwarnbefore', 'auth_outage').': ').
|
||||
format_time($outage->get_warning_duration())
|
||||
).
|
||||
html_writer::div(
|
||||
html_writer::tag('b', get_string('tableheaderstarttime', 'auth_outage').': ').
|
||||
userdate($outage->starttime, get_string('datetimeformat', 'auth_outage'))
|
||||
).
|
||||
html_writer::div(
|
||||
html_writer::tag('b', get_string('tableheaderdurationplanned', 'auth_outage').': ').
|
||||
format_time($outage->get_duration_planned())
|
||||
).
|
||||
html_writer::div(
|
||||
html_writer::tag('b', get_string('tableheaderdurationactual', 'auth_outage').': ').
|
||||
$finished
|
||||
).
|
||||
html_writer::div(
|
||||
html_writer::tag('small',
|
||||
'Created by '.$created.
|
||||
', modified by '.$modified.' on '.
|
||||
userdate($outage->lastmodified, get_string('datetimeformat', 'auth_outage'))
|
||||
)
|
||||
).
|
||||
($buttons ? html_writer::div($linkedit.$linkdelete) : '')
|
||||
html_writer::div(html_writer::tag('b', $outage->get_title(), ['data-id' => $outage->id])).
|
||||
html_writer::div(html_writer::tag('i', $outage->get_description())).
|
||||
html_writer::div(
|
||||
html_writer::tag('b', get_string('tableheaderwarnbefore', 'auth_outage').': ').
|
||||
format_time($outage->get_warning_duration())
|
||||
).
|
||||
html_writer::div(
|
||||
html_writer::tag('b', get_string('tableheaderstarttime', 'auth_outage').': ').
|
||||
userdate($outage->starttime, get_string('datetimeformat', 'auth_outage'))
|
||||
).
|
||||
html_writer::div(
|
||||
html_writer::tag('b', get_string('tableheaderdurationplanned', 'auth_outage').': ').
|
||||
format_time($outage->get_duration_planned())
|
||||
).
|
||||
html_writer::div(
|
||||
html_writer::tag('b', get_string('tableheaderdurationactual', 'auth_outage').': ').
|
||||
$finished
|
||||
).
|
||||
html_writer::div(
|
||||
html_writer::tag('small',
|
||||
'Created by '.$created.
|
||||
', modified by '.$modified.' on '.
|
||||
userdate($outage->lastmodified, get_string('datetimeformat', 'auth_outage'))
|
||||
)
|
||||
).
|
||||
($buttons ? html_writer::div($linkedit.$linkdelete) : '')
|
||||
)
|
||||
);
|
||||
return $start . $outagehtml . $end;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,9 @@
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace auth_outage\privacy;
|
||||
|
||||
/**
|
||||
* Privacy Subsystem implementation for auth_outage.
|
||||
*
|
||||
@@ -20,24 +23,9 @@
|
||||
* @copyright 2018 Olivier SECRET <olivier.secret@catalyst-au.net>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\privacy;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use core_privacy\local\legacy_polyfill;
|
||||
|
||||
/**
|
||||
* Privacy provider for the authentication manual.
|
||||
*
|
||||
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class provider implements
|
||||
\core_privacy\local\metadata\null_provider {
|
||||
|
||||
use legacy_polyfill;
|
||||
|
||||
/**
|
||||
* Get the language string identifier with the component's language
|
||||
* file to explain why this plugin stores no data.
|
||||
@@ -47,7 +35,7 @@ class provider implements
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function _get_reason() {
|
||||
public static function get_reason(): string {
|
||||
return 'privacy:no_data_reason';
|
||||
}
|
||||
|
||||
|
||||
@@ -14,23 +14,11 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* update_static_page class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace auth_outage\task;
|
||||
|
||||
use auth_outage\local\controllers\infopage;
|
||||
use auth_outage\local\outagelib;
|
||||
use core\task\scheduled_task;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* update_static_page class.
|
||||
*
|
||||
|
||||
37
db/access.php
Normal file
37
db/access.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Define capabilities for plugin.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Andrew Madden <andrewmadden@catalyst-au.net>
|
||||
* @copyright 2021 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$capabilities = [
|
||||
'auth/outage:viewinfo' => [
|
||||
'captype' => 'read',
|
||||
'contextlevel' => CONTEXT_SYSTEM,
|
||||
'archetypes' => [
|
||||
'guest' => CAP_ALLOW,
|
||||
'user' => CAP_ALLOW,
|
||||
],
|
||||
],
|
||||
];
|
||||
34
db/hooks.php
Normal file
34
db/hooks.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Hook callbacks for auth_outage
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Benjamin Walker (benjaminwalker@catalyst-au.net)
|
||||
* @copyright 2024 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$callbacks = [
|
||||
[
|
||||
'hook' => \core\hook\output\before_standard_top_of_body_html_generation::class,
|
||||
'callback' => '\auth_outage\hook_callbacks::before_standard_top_of_body_html_generation',
|
||||
'priority' => 0,
|
||||
],
|
||||
];
|
||||
@@ -17,6 +17,7 @@
|
||||
<FIELD NAME="modifiedby" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="Who last modified this entry."/>
|
||||
<FIELD NAME="lastmodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="When was this entry last modified."/>
|
||||
<FIELD NAME="finished" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="Timestamp of when the outage really finished."/>
|
||||
<FIELD NAME="accesskey" TYPE="char" LENGTH="16" NOTNULL="false" SEQUENCE="false" COMMENT="Unique key used to access during outage"/>
|
||||
</FIELDS>
|
||||
<KEYS>
|
||||
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
|
||||
|
||||
@@ -24,14 +24,11 @@
|
||||
*/
|
||||
use auth_outage\local\controllers\maintenance_static_page;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Auth Outage plugin uninstall code.
|
||||
* @return bool result
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
|
||||
function xmldb_auth_outage_uninstall() {
|
||||
global $DB;
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* Outage plugin upgrade code
|
||||
@@ -48,5 +47,20 @@ function xmldb_auth_outage_upgrade($oldversion) {
|
||||
upgrade_plugin_savepoint(true, 2016092200, 'auth', 'outage');
|
||||
}
|
||||
|
||||
if ($oldversion < 2024081900) {
|
||||
|
||||
// Define field accesskey to be added to auth_outage.
|
||||
$table = new xmldb_table('auth_outage');
|
||||
$field = new xmldb_field('accesskey', XMLDB_TYPE_CHAR, '16', null, null, null, null, 'finished');
|
||||
|
||||
// Conditionally launch add field accesskey.
|
||||
if (!$dbman->field_exists($table, $field)) {
|
||||
$dbman->add_field($table, $field);
|
||||
}
|
||||
|
||||
// Outage savepoint reached.
|
||||
upgrade_plugin_savepoint(true, 2024081900, 'auth', 'outage');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
11
delete.php
11
delete.php
@@ -33,13 +33,14 @@ require_once($CFG->libdir.'/formslib.php');
|
||||
|
||||
admin_externalpage_setup('auth_outage_manage');
|
||||
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
|
||||
$output = $PAGE->get_renderer('auth_outage');
|
||||
|
||||
$mform = new delete();
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect('/auth/outage/manage.php');
|
||||
redirect(new moodle_url('/auth/outage/manage.php'));
|
||||
} else if ($fromform = $mform->get_data()) {
|
||||
outagedb::delete($fromform->id);
|
||||
redirect('/auth/outage/manage.php');
|
||||
redirect(new moodle_url('/auth/outage/manage.php'));
|
||||
}
|
||||
|
||||
$id = required_param('id', PARAM_INT);
|
||||
@@ -52,10 +53,10 @@ $dataid = new stdClass();
|
||||
$dataid->id = $outage->id;
|
||||
$mform->set_data($dataid);
|
||||
|
||||
echo $OUTPUT->header();
|
||||
echo $output->header();
|
||||
|
||||
echo renderer::get()->renderdeleteconfirmation($outage);
|
||||
echo $output->renderdeleteconfirmation($outage);
|
||||
|
||||
$mform->display();
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
echo $output->footer();
|
||||
|
||||
17
edit.php
17
edit.php
@@ -34,19 +34,21 @@ require_once($CFG->libdir.'/adminlib.php');
|
||||
require_once($CFG->libdir.'/formslib.php');
|
||||
|
||||
admin_externalpage_setup('auth_outage_manage');
|
||||
$output = $PAGE->get_renderer('auth_outage');
|
||||
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
|
||||
|
||||
$mform = new edit();
|
||||
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect('/auth/outage/manage.php');
|
||||
redirect(new moodle_url('/auth/outage/manage.php'));
|
||||
} else if ($outage = $mform->get_data()) {
|
||||
$id = outagedb::save($outage);
|
||||
redirect('/auth/outage/manage.php#auth_outage_id_'.$id);
|
||||
redirect(new moodle_url('/auth/outage/manage.php'));
|
||||
}
|
||||
|
||||
$clone = optional_param('clone', 0, PARAM_INT);
|
||||
$edit = optional_param('edit', 0, PARAM_INT);
|
||||
$time = optional_param('starttime', 0, PARAM_INT);
|
||||
if ($clone && $edit) {
|
||||
throw new invalid_parameter_exception('Cannot provide both clone and edit ids.');
|
||||
}
|
||||
@@ -60,7 +62,10 @@ if ($clone) {
|
||||
$action = 'outageedit';
|
||||
} else {
|
||||
$config = outagelib::get_config();
|
||||
$time = time();
|
||||
if (empty($time)) {
|
||||
$time = outagelib::get_next_window();
|
||||
}
|
||||
|
||||
$outage = new outage([
|
||||
'autostart' => $config->default_autostart,
|
||||
'starttime' => $time,
|
||||
@@ -79,7 +84,7 @@ if ($outage == null) {
|
||||
$mform->set_data($outage);
|
||||
|
||||
$PAGE->navbar->add(get_string($action.'crumb', 'auth_outage'));
|
||||
echo $OUTPUT->header();
|
||||
echo renderer::get()->rendersubtitle($action);
|
||||
echo $output->header();
|
||||
echo $output->rendersubtitle($action);
|
||||
$mform->display();
|
||||
echo $OUTPUT->footer();
|
||||
echo $output->footer();
|
||||
|
||||
3
file.php
3
file.php
@@ -57,7 +57,6 @@ header('Accept-Ranges: none');
|
||||
|
||||
/**
|
||||
* Callback used in bootstrap.
|
||||
* @SupressWarnings(PHPMD)
|
||||
*/
|
||||
function auth_outage_bootstrap_callback() {
|
||||
// Not using classes as classloader has not been initialized yet. Keep it minimalist.
|
||||
@@ -74,7 +73,9 @@ function auth_outage_bootstrap_callback() {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
require_once(__DIR__.'/../../config.php');
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
// We should never reach here if config.php and auth/outage/bootstrap.php intercepted it correctly.
|
||||
// If config.php did not execute the callback function we can use the debugging function here.
|
||||
|
||||
11
finish.php
11
finish.php
@@ -33,13 +33,14 @@ require_once($CFG->libdir.'/formslib.php');
|
||||
|
||||
admin_externalpage_setup('auth_outage_manage');
|
||||
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
|
||||
$output = $PAGE->get_renderer('auth_outage');
|
||||
|
||||
$mform = new finish();
|
||||
if ($mform->is_cancelled()) {
|
||||
redirect('/auth/outage/manage.php');
|
||||
redirect(new moodle_url('/auth/outage/manage.php'));
|
||||
} else if ($fromform = $mform->get_data()) {
|
||||
outagedb::finish($fromform->id);
|
||||
redirect('/auth/outage/manage.php');
|
||||
redirect(new moodle_url('/auth/outage/manage.php'));
|
||||
}
|
||||
|
||||
$id = required_param('id', PARAM_INT);
|
||||
@@ -52,10 +53,10 @@ $dataid = new stdClass();
|
||||
$dataid->id = $outage->id;
|
||||
$mform->set_data($dataid);
|
||||
|
||||
echo $OUTPUT->header();
|
||||
echo $output->header();
|
||||
|
||||
echo renderer::get()->renderfinishconfirmation($outage);
|
||||
echo $output->renderfinishconfirmation($outage);
|
||||
|
||||
$mform->display();
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
echo $output->footer();
|
||||
|
||||
2
info.php
2
info.php
@@ -25,7 +25,9 @@
|
||||
|
||||
use auth_outage\local\controllers\infopage;
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
require_once(__DIR__.'/../../config.php');
|
||||
// @codingStandardsIgnoreEnd
|
||||
|
||||
$info = new infopage();
|
||||
$info->output();
|
||||
|
||||
@@ -26,7 +26,10 @@
|
||||
$string['auth_outagedescription'] = 'Auxiliary plugin that warns users about a future outage and prevents them from logging in once the outage starts.';
|
||||
$string['autostart'] = 'Auto start maintenance mode.';
|
||||
$string['autostart_help'] = 'If selected, when the outage starts it will automatically turn on Moodle maintenance mode.';
|
||||
$string['builtinallowediplist'] = 'Builtin Allowed IP List';
|
||||
$string['builtinallowediplist_desc'] = 'A second allowed IP list which makes it easier to have some IPs forced in config.php and others editable in the UI';
|
||||
$string['clicreatehelp'] = 'Creates a new outage.';
|
||||
$string['clicreateexamples'] = "Create an outage starting in 10 seconds\n\n> php create.php -s=10";
|
||||
$string['clicreateparamautostart'] = 'must be Y or N, sets if the outage automatically triggers maintenance mode.';
|
||||
$string['clicreateparamblock'] = 'blocks until outage starts.';
|
||||
$string['clicreateparamclone'] = 'clone another outage except for the start time.';
|
||||
@@ -34,10 +37,11 @@ $string['clicreateparamdescription'] = 'the description of the outage.';
|
||||
$string['clicreateparamduration'] = 'how many seconds should the outage last.';
|
||||
$string['clicreateparamhelp'] = 'shows parameters help.';
|
||||
$string['clicreateparamonlyid'] = 'only outputs the new outage id, useful for scripts.';
|
||||
$string['clicreateparamstart'] = 'in how many seconds should this outage start. Required.';
|
||||
$string['clicreateparamstart'] = 'in how many seconds should this outage start or unix time to start outage. Required.';
|
||||
$string['clicreateparamtitle'] = 'the title of the outage.';
|
||||
$string['clicreateparamwarn'] = 'how many seconds before it starts to display a warning.';
|
||||
$string['clifinishhelp'] = 'Finishes an ongoing outage.';
|
||||
$string['clifinishexamples'] = '';
|
||||
$string['clifinishnotongoing'] = 'Outage is not ongoing.';
|
||||
$string['clifinishparamhelp'] = 'shows parameters help.';
|
||||
$string['clifinishparamactive'] = 'finishes the currently active outage.';
|
||||
@@ -45,6 +49,7 @@ $string['clifinishparamoutageid'] = 'the id of the outage to finish.';
|
||||
$string['cliinmaintenancemode'] = 'Moodle maintenance mode is on. Use "php admin/cli/maintenance.php --disable" to disable it before finishing the outage.';
|
||||
$string['cliwaitforiterroridxoractive'] = 'You must use --outageid=# or --active parameter but not both.';
|
||||
$string['cliwaitforithelp'] = 'Waits until an outage starts.';
|
||||
$string['cliwaitforitexamples'] = '';
|
||||
$string['cliwaitforitoutagestarted'] = 'Outage started!';
|
||||
$string['cliwaitforitoutagestartingin'] = 'Outage starting in {$a->countdown}.';
|
||||
$string['cliwaitforitparamactive'] = 'wait for the currently active outage.';
|
||||
@@ -53,6 +58,12 @@ $string['cliwaitforitparamoutageid'] = 'the id of the outage to wait until it st
|
||||
$string['cliwaitforitparamsleep'] = 'maximum amount of seconds before status output.';
|
||||
$string['cliwaitforitparamverbose'] = 'enable verbose mode.';
|
||||
$string['clierrorinvalidvalue'] = 'Invalid value for parameter: {$a->param}';
|
||||
$string['clierrorinvalidvaluenotid'] = 'Param --{$a->param} must be an id number';
|
||||
$string['clierrorinvalidvaluenotbool'] = 'Param --{$a->param} must be set to either Y or N';
|
||||
$string['clierrorinvalidvaluenotnumber'] = 'Param --{$a->param} must be a number';
|
||||
$string['clierrorinvalidvaluenegativenumber'] = 'Param --{$a->param} must be a positive number';
|
||||
$string['clierrorinvalidvaluenotstring'] = 'Param --{$a->param} must be a string';
|
||||
$string['clierrorinvalidvalueemptystring'] = 'Param --{$a->param} must not be an empty string';
|
||||
$string['clierrormissingparamaters'] = 'You must specify the start time, use --help for more information.';
|
||||
$string['clierroroutagechanged'] = 'Outage was changed while waiting.';
|
||||
$string['clierroroutageended'] = 'Outage has already ended.';
|
||||
@@ -71,12 +82,14 @@ $string['defaultoutageduration'] = 'Outage duration';
|
||||
$string['defaultoutagedurationdescription'] = 'Default duration (in minutes) of an outage.';
|
||||
$string['defaultwarningduration'] = 'Warning duration';
|
||||
$string['defaultwarningdurationdescription'] = 'Default warning time (in minutes) for outages.';
|
||||
$string['defaulttime'] = 'Default time';
|
||||
$string['defaulttimedescription'] = 'The default time for the next outage, expressed in natural language eg "next Thursday 7pm". See <a target=_blank href="https://www.php.net/manual/en/datetime.formats.relative.php">PHP relative dates</a>';
|
||||
$string['defaulttitle'] = 'Title';
|
||||
$string['defaulttitledescription'] = 'Default title for outages. Use {{start}} and {{stop}} placeholders as required.';
|
||||
$string['defaulttitlevalue'] = 'System down from {{start}} for {{duration}}.';
|
||||
$string['defaulttitlevalue'] = 'System down from {{start}} for {{duration}}';
|
||||
$string['defaultdescription'] = 'Description';
|
||||
$string['defaultdescriptiondescription'] = 'Default warning message for outages. Use {{start}} and {{stop}} placeholders as required.';
|
||||
$string['defaultdescriptionvalue'] = 'There is an scheduled maintenance from {{start}} to {{stop}} and our system will not be available during that time.';
|
||||
$string['defaultdescriptionvalue'] = 'There is maintenance scheduled from {{start}} to {{stop}} and our system will not be available during that time.';
|
||||
$string['description'] = 'Public Description';
|
||||
$string['description_help'] = 'A full description of the outage, publicly visible by all users.';
|
||||
$string['finish'] = 'Finish';
|
||||
@@ -89,18 +102,22 @@ $string['infostart'] = 'start';
|
||||
$string['infostartofwarning'] = 'start of warning';
|
||||
$string['infostaticpage'] = 'static page';
|
||||
$string['infopagestaticgenerated'] = 'This warning was generated on {$a->time}.';
|
||||
$string['allowedipsempty'] = 'When the allowed IPs list is empty we will not block anyone. You can add your own IP address (<i>{$a->ip}</i>) and block all other IPs.';
|
||||
$string['allowedipshasmyip'] = 'Your IP (<i>{$a->ip}</i>) is in the list and you will not be blocked out during an Outage.';
|
||||
$string['allowedipshasntmyip'] = 'Your IP (<i>{$a->ip}</i>) is not in the list and you will be blocked out during an outage.';
|
||||
$string['ips_combine'] = 'The IPs listed above will be combined with the IPs listed below.';
|
||||
$string['allowedipsempty'] = 'No one will be blocked by IP because the list is empty. You can add your own IP address (<i>{$a->ip}</i>) and block all other IPs. IP blocking is in addition to access key blocking (if setup in outage)';
|
||||
$string['allowedipshasmyip'] = 'Your IP (<i>{$a->ip}</i>) is in the list and your IP will not be blocked out during an Outage.';
|
||||
$string['allowedipshasntmyip'] = 'Your IP (<i>{$a->ip}</i>) is not in the list and your IP will be blocked out during an outage.';
|
||||
$string['allowedipsnoconfig'] = 'Your config.php does not have the extra setup to allow blocking via IP.<br />Please refer to our <a href="https://github.com/catalyst/moodle-auth_outage#installation" target="_blank">README.md</a> file for more information.';
|
||||
$string['logformaintmodeconfig'] = 'Update maintenance mode configuration.';
|
||||
$string['logformaintmodeconfigcomplete'] = 'Updating maintenance mode configuration complete.';
|
||||
$string['menusettings'] = 'Settings';
|
||||
$string['menumanage'] = 'Manage';
|
||||
$string['menumanage'] = 'Manage outages';
|
||||
$string['messageoutagebackonline'] = 'We are back online!';
|
||||
$string['messageoutagebackonlinedescription'] = 'You may resume browsing safely.';
|
||||
$string['messageoutageongoing'] = 'Back online at {$a->stop}.';
|
||||
$string['messageoutagewarning'] = 'Shutting down in {{countdown}}';
|
||||
$string['na'] = 'n/a';
|
||||
$string['notfound'] = 'No outages found.';
|
||||
$string['outage:viewinfo'] = 'View outage info';
|
||||
$string['outageedit'] = 'Edit outage';
|
||||
$string['outageeditcrumb'] = 'Edit';
|
||||
$string['outageclone'] = 'Clone outage';
|
||||
@@ -116,6 +133,7 @@ $string['outagefinish'] = 'Finish outage';
|
||||
$string['outagefinishwarning'] = 'You are about to mark this outage as finished. The system will be immediately back online.';
|
||||
$string['outageslistfuture'] = 'Planned outages';
|
||||
$string['outageslistpast'] = 'Outage history';
|
||||
$string['outage:updatenotify'] = '';
|
||||
$string['pluginname'] = 'Outage manager';
|
||||
$string['removeselectors'] = 'Remove selectors';
|
||||
$string['removeselectorsdescription'] = 'CSS selectors to remove when rendering a static themed maintenance page. One selector per line.';
|
||||
@@ -143,6 +161,11 @@ $string['title_help'] = 'A short title to for this outage. It will be displayed
|
||||
$string['warningdurationerrorinvalid'] = 'Warning duration must be positive.';
|
||||
$string['warningduration'] = 'Warning duration';
|
||||
$string['warningduration_help'] = 'How long before the start of the outage should the warning be displayed.';
|
||||
$string['warningreenablemaintenancemode'] = 'Please note that saving this outage will re-enable maintenance mode.<br />Untick "Auto start maintenance mode" if you want to prevent this.';
|
||||
$string['accesskey'] = 'Access key';
|
||||
$string['accesskey_help'] = 'Testers should pass the access key initially in the url parameters e.g. ?accesskey=xyz. This will then be stored in a cookie for 24 hours, during which the url parameter will not be necessary.<br /><b>Note:</b> the access key is in addition to any IP restrictions setup.';
|
||||
$string['useaccesskey'] = 'Use access key';
|
||||
$string['useaccesskey:desc'] = 'Require testers to access site during outage by providing the access key below';
|
||||
|
||||
/*
|
||||
* Privacy provider (GDPR)
|
||||
|
||||
40
lib.php
40
lib.php
@@ -25,29 +25,6 @@
|
||||
|
||||
use auth_outage\local\outagelib;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
/**
|
||||
* Used in Moodle 30+ when a user is logged on.
|
||||
*/
|
||||
function auth_outage_extend_navigation_user_settings() {
|
||||
outagelib::inject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used in Moodle 30+ on the frontpage.
|
||||
*/
|
||||
function auth_outage_extend_navigation_frontpage() {
|
||||
outagelib::inject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used in Moodle 31+ when a user is logged on.
|
||||
*/
|
||||
function auth_outage_extend_navigation_user() {
|
||||
outagelib::inject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for adminlib::set_updatedcallback which requires a string that resolves to a function.
|
||||
*
|
||||
@@ -62,7 +39,7 @@ function auth_outage_outagelib_prepare_next_outage() {
|
||||
*
|
||||
* To keep it minimalist it was not added to the outagelib.php class.
|
||||
*
|
||||
* @param $file string Filename to fetch from sitedata
|
||||
* @param string $file Filename to fetch from sitedata
|
||||
* @return string|null Full path to the sitedata file or null if file is not valid.
|
||||
*/
|
||||
function auth_outage_get_climaintenance_resource_file($file) {
|
||||
@@ -75,7 +52,7 @@ function auth_outage_get_climaintenance_resource_file($file) {
|
||||
|
||||
// Protect against path traversal attacks.
|
||||
$basename = basename($file);
|
||||
if ($basename !== $file) {
|
||||
if ($basename !== $file && $file !== 'preview/' . $basename) {
|
||||
// @codingStandardsIgnoreStart
|
||||
if (!PHPUNIT_TEST) {
|
||||
error_log('Possible attempt for Path Traversal Attack (only filename expected): '.$file);
|
||||
@@ -87,3 +64,16 @@ function auth_outage_get_climaintenance_resource_file($file) {
|
||||
$realpath = realpath($resourcedir.'/'.$file);
|
||||
return ($realpath == false) ? null : $realpath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject the warning bar into the page if there is currently an outage.
|
||||
*
|
||||
* This is a legacy callback that is used for compatibility with older Moodle versions.
|
||||
* Moodle 4.4+ will use auth_outage\hook_callbacks::before_standard_top_of_body_html_generation instead.
|
||||
*
|
||||
* @return string|void
|
||||
*/
|
||||
function auth_outage_before_standard_top_of_body_html() {
|
||||
// Get code to inject.
|
||||
return outagelib::get_inject_code();
|
||||
}
|
||||
|
||||
@@ -32,16 +32,17 @@ require_once($CFG->libdir.'/adminlib.php');
|
||||
|
||||
admin_externalpage_setup('auth_outage_manage');
|
||||
$PAGE->set_url(new moodle_url('/auth/outage/manage.php'));
|
||||
$output = $PAGE->get_renderer('auth_outage');
|
||||
|
||||
echo $OUTPUT->header();
|
||||
echo $output->header();
|
||||
|
||||
// Give it a consistent time so all outages are listed. Useful when debugging.
|
||||
$now = time();
|
||||
|
||||
renderer::get()->output_view('manage.php', [
|
||||
$output->output_view('manage.php', [
|
||||
'unended' => outagedb::get_all_unended($now),
|
||||
'ended' => outagedb::get_all_ended($now),
|
||||
'warning' => outagelib::generate_plugin_configuration_warning(),
|
||||
]);
|
||||
|
||||
echo $OUTPUT->footer();
|
||||
echo $output->footer();
|
||||
|
||||
BIN
pix/auth_outage.png
Normal file
BIN
pix/auth_outage.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 343 B |
1
pix/auth_outage.svg
Normal file
1
pix/auth_outage.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" height="16" width="16" viewBox="0 0 512 512" preserveAspectRatio="xMinYMid meet"><!--!Font Awesome Free 6.5.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free Copyright 2024 Fonticons, Inc.--><path d="M288 32c0-17.7-14.3-32-32-32s-32 14.3-32 32V256c0 17.7 14.3 32 32 32s32-14.3 32-32V32zM143.5 120.6c13.6-11.3 15.4-31.5 4.1-45.1s-31.5-15.4-45.1-4.1C49.7 115.4 16 181.8 16 256c0 132.5 107.5 240 240 240s240-107.5 240-240c0-74.2-33.8-140.6-86.6-184.6c-13.6-11.3-33.8-9.4-45.1 4.1s-9.4 33.8 4.1 45.1c38.9 32.3 63.5 81 63.5 135.4c0 97.2-78.8 176-176 176s-176-78.8-176-176c0-54.4 24.7-103.1 63.5-135.4z"/></svg>
|
||||
|
After Width: | Height: | Size: 686 B |
@@ -28,7 +28,9 @@
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\controllers\maintenance_static_page;
|
||||
|
||||
// @codingStandardsIgnoreStart
|
||||
require_once(__DIR__.'/../../config.php');
|
||||
// @codingStandardsIgnoreEnd
|
||||
$id = optional_param('id', null, PARAM_INT);
|
||||
$outage = is_null($id) ? outagedb::get_next_starting() : outagedb::get_by_id($id);
|
||||
if (is_null($outage)) {
|
||||
|
||||
@@ -26,8 +26,6 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* auth_outage_renderer class.
|
||||
*
|
||||
|
||||
45
settings.php
45
settings.php
@@ -26,25 +26,30 @@
|
||||
* @var admin_settingpage $settings
|
||||
* @var bootstrap_renderer $OUTPUT
|
||||
* @var admin_root $ADMIN
|
||||
* @var moodle_page $PAGE
|
||||
*/
|
||||
use auth_outage\local\outagelib;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die;
|
||||
|
||||
if ($hassiteconfig && is_enabled_auth('outage')) {
|
||||
if ($hassiteconfig) {
|
||||
$defaults = outagelib::get_config_defaults();
|
||||
$settings->visiblename = get_string('menusettings', 'auth_outage');
|
||||
$description = outagelib::generate_plugin_configuration_warning();
|
||||
|
||||
$settings->add(new admin_setting_heading(
|
||||
'defaults',
|
||||
get_string('settingssectiondefaults', 'auth_outage'),
|
||||
get_string('settingssectiondefaultsdescription', 'auth_outage')));
|
||||
get_string('settingssectiondefaultsdescription', 'auth_outage') . $description
|
||||
));
|
||||
|
||||
$settings->add(new admin_setting_configcheckbox(
|
||||
'auth_outage/default_autostart',
|
||||
get_string('defaultoutageautostart', 'auth_outage'),
|
||||
get_string('defaultoutageautostartdescription', 'auth_outage'),
|
||||
$defaults['default_autostart']
|
||||
));
|
||||
|
||||
$settings->add(new admin_setting_configduration(
|
||||
'auth_outage/default_warning_duration',
|
||||
get_string('defaultwarningduration', 'auth_outage'),
|
||||
@@ -59,6 +64,13 @@ if ($hassiteconfig && is_enabled_auth('outage')) {
|
||||
$defaults['default_duration'],
|
||||
60
|
||||
));
|
||||
$settings->add(new admin_setting_configtext(
|
||||
'auth_outage/default_time',
|
||||
get_string('defaulttime', 'auth_outage'),
|
||||
get_string('defaulttimedescription', 'auth_outage'),
|
||||
'',
|
||||
PARAM_TEXT
|
||||
));
|
||||
$settings->add(new admin_setting_configtext(
|
||||
'auth_outage/default_title',
|
||||
get_string('defaulttitle', 'auth_outage'),
|
||||
@@ -89,20 +101,23 @@ if ($hassiteconfig && is_enabled_auth('outage')) {
|
||||
|
||||
// Create 'Allowed IPs' settings.
|
||||
$allowedips = outagelib::get_config()->allowedips;
|
||||
$description = outagelib::generate_plugin_configuration_warning();
|
||||
if (trim($allowedips) == '') {
|
||||
$message = 'allowedipsempty';
|
||||
$type = 'notifymessage';
|
||||
} else if (remoteip_in_list($allowedips)) {
|
||||
$message = 'allowedipshasmyip';
|
||||
$type = 'notifysuccess';
|
||||
} else {
|
||||
$message = 'allowedipshasntmyip';
|
||||
$type = 'notifyerror';
|
||||
if (remoteip_in_list($allowedips)) {
|
||||
$message = 'allowedipshasmyip';
|
||||
$type = 'notifysuccess';
|
||||
} else {
|
||||
$message = 'allowedipshasntmyip';
|
||||
$type = 'notifyerror';
|
||||
}
|
||||
};
|
||||
$description .= $OUTPUT->notification(get_string($message, 'auth_outage', ['ip' => getremoteaddr()]), $type);
|
||||
$description = $OUTPUT->notification(get_string($message, 'auth_outage', ['ip' => getremoteaddr()]), $type);
|
||||
|
||||
$description .= '<p>' . get_string('ipblockersyntax', 'admin') . '</p>';
|
||||
$description .= '<p>' . get_string('ips_combine', 'auth_outage') . '</p>';
|
||||
|
||||
$description .= '<p>'.get_string('ipblockersyntax', 'admin').'</p>';
|
||||
$iplist = new admin_setting_configiplist(
|
||||
'auth_outage/allowedips',
|
||||
get_string('allowediplist', 'admin'),
|
||||
@@ -112,6 +127,14 @@ if ($hassiteconfig && is_enabled_auth('outage')) {
|
||||
$iplist->set_updatedcallback('auth_outage_outagelib_prepare_next_outage');
|
||||
$settings->add($iplist);
|
||||
|
||||
$iplist = new admin_setting_configiplist(
|
||||
'auth_outage/allowedips_forced',
|
||||
get_string('builtinallowediplist', 'auth_outage'),
|
||||
get_string('builtinallowediplist_desc', 'auth_outage'),
|
||||
''
|
||||
);
|
||||
$settings->add($iplist);
|
||||
|
||||
// Create 'Static Page - Elements to Remove' settings.
|
||||
$toremove = new admin_setting_configtextarea(
|
||||
'auth_outage/remove_selectors',
|
||||
@@ -133,7 +156,7 @@ if ($hassiteconfig && is_enabled_auth('outage')) {
|
||||
new admin_externalpage(
|
||||
'auth_outage_manage',
|
||||
get_string('menumanage', 'auth_outage'),
|
||||
new moodle_url($CFG->wwwroot.'/auth/outage/manage.php')
|
||||
new moodle_url($CFG->wwwroot . '/auth/outage/manage.php')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
|
||||
/**
|
||||
* Base testcase for auth outage tests.
|
||||
*
|
||||
@@ -30,19 +33,7 @@
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
/**
|
||||
* auth_outage_base_testcase class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
*/
|
||||
abstract class auth_outage_base_testcase extends advanced_testcase {
|
||||
abstract class base_testcase extends \core_phpunit\testcase {
|
||||
/**
|
||||
* Checks PHPUnit version and calls the functions accordingly.
|
||||
* @param string $exception Expected exception class.
|
||||
@@ -64,14 +55,56 @@ abstract class auth_outage_base_testcase extends advanced_testcase {
|
||||
}
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
/**
|
||||
* Revoke permission to see info page.
|
||||
*/
|
||||
protected function revoke_info_page_permissions() {
|
||||
global $DB;
|
||||
|
||||
$guestrole = $DB->get_record('role', ['shortname' => 'guest']);
|
||||
role_change_permission($guestrole->id, \context_system::instance(), 'auth/outage:viewinfo', CAP_PREVENT);
|
||||
|
||||
$this->setGuestUser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an outage object.
|
||||
*
|
||||
* @return \auth_outage\local\outage
|
||||
*/
|
||||
protected function get_dummy_outage() {
|
||||
$now = time();
|
||||
|
||||
return new outage([
|
||||
'id' => 1,
|
||||
'autostart' => false,
|
||||
'warntime' => $now - 100,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup testcase.
|
||||
*/
|
||||
public function setUp(): void {
|
||||
global $CFG;
|
||||
|
||||
parent::setUp();
|
||||
$this->resetAfterTest(true);
|
||||
}
|
||||
|
||||
// Do not use https.
|
||||
$CFG->wwwroot = 'http://www.example.com/moodle';
|
||||
$CFG->httpswwwroot = $CFG->wwwroot;
|
||||
/**
|
||||
* Tear down to restore the original DB reference.
|
||||
*/
|
||||
public function tearDown(): void {
|
||||
global $DB;
|
||||
|
||||
foreach (outagedb::get_all() as $i => $outage) {
|
||||
$DB->delete_records('auth_outage', ['id' => $outage->id]);
|
||||
}
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
||||
@@ -39,7 +39,6 @@ require_once(__DIR__.'/../../../../lib/behat/behat_base.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
*/
|
||||
class behat_auth_outage extends behat_base {
|
||||
/**
|
||||
@@ -155,6 +154,7 @@ class behat_auth_outage extends behat_base {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an specific action is not visible.
|
||||
* @Then /^I should see an empty settings text area "([^"]*)"$/
|
||||
* @param string $name
|
||||
*/
|
||||
@@ -163,6 +163,7 @@ class behat_auth_outage extends behat_base {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an specific action is not visible.
|
||||
* @When /^I go to the "Outage Settings" page$/
|
||||
*/
|
||||
public function i_go_to_the_outage_settings_page() {
|
||||
@@ -321,7 +322,7 @@ class behat_auth_outage extends behat_base {
|
||||
}
|
||||
if ($seconds >= 0) {
|
||||
$seconds++; // Give one extra second for things to happen.
|
||||
$this->getSession()->wait($seconds * 1000, false);
|
||||
$this->getSession()->wait($seconds * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,9 +11,8 @@ Feature: Change the default settings
|
||||
Given the authentication plugin "outage" is enabled
|
||||
And I am an administrator
|
||||
|
||||
|
||||
Scenario Outline: Check if I can save the default settings.
|
||||
When I navigate to "Settings" node in "Site administration > Plugins > Authentication > Outage manager"
|
||||
When I navigate to "Plugins > Authentication > Outage manager > Settings" in site administration
|
||||
And I set the following fields to these values:
|
||||
| s_auth_outage_default_autostart | <autostart> |
|
||||
| s_auth_outage_default_warning_duration[v] | <warning> |
|
||||
|
||||
48
tests/behat/disable_in_layout.feature
Normal file
48
tests/behat/disable_in_layout.feature
Normal file
@@ -0,0 +1,48 @@
|
||||
@auth @auth_outage @javascript @_file_upload @_switch_iframe
|
||||
Feature: Disable warning bar in embedded layout
|
||||
In order alert users about an outage
|
||||
As any user
|
||||
I need to view the warning bar, but not in embedded layout
|
||||
|
||||
Background:
|
||||
Given the authentication plugin "outage" is enabled
|
||||
And the following "courses" exist:
|
||||
| fullname | shortname |
|
||||
| Course 1 | C1 |
|
||||
And the following "activities" exist:
|
||||
| activity | name | intro | introformat | course | content | contentformat | idnumber |
|
||||
| page | PageName1 | PageDesc1 | 1 | C1 | H5Ptest | 1 | 1 |
|
||||
And the "displayh5p" filter is "on"
|
||||
|
||||
Scenario: Disable warning bar in embedded h5p in book activity
|
||||
Given I log in as "admin"
|
||||
And I am on "Course 1" course homepage with editing mode on
|
||||
And I add a "File" to section "1"
|
||||
And I set the following fields to these values:
|
||||
| Name | ipsumFile |
|
||||
And I upload "h5p/tests/fixtures/ipsums.h5p" file to "Select files" filemanager
|
||||
And I press "Save and return to course"
|
||||
And I follow "PageName1"
|
||||
And I navigate to "Edit settings" in current page administration
|
||||
And I click on "Insert H5P" "button" in the "#fitem_id_page" "css_element"
|
||||
And I click on "Browse repositories..." "button" in the "Insert H5P" "dialogue"
|
||||
And I click on "Server files" "link" in the ".fp-repo-area" "css_element"
|
||||
And I click on "ipsumFile (File)" "link"
|
||||
And I click on "ipsums.h5p" "link"
|
||||
And I click on "Select this file" "button"
|
||||
And I click on "Insert H5P" "button" in the "Insert H5P" "dialogue"
|
||||
And I wait until the page is ready
|
||||
And I click on "Save and display" "button"
|
||||
When there is the following outage:
|
||||
| warnbefore | startsin |
|
||||
| 0 | 0 |
|
||||
And I reload the page
|
||||
Then I should see "Back online at" in the warning bar
|
||||
And I should not see "Lorum ipsum"
|
||||
# Switch to iframe created by filter
|
||||
And I switch to "h5p-iframe" class iframe
|
||||
And I should not see the warning bar
|
||||
# Switch to iframe created by embed.php page
|
||||
And I switch to "h5p-iframe" class iframe
|
||||
And I should see "Lorum ipsum"
|
||||
And I should not see the warning bar
|
||||
@@ -8,14 +8,11 @@ Feature: IP Blocker
|
||||
- An ongoing outage does not block Moodle execution, although it can trigger maintenance mode.
|
||||
- Maintenance mode completely blocks Moodle and can only be deactivated using the CLI.
|
||||
|
||||
|
||||
Background:
|
||||
Given the authentication plugin "outage" is enabled
|
||||
|
||||
|
||||
Scenario: Default IP Whitelist Settings
|
||||
Given I am an administrator
|
||||
And I am on homepage
|
||||
When I navigate to "Settings" node in "Site administration > Plugins > Authentication > Outage manager"
|
||||
When I navigate to "Plugins > Authentication > Outage manager > Settings" in site administration
|
||||
Then I should see "Allowed IP list"
|
||||
And I should see an empty settings text area "allowedips"
|
||||
|
||||
@@ -11,22 +11,19 @@ Feature: Manage outages
|
||||
- finished is an outage that has explicitly been marked as finished.
|
||||
- stopped is an outage that has already ended but not explicitly marked as finished.
|
||||
|
||||
|
||||
Background: Always login as admin, enable the auth_outage plugin and go to the outage management page.
|
||||
Given the authentication plugin "outage" is enabled
|
||||
And I log in as "admin"
|
||||
And I wait "1" seconds
|
||||
|
||||
|
||||
Scenario: Check if I can navigate to management page.
|
||||
Given I am on homepage
|
||||
When I navigate to "Manage" node in "Site administration > Plugins > Authentication > Outage manager"
|
||||
When I navigate to "Plugins > Authentication > Outage manager > Manage outages" in site administration
|
||||
Then I should see "Planned outages"
|
||||
And I should see "No outages found." in the "#section_planned_outages" "css_element"
|
||||
And I should see "Outage history"
|
||||
And I should see "No outages found." in the "#section_outage_history" "css_element"
|
||||
|
||||
|
||||
Scenario Outline: Planned outages should include all outages not finished or stopped.
|
||||
Given there is a "<type>" outage
|
||||
When I am on Outage Management Page
|
||||
@@ -40,7 +37,6 @@ Feature: Manage outages
|
||||
| finished | outage_history |
|
||||
| stopped | outage_history |
|
||||
|
||||
|
||||
Scenario Outline: Planned and history outages have different actions.
|
||||
Given there is a "<type>" outage
|
||||
When I am on Outage Management Page
|
||||
@@ -59,14 +55,12 @@ Feature: Manage outages
|
||||
| finished | see | see | not see | not see | not see |
|
||||
| stopped | see | see | not see | not see | not see |
|
||||
|
||||
|
||||
# Scenario: Create an outage using defaults.
|
||||
# Given I am on Outage Management Page
|
||||
# When I press "Create outage"
|
||||
# And I press "Save changes"
|
||||
# And I should not see "No outages found." in the "#section_planned_outages" "css_element"
|
||||
# And I should see "No outages found." in the "#section_outage_history" "css_element"
|
||||
|
||||
Scenario: Create an outage using defaults.
|
||||
Given I am on Outage Management Page
|
||||
When I press "Create outage"
|
||||
And I press "Save changes"
|
||||
And I should not see "No outages found." in the "#section_planned_outages" "css_element"
|
||||
And I should see "No outages found." in the "#section_outage_history" "css_element"
|
||||
|
||||
Scenario: View an outage which should open in a new window or tab.
|
||||
Given there is a "waiting" outage
|
||||
@@ -75,29 +69,26 @@ Feature: Manage outages
|
||||
Then I should be in a new window
|
||||
And I should see "Example of waiting outage"
|
||||
|
||||
Scenario: Clone an outage.
|
||||
Given there is a "waiting" outage
|
||||
And I am on Outage Management Page
|
||||
When I click on the "Clone" action button
|
||||
Then I should see "Clone outage"
|
||||
And I set the field "title" to "My cloned outage"
|
||||
And I press "Save changes"
|
||||
Then I should see "Example of waiting outage"
|
||||
And I should see "My cloned outage"
|
||||
|
||||
# Scenario: Clone an outage.
|
||||
# Given there is a "waiting" outage
|
||||
# And I am on Outage Management Page
|
||||
# When I click on the "Clone" action button
|
||||
# Then I should see "Clone outage"
|
||||
# And I set the field "title" to "My cloned outage"
|
||||
# And I press "Save changes"
|
||||
# Then I should see "Example of waiting outage"
|
||||
# And I should see "My cloned outage"
|
||||
|
||||
|
||||
# Scenario: Edit an outage.
|
||||
# Given there is a "warning" outage
|
||||
# And I am on Outage Management Page
|
||||
# And I should see "Example of warning outage"
|
||||
# When I click on the "Edit" action button
|
||||
# Then I should see "Edit outage"
|
||||
# And I set the field "title" to "My previous warning outage"
|
||||
# And I press "Save changes"
|
||||
# Then I should not see "Example of warning outage"
|
||||
# And I should see "My previous warning outage"
|
||||
|
||||
Scenario: Edit an outage.
|
||||
Given there is a "warning" outage
|
||||
And I am on Outage Management Page
|
||||
And I should see "Example of warning outage"
|
||||
When I click on the "Edit" action button
|
||||
Then I should see "Edit outage"
|
||||
And I set the field "title" to "My previous warning outage"
|
||||
And I press "Save changes"
|
||||
Then I should not see "Example of warning outage"
|
||||
And I should see "My previous warning outage"
|
||||
|
||||
Scenario: Delete an outage
|
||||
Given there is a "warning" outage
|
||||
@@ -109,7 +100,6 @@ Feature: Manage outages
|
||||
Then I press "Delete"
|
||||
And I should not see "Example of warning outage"
|
||||
|
||||
|
||||
Scenario: Finish an outage
|
||||
Given there is a "ongoing" outage
|
||||
And I am on Outage Management Page
|
||||
|
||||
@@ -11,46 +11,45 @@ Feature: Warning bar
|
||||
- finished is an outage that has explicitly been marked as finished.
|
||||
- stopped is an outage that has already ended but not explicitly marked as finished.
|
||||
|
||||
|
||||
Background:
|
||||
Given the authentication plugin "outage" is enabled
|
||||
|
||||
|
||||
# Scenario: This is how an outage should happend without maintenance mode and manual finish.
|
||||
# Given there is the following outage:
|
||||
# | warnbefore | startsin | stopsafter |
|
||||
# | 10 | 20 | 10 |
|
||||
# When I am on homepage
|
||||
# Then I should not see the warning bar
|
||||
# When I wait until the outage warns
|
||||
# And I reload the page
|
||||
# Then I should see "Shutting down in" in the warning bar
|
||||
# When I wait until the outage starts
|
||||
# Then I should see "Back online at" in the warning bar
|
||||
# When I wait until the outage stops
|
||||
# Then I should see "We are back online!" in the warning bar
|
||||
# When I reload the page
|
||||
# Then I should not see the warning bar
|
||||
|
||||
|
||||
Scenario Outline: Some stages should show its own warning message.
|
||||
Given there is a "<type>" outage
|
||||
When I am on homepage
|
||||
Then I should see "<see>" in the warning bar
|
||||
|
||||
Examples:
|
||||
| type | see |
|
||||
| warning | Shutting down in |
|
||||
| ongoing | Back online at |
|
||||
|
||||
|
||||
Scenario Outline: Some stages should not have a warning bar.
|
||||
Given there is a "<type>" outage
|
||||
Scenario: This is how an outage should happen without maintenance mode and manual finish.
|
||||
Given there is the following outage:
|
||||
| warnbefore | startsin | stopsafter |
|
||||
| 10 | 20 | 10 |
|
||||
When I am on homepage
|
||||
Then I should not see the warning bar
|
||||
|
||||
Examples:
|
||||
| type |
|
||||
| waiting |
|
||||
| finished |
|
||||
| stopped |
|
||||
When I wait until the outage warns
|
||||
And I reload the page
|
||||
Then I should see "Shutting down in" in the warning bar
|
||||
When I wait until the outage starts
|
||||
And I reload the page
|
||||
Then I should see "Back online at" in the warning bar
|
||||
When I wait until the outage stops
|
||||
Then I should see "We are back online!" in the warning bar
|
||||
When I reload the page
|
||||
Then I should not see the warning bar
|
||||
#
|
||||
#
|
||||
# Scenario Outline: Some stages should show its own warning message.
|
||||
# Given there is a "<type>" outage
|
||||
# When I am on homepage
|
||||
# Then I should see "<see>" in the warning bar
|
||||
#
|
||||
# Examples:
|
||||
# | type | see |
|
||||
# | warning | Shutting down in |
|
||||
# | ongoing | Back online at |
|
||||
#
|
||||
#
|
||||
# Scenario Outline: Some stages should not have a warning bar.
|
||||
# Given there is a "<type>" outage
|
||||
# When I am on homepage
|
||||
# Then I should not see the warning bar
|
||||
#
|
||||
# Examples:
|
||||
# | type |
|
||||
# | waiting |
|
||||
# | finished |
|
||||
# | stopped |
|
||||
|
||||
@@ -23,10 +23,8 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use auth_outage\calendar\calendar;
|
||||
use auth_outage\local\outage;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
use auth_outage\calendar\calendar;
|
||||
|
||||
/**
|
||||
* calendar_test test class.
|
||||
@@ -38,8 +36,9 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \auth_outage\calendar\calendar
|
||||
*/
|
||||
class calendar_test extends advanced_testcase {
|
||||
class calendar_test extends \core_phpunit\testcase {
|
||||
/**
|
||||
* @var outage|null The calendar entry owner.
|
||||
*/
|
||||
@@ -49,8 +48,8 @@ class calendar_test extends advanced_testcase {
|
||||
* Creates an outage and checks if its in the calendar.
|
||||
*/
|
||||
public function test_create() {
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$this->resetAfterTest(false);
|
||||
|
||||
$time = time();
|
||||
self::$outage = new outage([
|
||||
@@ -70,8 +69,20 @@ class calendar_test extends advanced_testcase {
|
||||
* Updates an outage and checks the calendar.
|
||||
*/
|
||||
public function test_update() {
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$this->resetAfterTest(false);
|
||||
|
||||
$time = time();
|
||||
self::$outage = new outage([
|
||||
'id' => 1,
|
||||
'autostart' => false,
|
||||
'warntime' => $time - 100,
|
||||
'starttime' => $time,
|
||||
'stoptime' => $time + (2 * 60 * 60),
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
calendar::create(self::$outage);
|
||||
|
||||
self::$outage->title = 'New Title';
|
||||
calendar::update(self::$outage);
|
||||
@@ -82,8 +93,22 @@ class calendar_test extends advanced_testcase {
|
||||
* Deletes an outage and checks the calendar.
|
||||
*/
|
||||
public function test_delete() {
|
||||
self::setAdminUser();
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
|
||||
$time = time();
|
||||
self::$outage = new outage([
|
||||
'id' => 1,
|
||||
'autostart' => false,
|
||||
'warntime' => $time - 100,
|
||||
'starttime' => $time,
|
||||
'stoptime' => $time + (2 * 60 * 60),
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
calendar::create(self::$outage);
|
||||
|
||||
$this->check_calendar();
|
||||
|
||||
calendar::delete(self::$outage->id);
|
||||
self::assertNull(calendar::load(self::$outage->id));
|
||||
@@ -93,8 +118,8 @@ class calendar_test extends advanced_testcase {
|
||||
* Try to update a non existing outage.
|
||||
*/
|
||||
public function test_update_notfound() {
|
||||
self::setAdminUser();
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
|
||||
$time = time();
|
||||
$outage = new outage([
|
||||
@@ -108,19 +133,20 @@ class calendar_test extends advanced_testcase {
|
||||
]);
|
||||
|
||||
calendar::update($outage);
|
||||
self::assertCount(1, phpunit_util::get_debugging_messages());
|
||||
phpunit_util::reset_debugging();
|
||||
self::assertCount(1, $this->getDebuggingMessages());
|
||||
$this->resetDebugging();
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to delete a non existing outage.
|
||||
*/
|
||||
public function test_delete_notfound() {
|
||||
self::setAdminUser();
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
|
||||
calendar::delete(1);
|
||||
self::assertCount(1, phpunit_util::get_debugging_messages());
|
||||
phpunit_util::reset_debugging();
|
||||
self::assertCount(1, $this->getDebuggingMessages());
|
||||
$this->resetDebugging();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,9 +24,9 @@
|
||||
*/
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\cli\cli_exception;
|
||||
use auth_outage\local\cli\create;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\cli\create;
|
||||
use auth_outage\local\cli\cli_exception;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/cli_testcase.php');
|
||||
@@ -38,9 +38,14 @@ require_once(__DIR__.'/cli_testcase.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
* @covers \auth_outage\local\cli\create
|
||||
*/
|
||||
class create_test extends auth_outage_cli_testcase {
|
||||
class cli_create_test extends cli_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests without any arguments.
|
||||
*/
|
||||
@@ -121,8 +126,8 @@ class create_test extends auth_outage_cli_testcase {
|
||||
$this->set_parameters(['--help']);
|
||||
$cli = new create();
|
||||
$output = $this->execute($cli);
|
||||
self::assertContains('Creates', $output);
|
||||
self::assertContains('--help', $output);
|
||||
self::assertStringContainsString('Creates', $output);
|
||||
self::assertStringContainsString('--help', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,10 +163,10 @@ class create_test extends auth_outage_cli_testcase {
|
||||
$cli = new create();
|
||||
$cli->set_referencetime($now);
|
||||
$text = $this->execute($cli);
|
||||
self::assertContains('created', $text);
|
||||
self::assertStringContainsString('created', $text);
|
||||
// Check creted outage.
|
||||
list(, $id) = explode(':', $text);
|
||||
$id = (int)$id;
|
||||
$clioutput = explode(':', $text);
|
||||
$id = (int)end($clioutput);
|
||||
$outage = outagedb::get_by_id($id);
|
||||
self::assertSame($now, $outage->starttime);
|
||||
self::assertSame(10, $outage->get_warning_duration());
|
||||
@@ -222,10 +227,10 @@ class create_test extends auth_outage_cli_testcase {
|
||||
'description' => 'Default Description',
|
||||
]);
|
||||
$text = $this->execute($cli);
|
||||
self::assertContains('created', $text);
|
||||
self::assertStringContainsString('created', $text);
|
||||
// Check creted outage.
|
||||
list(, $id) = explode(':', $text);
|
||||
$id = (int)$id;
|
||||
$clioutput = explode(':', $text);
|
||||
$id = (int)end($clioutput);
|
||||
$outage = outagedb::get_by_id($id);
|
||||
self::assertSame($now + 50, $outage->starttime, 'Wrong starttime.');
|
||||
self::assertSame($outage->starttime - 100, $outage->warntime, 'Wrong warntime.');
|
||||
@@ -300,8 +305,8 @@ class create_test extends auth_outage_cli_testcase {
|
||||
$cli = new create();
|
||||
$cli->set_referencetime($now);
|
||||
$text = $this->execute($cli);
|
||||
self::assertContains('created', $text);
|
||||
self::assertContains('started', $text);
|
||||
self::assertStringContainsString('created', $text);
|
||||
self::assertStringContainsString('started', $text);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,9 +24,9 @@
|
||||
*/
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\cli\cli_exception;
|
||||
use auth_outage\local\cli\finish;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\cli\finish;
|
||||
use auth_outage\local\cli\cli_exception;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/cli_testcase.php');
|
||||
@@ -38,9 +38,14 @@ require_once(__DIR__.'/cli_testcase.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
* @covers \auth_outage\local\cli\finish
|
||||
*/
|
||||
class finish_test extends auth_outage_cli_testcase {
|
||||
class cli_finish_test extends cli_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor.
|
||||
*/
|
||||
@@ -73,8 +78,8 @@ class finish_test extends auth_outage_cli_testcase {
|
||||
$this->set_parameters(['--help']);
|
||||
$cli = new finish();
|
||||
$text = $this->execute($cli);
|
||||
self::assertContains('Finishes', $text);
|
||||
self::assertContains('--help', $text);
|
||||
self::assertStringContainsString('Finishes', $text);
|
||||
self::assertStringContainsString('--help', $text);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,9 +36,14 @@ require_once(__DIR__.'/cli_testcase.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
* @covers \auth_outage\local\cli\create
|
||||
*/
|
||||
class cli_test extends auth_outage_cli_testcase {
|
||||
class cli_test extends cli_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests providing an unknown parameter.
|
||||
*/
|
||||
@@ -82,8 +87,8 @@ class cli_test extends auth_outage_cli_testcase {
|
||||
$this->set_parameters(['-h']);
|
||||
$cli = new create();
|
||||
$output = $this->execute($cli);
|
||||
self::assertContains('-h', $output);
|
||||
self::assertContains('--help', $output);
|
||||
self::assertStringContainsString('-h', $output);
|
||||
self::assertStringContainsString('--help', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,7 +106,7 @@ class cli_test extends auth_outage_cli_testcase {
|
||||
// Disable all auth plugins.
|
||||
set_config('auth', '');
|
||||
\core\session\manager::gc(); // Remove stale sessions.
|
||||
core_plugin_manager::reset_caches();
|
||||
\core_plugin_manager::reset_caches();
|
||||
// Try to create an CLI object.
|
||||
$this->set_expected_cli_exception(cli_exception::ERROR_PLUGIN_CONFIGURATION);
|
||||
new create();
|
||||
@@ -15,7 +15,7 @@
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* auth_outage_cli_testcase class.
|
||||
* cli_testcase class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
@@ -23,25 +23,27 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use auth_outage\local\cli\clibase;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/../../base_testcase.php');
|
||||
require_once(__DIR__.'/base_testcase.php');
|
||||
|
||||
/**
|
||||
* auth_outage_cli_testcase class.
|
||||
* cli_testcase class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
*/
|
||||
abstract class auth_outage_cli_testcase extends auth_outage_base_testcase {
|
||||
abstract class cli_testcase extends base_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Always enable the auth outage plugin, resets after test and set no parameters.
|
||||
*/
|
||||
public function setUp() {
|
||||
public function setUp(): void {
|
||||
global $CFG;
|
||||
|
||||
// PHPUnit does not load config.php file.
|
||||
@@ -50,7 +52,7 @@ abstract class auth_outage_cli_testcase extends auth_outage_base_testcase {
|
||||
// Enable auth plugins.
|
||||
set_config('auth', 'outage');
|
||||
\core\session\manager::gc(); // Remove stale sessions.
|
||||
core_plugin_manager::reset_caches();
|
||||
\core_plugin_manager::reset_caches();
|
||||
|
||||
$this->set_parameters([]);
|
||||
parent::setUp();
|
||||
@@ -74,7 +76,7 @@ abstract class auth_outage_cli_testcase extends auth_outage_base_testcase {
|
||||
*
|
||||
* @return string The output text.
|
||||
*/
|
||||
protected function execute(clibase $cli) {
|
||||
protected function execute(mixed $cli) {
|
||||
ob_start();
|
||||
try {
|
||||
$cli->execute();
|
||||
@@ -24,9 +24,9 @@
|
||||
*/
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\cli\cli_exception;
|
||||
use auth_outage\local\cli\waitforit;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\cli\waitforit;
|
||||
use auth_outage\local\cli\cli_exception;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/cli_testcase.php');
|
||||
@@ -38,9 +38,14 @@ require_once(__DIR__.'/cli_testcase.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
* @covers \auth_outage\local\cli\waitforit
|
||||
*/
|
||||
class waitforit_test extends auth_outage_cli_testcase {
|
||||
class cli_waitforit_test extends cli_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor.
|
||||
*/
|
||||
@@ -79,8 +84,8 @@ class waitforit_test extends auth_outage_cli_testcase {
|
||||
$this->set_parameters(['--help']);
|
||||
$cli = new waitforit();
|
||||
$text = $this->execute($cli);
|
||||
self::assertContains('Waits', $text);
|
||||
self::assertContains('--help', $text);
|
||||
self::assertStringContainsString('Waits', $text);
|
||||
self::assertStringContainsString('--help', $text);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,9 +157,9 @@ class waitforit_test extends auth_outage_cli_testcase {
|
||||
$cli = new waitforit();
|
||||
$cli->set_referencetime($now);
|
||||
$output = $this->execute($cli);
|
||||
self::assertContains('Verbose mode', $output);
|
||||
self::assertContains('starting in 1 sec', $output);
|
||||
self::assertContains('started', $output);
|
||||
self::assertStringContainsString('Verbose mode', $output);
|
||||
self::assertStringContainsString('starting in 1 sec', $output);
|
||||
self::assertStringContainsString('started', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -179,11 +184,11 @@ class waitforit_test extends auth_outage_cli_testcase {
|
||||
return $now;
|
||||
});
|
||||
$output = $this->execute($cli);
|
||||
self::assertContains("starting in 45", $output);
|
||||
self::assertContains("sleep 30 second", $output);
|
||||
self::assertContains("starting in 15", $output);
|
||||
self::assertContains("sleep 15 second", $output);
|
||||
self::assertContains("started!", $output);
|
||||
self::assertStringContainsString("starting in 45", $output);
|
||||
self::assertStringContainsString("sleep 30 second", $output);
|
||||
self::assertStringContainsString("starting in 15", $output);
|
||||
self::assertStringContainsString("sleep 15 second", $output);
|
||||
self::assertStringContainsString("started!", $output);
|
||||
}
|
||||
|
||||
/**
|
||||
171
tests/controllers_infopage_test.php
Normal file
171
tests/controllers_infopage_test.php
Normal file
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Tests performed on infopage controller class and update_static_page task class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\controllers\infopage;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/base_testcase.php');
|
||||
|
||||
/**
|
||||
* Tests performed on infopage controller class and update_static_page task class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \auth_outage\local\controllers\infopage
|
||||
*/
|
||||
class controllers_infopage_test extends base_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor.
|
||||
*/
|
||||
public function test_constructor() {
|
||||
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
|
||||
|
||||
new infopage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor with given parameters.
|
||||
*/
|
||||
public function test_constructor_withparams() {
|
||||
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
|
||||
|
||||
$_GET = ['id' => 1, 'static' => 'true'];
|
||||
new infopage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor with different id and outage id.
|
||||
*/
|
||||
public function test_constructor_idmismatch() {
|
||||
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
|
||||
|
||||
$outage = $this->get_dummy_outage();
|
||||
$this->set_expected_exception('coding_exception', 'Provided id and outage->id do not match. (2/1)');
|
||||
new infopage(['id' => 2, 'outage' => $outage]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor with an invalid outage.
|
||||
*/
|
||||
public function test_constructor_invalidoutage() {
|
||||
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
|
||||
|
||||
$this->set_expected_exception('coding_exception', 'Provided outage is not a valid outage object. (My outage)');
|
||||
new infopage(['outage' => 'My outage']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the output of the info page.
|
||||
*/
|
||||
public function test_output() {
|
||||
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
|
||||
|
||||
$outage = $this->get_dummy_outage();
|
||||
|
||||
$info = new infopage(['outage' => $outage]);
|
||||
$output = $info->get_output();
|
||||
self::assertStringContainsString('auth_outage_info', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the output of the info page.
|
||||
*/
|
||||
public function test_output_without_permission() {
|
||||
$this->revoke_info_page_permissions();
|
||||
$this->assertFalse(has_capability('auth/outage:viewinfo', context_system::instance()));
|
||||
|
||||
$outage = $this->get_dummy_outage();
|
||||
$info = new infopage(['outage' => $outage]);
|
||||
|
||||
$this->set_expected_exception('moodle_exception', 'Unsupported redirect detected, script execution terminated');
|
||||
$output = $info->get_output();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the output of the info page.
|
||||
*/
|
||||
public function test_output_without_permission_but_static() {
|
||||
$this->revoke_info_page_permissions();
|
||||
$this->assertFalse(has_capability('auth/outage:viewinfo', context_system::instance()));
|
||||
|
||||
$outage = $this->get_dummy_outage();
|
||||
$info = new infopage(['outage' => $outage, 'static' => true]);
|
||||
|
||||
$output = $info->get_output();
|
||||
self::assertStringContainsString('auth_outage_info', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the output of the info page.
|
||||
*/
|
||||
public function test_output_with_forcelogin() {
|
||||
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
|
||||
|
||||
set_config('forcelogin', true);
|
||||
|
||||
$outage = $this->get_dummy_outage();
|
||||
$info = new infopage(['outage' => $outage]);
|
||||
|
||||
$this->set_expected_exception('moodle_exception', 'Unsupported redirect detected, script execution terminated');
|
||||
$info->get_output();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the output of the info page.
|
||||
*/
|
||||
public function test_output_with_forcelogin_if_static() {
|
||||
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
|
||||
|
||||
set_config('forcelogin', true);
|
||||
|
||||
$outage = $this->get_dummy_outage();
|
||||
|
||||
$info = new infopage(['outage' => $outage, 'static' => true]);
|
||||
|
||||
$output = $info->get_output();
|
||||
self::assertStringContainsString('auth_outage_info', $output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor enables SVG support.
|
||||
*/
|
||||
public function test_svgicons_is_true() {
|
||||
global $CFG;
|
||||
|
||||
$this->assertTrue(has_capability('auth/outage:viewinfo', context_system::instance()));
|
||||
|
||||
$CFG->svgicons = false;
|
||||
new infopage();
|
||||
self::assertTrue($CFG->svgicons);
|
||||
}
|
||||
}
|
||||
@@ -23,12 +23,13 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use auth_outage\task\update_static_page;
|
||||
use auth_outage\local\controllers\maintenance_static_page;
|
||||
use auth_outage\local\controllers\maintenance_static_page_io;
|
||||
use auth_outage\task\update_static_page;
|
||||
use auth_outage\local\controllers\maintenance_static_page_generator;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/../../base_testcase.php');
|
||||
require_once(__DIR__.'/base_testcase.php');
|
||||
|
||||
/**
|
||||
* maintenance_static_page_test class.
|
||||
@@ -37,10 +38,17 @@ require_once(__DIR__.'/../../base_testcase.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
* @SuppressWarnings(methods) Allow as many methods as needed.
|
||||
* @covers \auth_outage\local\controllers\maintenance_static_page_generator
|
||||
*/
|
||||
class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
class controllers_maintenance_static_page_test extends base_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test template file.
|
||||
*/
|
||||
public function test_templatefile() {
|
||||
global $CFG;
|
||||
$page = maintenance_static_page::create_from_html('<html></html>');
|
||||
@@ -50,6 +58,9 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
$page->get_io()->get_template_file());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test resources folder.
|
||||
*/
|
||||
public function test_resourcesfolder() {
|
||||
global $CFG;
|
||||
$page = maintenance_static_page::create_from_html('<html></html>');
|
||||
@@ -58,10 +69,16 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
self::assertSame($CFG->dataroot.'/auth_outage/climaintenance/preview', $page->get_io()->get_resources_folder());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create from outage.
|
||||
*/
|
||||
public function test_createfromoutage() {
|
||||
// How to fetch a page from PHPUnit environment?
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create from HTML.
|
||||
*/
|
||||
public function test_createfromhtml() {
|
||||
$html = "<!DOCTYPE html>\n<html><head><title>Title</title></head><body>Content</body></html>";
|
||||
$expected = "<!DOCTYPE html>\n<html><head><title>Title</title><meta http-equiv=\"refresh\" content=\"300\">".
|
||||
@@ -69,6 +86,9 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
self::assertSame($expected, $this->generated_page_html($html));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove script tags.
|
||||
*/
|
||||
public function test_removescripttags() {
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><script type="text/javascript" src="http://xyz"></script><title>Title</title></head>'.
|
||||
@@ -76,24 +96,30 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
maintenance_static_page::create_from_html($html)->generate();
|
||||
|
||||
$generated = $this->generated_page_html($html);
|
||||
self::assertNotContains('<script', $generated);
|
||||
self::assertStringNotContainsString('<script', $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove script tags.
|
||||
*/
|
||||
public function test_updatelinkstylesheet() {
|
||||
$localcsslink = $this->get_fixture_path('simple.css');
|
||||
$localcsslink = $this->get_fixture_path_location('simple.css');
|
||||
$externalcsslink = 'http://google.com/coolstuff.css';
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<body>Content<link rel="stylesheet" href="'.$externalcsslink.'"></body></html>';
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertContains('http://www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
self::assertNotContains($localcsslink, $generated);
|
||||
self::assertContains($externalcsslink, $generated);
|
||||
self::assertStringContainsString('www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
self::assertStringNotContainsString($localcsslink, $generated);
|
||||
self::assertStringContainsString($externalcsslink, $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update link style sheet urls.
|
||||
*/
|
||||
public function test_updatelinkstylesheet_urls() {
|
||||
$localcsslink = $this->get_fixture_path('withurls.css');
|
||||
$localcsslink = $this->get_fixture_path_location('withurls.css');
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<body>Content</body></html>';
|
||||
@@ -101,14 +127,17 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
$page->generate();
|
||||
|
||||
// Check for css file.
|
||||
self::assertFileExists($page->get_io()->get_resources_folder().'/622ef6e83acfcb274cdf37bdb3bffa0923f9a7ad.dGV4dC9wbGFpbg');
|
||||
self::assertFileExists($page->get_io()->get_resources_folder().'/b09bd4b66cc3964d5fc5978752fc554f5666daa3.dGV4dC9wbGFpbg');
|
||||
|
||||
// Check for catalyst.png file referenced in url(..) of css.
|
||||
self::assertFileExists($page->get_io()->get_resources_folder().'/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update link style sheet urls quoted.
|
||||
*/
|
||||
public function test_updatelinkstylesheet_urls_quoted() {
|
||||
$localcsslink = $this->get_fixture_path('withurls-quoted.css');
|
||||
$localcsslink = $this->get_fixture_path_location('withurls-quoted.css');
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<body>Content</body></html>';
|
||||
@@ -116,14 +145,17 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
$page->generate();
|
||||
|
||||
// Check for css file.
|
||||
self::assertFileExists($page->get_io()->get_resources_folder().'/1d84b6d321fef780237f84834b7316c079221a31.dGV4dC9wbGFpbg');
|
||||
self::assertFileExists($page->get_io()->get_resources_folder().'/2ec04228cc8bb37782f511aaeb01ee553cc884a4.dGV4dC9wbGFpbg');
|
||||
|
||||
// Check for catalyst.png file referenced in url(..) of css.
|
||||
self::assertFileExists($page->get_io()->get_resources_folder().'/ff7f7f87a26a908fc72930eaefb6b57306361d16.aW1hZ2UvcG5n');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update link style sheet urls with sub dir.
|
||||
*/
|
||||
public function test_updatelinkstylesheet_urls_subdir() {
|
||||
$localcsslink = $this->get_fixture_path('subdir/withurls-subdir.css');
|
||||
$localcsslink = $this->get_fixture_path_location('subdir/withurls-subdir.css');
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><link href="'.$localcsslink.'" rel="stylesheet" /><title>Title</title></head>'.
|
||||
'<body>Content</body></html>';
|
||||
@@ -137,32 +169,96 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
self::assertFileExists($page->get_io()->get_resources_folder().'/a02a8a442fa82d5205ffb24722d9df7f35161f56.dGV4dC9wbGFpbg');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update images to file.php style link.
|
||||
*/
|
||||
public function test_updateimages() {
|
||||
$localimglink = $this->get_fixture_path('catalyst.png');
|
||||
$localimglink = $this->get_fixture_path_location('catalyst.png');
|
||||
$externalimglink = 'http://google.com/coolstyle.css';
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><title>Title</title></head>'.
|
||||
'<body><img src="'.$localimglink.'">Content<img src="'.$externalimglink.'" /></body></html>';
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertContains('http://www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
self::assertNotContains($localimglink, $generated);
|
||||
self::assertContains($externalimglink, $generated);
|
||||
self::assertStringContainsString('www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
self::assertStringNotContainsString($localimglink, $generated);
|
||||
self::assertStringContainsString($externalimglink, $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update favicon to file.php style link.
|
||||
*/
|
||||
public function test_updatelinkfavicon() {
|
||||
$link = $this->get_fixture_path('catalyst.png');
|
||||
$link = $this->get_fixture_path_location('catalyst.png');
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><title>Title</title><link rel="shortcut icon" href="'.$link.'""></head>'.
|
||||
'<body>Content</body></html>';
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertNotContains($link, $generated);
|
||||
self::assertContains('http://www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
self::assertStringNotContainsString($link, $generated);
|
||||
self::assertStringContainsString('www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Data provider for test_update_inline_background_images
|
||||
* @return array
|
||||
*/
|
||||
public static function update_inline_background_images_provider(): array {
|
||||
return [
|
||||
// Empty string.
|
||||
["", false],
|
||||
// URLs that should be retrieved.
|
||||
["color: #FF00FF; background: lightblue url(/pluginfile.php/1/theme_custom/banner/251298630/0001.png) no-repeat", true],
|
||||
["background: lightblue
|
||||
url(https://www.example.com/moodle/pluginfile.php/1/theme_custom/banner/251298630/0001.png) no-repeat", true],
|
||||
["background:url('https://www.example.com/moodle/pluginfile.php/1/theme_custom/banner/251298630/0001.png')", true],
|
||||
["background-image : url( /pix/help.png);", true],
|
||||
["background-image: url ('/pix/help.png')", true],
|
||||
// URLs that should not be retrieved.
|
||||
["background-image:url(data:image/gif;base64,R0lGODlhYADIAP=)", false],
|
||||
["background-image:url('data:image/gif;base64,R0lGODlhYADIAP=')", false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests update_inline_background_images() method to update the background images.
|
||||
*
|
||||
* @dataProvider update_inline_background_images_provider
|
||||
* @param string $stylecontent Content of the style to test
|
||||
* @param bool $rewrite Flag if URL should be rewritten
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function test_update_inline_background_images($stylecontent, $rewrite) {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
$generator = new maintenance_static_page_generator(new DOMDocument(), new maintenance_static_page_io());
|
||||
|
||||
$html = '<!DOCTYPE html>\n'.
|
||||
'<html><head><title>Title</title></head>'.
|
||||
'<body><div style="'.$stylecontent.'">Content</div></body></html>';
|
||||
|
||||
// Temporarily disable debugging to prevent errors because file does not exist.
|
||||
$debuglevel = $CFG->debug;
|
||||
$CFG->debug = '';
|
||||
$generated = $this->generated_page_html($html);
|
||||
// Restore debugging level.
|
||||
$CFG->debug = $debuglevel;
|
||||
$matches = $generator->get_url_from_inline_style($stylecontent);
|
||||
if ($rewrite) {
|
||||
self::assertStringNotContainsString($matches[1], $generated);
|
||||
self::assertStringContainsString('www.example.com/moodle/auth/outage/file.php?file=', $generated);
|
||||
self::assertIsArray($matches);
|
||||
} else {
|
||||
self::assertStringContainsString($stylecontent, $generated);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test update preview path to file.php style link.
|
||||
*/
|
||||
public function test_previewpath() {
|
||||
$link = $this->get_fixture_path('catalyst.png');
|
||||
$link = $this->get_fixture_path_location('catalyst.png');
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><title>Title</title><link rel="shortcut icon" href="'.$link.'""></head>'.
|
||||
'<body>Content</body></html>';
|
||||
@@ -171,8 +267,8 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
$page->generate();
|
||||
$generated = trim(file_get_contents($page->get_io()->get_template_file()));
|
||||
|
||||
self::assertNotContains($link, $generated);
|
||||
self::assertContains('http://www.example.com/moodle/auth/outage/file.php?file=preview%2F', $generated);
|
||||
self::assertStringNotContainsString($link, $generated);
|
||||
self::assertStringContainsString('www.example.com/moodle/auth/outage/file.php?file=preview%2F', $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -208,13 +304,21 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
touch($file);
|
||||
self::assertFileExists($file);
|
||||
maintenance_static_page::create_from_outage(null)->generate();
|
||||
self::assertFileNotExists($file);
|
||||
// Backwards compatibility with older PHPUnit - use old assertFile method.
|
||||
if (method_exists($this, 'assertFileDoesNotExist')) {
|
||||
self::assertFileDoesNotExist($file);
|
||||
} else {
|
||||
self::assertFileNotExists($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests created file.
|
||||
*/
|
||||
public function test_createdfile() {
|
||||
global $CFG;
|
||||
|
||||
$link = $this->get_fixture_path('catalyst.png');
|
||||
$link = $this->get_fixture_path_location('catalyst.png');
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
'<html><head><title>Title</title></head>'.
|
||||
'<body>Content<img src="'.$link.'" /></body></html>';
|
||||
@@ -227,45 +331,77 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
|
||||
// We can still assert the contents really match, not just the hash.
|
||||
$found = file_get_contents($file);
|
||||
$expected = file_get_contents($CFG->dirroot.'/auth/outage/tests/phpunit/local/controllers/fixtures/catalyst.png');
|
||||
$expected = file_get_contents(__DIR__.'/fixtures/catalyst.png');
|
||||
self::assertSame($found, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a fixture file for this test case.
|
||||
*
|
||||
* @param $file
|
||||
* @param string $file file name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function get_fixture_path($file) {
|
||||
return (string)new moodle_url('/auth/outage/tests/phpunit/local/controllers/fixtures/'.$file);
|
||||
private function get_fixture_path_location($file) {
|
||||
return (string)new \moodle_url('/auth/outage/tests/fixtures/'.$file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test saving empty string for template file.
|
||||
*/
|
||||
public function test_invalid_string_saving_template_empty() {
|
||||
$io = new maintenance_static_page_io();
|
||||
$this->set_expected_exception('coding_exception');
|
||||
$io->save_template_file('');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test saving non string for template file.
|
||||
*/
|
||||
public function test_invalid_string_saving_template_nostring() {
|
||||
$io = new maintenance_static_page_io();
|
||||
$this->set_expected_exception('coding_exception');
|
||||
$io->save_template_file(50);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get url for file.
|
||||
*/
|
||||
public function test_get_url_for_file() {
|
||||
$io = new maintenance_static_page_io();
|
||||
self::assertSame('http://www.example.com/moodle/auth/outage/file.php?file=img.png', $io->get_url_for_file('img.png'));
|
||||
self::assertStringContainsString(
|
||||
'www.example.com/moodle/auth/outage/file.php?file=img.png',
|
||||
$io->get_url_for_file('img.png')
|
||||
);
|
||||
}
|
||||
|
||||
public function test_is_url() {
|
||||
self::assertTrue(maintenance_static_page_io::is_url('http://catalyst.net.nz'));
|
||||
self::assertTrue(maintenance_static_page_io::is_url('https://www.catalyst-au.net/'));
|
||||
self::assertFalse(maintenance_static_page_io::is_url('/homepage'));
|
||||
self::assertFalse(maintenance_static_page_io::is_url('file://homepage'));
|
||||
/**
|
||||
* Return array of url data provider and true or false.
|
||||
*/
|
||||
public static function is_url_dataprovider(): array {
|
||||
return [
|
||||
[true, 'http://catalyst.net.nz'],
|
||||
[true, 'https://www.catalyst-au.net/'],
|
||||
[false, '/homepage'],
|
||||
[false, 'file://homepage'],
|
||||
[true, '//catalyst-au.net/img/test.jpg'],
|
||||
[false, '://www.catalyst-au.net/img/test.jpg'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if it is url
|
||||
* @dataProvider is_url_dataprovider
|
||||
* @param string $result expected result
|
||||
* @param string $url url to be checked
|
||||
*/
|
||||
public function test_is_url($result, $url) {
|
||||
self::assertEquals($result, maintenance_static_page_io::is_url($url));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test file get_data.
|
||||
*/
|
||||
public function test_file_get_data() {
|
||||
$file = __DIR__.'/fixtures/catalyst.png';
|
||||
$found = maintenance_static_page_io::file_get_data($file);
|
||||
@@ -273,19 +409,59 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
self::assertSame('image/png', $found['mime']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test invalid file get_data.
|
||||
*/
|
||||
public function test_file_get_data_invalidfile() {
|
||||
$found = maintenance_static_page_io::file_get_data(__DIR__.'/fixtures/invalidfile');
|
||||
self::assertSame('', $found['contents']);
|
||||
self::assertSame('unknown', $found['mime']);
|
||||
self::assertCount(1, phpunit_util::get_debugging_messages());
|
||||
phpunit_util::reset_debugging();
|
||||
self::assertCount(1, $this->getDebuggingMessages());
|
||||
$this->resetDebugging();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test invalid file get_data.
|
||||
*/
|
||||
public function test_file_get_data_invalidfilename() {
|
||||
$this->set_expected_exception('coding_exception');
|
||||
maintenance_static_page_io::file_get_data(200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test file_get_data with curlsecurityblockedhosts.
|
||||
* We will use an external URL to test passing ignoresecurity inside of file_get_data works,
|
||||
* ideally in real code we should only be calling file_get_data with internal URLs.
|
||||
*/
|
||||
public function test_file_get_data_curlsecurityblockedhosts() {
|
||||
global $CFG, $USER;
|
||||
|
||||
$testhtml = $this->getExternalTestFileUrl('/test.html');
|
||||
$url = new \moodle_url($testhtml);
|
||||
$host = $url->get_host();
|
||||
set_config('curlsecurityblockedhosts', $host); // Blocks $host.
|
||||
|
||||
// Test a regular curl with the default security enabled does in fact get blocked.
|
||||
$curl = new \curl();
|
||||
$contents = $curl->get($testhtml);
|
||||
$expected = $curl->get_security()->get_blocked_url_string();
|
||||
self::assertSame($expected, $contents);
|
||||
self::assertSame(0, $curl->get_errno());
|
||||
if ($CFG->branch >= 403) {
|
||||
self::assertDebuggingCalled(
|
||||
"Blocked $testhtml: The URL is blocked. [user {$USER->id}]", DEBUG_NONE);
|
||||
}
|
||||
|
||||
// Test file_get_data does return the page and isn't blocked by security.
|
||||
$found = maintenance_static_page_io::file_get_data($url->out());
|
||||
$expected = '47250a973d1b88d9445f94db4ef2c97a';
|
||||
self::assertSame($expected, md5($found['contents']));
|
||||
self::assertSame('text/html', $found['mime']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove css selector.
|
||||
*/
|
||||
public function test_remove_css_selector() {
|
||||
$this->resetAfterTest(true);
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
@@ -294,10 +470,13 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
set_config('remove_selectors', '.removeme', 'auth_outage');
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertNotContains('removeme', $generated);
|
||||
self::assertNotContains('Goodbye cruel world', $generated);
|
||||
self::assertStringNotContainsString('removeme', $generated);
|
||||
self::assertStringNotContainsString('Goodbye cruel world', $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove css selector id.
|
||||
*/
|
||||
public function test_remove_css_selector_id() {
|
||||
$this->resetAfterTest(true);
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
@@ -306,10 +485,13 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
set_config('remove_selectors', '#removeme', 'auth_outage');
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertNotContains('removeme', $generated);
|
||||
self::assertNotContains('Goodbye cruel world', $generated);
|
||||
self::assertStringNotContainsString('removeme', $generated);
|
||||
self::assertStringNotContainsString('Goodbye cruel world', $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove css selector with multi lines.
|
||||
*/
|
||||
public function test_remove_css_selector_with_multiline() {
|
||||
$this->resetAfterTest(true);
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
@@ -321,11 +503,14 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
set_config('remove_selectors', ".removeme\n.deleteme", 'auth_outage');
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertNotContains('removeme', $generated);
|
||||
self::assertNotContains('deleteme', $generated);
|
||||
self::assertNotContains('Goodbye cruel world', $generated);
|
||||
self::assertStringNotContainsString('removeme', $generated);
|
||||
self::assertStringNotContainsString('deleteme', $generated);
|
||||
self::assertStringNotContainsString('Goodbye cruel world', $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove css selector needs trim.
|
||||
*/
|
||||
public function test_remove_css_selector_needing_trim() {
|
||||
$this->resetAfterTest(true);
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
@@ -337,11 +522,14 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
set_config('remove_selectors', " .removeme \n .deleteme ", 'auth_outage');
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertNotContains('removeme', $generated);
|
||||
self::assertNotContains('deleteme', $generated);
|
||||
self::assertNotContains('Goodbye cruel world', $generated);
|
||||
self::assertStringNotContainsString('removeme', $generated);
|
||||
self::assertStringNotContainsString('deleteme', $generated);
|
||||
self::assertStringNotContainsString('Goodbye cruel world', $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove css selector with empty line.
|
||||
*/
|
||||
public function test_remove_css_selector_with_empty_line() {
|
||||
$this->resetAfterTest(true);
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
@@ -353,11 +541,14 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
set_config('remove_selectors', "\n\n.removeme\n\n\n\n.deleteme\n\n", 'auth_outage');
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertNotContains('removeme', $generated);
|
||||
self::assertNotContains('deleteme', $generated);
|
||||
self::assertNotContains('Goodbye cruel world', $generated);
|
||||
self::assertStringNotContainsString('removeme', $generated);
|
||||
self::assertStringNotContainsString('deleteme', $generated);
|
||||
self::assertStringNotContainsString('Goodbye cruel world', $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test remove css selector with invalid id.
|
||||
*/
|
||||
public function test_remove_css_selector_with_invalid_id() {
|
||||
$this->resetAfterTest(true);
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
@@ -366,10 +557,13 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
set_config('remove_selectors', '#invalidid', 'auth_outage');
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertContains('removeme', $generated);
|
||||
self::assertContains('Goodbye cruel world', $generated);
|
||||
self::assertStringContainsString('removeme', $generated);
|
||||
self::assertStringContainsString('Goodbye cruel world', $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test meta refresh 5 minutes.
|
||||
*/
|
||||
public function test_meta_refresh_5minutes() {
|
||||
$this->resetAfterTest(true);
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
@@ -378,9 +572,12 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
set_config('remove_selectors', '#invalidid', 'auth_outage');
|
||||
$generated = $this->generated_page_html($html);
|
||||
|
||||
self::assertContains('<meta http-equiv="refresh" content="300">', $generated);
|
||||
self::assertStringContainsString('<meta http-equiv="refresh" content="300">', $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test meta refresh maximum 5 minutes.
|
||||
*/
|
||||
public function test_meta_refresh_maximum_5seconds() {
|
||||
$this->resetAfterTest(true);
|
||||
$html = "<!DOCTYPE html>\n".
|
||||
@@ -393,6 +590,47 @@ class maintenance_static_page_test extends auth_outage_base_testcase {
|
||||
$generated = trim(file_get_contents($page->get_io()->get_template_file()));
|
||||
return $generated;
|
||||
|
||||
self::assertContains('<meta http-equiv="refresh" content="5">', $generated);
|
||||
self::assertStringContainsString('<meta http-equiv="refresh" content="5">', $generated);
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider for test_get_urls_from_stylesheet
|
||||
* @return array
|
||||
*/
|
||||
public static function get_urls_from_stylesheet_provider(): array {
|
||||
return [
|
||||
// Empty string.
|
||||
["", 0],
|
||||
// URLs that should be retrieved.
|
||||
["background:url(/theme/image.php/_s/boost/core/1581292565/t/expanded)", 1],
|
||||
["background:url('/theme/image.php/_s/boost/core/1581292565/t/expanded')", 1],
|
||||
["src:url(\"/theme/font.php/boost/core/1581292565/fontawesome-webfont.eot?#iefix&v=4.7.0\")", 1],
|
||||
["background-image:url(pix/vline-rtl.gif)", 1],
|
||||
// URLs that should not be retrieved.
|
||||
["background-image:url(data:image/gif;base64,R0lGODlhYADIAP=)", 0],
|
||||
["background-image:url('data:image/gif;base64,R0lGODlhYADIAP=')", 0],
|
||||
["background-image:url(\"data:image/svg+xml;charset=utf8,%3Csvg xmlns=\'http://www.w3.org/2000/svg\'\")", 0],
|
||||
// Combination of URLs used above.
|
||||
["background-image:url(pix/vline-rtl.gif) background:url(/theme/image.php/_s/boost/core/158/t/expanded)", 2],
|
||||
["background-image:url(data:image/gif;base64,R0lG=)src:url(\"/theme/font.php/fontawesome-webfont.eot\")", 1],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests get_urls_from_stylesheet() method to get all appropriate URLS from the file.
|
||||
*
|
||||
* @dataProvider get_urls_from_stylesheet_provider
|
||||
* @param string $filecontent Content of the file
|
||||
* @param int $count Expected quantity of found URLs
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function test_get_urls_from_stylesheet($filecontent, $count) {
|
||||
$this->resetAfterTest(true);
|
||||
$generator = new maintenance_static_page_generator(new DOMDocument(), new maintenance_static_page_io());
|
||||
$matches = $generator->get_urls_from_stylesheet($filecontent);
|
||||
|
||||
self::assertIsArray($matches);
|
||||
self::assertCount(2, $matches);
|
||||
self::assertCount($count, $matches[1]);
|
||||
}
|
||||
}
|
||||
@@ -23,10 +23,8 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
use auth_outage\dml\outagedb;
|
||||
|
||||
/**
|
||||
* events_test tests class.
|
||||
@@ -38,8 +36,9 @@ defined('MOODLE_INTERNAL') || die();
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \auth_outage\dml\outagedb
|
||||
*/
|
||||
class events_test extends advanced_testcase {
|
||||
class dml_events_test extends \core_phpunit\testcase {
|
||||
/**
|
||||
* @var outage|null Outage used in the tests.
|
||||
*/
|
||||
@@ -57,7 +56,7 @@ class events_test extends advanced_testcase {
|
||||
public function test_save() {
|
||||
global $DB;
|
||||
self::setAdminUser();
|
||||
$this->resetAfterTest(false);
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Save new outage.
|
||||
$now = time();
|
||||
@@ -90,7 +89,20 @@ class events_test extends advanced_testcase {
|
||||
global $DB;
|
||||
|
||||
self::setAdminUser();
|
||||
$this->resetAfterTest(false);
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Save new outage.
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => false,
|
||||
'warntime' => $now - 60,
|
||||
'starttime' => 60,
|
||||
'stoptime' => 120,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
$outage->id = outagedb::save($outage);
|
||||
self::$outage = $outage;
|
||||
|
||||
self::$outage->starttime += 10;
|
||||
outagedb::save(self::$outage);
|
||||
@@ -117,6 +129,19 @@ class events_test extends advanced_testcase {
|
||||
self::setAdminUser();
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
// Save new outage.
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => false,
|
||||
'warntime' => $now - 60,
|
||||
'starttime' => 60,
|
||||
'stoptime' => 120,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
$outage->id = outagedb::save($outage);
|
||||
self::$outage = $outage;
|
||||
|
||||
outagedb::delete(self::$outage->id);
|
||||
|
||||
// Should not exist.
|
||||
@@ -26,6 +26,7 @@
|
||||
*/
|
||||
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\dml\outagedb;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/base_testcase.php');
|
||||
@@ -39,9 +40,14 @@ require_once(__DIR__.'/base_testcase.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
* @covers \auth_outage\dml\outagedb
|
||||
*/
|
||||
class installation_test extends auth_outage_base_testcase {
|
||||
class dml_installation_test extends base_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if plugin cleans up data after uninstall.
|
||||
*
|
||||
@@ -64,15 +70,19 @@ class installation_test extends auth_outage_base_testcase {
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
\auth_outage\dml\outagedb::save($outage);
|
||||
ob_start();
|
||||
outagedb::save($outage);
|
||||
$text = trim(ob_get_contents());
|
||||
ob_end_clean();
|
||||
self::assertStringContainsString('Update maintenance mode configuration', $text);
|
||||
self::assertSame(1, $DB->count_records_select('event', "eventtype = 'auth_outage'", null));
|
||||
|
||||
// Uninstall plugin.
|
||||
require_once($CFG->libdir.'/adminlib.php');
|
||||
$progress = new progress_trace_buffer(new text_progress_trace(), false);
|
||||
core_plugin_manager::instance()->uninstall_plugin('auth_outage', $progress);
|
||||
$progress = new \progress_trace_buffer(new \text_progress_trace(), false);
|
||||
\core_plugin_manager::instance()->uninstall_plugin('auth_outage', $progress);
|
||||
$progress->finished();
|
||||
self::assertContains('++ Success ++', $progress->get_buffer());
|
||||
self::assertStringContainsString('++ Success ++', $progress->get_buffer());
|
||||
|
||||
// Check ...
|
||||
self::assertSame(0, $DB->count_records_select('event', "eventtype = 'auth_outage'", null),
|
||||
@@ -23,11 +23,11 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\dml\outagedb;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/../base_testcase.php');
|
||||
require_once(__DIR__.'/base_testcase.php');
|
||||
|
||||
/**
|
||||
* outagedb_test tests class.
|
||||
@@ -36,9 +36,14 @@ require_once(__DIR__.'/../base_testcase.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
* @covers \auth_outage\dml\outagedb
|
||||
*/
|
||||
class outagedb_test extends auth_outage_base_testcase {
|
||||
class dml_outagedb_test extends base_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array of ids in from the given outages array.
|
||||
* @param outage[] $outages An array of outages.
|
||||
@@ -79,7 +84,7 @@ class outagedb_test extends auth_outage_base_testcase {
|
||||
/**
|
||||
* Ensure DB tests run as admin.
|
||||
*/
|
||||
public function setUp() {
|
||||
public function setUp(): void {
|
||||
parent::setUp();
|
||||
$this->setAdminUser();
|
||||
}
|
||||
@@ -245,6 +250,15 @@ class outagedb_test extends auth_outage_base_testcase {
|
||||
$activeid = self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
|
||||
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
|
||||
|
||||
$activeid = self::saveoutage(false, $now, -2, 0, 2, 'An outage starts now.');
|
||||
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
|
||||
|
||||
self::saveoutage(false, $now, -2, 0, -1, 'Invalid outage.');
|
||||
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
|
||||
|
||||
self::saveoutage(false, $now, -2, 0, 0, 'Invalid outage.');
|
||||
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
|
||||
|
||||
self::saveoutage(false, $now, -1, 2, 3,
|
||||
'Another outage in warning period, but ignored as it starts after the previous one.');
|
||||
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
|
||||
@@ -428,8 +442,8 @@ class outagedb_test extends auth_outage_base_testcase {
|
||||
public function test_finish_now_notfound() {
|
||||
$this->resetAfterTest(true);
|
||||
outagedb::finish(1);
|
||||
self::assertCount(1, phpunit_util::get_debugging_messages());
|
||||
phpunit_util::reset_debugging();
|
||||
self::assertCount(1, $this->getDebuggingMessages());
|
||||
$this->resetDebugging();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -450,8 +464,8 @@ class outagedb_test extends auth_outage_base_testcase {
|
||||
self::assertTrue(!$outage->is_ongoing($time));
|
||||
|
||||
outagedb::finish($id, $time);
|
||||
self::assertCount(1, phpunit_util::get_debugging_messages());
|
||||
phpunit_util::reset_debugging();
|
||||
self::assertCount(1, $this->getDebuggingMessages());
|
||||
$this->resetDebugging();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 2.8 KiB |
7
tests/fixtures/withurls-quoted.css
vendored
Normal file
7
tests/fixtures/withurls-quoted.css
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
a {
|
||||
font-size: 200%;
|
||||
}
|
||||
|
||||
div {
|
||||
background-image: url('/moodle/auth/outage/tests/fixtures/catalyst.png');
|
||||
}
|
||||
7
tests/fixtures/withurls.css
vendored
Normal file
7
tests/fixtures/withurls.css
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
a {
|
||||
font-size: 200%;
|
||||
}
|
||||
|
||||
div {
|
||||
background-image: url(/moodle/auth/outage/tests/fixtures/catalyst.png);
|
||||
}
|
||||
@@ -23,13 +23,13 @@
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use auth_outage\form\outage\delete;
|
||||
use auth_outage\form\outage\edit;
|
||||
use auth_outage\form\outage\finish;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\form\outage\delete;
|
||||
use auth_outage\form\outage\finish;
|
||||
use auth_outage\form\outage\edit;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/../base_testcase.php');
|
||||
require_once(__DIR__.'/base_testcase.php');
|
||||
|
||||
/**
|
||||
* forms_test test class.
|
||||
@@ -38,8 +38,14 @@ require_once(__DIR__.'/../base_testcase.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \auth_outage\form\outage\edit
|
||||
*/
|
||||
class forms_test extends auth_outage_base_testcase {
|
||||
class forms_test extends base_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a delete form.
|
||||
*/
|
||||
@@ -146,8 +152,8 @@ class forms_test extends auth_outage_base_testcase {
|
||||
$_POST['description'] = ['text' => 'The <b>description</b>.', 'format' => '2'];
|
||||
$edit = new edit();
|
||||
self::assertNull($edit->get_data());
|
||||
self::assertCount(1, phpunit_util::get_debugging_messages());
|
||||
phpunit_util::reset_debugging();
|
||||
self::assertCount(1, $this->getDebuggingMessages());
|
||||
$this->resetDebugging();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,6 +204,10 @@ class forms_test extends auth_outage_base_testcase {
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Skip tests for moodle below 30.
|
||||
* @param string $reason reason to be filled
|
||||
*/
|
||||
private function skip_because_moodle_is_below_30($reason = '') {
|
||||
global $CFG;
|
||||
|
||||
@@ -14,18 +14,9 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* tests for lib.php
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2017 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/base_testcase.php');
|
||||
require_once(__DIR__.'/../../lib.php');
|
||||
require_once(__DIR__.'/../lib.php');
|
||||
|
||||
/**
|
||||
* tests for lib.php
|
||||
@@ -34,9 +25,12 @@ require_once(__DIR__.'/../../lib.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2017 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
* @covers ::auth_outage_get_climaintenance_resource_file
|
||||
*/
|
||||
class lib_test extends auth_outage_base_testcase {
|
||||
class lib_test extends \core_phpunit\testcase {
|
||||
/**
|
||||
* Test this plugin gets climaintenance resource file.
|
||||
*/
|
||||
public function test_auth_outage_get_climaintenance_resource_file_resolves_a_file() {
|
||||
global $CFG;
|
||||
$dir = $CFG->dataroot.'/auth_outage/climaintenance';
|
||||
@@ -88,6 +82,9 @@ class lib_test extends auth_outage_base_testcase {
|
||||
self::assertSame($realfile, $actual);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test this plugin gets climaintenance resource file and prevents path traversal attack.
|
||||
*/
|
||||
public function test_auth_outage_get_climaintenance_resource_file_prevent_path_traversal() {
|
||||
global $CFG;
|
||||
|
||||
@@ -14,19 +14,10 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* outage_test test class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use auth_outage\local\outage;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/../base_testcase.php');
|
||||
require_once(__DIR__.'/base_testcase.php');
|
||||
|
||||
/**
|
||||
* outage_test test class.
|
||||
@@ -35,9 +26,14 @@ require_once(__DIR__.'/../base_testcase.php');
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
* @covers \auth_outage\local\outage
|
||||
*/
|
||||
class outage_test extends auth_outage_base_testcase {
|
||||
class outage_test extends base_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor.
|
||||
*/
|
||||
@@ -55,7 +51,7 @@ class outage_test extends auth_outage_base_testcase {
|
||||
* Tests the constructor, giving data as an object.
|
||||
*/
|
||||
public function test_constructor_object() {
|
||||
$obj = new stdClass();
|
||||
$obj = new \stdClass();
|
||||
$obj->id = 1;
|
||||
$obj->autostart = true;
|
||||
$obj->warntime = 2;
|
||||
@@ -281,11 +277,11 @@ class outage_test extends auth_outage_base_testcase {
|
||||
'description' => 'Description {{start}} {{stop}} {{duration}}',
|
||||
]);
|
||||
$title = $outage->get_title();
|
||||
self::assertNotContains('{', $title);
|
||||
self::assertNotContains('}', $title);
|
||||
self::assertStringNotContainsString('{', $title);
|
||||
self::assertStringNotContainsString('}', $title);
|
||||
$description = $outage->get_description();
|
||||
self::assertNotContains('{', $description);
|
||||
self::assertNotContains('}', $description);
|
||||
self::assertStringNotContainsString('{', $description);
|
||||
self::assertStringNotContainsString('}', $description);
|
||||
}
|
||||
|
||||
/**
|
||||
809
tests/outagelib_test.php
Normal file
809
tests/outagelib_test.php
Normal file
@@ -0,0 +1,809 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\outagelib;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
global $CFG;
|
||||
require_once($CFG->libdir.'/adminlib.php');
|
||||
require_once(__DIR__.'/base_testcase.php');
|
||||
|
||||
/**
|
||||
* outagelib_test test class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @covers \auth_outage\local\outagelib
|
||||
*/
|
||||
class outagelib_test extends base_testcase {
|
||||
|
||||
public function tearDown(): void {
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if maintenance message is disabled as needed.
|
||||
*/
|
||||
public function test_maintenancemessage() {
|
||||
$this->resetAfterTest(true);
|
||||
static::setAdminUser();
|
||||
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
|
||||
set_config('maintenance_message', 'A message.');
|
||||
ob_start();
|
||||
outagedb::save($outage);
|
||||
$text = trim(ob_get_contents());
|
||||
ob_end_clean();
|
||||
self::assertStringContainsString('Update maintenance mode configuration', $text);
|
||||
self::assertFalse((bool)get_config('moodle', 'maintenance_message'));
|
||||
self::assertCount(2, $this->getDebuggingMessages());
|
||||
$this->resetDebugging();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if maintenance later is removed if no outage set.
|
||||
*/
|
||||
public function test_maintenancelater_nonext() {
|
||||
$this->resetAfterTest(true);
|
||||
set_config('maintenance_later', time() + (60 * 60 * 24 * 7)); // In 1 week.
|
||||
self::assertNotEmpty(get_config('moodle', 'maintenance_later'));
|
||||
outagelib::prepare_next_outage();
|
||||
self::assertEmpty(get_config('moodle', 'maintenance_later'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check outagelib::inject() works as expected.
|
||||
*/
|
||||
public function test_inject() {
|
||||
global $OUTPUT;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
ob_start();
|
||||
$outage->id = outagedb::save($outage);
|
||||
$text = trim(ob_get_contents());
|
||||
ob_end_clean();
|
||||
self::assertStringContainsString('Update maintenance mode configuration', $text);
|
||||
|
||||
outagelib::reset_injectcalled();
|
||||
// Get full header to avoid interactions with other single inject plugins.
|
||||
$header1 = $OUTPUT->standard_top_of_body_html();
|
||||
self::assertStringContainsString('<style>', $header1);
|
||||
self::assertStringContainsString('<script>', $header1);
|
||||
|
||||
// Should not inject more than once.
|
||||
$size = strlen($OUTPUT->standard_top_of_body_html());
|
||||
self::assertSame($size, strlen($OUTPUT->standard_top_of_body_html()));
|
||||
// Check styles aren't reinjected.
|
||||
self::assertStringNotContainsString('<style>', $OUTPUT->standard_top_of_body_html());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check outagelib::inject() will not break the page if something goes wrong.
|
||||
*/
|
||||
public function test_inject_broken() {
|
||||
$_GET = ['auth_outage_break_code' => '1'];
|
||||
outagelib::reset_injectcalled();
|
||||
$header = outagelib::get_inject_code();
|
||||
self::assertCount(2, $this->getDebuggingMessages());
|
||||
$this->resetDebugging();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if injection works with preview.
|
||||
*/
|
||||
public function test_inject_preview() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
ob_start();
|
||||
$outage->id = outagedb::save($outage);
|
||||
$text = trim(ob_get_contents());
|
||||
ob_end_clean();
|
||||
self::assertStringContainsString('Update maintenance mode configuration', $text);
|
||||
|
||||
$_GET = ['auth_outage_preview' => (string)$outage->id];
|
||||
|
||||
outagelib::reset_injectcalled();
|
||||
$header = outagelib::get_inject_code();
|
||||
self::assertStringContainsString('<style>', $header);
|
||||
self::assertStringContainsString('<script>', $header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if injection works with invalid preview without stopping the page.
|
||||
*/
|
||||
public function test_inject_preview_notfound() {
|
||||
global $CFG;
|
||||
|
||||
$_GET = ['auth_outage_preview' => '1'];
|
||||
// Should not throw exception or halt anything, silently ignore it.
|
||||
outagelib::reset_injectcalled();
|
||||
$header = outagelib::get_inject_code();
|
||||
self::assertEmpty($header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test injection with preview and delta.
|
||||
*/
|
||||
public function test_inject_preview_withdelta() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
ob_start();
|
||||
$outage->id = outagedb::save($outage);
|
||||
$text = trim(ob_get_contents());
|
||||
ob_end_clean();
|
||||
self::assertStringContainsString('Update maintenance mode configuration', $text);
|
||||
$_GET = ['auth_outage_preview' => (string)$outage->id, 'auth_outage_delta' => '500'];
|
||||
outagelib::reset_injectcalled();
|
||||
$header = outagelib::get_inject_code();
|
||||
// Still empty, delta is too high (outage ended).
|
||||
self::assertEmpty($header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test injection without active outage.
|
||||
*/
|
||||
public function test_inject_noactive() {
|
||||
outagelib::reset_injectcalled();
|
||||
outagelib::get_inject_code();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if get config works without getting defaults.
|
||||
*/
|
||||
public function test_get_config() {
|
||||
$this->resetAfterTest(true);
|
||||
$keys = [
|
||||
'css',
|
||||
'default_autostart',
|
||||
'default_description',
|
||||
'default_duration',
|
||||
'default_title',
|
||||
'default_warning_duration',
|
||||
'allowedips',
|
||||
'remove_selectors',
|
||||
];
|
||||
// Set config with values.
|
||||
foreach ($keys as $k) {
|
||||
set_config($k, $k.'_value', 'auth_outage');
|
||||
}
|
||||
// Ensure it is not using any defaults.
|
||||
$config = outagelib::get_config();
|
||||
foreach ($keys as $k) {
|
||||
self::assertSame($config->$k, $k.'_value', 'auth_outage');
|
||||
}
|
||||
|
||||
set_config('allowedips_forced', 'allowedips_forced_value', 'auth_outage');
|
||||
$config = outagelib::get_config();
|
||||
self::assertSame($config->allowedips, "allowedips_value\nallowedips_forced_value", 'auth_outage');
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that config has key.
|
||||
*/
|
||||
public function test_config_keys() {
|
||||
$this->resetAfterTest(true);
|
||||
$keys = [
|
||||
'allowedips',
|
||||
'css',
|
||||
'default_autostart',
|
||||
'default_description',
|
||||
'default_duration',
|
||||
'default_title',
|
||||
'default_warning_duration',
|
||||
'remove_selectors',
|
||||
];
|
||||
$defaults = outagelib::get_config_defaults();
|
||||
foreach ($keys as $k) {
|
||||
self::assertArrayHasKey($k, $defaults);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if get config works getting defaults when needed.
|
||||
*/
|
||||
public function test_get_config_invalid() {
|
||||
$this->resetAfterTest(true);
|
||||
// Set config with invalid values.
|
||||
set_config('allowedips', " \n", 'auth_outage');
|
||||
set_config('css', " \n", 'auth_outage');
|
||||
set_config('default_autostart', " \n", 'auth_outage');
|
||||
set_config('default_description', " \n", 'auth_outage');
|
||||
set_config('default_duration', " \n", 'auth_outage');
|
||||
set_config('default_title', " \n", 'auth_outage');
|
||||
set_config('default_warning_duration', " \n", 'auth_outage');
|
||||
// Get defaults.
|
||||
$defaults = outagelib::get_config_defaults();
|
||||
$config = outagelib::get_config();
|
||||
// Ensure it is using all defaults.
|
||||
foreach ($defaults as $k => $v) {
|
||||
self::assertSame($v, $config->$k);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if outagelib::inject() does not inject on admin/settings.php?section=additionalhtml
|
||||
*/
|
||||
public function test_inject_settings() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
ob_start();
|
||||
$outage->id = outagedb::save($outage);
|
||||
$text = trim(ob_get_contents());
|
||||
ob_end_clean();
|
||||
self::assertStringContainsString('Update maintenance mode configuration', $text);
|
||||
|
||||
// Pretend we are there...
|
||||
$_SERVER['SCRIPT_FILENAME'] = '/var/www/alternativepath/admin/settings.php'; // Issue #88 regression test.
|
||||
$_SERVER['SCRIPT_NAME'] = '/admin/settings.php';
|
||||
$_GET['section'] = 'additionalhtml';
|
||||
outagelib::reset_injectcalled();
|
||||
$header = outagelib::get_inject_code();
|
||||
|
||||
self::assertEmpty($header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create maintenance php code
|
||||
*/
|
||||
public function test_createmaintenancephpcode() {
|
||||
global $CFG;
|
||||
$CFG->cookiehttponly = false;
|
||||
|
||||
$expected = <<<'EOT'
|
||||
<?php
|
||||
if ((time() >= 123) && (time() < 456)) {
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
define('MOODLE_INTERNAL', true);
|
||||
}
|
||||
require_once($CFG->dirroot.'/lib/moodlelib.php');
|
||||
if (file_exists($CFG->dirroot.'/lib/classes/ip_utils.php')) {
|
||||
require_once($CFG->dirroot.'/lib/classes/ip_utils.php');
|
||||
}
|
||||
// Put access key as a cookie if given. This stops the need to put it as a url param on every request.
|
||||
$urlaccesskey = optional_param('accesskey', null, PARAM_TEXT);
|
||||
$isphpunit = defined('PHPUNIT_TEST');
|
||||
|
||||
if (!empty($urlaccesskey) && !$isphpunit) {
|
||||
setcookie('auth_outage_accesskey', $urlaccesskey, time() + 86400, '/', '', true, false);
|
||||
}
|
||||
|
||||
// Use url access key if given, else the cookie, else null.
|
||||
$useraccesskey = $urlaccesskey ?: $_COOKIE['auth_outage_accesskey'] ?? null;
|
||||
|
||||
$ipblocked = !remoteip_in_list('hey\'\"you
|
||||
a.b.c.d
|
||||
e.e.e.e/20');
|
||||
$accesskeyblocked = $useraccesskey != '12345';
|
||||
$allowed = (true && !$accesskeyblocked) || (true && !$ipblocked);
|
||||
|
||||
if (!$allowed) {
|
||||
if (!$isphpunit) {
|
||||
header($_SERVER['SERVER_PROTOCOL'] . ' 503 Moodle under maintenance');
|
||||
header('Status: 503 Moodle under maintenance');
|
||||
header('Retry-After: 300');
|
||||
header('Content-type: text/html; charset=utf-8');
|
||||
header('X-UA-Compatible: IE=edge');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
header('Accept-Ranges: none');
|
||||
header('X-Moodle-Maintenance: manager');
|
||||
}
|
||||
|
||||
if (!$isphpunit && ((defined('AJAX_SCRIPT') && AJAX_SCRIPT) || (defined('WS_SERVER') && WS_SERVER))) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (true && $ipblocked) {
|
||||
echo '<!-- Blocked by ip, your ip: '.getremoteaddr('n/a').' -->';
|
||||
}
|
||||
|
||||
if (true && $accesskeyblocked) {
|
||||
echo '<!-- Blocked by missing or incorrect access key, access key given: '. $useraccesskey .' -->';
|
||||
}
|
||||
|
||||
if (!$isphpunit) {
|
||||
if (file_exists($CFG->dataroot.'/climaintenance.template.html')) {
|
||||
require($CFG->dataroot.'/climaintenance.template.html');
|
||||
exit(0);
|
||||
}
|
||||
// The file above should always exist, but just in case...
|
||||
die('We are currently under maintentance, please try again later.');
|
||||
}
|
||||
}
|
||||
}
|
||||
EOT;
|
||||
$found = outagelib::create_climaintenancephp_code(123, 456, "hey'\"you\na.b.c.d\ne.e.e.e/20", '12345');
|
||||
self::assertSame($expected, $found);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create maintenance php code without age
|
||||
*
|
||||
* @param string $configkey The key of the config.
|
||||
* @dataProvider createmaintenancephpcode_withoutage_provider
|
||||
*/
|
||||
public function test_createmaintenancephpcode_withoutage($configkey) {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
$CFG->cookiehttponly = false;
|
||||
|
||||
$expected = <<<'EOT'
|
||||
<?php
|
||||
if ((time() >= 123) && (time() < 456)) {
|
||||
if (!defined('MOODLE_INTERNAL')) {
|
||||
define('MOODLE_INTERNAL', true);
|
||||
}
|
||||
require_once($CFG->dirroot.'/lib/moodlelib.php');
|
||||
if (file_exists($CFG->dirroot.'/lib/classes/ip_utils.php')) {
|
||||
require_once($CFG->dirroot.'/lib/classes/ip_utils.php');
|
||||
}
|
||||
// Put access key as a cookie if given. This stops the need to put it as a url param on every request.
|
||||
$urlaccesskey = optional_param('accesskey', null, PARAM_TEXT);
|
||||
$isphpunit = defined('PHPUNIT_TEST');
|
||||
|
||||
if (!empty($urlaccesskey) && !$isphpunit) {
|
||||
setcookie('auth_outage_accesskey', $urlaccesskey, time() + 86400, '/', '', true, false);
|
||||
}
|
||||
|
||||
// Use url access key if given, else the cookie, else null.
|
||||
$useraccesskey = $urlaccesskey ?: $_COOKIE['auth_outage_accesskey'] ?? null;
|
||||
|
||||
$ipblocked = !remoteip_in_list('127.0.0.1');
|
||||
$accesskeyblocked = $useraccesskey != '5678';
|
||||
$allowed = (true && !$accesskeyblocked) || (true && !$ipblocked);
|
||||
|
||||
if (!$allowed) {
|
||||
if (!$isphpunit) {
|
||||
header($_SERVER['SERVER_PROTOCOL'] . ' 503 Moodle under maintenance');
|
||||
header('Status: 503 Moodle under maintenance');
|
||||
header('Retry-After: 300');
|
||||
header('Content-type: text/html; charset=utf-8');
|
||||
header('X-UA-Compatible: IE=edge');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
header('Accept-Ranges: none');
|
||||
header('X-Moodle-Maintenance: manager');
|
||||
}
|
||||
|
||||
if (!$isphpunit && ((defined('AJAX_SCRIPT') && AJAX_SCRIPT) || (defined('WS_SERVER') && WS_SERVER))) {
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (true && $ipblocked) {
|
||||
echo '<!-- Blocked by ip, your ip: '.getremoteaddr('n/a').' -->';
|
||||
}
|
||||
|
||||
if (true && $accesskeyblocked) {
|
||||
echo '<!-- Blocked by missing or incorrect access key, access key given: '. $useraccesskey .' -->';
|
||||
}
|
||||
|
||||
if (!$isphpunit) {
|
||||
if (file_exists($CFG->dataroot.'/climaintenance.template.html')) {
|
||||
require($CFG->dataroot.'/climaintenance.template.html');
|
||||
exit(0);
|
||||
}
|
||||
// The file above should always exist, but just in case...
|
||||
die('We are currently under maintentance, please try again later.');
|
||||
}
|
||||
}
|
||||
}
|
||||
EOT;
|
||||
$outage = new outage([
|
||||
'starttime' => 123,
|
||||
'stoptime' => 456,
|
||||
'accesskey' => '5678',
|
||||
]);
|
||||
$file = $CFG->dataroot.'/climaintenance.php';
|
||||
set_config($configkey, '127.0.0.1', 'auth_outage');
|
||||
|
||||
outagelib::update_climaintenance_code($outage);
|
||||
self::assertFileExists($file);
|
||||
$found = file_get_contents($file);
|
||||
self::assertSame($found, $expected);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides values to test_createmaintenancephpcode_withoutage
|
||||
* @return array
|
||||
*/
|
||||
public static function createmaintenancephpcode_withoutage_provider(): array {
|
||||
return [['allowedips'], ['allowedips_forced']];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create maintenance php code without IPs or accesskey
|
||||
*/
|
||||
public function test_createmaintenancephpcode_withoutips_or_accesskey() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$outage = new outage([
|
||||
'starttime' => 123,
|
||||
'stoptime' => 456,
|
||||
'accesskey' => null,
|
||||
]);
|
||||
$file = $CFG->dataroot.'/climaintenance.php';
|
||||
set_config('allowedips', '', 'auth_outage');
|
||||
set_config('allowedips_forced', '', 'auth_outage');
|
||||
|
||||
touch($file);
|
||||
outagelib::update_climaintenance_code($outage);
|
||||
// Backwards compatibility with older PHPUnit - use old assertFile method.
|
||||
if (method_exists($this, 'assertFileDoesNotExist')) {
|
||||
self::assertFileDoesNotExist($file);
|
||||
} else {
|
||||
self::assertFileNotExists($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test create maintenance php code without outage
|
||||
*/
|
||||
public function test_createmaintenancephpcode_withoutoutage() {
|
||||
global $CFG;
|
||||
$file = $CFG->dataroot.'/climaintenance.php';
|
||||
|
||||
touch($file);
|
||||
outagelib::update_climaintenance_code(null);
|
||||
// Backwards compatibility with older PHPUnit - use old assertFile method.
|
||||
if (method_exists($this, 'assertFileDoesNotExist')) {
|
||||
self::assertFileDoesNotExist($file);
|
||||
} else {
|
||||
self::assertFileNotExists($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Related to Issue #70: Creating ongoing outage does not trigger maintenance file creation.
|
||||
*/
|
||||
public function test_preparenextoutage_notautostart() {
|
||||
global $CFG;
|
||||
|
||||
$this->create_outage();
|
||||
|
||||
// The method outagelib::prepare_next_outage() should have been called by save().
|
||||
foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) {
|
||||
self::assertFileExists($file);
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression Test - Issue #82: When changing the IP address list it should recreate the maintenance files.
|
||||
*/
|
||||
public function test_when_we_change_allowed_ips_in_settings_it_updates_the_templates() {
|
||||
global $CFG;
|
||||
|
||||
$this->create_outage();
|
||||
|
||||
// Change settings.
|
||||
set_config('s_auth_outage_allowedips', '127', 'auth_outage');
|
||||
|
||||
// The method outagelib::prepare_next_outage() should have been called from admin_write_settings().
|
||||
foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) {
|
||||
self::assertFileExists($file);
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Problem detected while solving Issue #82.
|
||||
*/
|
||||
public function test_when_we_change_remove_selectors_in_settings_it_updates_the_templates() {
|
||||
global $CFG;
|
||||
|
||||
$this->create_outage();
|
||||
|
||||
// Change settings.
|
||||
set_config('s_auth_outage_remove_selectors', '.something', 'auth_outage');
|
||||
|
||||
// The method outagelib::prepare_next_outage() should have been called from admin_write_settings().
|
||||
foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) {
|
||||
self::assertFileExists($file);
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Related to Issue #72: IP Block still triggers cli maintenance mode even without autostart.
|
||||
*/
|
||||
public function test_preparenextoutage_noautostarttrigger() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => false,
|
||||
'warntime' => $now - 200,
|
||||
'starttime' => $now - 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
outagedb::save($outage);
|
||||
|
||||
// The method outagelib::prepare_next_outage() should have been called by save().
|
||||
self::assertFalse(get_config('moodle', 'maintenance_later'));
|
||||
// This file should not exist even if the statement above fails as Moodle does not create it immediately but test anyway.
|
||||
// Backwards compatibility with older PHPUnit - use old assertFile method.
|
||||
if (method_exists($this, 'assertFileDoesNotExist')) {
|
||||
self::assertFileDoesNotExist($CFG->dataroot.'/climaintenance.html');
|
||||
} else {
|
||||
self::assertFileNotExists($CFG->dataroot.'/climaintenance.html');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression test for issue #85.
|
||||
*/
|
||||
public function test_it_can_inject_in_settings_if_not_additional_html() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
ob_start();
|
||||
$outage->id = outagedb::save($outage);
|
||||
$text = trim(ob_get_contents());
|
||||
ob_end_clean();
|
||||
self::assertStringContainsString('Update maintenance mode configuration', $text);
|
||||
// Pretend we are there...
|
||||
$_SERVER['SCRIPT_FILENAME'] = '/var/www/alternativepath/admin/settings.php'; // Issue #88 regression test.
|
||||
$_SERVER['SCRIPT_NAME'] = '/admin/settings.php';
|
||||
$_GET['section'] = 'notadditionalhtml';
|
||||
outagelib::reset_injectcalled();
|
||||
|
||||
$header = outagelib::get_inject_code();
|
||||
self::assertNotEmpty($header);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates outage for tests.
|
||||
*/
|
||||
private function create_outage() {
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => false,
|
||||
'warntime' => $now - 200,
|
||||
'starttime' => $now - 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
set_config('allowedips', '127.0.0.1', 'auth_outage');
|
||||
outagedb::save($outage);
|
||||
|
||||
// Enable outage plugin so settings can be changed.
|
||||
set_config('auth', 'outage');
|
||||
\core\session\manager::gc(); // Remove stale sessions.
|
||||
\core_plugin_manager::reset_caches();
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides values to test_evaluation_maintenancepage
|
||||
* @return array
|
||||
*/
|
||||
public static function evaluation_maintenancepage_provider(): array {
|
||||
$blockedipout = '<!-- Blocked by ip, your ip:';
|
||||
$blockedaccesskeyout = '<!-- Blocked by missing or incorrect access key, access key given:';
|
||||
|
||||
return [
|
||||
// IP set up, access key not set up.
|
||||
'ip allowed, no access key setup' => [
|
||||
'allowedips' => '127.0.0.1',
|
||||
'iptouse' => '127.0.0.1',
|
||||
'accesskey' => null,
|
||||
'accesskeytouse' => null,
|
||||
'expectedoutputs' => [],
|
||||
],
|
||||
'ip not allowed, no access key setup' => [
|
||||
'allowedips' => '5.5.5.5',
|
||||
'iptouse' => '127.0.0.1',
|
||||
'accesskey' => null,
|
||||
'accesskeytouse' => null,
|
||||
'expectedoutputs' => [$blockedipout],
|
||||
],
|
||||
// IP not set up, access key set up.
|
||||
'access key incorrect, no ip setup' => [
|
||||
'allowedips' => null,
|
||||
'iptouse' => null,
|
||||
'accesskey' => '12345',
|
||||
'accesskeytouse' => 'wrong',
|
||||
'expectedoutputs' => [$blockedaccesskeyout],
|
||||
],
|
||||
'access key correct, no ip setup' => [
|
||||
'allowedips' => null,
|
||||
'iptouse' => null,
|
||||
'accesskey' => '12345',
|
||||
'accesskeytouse' => '12345',
|
||||
'expectedoutputs' => [],
|
||||
],
|
||||
// Both IP and access key set up.
|
||||
'access key incorrect, ip incorrect' => [
|
||||
'allowedips' => '127.0.0.1',
|
||||
'iptouse' => '5.5.5.5',
|
||||
'accesskey' => '12345',
|
||||
'accesskeytouse' => 'wrong',
|
||||
'expectedoutputs' => [$blockedipout, $blockedaccesskeyout],
|
||||
],
|
||||
'access key correct, ip incorrect' => [
|
||||
'allowedips' => '127.0.0.1',
|
||||
'iptouse' => '5.5.5.5',
|
||||
'accesskey' => '12345',
|
||||
'accesskeytouse' => '12345',
|
||||
'expectedoutputs' => [],
|
||||
],
|
||||
'access key incorrect, ip correct' => [
|
||||
'allowedips' => '127.0.0.1',
|
||||
'iptouse' => '127.0.0.1',
|
||||
'accesskey' => '12345',
|
||||
'accesskeytouse' => 'wrong',
|
||||
'expectedoutputs' => [],
|
||||
],
|
||||
'access key correct, ip correct' => [
|
||||
'allowedips' => '127.0.0.1',
|
||||
'iptouse' => '127.0.0.1',
|
||||
'accesskey' => '12345',
|
||||
'accesskeytouse' => '12345',
|
||||
'expectedoutputs' => [],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the evaluation logic of the generated maintenance page.
|
||||
*
|
||||
* @param string|null $allowedips config to set as allowed ips - null to not set
|
||||
* @param string|null $iptouse ip to 'fake' as the remote ip, or null to not set.
|
||||
* @param string|null $accesskey config to set as the access key in the outage - null to not set
|
||||
* @param string|null $accesskeytouse access key to pass in as fake url params - null to not set
|
||||
* @param array $expectedoutputs expected output strings, if empty will test that the output was also empty.
|
||||
*
|
||||
* @dataProvider evaluation_maintenancepage_provider
|
||||
*
|
||||
* We need this because we modify the request headers,
|
||||
* see https://github.com/sebastianbergmann/phpunit/issues/720#issuecomment-10421092
|
||||
* @runClassInSeparateProcess
|
||||
*/
|
||||
public function test_evaluation_maintenancepage(?string $allowedips, ?string $iptouse, ?string $accesskey,
|
||||
?string $accesskeytouse, array $expectedoutputs) {
|
||||
|
||||
global $CFG, $_SERVER, $_GET;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => false,
|
||||
'warntime' => $now - 200,
|
||||
'starttime' => $now - 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
'accesskey' => $accesskey,
|
||||
]);
|
||||
|
||||
if (!is_null($allowedips)) {
|
||||
set_config('allowedips', $allowedips, 'auth_outage');
|
||||
}
|
||||
// Ensure if the file exists we clean it (e.g. from a previous test run).
|
||||
$file = $CFG->dataroot.'/climaintenance.php';
|
||||
if (file_exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
|
||||
// This basically sets the output of getremoteaddr().
|
||||
if (!is_null($iptouse)) {
|
||||
$_SERVER['REMOTE_ADDR'] = $iptouse;
|
||||
}
|
||||
|
||||
// This sets the output of optional_param().
|
||||
if (!is_null($accesskeytouse)) {
|
||||
$_GET['accesskey'] = $accesskeytouse;
|
||||
}
|
||||
|
||||
outagelib::update_climaintenance_code($outage);
|
||||
self::assertFileExists($file);
|
||||
|
||||
// Require the file to execute it.
|
||||
// Normally this would die, but we have baked some goodies in there
|
||||
// that stop it die'ing during a unit test.
|
||||
ob_start();
|
||||
require($file);
|
||||
$contents = ob_get_clean();
|
||||
|
||||
// Check each output is as expected.
|
||||
foreach ($expectedoutputs as $expectedoutput) {
|
||||
$this->assertStringContainsString($expectedoutput, $contents);
|
||||
}
|
||||
|
||||
// Ensure if nothing was expected, that it is empty.
|
||||
if (empty($expectedoutputs)) {
|
||||
$this->assertEmpty($contents);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
a {
|
||||
font-size: 200%;
|
||||
}
|
||||
|
||||
div {
|
||||
background-image: url('/auth/outage/tests/phpunit/local/controllers/fixtures/catalyst.png');
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
a {
|
||||
font-size: 200%;
|
||||
}
|
||||
|
||||
div {
|
||||
background-image: url(/auth/outage/tests/phpunit/local/controllers/fixtures/catalyst.png);
|
||||
}
|
||||
@@ -1,109 +0,0 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Tests performed on infopage controller class and update_static_page task class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use auth_outage\local\controllers\infopage;
|
||||
use auth_outage\local\outage;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
require_once(__DIR__.'/../../base_testcase.php');
|
||||
|
||||
/**
|
||||
* Tests performed on infopage controller class and update_static_page task class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
*/
|
||||
class infopagecontroller_test extends auth_outage_base_testcase {
|
||||
/**
|
||||
* Tests the constructor.
|
||||
*/
|
||||
public function test_constructor() {
|
||||
new infopage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor with given parameters.
|
||||
*/
|
||||
public function test_constructor_withparams() {
|
||||
$_GET = ['id' => 1, 'static' => 'true'];
|
||||
new infopage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor with different id and outage id.
|
||||
*/
|
||||
public function test_constructor_idmismatch() {
|
||||
$outage = new outage([
|
||||
'id' => 1,
|
||||
'autostart' => false,
|
||||
'warntime' => time() - 60,
|
||||
'starttime' => time(),
|
||||
'stoptime' => time() + 60,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
$this->set_expected_exception('coding_exception');
|
||||
new infopage(['id' => 2, 'outage' => $outage]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the constructor with an invalid outage.
|
||||
*/
|
||||
public function test_constructor_invalidoutage() {
|
||||
$this->set_expected_exception('coding_exception');
|
||||
new infopage(['outage' => 'My outage']);
|
||||
}
|
||||
|
||||
/**
|
||||
* We should have an exception because CLI cannot redirect.
|
||||
*/
|
||||
public function test_output_nonstatic_nooutage() {
|
||||
$info = new infopage(['static' => false]);
|
||||
$this->set_expected_exception('moodle_exception');
|
||||
$info->output();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the output of the info page.
|
||||
*/
|
||||
public function test_output() {
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'id' => 1,
|
||||
'autostart' => false,
|
||||
'warntime' => $now - 100,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
$info = new infopage(['outage' => $outage]);
|
||||
$output = $info->get_output();
|
||||
self::assertContains('auth_outage_info', $output);
|
||||
}
|
||||
}
|
||||
@@ -1,530 +0,0 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* outagelib_test test class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\outagelib;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $CFG;
|
||||
require_once($CFG->libdir.'/adminlib.php');
|
||||
|
||||
/**
|
||||
* outagelib_test test class.
|
||||
*
|
||||
* @package auth_outage
|
||||
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
* @SuppressWarnings(public) Allow as many methods as needed.
|
||||
*/
|
||||
class outagelib_test extends advanced_testcase {
|
||||
/**
|
||||
* Check if maintenance message is disabled as needed.
|
||||
*/
|
||||
public function test_maintenancemessage() {
|
||||
$this->resetAfterTest(true);
|
||||
static::setAdminUser();
|
||||
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
|
||||
set_config('maintenance_message', 'A message.');
|
||||
outagedb::save($outage);
|
||||
self::assertFalse((bool)get_config('moodle', 'maintenance_message'));
|
||||
self::assertCount(2, phpunit_util::get_debugging_messages());
|
||||
phpunit_util::reset_debugging();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if maintenance later is removed if no outage set.
|
||||
*/
|
||||
public function test_maintenancelater_nonext() {
|
||||
$this->resetAfterTest(true);
|
||||
set_config('maintenance_later', time() + (60 * 60 * 24 * 7)); // In 1 week.
|
||||
self::assertNotEmpty(get_config('moodle', 'maintenance_later'));
|
||||
outagelib::prepare_next_outage();
|
||||
self::assertEmpty(get_config('moodle', 'maintenance_later'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Check outagelib::inject() works as expected.
|
||||
*/
|
||||
public function test_inject() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
$outage->id = outagedb::save($outage);
|
||||
self::assertEmpty($CFG->additionalhtmltopofbody);
|
||||
|
||||
outagelib::reinject();
|
||||
self::assertContains('<style>', $CFG->additionalhtmltopofbody);
|
||||
self::assertContains('<script>', $CFG->additionalhtmltopofbody);
|
||||
|
||||
// Should not inject more than once with the inject() function.
|
||||
$size = strlen($CFG->additionalhtmltopofbody);
|
||||
outagelib::inject();
|
||||
self::assertSame($size, strlen($CFG->additionalhtmltopofbody));
|
||||
|
||||
$this->resetDebugging(); // Function pix_url deprecated in Moodle 33+.
|
||||
}
|
||||
|
||||
/**
|
||||
* Check outagelib::inject() will not break the page if something goes wrong.
|
||||
*/
|
||||
public function test_inject_broken() {
|
||||
$_GET = ['auth_outage_break_code' => '1'];
|
||||
outagelib::reinject();
|
||||
self::assertCount(2, phpunit_util::get_debugging_messages());
|
||||
phpunit_util::reset_debugging();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if injection works with preview.
|
||||
*/
|
||||
public function test_inject_preview() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
$outage->id = outagedb::save($outage);
|
||||
self::assertEmpty($CFG->additionalhtmltopofbody);
|
||||
$_GET = ['auth_outage_preview' => (string)$outage->id];
|
||||
|
||||
outagelib::reinject();
|
||||
self::assertContains('<style>', $CFG->additionalhtmltopofbody);
|
||||
self::assertContains('<script>', $CFG->additionalhtmltopofbody);
|
||||
|
||||
$this->resetDebugging(); // Function pix_url deprecated in Moodle 33+.
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if injection works with invalid preview without stopping the page.
|
||||
*/
|
||||
public function test_inject_preview_notfound() {
|
||||
global $CFG;
|
||||
self::assertEmpty($CFG->additionalhtmltopofbody);
|
||||
$_GET = ['auth_outage_preview' => '1'];
|
||||
// Should not throw exception or halt anything, silently ignore it.
|
||||
outagelib::reinject();
|
||||
self::assertEmpty($CFG->additionalhtmltopofbody);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test injection with preview and delta.
|
||||
*/
|
||||
public function test_inject_preview_withdelta() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
$outage->id = outagedb::save($outage);
|
||||
self::assertEmpty($CFG->additionalhtmltopofbody);
|
||||
$_GET = ['auth_outage_preview' => (string)$outage->id, 'auth_outage_delta' => '500'];
|
||||
outagelib::reinject();
|
||||
// Still empty, delta is too high (outage ended).
|
||||
self::assertEmpty($CFG->additionalhtmltopofbody);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test injection without active outage.
|
||||
*/
|
||||
public function test_inject_noactive() {
|
||||
outagelib::reinject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if get config works without getting defaults.
|
||||
*/
|
||||
public function test_get_config() {
|
||||
$this->resetAfterTest(true);
|
||||
$keys = [
|
||||
'css',
|
||||
'default_autostart',
|
||||
'default_description',
|
||||
'default_duration',
|
||||
'default_title',
|
||||
'default_warning_duration',
|
||||
'allowedips',
|
||||
'remove_selectors',
|
||||
];
|
||||
// Set config with values.
|
||||
foreach ($keys as $k) {
|
||||
set_config($k, $k.'_value', 'auth_outage');
|
||||
}
|
||||
// Ensure it is not using any defaults.
|
||||
$config = outagelib::get_config();
|
||||
foreach ($keys as $k) {
|
||||
self::assertSame($config->$k, $k.'_value', 'auth_outage');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that config has key.
|
||||
*/
|
||||
public function test_config_keys() {
|
||||
$this->resetAfterTest(true);
|
||||
$keys = [
|
||||
'allowedips',
|
||||
'css',
|
||||
'default_autostart',
|
||||
'default_description',
|
||||
'default_duration',
|
||||
'default_title',
|
||||
'default_warning_duration',
|
||||
'remove_selectors',
|
||||
];
|
||||
$defaults = outagelib::get_config_defaults();
|
||||
foreach ($keys as $k) {
|
||||
self::assertArrayHasKey($k, $defaults);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if get config works getting defaults when needed.
|
||||
*/
|
||||
public function test_get_config_invalid() {
|
||||
$this->resetAfterTest(true);
|
||||
// Set config with invalid values.
|
||||
set_config('allowedips', " \n", 'auth_outage');
|
||||
set_config('css', " \n", 'auth_outage');
|
||||
set_config('default_autostart', " \n", 'auth_outage');
|
||||
set_config('default_description', " \n", 'auth_outage');
|
||||
set_config('default_duration', " \n", 'auth_outage');
|
||||
set_config('default_title', " \n", 'auth_outage');
|
||||
set_config('default_warning_duration', " \n", 'auth_outage');
|
||||
// Get defaults.
|
||||
$defaults = outagelib::get_config_defaults();
|
||||
$config = outagelib::get_config();
|
||||
// Ensure it is using all defaults.
|
||||
foreach ($defaults as $k => $v) {
|
||||
self::assertSame($v, $config->$k);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if outagelib::inject() does not inject on admin/settings.php?section=additionalhtml
|
||||
*/
|
||||
public function test_inject_settings() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
$outage->id = outagedb::save($outage);
|
||||
self::assertEmpty($CFG->additionalhtmltopofbody);
|
||||
|
||||
// Pretend we are there...
|
||||
$_SERVER['SCRIPT_FILENAME'] = '/var/www/alternativepath/admin/settings.php'; // Issue #88 regression test.
|
||||
$_SERVER['SCRIPT_NAME'] = '/admin/settings.php';
|
||||
$_GET['section'] = 'additionalhtml';
|
||||
outagelib::reinject();
|
||||
|
||||
self::assertEmpty($CFG->additionalhtmltopofbody);
|
||||
}
|
||||
|
||||
public function test_createmaintenancephpcode() {
|
||||
$expected = <<<'EOT'
|
||||
<?php
|
||||
if ((time() >= 123) && (time() < 456)) {
|
||||
define('MOODLE_INTERNAL', true);
|
||||
require_once($CFG->dirroot.'/lib/moodlelib.php');
|
||||
if (!remoteip_in_list('heyyou
|
||||
a.b.c.d
|
||||
e.e.e.e/20')) {
|
||||
header($_SERVER['SERVER_PROTOCOL'] . ' 503 Moodle under maintenance');
|
||||
header('Status: 503 Moodle under maintenance');
|
||||
header('Retry-After: 300');
|
||||
header('Content-type: text/html; charset=utf-8');
|
||||
header('X-UA-Compatible: IE=edge');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
header('Accept-Ranges: none');
|
||||
if ((defined('AJAX_SCRIPT') && AJAX_SCRIPT) || (defined('WS_SERVER') && WS_SERVER)) {
|
||||
exit(0);
|
||||
}
|
||||
echo '<!-- Blocked by ip, your ip: '.getremoteaddr('n/a').' -->';
|
||||
if (file_exists($CFG->dataroot.'/climaintenance.template.html')) {
|
||||
require($CFG->dataroot.'/climaintenance.template.html');
|
||||
exit(0);
|
||||
}
|
||||
// The file above should always exist, but just in case...
|
||||
die('We are currently under maintentance, please try again later.');
|
||||
}
|
||||
}
|
||||
EOT;
|
||||
$found = outagelib::create_climaintenancephp_code(123, 456, "hey'\"you\na.b.c.d\ne.e.e.e/20");
|
||||
self::assertSame($expected, $found);
|
||||
}
|
||||
|
||||
public function test_createmaintenancephpcode_withoutage() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$expected = <<<'EOT'
|
||||
<?php
|
||||
if ((time() >= 123) && (time() < 456)) {
|
||||
define('MOODLE_INTERNAL', true);
|
||||
require_once($CFG->dirroot.'/lib/moodlelib.php');
|
||||
if (!remoteip_in_list('127.0.0.1')) {
|
||||
header($_SERVER['SERVER_PROTOCOL'] . ' 503 Moodle under maintenance');
|
||||
header('Status: 503 Moodle under maintenance');
|
||||
header('Retry-After: 300');
|
||||
header('Content-type: text/html; charset=utf-8');
|
||||
header('X-UA-Compatible: IE=edge');
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
header('Accept-Ranges: none');
|
||||
if ((defined('AJAX_SCRIPT') && AJAX_SCRIPT) || (defined('WS_SERVER') && WS_SERVER)) {
|
||||
exit(0);
|
||||
}
|
||||
echo '<!-- Blocked by ip, your ip: '.getremoteaddr('n/a').' -->';
|
||||
if (file_exists($CFG->dataroot.'/climaintenance.template.html')) {
|
||||
require($CFG->dataroot.'/climaintenance.template.html');
|
||||
exit(0);
|
||||
}
|
||||
// The file above should always exist, but just in case...
|
||||
die('We are currently under maintentance, please try again later.');
|
||||
}
|
||||
}
|
||||
EOT;
|
||||
$outage = new outage([
|
||||
'starttime' => 123,
|
||||
'stoptime' => 456,
|
||||
]);
|
||||
$file = $CFG->dataroot.'/climaintenance.php';
|
||||
set_config('allowedips', '127.0.0.1', 'auth_outage');
|
||||
|
||||
outagelib::update_climaintenance_code($outage);
|
||||
self::assertFileExists($file);
|
||||
$found = file_get_contents($file);
|
||||
self::assertSame($found, $expected);
|
||||
}
|
||||
|
||||
public function test_createmaintenancephpcode_withoutips() {
|
||||
global $CFG;
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$outage = new outage([
|
||||
'starttime' => 123,
|
||||
'stoptime' => 456,
|
||||
]);
|
||||
$file = $CFG->dataroot.'/climaintenance.php';
|
||||
set_config('allowedips', '', 'auth_outage');
|
||||
|
||||
touch($file);
|
||||
outagelib::update_climaintenance_code($outage);
|
||||
self::assertFileNotExists($file);
|
||||
}
|
||||
|
||||
public function test_createmaintenancephpcode_withoutoutage() {
|
||||
global $CFG;
|
||||
$file = $CFG->dataroot.'/climaintenance.php';
|
||||
|
||||
touch($file);
|
||||
outagelib::update_climaintenance_code(null);
|
||||
self::assertFileNotExists($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Related to Issue #70: Creating ongoing outage does not trigger maintenance file creation.
|
||||
*/
|
||||
public function test_preparenextoutage_notautostart() {
|
||||
global $CFG;
|
||||
|
||||
$this->create_outage();
|
||||
|
||||
// The method outagelib::prepare_next_outage() should have been called by save().
|
||||
foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) {
|
||||
self::assertFileExists($file);
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression Test - Issue #82: When changing the IP address list it should recreate the maintenance files.
|
||||
*/
|
||||
public function test_when_we_change_allowed_ips_in_settings_it_updates_the_templates() {
|
||||
global $CFG;
|
||||
|
||||
$this->create_outage();
|
||||
|
||||
// Change settings.
|
||||
admin_write_settings((object)[
|
||||
's_auth_outage_allowedips' => '127',
|
||||
]);
|
||||
|
||||
// The method outagelib::prepare_next_outage() should have been called from admin_write_settings().
|
||||
foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) {
|
||||
self::assertFileExists($file);
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Problem detected while solving Issue #82.
|
||||
*/
|
||||
public function test_when_we_change_remove_selectors_in_settings_it_updates_the_templates() {
|
||||
global $CFG;
|
||||
|
||||
$this->create_outage();
|
||||
|
||||
// Change settings.
|
||||
admin_write_settings((object)[
|
||||
's_auth_outage_remove_selectors' => '.something',
|
||||
]);
|
||||
|
||||
// The method outagelib::prepare_next_outage() should have been called from admin_write_settings().
|
||||
foreach ([$CFG->dataroot.'/climaintenance.template.html', $CFG->dataroot.'/climaintenance.php'] as $file) {
|
||||
self::assertFileExists($file);
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Related to Issue #72: IP Block still triggers cli maintenance mode even without autostart.
|
||||
*/
|
||||
public function test_preparenextoutage_noautostarttrigger() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => false,
|
||||
'warntime' => $now - 200,
|
||||
'starttime' => $now - 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
outagedb::save($outage);
|
||||
|
||||
// The method outagelib::prepare_next_outage() should have been called by save().
|
||||
self::assertFalse(get_config('moodle', 'maintenance_later'));
|
||||
// This file should not exist even if the statement above fails as Moodle does not create it immediately but test anyway.
|
||||
self::assertFileNotExists($CFG->dataroot.'/climaintenance.html');
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression test for issue #85.
|
||||
*/
|
||||
public function test_it_can_inject_in_settings_if_not_additional_html() {
|
||||
global $CFG;
|
||||
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => true,
|
||||
'warntime' => $now,
|
||||
'starttime' => $now + 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
$outage->id = outagedb::save($outage);
|
||||
self::assertEmpty($CFG->additionalhtmltopofbody);
|
||||
|
||||
// Pretend we are there...
|
||||
$_SERVER['SCRIPT_FILENAME'] = '/var/www/alternativepath/admin/settings.php'; // Issue #88 regression test.
|
||||
$_SERVER['SCRIPT_NAME'] = '/admin/settings.php';
|
||||
$_GET['section'] = 'notadditionalhtml';
|
||||
outagelib::reinject();
|
||||
|
||||
self::assertNotEmpty($CFG->additionalhtmltopofbody);
|
||||
|
||||
$this->resetDebugging(); // Function pix_url deprecated in Moodle 33+.
|
||||
}
|
||||
|
||||
private function create_outage() {
|
||||
$this->resetAfterTest(true);
|
||||
self::setAdminUser();
|
||||
$now = time();
|
||||
$outage = new outage([
|
||||
'autostart' => false,
|
||||
'warntime' => $now - 200,
|
||||
'starttime' => $now - 100,
|
||||
'stoptime' => $now + 200,
|
||||
'title' => 'Title',
|
||||
'description' => 'Description',
|
||||
]);
|
||||
set_config('allowedips', '127.0.0.1', 'auth_outage');
|
||||
outagedb::save($outage);
|
||||
|
||||
// Enable outage plugin so settings can be changed.
|
||||
set_config('auth', 'outage');
|
||||
\core\session\manager::gc(); // Remove stale sessions.
|
||||
core_plugin_manager::reset_caches();
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,8 @@
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$plugin->component = "auth_outage";
|
||||
$plugin->version = 2018062500; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->release = '1.0.9'; // Human-readable release information.
|
||||
$plugin->requires = 2014051200; // Requires Moodle 2.7 or later. Moodle 3.0 or later recommended.
|
||||
$plugin->version = 2024081900; // The current plugin version (Date: YYYYMMDDXX).
|
||||
$plugin->release = 2024081900; // Human-readable release information.
|
||||
$plugin->requires = 2017111309; // 2017111309 = T13, but this really requires 3.9 and higher.
|
||||
$plugin->maturity = MATURITY_STABLE; // Suitable for PRODUCTION environments!
|
||||
$plugin->supported = [39, 405]; // A range of branch numbers of supported moodle versions.
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
*
|
||||
* @var array $viewbag
|
||||
*/
|
||||
// phpcs:disable moodle.Commenting.MissingDocblock.File
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
?>
|
||||
|
||||
@@ -22,20 +22,25 @@
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
// phpcs:disable moodle.Commenting.MissingDocblock.File
|
||||
|
||||
use auth_outage\output\manage\history_table;
|
||||
use auth_outage\output\manage\planned_table;
|
||||
use auth_outage\output\renderer;
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\outagelib;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
global $PAGE;
|
||||
$output = $PAGE->get_renderer('auth_outage');
|
||||
$urlnew = new moodle_url('/auth/outage/edit.php');
|
||||
|
||||
echo $viewbag['warning'];
|
||||
?>
|
||||
|
||||
<section id="section_planned_outages">
|
||||
<?php echo renderer::get()->rendersubtitle('outageslistfuture'); ?>
|
||||
<?php echo $output->rendersubtitle('outageslistfuture'); ?>
|
||||
<?php if (empty($viewbag['unended'])): ?>
|
||||
<p>
|
||||
<small><?php echo get_string('notfound', 'auth_outage'); ?></small>
|
||||
@@ -47,13 +52,27 @@ echo $viewbag['warning'];
|
||||
$table->finish_output();
|
||||
?>
|
||||
<?php endif; ?>
|
||||
<input type="button" class="form-submit"
|
||||
value="<?php echo get_string('outagecreate', 'auth_outage'); ?>"
|
||||
onclick="location.href='<?php echo $urlnew; ?>';"/>
|
||||
<?php
|
||||
$outage = outagedb::get_ongoing();
|
||||
if (is_null($outage)) :
|
||||
$config = outagelib::get_config();
|
||||
$default = $config->default_time;
|
||||
$max = $default ? 3 : 1;
|
||||
$next = time();
|
||||
for ($c = 0; $c < $max; $c++) {
|
||||
echo '<p>';
|
||||
$next = outagelib::get_next_window($next);
|
||||
$urlnew->param('starttime', $next);
|
||||
echo $output->single_button($urlnew, get_string('outagecreate', 'auth_outage'));
|
||||
if ($default) {
|
||||
echo ' ' . userdate( $next, get_string('datetimeformat', 'auth_outage'));
|
||||
}
|
||||
}
|
||||
endif; ?>
|
||||
</section>
|
||||
|
||||
<section id="section_outage_history">
|
||||
<?php echo renderer::get()->rendersubtitle('outageslistpast'); ?>
|
||||
<?php echo $output->rendersubtitle('outageslistpast'); ?>
|
||||
<?php if (empty($viewbag['ended'])): ?>
|
||||
<p>
|
||||
<small><?php echo get_string('notfound', 'auth_outage'); ?></small>
|
||||
|
||||
@@ -7,71 +7,78 @@ If you need to make changes here, remember to update your settings inside Moodle
|
||||
background-color: red;
|
||||
box-sizing: content-box;
|
||||
color: white;
|
||||
height: 90px;
|
||||
height: 100px;
|
||||
left: 0;
|
||||
padding: 0;
|
||||
position: fixed;
|
||||
text-align: center;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
top: 0;
|
||||
transition: background 3s ease-out;
|
||||
width: 100%;
|
||||
z-index: 9999;
|
||||
}
|
||||
|
||||
#auth_outage_warningbar_box .auth_outage_warningbar_center {
|
||||
/* fix to prevent existing theme hacks to fix issue */
|
||||
margin-top: 0;
|
||||
position: static;
|
||||
}
|
||||
|
||||
#auth_outage_warningbar_box.auth_outage_warning_period {
|
||||
background: repeating-linear-gradient(
|
||||
background:
|
||||
repeating-linear-gradient(
|
||||
-45deg,
|
||||
#ff7c00,
|
||||
#ff7c00 10px,
|
||||
#ff6c00 10px,
|
||||
#ff6c00 20px
|
||||
);
|
||||
);
|
||||
background-color: #ff7c00;
|
||||
}
|
||||
|
||||
#auth_outage_warningbar_box.auth_outage_imminent_period {
|
||||
background: repeating-linear-gradient(
|
||||
background:
|
||||
repeating-linear-gradient(
|
||||
-45deg,
|
||||
#a000a0,
|
||||
#a000a0 10px,
|
||||
#800080 10px,
|
||||
#800080 20px
|
||||
);
|
||||
);
|
||||
background-color: #800080;
|
||||
}
|
||||
|
||||
#auth_outage_warningbar_box.auth_outage_ongoing_period {
|
||||
background: repeating-linear-gradient(
|
||||
background:
|
||||
repeating-linear-gradient(
|
||||
-45deg,
|
||||
#ee0000,
|
||||
#ee0000 10px,
|
||||
#cc0000 10px,
|
||||
#cc0000 20px
|
||||
);
|
||||
background-color: #ee0000;
|
||||
#e00,
|
||||
#e00 10px,
|
||||
#c00 10px,
|
||||
#c00 20px
|
||||
);
|
||||
background-color: #e00;
|
||||
}
|
||||
|
||||
#auth_outage_warningbar_box.auth_outage_finished_period {
|
||||
background: repeating-linear-gradient(
|
||||
background:
|
||||
repeating-linear-gradient(
|
||||
-45deg,
|
||||
#00aa00,
|
||||
#00aa00 10px,
|
||||
#009900 10px,
|
||||
#009900 20px
|
||||
);
|
||||
background-color: #009900;
|
||||
}
|
||||
|
||||
.auth_outage_warningbar_center {
|
||||
margin-top: -35px;
|
||||
position: relative;
|
||||
top: 50%;
|
||||
#0a0,
|
||||
#0a0 10px,
|
||||
#090 10px,
|
||||
#090 20px
|
||||
);
|
||||
background-color: #090;
|
||||
}
|
||||
|
||||
#auth_outage_warningbar_message {
|
||||
font-size: 200%;
|
||||
font-weight: bold;
|
||||
margin: 10px 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
a.auth_outage_warningbar_box_title {
|
||||
@@ -83,6 +90,7 @@ a.auth_outage_warningbar_box_finish {
|
||||
border: 1px solid black;
|
||||
border-radius: 5px;
|
||||
color: darkgray;
|
||||
white-space: nowrap;
|
||||
font-weight: bold;
|
||||
margin-left: 10px;
|
||||
padding-left: 5px;
|
||||
@@ -95,10 +103,36 @@ a.auth_outage_warningbar_box_finish:hover {
|
||||
background-color: black;
|
||||
}
|
||||
|
||||
.navbar.navbar-fixed-top {
|
||||
top: 90px;
|
||||
body.auth_outage #nav-drawer {
|
||||
top: 150px;
|
||||
}
|
||||
|
||||
.auth_outage_warningbar_spacer {
|
||||
height: 80px;
|
||||
body.auth_outage nav.fixed-top.navbar {
|
||||
top: 100px;
|
||||
}
|
||||
|
||||
body.auth_outage #page {
|
||||
margin-top: 150px;
|
||||
}
|
||||
|
||||
body.auth_outage.path-mod-assign [data-region="grading-navigation-panel"] {
|
||||
top: 100px;
|
||||
}
|
||||
|
||||
body.auth_outage.path-mod-assign [data-region="grade-panel"] {
|
||||
top: 185px;
|
||||
}
|
||||
|
||||
body.auth_outage .layout.fullscreen {
|
||||
top: 150px;
|
||||
}
|
||||
|
||||
body.auth_outage .modal-dialog {
|
||||
margin: calc(150px + 1.75rem) auto;
|
||||
}
|
||||
|
||||
@media (max-width: 767px) {
|
||||
#auth_outage_warningbar_message {
|
||||
font-size: 120%;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,8 @@ var authOutageWarningBar = {
|
||||
},
|
||||
|
||||
tickOngoing: function() {
|
||||
const MINSECS = 60;
|
||||
|
||||
if (this.finished) {
|
||||
return;
|
||||
}
|
||||
@@ -78,13 +80,8 @@ var authOutageWarningBar = {
|
||||
xmlhttp.open("GET", this.checkfinishedurl, true);
|
||||
xmlhttp.send();
|
||||
|
||||
var estimatedServerTime = this.servertime + (Date.now() - this.clienttime);
|
||||
var sleepSeconds = this.stops - estimatedServerTime; // How long to sleep until it stops.
|
||||
if (sleepSeconds <= 0) {
|
||||
sleepSeconds = 5; // It should be back, keep checking every 5 seconds.
|
||||
} else {
|
||||
sleepSeconds = Math.min(sleepSeconds, (5 * 60)); // Check at least every 5 minutes.
|
||||
}
|
||||
// Checking if the site is back online every 4-6 minutes.
|
||||
var sleepSeconds = 4 * MINSECS + (2 * MINSECS * Math.random());
|
||||
|
||||
setTimeout(function() {
|
||||
$this.tickOngoing();
|
||||
@@ -114,8 +111,10 @@ var authOutageWarningBar = {
|
||||
seconds2hms: function(seconds) {
|
||||
var minutes = Math.floor(seconds / 60);
|
||||
var hours = Math.floor(minutes / 60);
|
||||
var days = Math.floor(hours / 24);
|
||||
seconds %= 60;
|
||||
minutes %= 60;
|
||||
hours %= 24;
|
||||
// Cross-browser simple solution for padding zeroes.
|
||||
if (minutes < 10) {
|
||||
minutes = "0" + minutes;
|
||||
@@ -123,6 +122,15 @@ var authOutageWarningBar = {
|
||||
if (seconds < 10) {
|
||||
seconds = "0" + seconds;
|
||||
}
|
||||
return hours + ':' + minutes + ':' + seconds;
|
||||
if (days > 0) {
|
||||
if (days > 1) {
|
||||
days = days + ' days ';
|
||||
} else {
|
||||
days = days + ' day ';
|
||||
}
|
||||
} else {
|
||||
days = '';
|
||||
}
|
||||
return days + hours + ':' + minutes + ':' + seconds;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
* @copyright 2016 Catalyst IT
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
// phpcs:disable moodle.Commenting.MissingDocblock.File
|
||||
|
||||
use auth_outage\local\outagelib;
|
||||
|
||||
@@ -46,17 +47,15 @@ if (!$viewbag['static']) {
|
||||
['target' => '_blank', 'class' => 'auth_outage_warningbar_box_title']
|
||||
);
|
||||
if (is_siteadmin()) {
|
||||
$url = new moodle_url('/auth/outage/finish.php', ['id' => $viewbag['outage']->id]);
|
||||
$text = html_writer::empty_tag('img', [
|
||||
'src' => $OUTPUT->pix_url('t/check'),
|
||||
'alt' => get_string('finish', 'auth_outage'),
|
||||
'class' => 'iconsmall',
|
||||
]).' '.get_string('finish', 'auth_outage');
|
||||
$attr = [
|
||||
'title' => get_string('finish', 'auth_outage'),
|
||||
'class' => 'auth_outage_warningbar_box_finish',
|
||||
];
|
||||
$title .= ' '.html_writer::span(html_writer::link($url, $text, $attr), '', ['id' => 'auth_outage_warningbar_button']);
|
||||
$link = html_writer::link(
|
||||
new moodle_url('/auth/outage/finish.php', ['id' => $viewbag['outage']->id]),
|
||||
$OUTPUT->pix_icon('t/check', get_string('finish', 'auth_outage'), 'moodle', array('class' => 'iconsmall')) . get_string('finish', 'auth_outage'),
|
||||
[
|
||||
'title' => get_string('finish', 'auth_outage'),
|
||||
'class' => 'auth_outage_warningbar_box_finish',
|
||||
]
|
||||
);
|
||||
$title .= ' '.html_writer::span($link, '', ['id' => 'auth_outage_warningbar_button']);
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -74,8 +73,6 @@ if (!$viewbag['static']) {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="auth_outage_warningbar_spacer"> </div>
|
||||
|
||||
<?php if (!$viewbag['static']): ?>
|
||||
<script>
|
||||
document.body.className += ' auth_outage';
|
||||
|
||||
Reference in New Issue
Block a user