From 39b744347db05292681bbf9687e4e9e884689bf4 Mon Sep 17 00:00:00 2001 From: TamaroWalter Date: Tue, 14 May 2024 16:15:31 +0200 Subject: [PATCH 1/7] delete unnecessary line in gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4865013..3eef2fb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ townsquareexpansion/* -!townsquareexpansion/readme.md !townsquareexpansion/README.md From 9ddf93cbfbe7fc5161927ac8d397601afe71badf Mon Sep 17 00:00:00 2001 From: TamaroWalter Date: Fri, 7 Jun 2024 16:29:31 +0200 Subject: [PATCH 2/7] fix in language file --- lang/en/local_townsquaresupport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/en/local_townsquaresupport.php b/lang/en/local_townsquaresupport.php index 99f4aa3..752826a 100644 --- a/lang/en/local_townsquaresupport.php +++ b/lang/en/local_townsquaresupport.php @@ -26,8 +26,8 @@ defined('MOODLE_INTERNAL') || die(); // Core strings for the installation. -$string['pluginname'] = 'Townsquare support plugin'; $string['pluginname'] = 'Townsquare support'; +$string['plugintitle'] = 'Townsquare support plugin'; // Subplugin strings. $string['subplugintype_townsquareexpansion'] = 'Townsquare event expansion'; From 054c3aad4a28015013feec3dab1cf40079e3a23a Mon Sep 17 00:00:00 2001 From: TamaroWalter Date: Thu, 13 Jun 2024 11:35:29 +0200 Subject: [PATCH 3/7] codecleaning --- classes/townsquaresupportinterface.php | 7 +++++++ lang/en/local_townsquaresupport.php | 3 --- lib.php | 10 ++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/classes/townsquaresupportinterface.php b/classes/townsquaresupportinterface.php index 50ad503..86ee091 100644 --- a/classes/townsquaresupportinterface.php +++ b/classes/townsquaresupportinterface.php @@ -31,6 +31,13 @@ */ namespace local_townsquaresupport; +/** + * Interface that need to be implemented + * + * @package local_townsquaresupport + * @copyright 2024 Tamaro Walter + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ interface townsquaresupportinterface { /** * Function to gather the events diff --git a/lang/en/local_townsquaresupport.php b/lang/en/local_townsquaresupport.php index 752826a..402f634 100644 --- a/lang/en/local_townsquaresupport.php +++ b/lang/en/local_townsquaresupport.php @@ -25,10 +25,7 @@ defined('MOODLE_INTERNAL') || die(); -// Core strings for the installation. $string['pluginname'] = 'Townsquare support'; $string['plugintitle'] = 'Townsquare support plugin'; - -// Subplugin strings. $string['subplugintype_townsquareexpansion'] = 'Townsquare event expansion'; $string['subplugintype_townsquareexpansion_plural'] = 'Townsquare event expansions'; diff --git a/lib.php b/lib.php index a0b9a54..a2b3176 100644 --- a/lib.php +++ b/lib.php @@ -21,6 +21,16 @@ * * 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 + * @copyright 2024 Tamaro Walter + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Core function of the townsquaresupport plugin. Retrieves all events from the subplugins and makes them available + * to the townsquare block. + * + * @return array */ function townsquaresupport_get_subplugin_events() { From 1a5fca3fcd521d349dd60b90e4b22a1a1e0811ee Mon Sep 17 00:00:00 2001 From: TamaroWalter Date: Thu, 15 Aug 2024 14:17:52 -0700 Subject: [PATCH 4/7] add check for subplugin events --- classes/plugininfo/townsquareexpansion.php | 4 +- classes/townsquaresupportinterface.php | 2 +- db/subplugins.php | 2 +- lang/en/local_townsquaresupport.php | 1 + lib.php | 23 ++++- locallib.php | 76 +++++++++++++++ tests/eventcheck_test.php | 105 +++++++++++++++++++++ tests/phpunit.xml | 39 ++++++++ 8 files changed, 243 insertions(+), 9 deletions(-) create mode 100644 locallib.php create mode 100644 tests/eventcheck_test.php create mode 100644 tests/phpunit.xml diff --git a/classes/plugininfo/townsquareexpansion.php b/classes/plugininfo/townsquareexpansion.php index 497d699..51045b8 100644 --- a/classes/plugininfo/townsquareexpansion.php +++ b/classes/plugininfo/townsquareexpansion.php @@ -15,7 +15,7 @@ // along with Moodle. If not, see . /** - * TODO: Add description. + * File to manage subplugins from type townsquareexpansion. * * @package local_townsquaresupport * @copyright 2024 Tamaro Walter @@ -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. } /** diff --git a/classes/townsquaresupportinterface.php b/classes/townsquaresupportinterface.php index 86ee091..8caaba9 100644 --- a/classes/townsquaresupportinterface.php +++ b/classes/townsquaresupportinterface.php @@ -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. diff --git a/db/subplugins.php b/db/subplugins.php index 46f5980..0f918bd 100644 --- a/db/subplugins.php +++ b/db/subplugins.php @@ -24,4 +24,4 @@ defined('MOODLE_INTERNAL') || die(); -$subplugins = ['townsquareexpantion' => 'local/townsquaresupport/townsquareexpansion']; +$subplugins = ['townsquareexpansion' => 'local/townsquaresupport/townsquareexpansion']; diff --git a/lang/en/local_townsquaresupport.php b/lang/en/local_townsquaresupport.php index 402f634..f258506 100644 --- a/lang/en/local_townsquaresupport.php +++ b/lang/en/local_townsquaresupport.php @@ -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'; diff --git a/lib.php b/lib.php index a2b3176..ebcd582 100644 --- a/lib.php +++ b/lib.php @@ -14,18 +14,23 @@ // You should have received a copy of the GNU General Public License // along with Moodle. If not, see . -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. @@ -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; diff --git a/locallib.php b/locallib.php new file mode 100644 index 0000000..f55d071 --- /dev/null +++ b/locallib.php @@ -0,0 +1,76 @@ +. + +/** + * 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); +} diff --git a/tests/eventcheck_test.php b/tests/eventcheck_test.php new file mode 100644 index 0000000..ec3c1ab --- /dev/null +++ b/tests/eventcheck_test.php @@ -0,0 +1,105 @@ +. + +/** + * 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']; + } + +} diff --git a/tests/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 0000000..e1b0f34 --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + local/townsquaresupport/tests + + + + From d51288a84858cf876900797ea4082bfc657a9341 Mon Sep 17 00:00:00 2001 From: TamaroWalter Date: Fri, 30 Aug 2024 10:41:06 +0200 Subject: [PATCH 5/7] new moodle ci --- .github/workflows/moodle-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/moodle-ci.yml b/.github/workflows/moodle-ci.yml index 2c5e23d..6acb741 100644 --- a/.github/workflows/moodle-ci.yml +++ b/.github/workflows/moodle-ci.yml @@ -50,7 +50,7 @@ jobs: - name: Initialise moodle-plugin-ci run: | - composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3 + composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci 4.5.4 echo $(cd ci/bin; pwd) >> $GITHUB_PATH echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH sudo locale-gen en_AU.UTF-8 @@ -176,7 +176,7 @@ jobs: - name: Initialise moodle-plugin-ci run: | - composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3 + composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci 4.5.4 echo $(cd ci/bin; pwd) >> $GITHUB_PATH echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH sudo locale-gen en_AU.UTF-8 From 41ab0d2ad4b1edb16bca817d4fb7263cf596397d Mon Sep 17 00:00:00 2001 From: TamaroWalter Date: Wed, 18 Sep 2024 18:16:31 +0200 Subject: [PATCH 6/7] fix wrong unit test --- locallib.php | 4 ++-- tests/eventcheck_test.php | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/locallib.php b/locallib.php index f55d071..34fc291 100644 --- a/locallib.php +++ b/locallib.php @@ -29,9 +29,9 @@ * of events have all the required attributes from the townsquaresupport interface. * * @param array $subevents - * @return int + * @return bool */ -function townsquaresupport_check_subplugin_events($subevents): int { +function townsquaresupport_check_subplugin_events($subevents): bool { if (!gettype($subevents == 'array')) { return false; } diff --git a/tests/eventcheck_test.php b/tests/eventcheck_test.php index ec3c1ab..bdcea33 100644 --- a/tests/eventcheck_test.php +++ b/tests/eventcheck_test.php @@ -96,9 +96,9 @@ private function helper_test_set_up(): void { '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->subevents1 = [(object)$incorrecteevent1, (object)$correctevent1]; + $this->testdata->subevents2 = [(object)$incorrecteevent2, (object)$correctevent2]; + $this->testdata->subevents3 = [(object)$correctevent1, (object)$correctevent2]; $this->testdata->subevents4 = ['arraykey' => 'incorrectsubevent']; } From 4d5db86cc18dabccd73f1ad3b926988f87d2d05b Mon Sep 17 00:00:00 2001 From: TamaroWalter Date: Wed, 18 Sep 2024 18:25:20 +0200 Subject: [PATCH 7/7] fix unit tests --- tests/eventcheck_test.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/eventcheck_test.php b/tests/eventcheck_test.php index bdcea33..b45f8e6 100644 --- a/tests/eventcheck_test.php +++ b/tests/eventcheck_test.php @@ -66,9 +66,8 @@ public function tearDown(): 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)); + $this->assertEquals(true, townsquaresupport_check_subplugin_events($this->testdata->subevents2)); + $this->assertEquals(false, townsquaresupport_check_subplugin_events($this->testdata->subevents3)); } // Helper functions. @@ -84,10 +83,6 @@ private function helper_test_set_up(): void { $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', ]; @@ -97,9 +92,8 @@ private function helper_test_set_up(): void { // Build different combinations of the events. $this->testdata->subevents1 = [(object)$incorrecteevent1, (object)$correctevent1]; - $this->testdata->subevents2 = [(object)$incorrecteevent2, (object)$correctevent2]; - $this->testdata->subevents3 = [(object)$correctevent1, (object)$correctevent2]; - $this->testdata->subevents4 = ['arraykey' => 'incorrectsubevent']; + $this->testdata->subevents2 = [(object)$correctevent1, (object)$correctevent2]; + $this->testdata->subevents3 = ['arraykey' => 'incorrectsubevent']; } }