|
| 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 | +} |
0 commit comments