Skip to content

Commit

Permalink
add check for subplugin events
Browse files Browse the repository at this point in the history
  • Loading branch information
TamaroWalter committed Aug 15, 2024
1 parent 054c3aa commit 1a5fca3
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 9 deletions.
4 changes: 2 additions & 2 deletions classes/plugininfo/townsquareexpansion.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

/**
* TODO: Add description.
* File to manage subplugins from type townsquareexpansion.
*
* @package local_townsquaresupport
* @copyright 2024 Tamaro Walter
Expand All @@ -36,7 +36,7 @@ class townsquareexpansion extends base {
* @return bool
*/
public function is_uninstall_allowed(): bool {
return true;
return true; // A subplugin can be deleted without condition.
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/townsquaresupportinterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface townsquaresupportinterface {
/**
* Function to gather the events
* Every event must gain sufficient data so that townsquare can build a letter from it.
* The array should contain following information:
* The array must contain following information:
* [courseid] => int Course ID from where the content comes from.
* [modulename] => string Name of the activity module.
* [instancename] => string Name of the instance that shows the notification.
Expand Down
2 changes: 1 addition & 1 deletion db/subplugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@

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

$subplugins = ['townsquareexpantion' => 'local/townsquaresupport/townsquareexpansion'];
$subplugins = ['townsquareexpansion' => 'local/townsquaresupport/townsquareexpansion'];
1 change: 1 addition & 0 deletions lang/en/local_townsquaresupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@

$string['pluginname'] = 'Townsquare support';
$string['plugintitle'] = 'Townsquare support plugin';
$string['subpluginerror'] = 'Error while retrieving events from an subplugin. There seems to be a coding error in the subplugin {$a->subpluginname}.';
$string['subplugintype_townsquareexpansion'] = 'Townsquare event expansion';
$string['subplugintype_townsquareexpansion_plural'] = 'Townsquare event expansions';
23 changes: 18 additions & 5 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,23 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace local_townsquaresupport;

/**
* Function to get the events from every subplugin that extends the town square.
*
* As every subplugin from townsquaresupport follows the same structure and has the get_event method located in the same
* place, this function can access it directly.
* @package local_townsquaresupport
* @package local_townsquaresupport
* @copyright 2024 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_townsquaresupport;

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

global $CFG;
require_once($CFG->dirroot . '/local/townsquaresupport/locallib.php');

/**
* Core function of the townsquaresupport plugin. Retrieves all events from the subplugins and makes them available
* to the townsquare block.
Expand All @@ -45,8 +50,16 @@ function townsquaresupport_get_subplugin_events() {
$classstring = "\\townsquareexpansion_" . $expansionname . "\\" . $expansionname;
$expansionclass = new $classstring();

// Get the events from the subplugin and add it to the events array.
$events = array_merge($events, $expansionclass->get_events());
// Get the events from the subplugin.
$subpluginevents = $expansionclass->get_events();

// Check if the events meet the requirements of the interface.
if (townsquaresupport_check_subplugin_events($subpluginevents)) {
$events = array_merge($events, $subpluginevents);
} else {
// Throw an error as there is an error in the subplugin code.
throw new \moodle_exception('subpluginerror', 'local_townsquaresupport', '', ['subpluginname' => $expansionname]);
}

}
return $events;
Expand Down
76 changes: 76 additions & 0 deletions locallib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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/>.

/**
* Internal library of functions for the townsquaresupport plugin
*
* @package local_townsquaresupport
* @copyright 2024 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_townsquaresupport;

/**
* Helper function for townsquaresupport_get_subplugin_events, that checks if an amount
* of events have all the required attributes from the townsquaresupport interface.
*
* @param array $subevents
* @return int
*/
function townsquaresupport_check_subplugin_events($subevents): int {
if (!gettype($subevents == 'array')) {
return false;
}

if ($subevents == []) {
// If no events are available, then everything is okay.
return true;
} else {

// Check every event.
foreach ($subevents as $event) {
if (gettype($event) != 'object') {
return false;
}

// Check if all variables are set.
$issetcheck = townsquaresupport_check_isset($event, 'courseid') &&
townsquaresupport_check_isset($event, 'modulename') &&
townsquaresupport_check_isset($event, 'instancename') &&
townsquaresupport_check_isset($event, 'content') &&
townsquaresupport_check_isset($event, 'timestart') &&
townsquaresupport_check_isset($event, 'coursemoduleid') &&
townsquaresupport_check_isset($event, 'eventtype');

if (!$issetcheck) {
return false;
}
}
}
return true;
}

/**
* Helper function for check_subplugin_events function that proves if a variable is set in an array.
*
* @param array $event Event that is checked.
* @param string $variablename Name of the variable
* @return bool
*/
function townsquaresupport_check_isset($event, $variablename): bool {
return isset($event->$variablename);
}
105 changes: 105 additions & 0 deletions tests/eventcheck_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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/>.

/**
* Unit tests for the local_townsquaresupport.
*
* @package local_townsquaresupport
* @copyright 2024 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_townsquaresupport;

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

global $CFG;
require_once($CFG->dirroot . '/local/townsquaresupport/locallib.php');

/**
* PHPUnit tests for testing the logic of proving if subplugin events satisfy the requirements of the townsquaresupport interface.
*
* @package local_townsquaresupport
* @copyright 2024 Tamaro Walter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @covers ::\local_townsquaresupport\townsquaresupport_check_subplugin_events()
*/
final class eventcheck_test extends \advanced_testcase {

// Attributes.

/** @var object The data that will be used for testing */
private $testdata;


// Construct functions.

public function setUp(): void {
$this->testdata = new \stdClass();
$this->resetAfterTest();
$this->helper_test_set_up();
}

public function tearDown(): void {
$this->testdata = null;
}

// Tests.

/**
* Test, if the check_subplugin_events function works correctly.
* @return void
*/
public function test_checkevents(): void {
// Test the subevents.
$this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents1));
$this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents2));
$this->assertEquals(true, townsquaresupport_check_subplugin_events($this->testdata->subevents3));
$this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents4));
}

