Skip to content

Commit 1a5fca3

Browse files
committed
add check for subplugin events
1 parent 054c3aa commit 1a5fca3

8 files changed

+243
-9
lines changed

classes/plugininfo/townsquareexpansion.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
1616

1717
/**
18-
* TODO: Add description.
18+
* File to manage subplugins from type townsquareexpansion.
1919
*
2020
* @package local_townsquaresupport
2121
* @copyright 2024 Tamaro Walter
@@ -36,7 +36,7 @@ class townsquareexpansion extends base {
3636
* @return bool
3737
*/
3838
public function is_uninstall_allowed(): bool {
39-
return true;
39+
return true; // A subplugin can be deleted without condition.
4040
}
4141

4242
/**

classes/townsquaresupportinterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ interface townsquaresupportinterface {
4242
/**
4343
* Function to gather the events
4444
* Every event must gain sufficient data so that townsquare can build a letter from it.
45-
* The array should contain following information:
45+
* The array must contain following information:
4646
* [courseid] => int Course ID from where the content comes from.
4747
* [modulename] => string Name of the activity module.
4848
* [instancename] => string Name of the instance that shows the notification.

db/subplugins.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424

2525
defined('MOODLE_INTERNAL') || die();
2626

27-
$subplugins = ['townsquareexpantion' => 'local/townsquaresupport/townsquareexpansion'];
27+
$subplugins = ['townsquareexpansion' => 'local/townsquaresupport/townsquareexpansion'];

lang/en/local_townsquaresupport.php

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@
2727

2828
$string['pluginname'] = 'Townsquare support';
2929
$string['plugintitle'] = 'Townsquare support plugin';
30+
$string['subpluginerror'] = 'Error while retrieving events from an subplugin. There seems to be a coding error in the subplugin {$a->subpluginname}.';
3031
$string['subplugintype_townsquareexpansion'] = 'Townsquare event expansion';
3132
$string['subplugintype_townsquareexpansion_plural'] = 'Townsquare event expansions';

lib.php

+18-5
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,23 @@
1414
// You should have received a copy of the GNU General Public License
1515
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
1616

17-
namespace local_townsquaresupport;
18-
1917
/**
2018
* Function to get the events from every subplugin that extends the town square.
2119
*
2220
* As every subplugin from townsquaresupport follows the same structure and has the get_event method located in the same
2321
* place, this function can access it directly.
24-
* @package local_townsquaresupport
22+
* @package local_townsquaresupport
2523
* @copyright 2024 Tamaro Walter
2624
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2725
*/
2826

27+
namespace local_townsquaresupport;
28+
29+
defined('MOODLE_INTERNAL') || die();
30+
31+
global $CFG;
32+
require_once($CFG->dirroot . '/local/townsquaresupport/locallib.php');
33+
2934
/**
3035
* Core function of the townsquaresupport plugin. Retrieves all events from the subplugins and makes them available
3136
* to the townsquare block.
@@ -45,8 +50,16 @@ function townsquaresupport_get_subplugin_events() {
4550
$classstring = "\\townsquareexpansion_" . $expansionname . "\\" . $expansionname;
4651
$expansionclass = new $classstring();
4752

48-
// Get the events from the subplugin and add it to the events array.
49-
$events = array_merge($events, $expansionclass->get_events());
53+
// Get the events from the subplugin.
54+
$subpluginevents = $expansionclass->get_events();
55+
56+
// Check if the events meet the requirements of the interface.
57+
if (townsquaresupport_check_subplugin_events($subpluginevents)) {
58+
$events = array_merge($events, $subpluginevents);
59+
} else {
60+
// Throw an error as there is an error in the subplugin code.
61+
throw new \moodle_exception('subpluginerror', 'local_townsquaresupport', '', ['subpluginname' => $expansionname]);
62+
}
5063

5164
}
5265
return $events;

locallib.php

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Internal library of functions for the townsquaresupport plugin
19+
*
20+
* @package local_townsquaresupport
21+
* @copyright 2024 Tamaro Walter
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace local_townsquaresupport;
26+
27+
/**
28+
* Helper function for townsquaresupport_get_subplugin_events, that checks if an amount
29+
* of events have all the required attributes from the townsquaresupport interface.
30+
*
31+
* @param array $subevents
32+
* @return int
33+
*/
34+
function townsquaresupport_check_subplugin_events($subevents): int {
35+
if (!gettype($subevents == 'array')) {
36+
return false;
37+
}
38+
39+
if ($subevents == []) {
40+
// If no events are available, then everything is okay.
41+
return true;
42+
} else {
43+
44+
// Check every event.
45+
foreach ($subevents as $event) {
46+
if (gettype($event) != 'object') {
47+
return false;
48+
}
49+
50+
// Check if all variables are set.
51+
$issetcheck = townsquaresupport_check_isset($event, 'courseid') &&
52+
townsquaresupport_check_isset($event, 'modulename') &&
53+
townsquaresupport_check_isset($event, 'instancename') &&
54+
townsquaresupport_check_isset($event, 'content') &&
55+
townsquaresupport_check_isset($event, 'timestart') &&
56+
townsquaresupport_check_isset($event, 'coursemoduleid') &&
57+
townsquaresupport_check_isset($event, 'eventtype');
58+
59+
if (!$issetcheck) {
60+
return false;
61+
}
62+
}
63+
}
64+
return true;
65+
}
66+
67+
/**
68+
* Helper function for check_subplugin_events function that proves if a variable is set in an array.
69+
*
70+
* @param array $event Event that is checked.
71+
* @param string $variablename Name of the variable
72+
* @return bool
73+
*/
74+
function townsquaresupport_check_isset($event, $variablename): bool {
75+
return isset($event->$variablename);
76+
}