// Helper functions.

/**
* Helper function that sets up the testdata.
* @return void
*/
private function helper_test_set_up(): void {
// Build different arrays of events that are incorrect (and one correct array).

// First incorrect event: variable 'content' is missing.
$incorrecteevent1 = ['courseid' => 12, 'modulename' => 'pluginname', 'instancename' => 'instance1',
'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];

// Second incorrect event: variable 'courseid' is not a integer.
$incorrecteevent2 = ['courseid' => '6', 'modulename' => 'pluginname', 'instancename' => 'instance1', 'content' => 'hello',
'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];

// Two completely correct events.
$correctevent1 = ['courseid' => 12, 'modulename' => 'pluginname', 'instancename' => 'instance1', 'content' => 'hello',
'timestart' => 123456789, 'coursemoduleid' => 13, 'eventtype' => 'eventtypeone', ];

$correctevent2 = ['courseid' => 15, 'modulename' => 'pluginname', 'instancename' => 'instance2', 'content' => 'bye',
'timestart' => 123456787, 'coursemoduleid' => 16, 'eventtype' => 'eventtypeone', ];

// Build different combinations of the events.
$this->testdata->subevents1 = [$incorrecteevent1, $correctevent1];
$this->testdata->subevents2 = [$incorrecteevent2, $correctevent2];
$this->testdata->subevents3 = [$correctevent1, $correctevent2];
$this->testdata->subevents4 = ['arraykey' => 'incorrectsubevent'];
}

}
39 changes: 39 additions & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="lib/phpunit/phpunit.xsd"
bootstrap="lib/phpunit/bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
backupGlobals="false"
backupStaticAttributes="false"
cacheResult="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
beStrictAboutTestsThatDoNotTestAnything="false"
beStrictAboutOutputDuringTests="true"
>

<php>
<!--<const name="PHPUNIT_LONGTEST" value="1"/> uncomment to execute also slow or otherwise expensive tests-->
<const name="PHPUNIT_SEQUENCE_START" value="195000"/>

<!--Following constants instruct tests to fetch external test files from alternative location
or skip tests if empty, clone https://github.com/moodlehq/moodle-exttests to local web server-->
<!--<const name="TEST_EXTERNAL_FILES_HTTP_URL" value="http://download.moodle.org/unittest"/>
uncomment and alter to fetch external test files from alternative location-->
<!--<const name="TEST_EXTERNAL_FILES_HTTPS_URL" value="https://download.moodle.org/unittest"/>
uncomment and alter to fetch external test files from alternative location-->
</php>

<testsuites>
<testsuite name="local_townsquaresupport_testsuite">
<directory suffix="_test.php">local/townsquaresupport/tests</directory>
</testsuite>
</testsuites>

</phpunit>

0 comments on commit 1a5fca3

Please sign in to comment.