tests/eventcheck_test.php

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Unit tests for the local_townsquaresupport.
19+
*
20+
* @package local_townsquaresupport
21+
* @copyright 2024 Tamaro Walter
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
namespace local_townsquaresupport;
25+
26+
defined('MOODLE_INTERNAL') || die();
27+
28+
global $CFG;
29+
require_once($CFG->dirroot . '/local/townsquaresupport/locallib.php');
30+
31+
/**
32+
* PHPUnit tests for testing the logic of proving if subplugin events satisfy the requirements of the townsquaresupport interface.
33+
*
34+
* @package local_townsquaresupport
35+
* @copyright 2024 Tamaro Walter
36+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37+
*
38+
* @covers ::\local_townsquaresupport\townsquaresupport_check_subplugin_events()
39+
*/
40+
final class eventcheck_test extends \advanced_testcase {
41+
42+
// Attributes.
43+
44+
/** @var object The data that will be used for testing */
45+
private $testdata;
46+
47+
48+
// Construct functions.
49+
50+
public function setUp(): void {
51+
$this->testdata = new \stdClass();
52+
$this->resetAfterTest();
53+
$this->helper_test_set_up();
54+
}
55+
56+
public function tearDown(): void {
57+
$this->testdata = null;
58+
}
59+
60+
// Tests.
61+
62+
/**
63+
* Test, if the check_subplugin_events function works correctly.
64+
* @return void
65+
*/
66+
public function test_checkevents(): void {
67+
// Test the subevents.
68+
$this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents1));
69+
$this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents2));
70+
$this->assertEquals(true, townsquaresupport_check_subplugin_events($this->testdata->subevents3));
71+
$this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents4));
72+
}
73+
74+
// Helper functions.
75+
76+
/**
77+
* Helper function that sets up the testdata.
78+
* @return void
79+
*/
80+
private function helper_test_set_up(): void {
81+
// Build different arrays of events that are incorrect (and one correct array).
82+
83+
// First incorrect event: variable 'content' is missing.
84+
$incorrecteevent1 = ['courseid' => 12, 'modulename' => 'pluginname', 'instancename' => 'instance1',
85+
'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];
86+
87+
// Second incorrect event: variable 'courseid' is not a integer.
88+
$incorrecteevent2 = ['courseid' => '6', 'modulename' => 'pluginname', 'instancename' => 'instance1', 'content' => 'hello',
89+
'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];
90+
91+
// Two completely correct events.
92+
$correctevent1 = ['courseid' => 12, 'modulename' => 'pluginname', 'instancename' => 'instance1', 'content' => 'hello',
93+
'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];
94+
95+
$correctevent2 = ['courseid' => 15, 'modulename' => 'pluginname', 'instancename' => 'instance2', 'content' => 'bye',
96+
'timestart' => 123456787, 'coursemoduleid' => 16, 'eventtype' => 'eventtypeone', ];
97+
98+
// Build different combinations of the events.
99+
$this->testdata->subevents1 = [$incorrecteevent1, $correctevent1];
100+
$this->testdata->subevents2 = [$incorrecteevent2, $correctevent2];
101+
$this->testdata->subevents3 = [$correctevent1, $correctevent2];
102+
$this->testdata->subevents4 = ['arraykey' => 'incorrectsubevent'];
103+
}
104+
105+
}

tests/phpunit.xml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="lib/phpunit/phpunit.xsd"
5+
bootstrap="lib/phpunit/bootstrap.php"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
backupGlobals="false"
11+
backupStaticAttributes="false"
12+
cacheResult="false"
13+
stopOnError="false"
14+
stopOnFailure="false"
15+
stopOnIncomplete="false"
16+
stopOnSkipped="false"
17+
beStrictAboutTestsThatDoNotTestAnything="false"
18+
beStrictAboutOutputDuringTests="true"
19+
>
20+
21+
<php>
22+
<!--<const name="PHPUNIT_LONGTEST" value="1"/> uncomment to execute also slow or otherwise expensive tests-->
23+
<const name="PHPUNIT_SEQUENCE_START" value="195000"/>
24+
25+
<!--Following constants instruct tests to fetch external test files from alternative location
26+
or skip tests if empty, clone https://github.com/moodlehq/moodle-exttests to local web server-->
27+
<!--<const name="TEST_EXTERNAL_FILES_HTTP_URL" value="http://download.moodle.org/unittest"/>
28+
uncomment and alter to fetch external test files from alternative location-->
29+
<!--<const name="TEST_EXTERNAL_FILES_HTTPS_URL" value="https://download.moodle.org/unittest"/>
30+
uncomment and alter to fetch external test files from alternative location-->
31+
</php>
32+
33+
<testsuites>
34+
<testsuite name="local_townsquaresupport_testsuite">
35+
<directory suffix="_test.php">local/townsquaresupport/tests</directory>
36+
</testsuite>
37+
</testsuites>
38+
39+
</phpunit>

0 commit comments

Comments
 (0)