From e374da71c03bcbf8496181cb2dd94ae680b2809d Mon Sep 17 00:00:00 2001 From: Ryan Smith <3045513+ryansmith94@users.noreply.github.com> Date: Thu, 16 Aug 2018 10:51:42 +0100 Subject: [PATCH] fix: Avoids error for unsupported events. (#227) --- src/transformer/handler.php | 16 ++++++++----- tests/core/unknown_event/test/data.json | 1 + tests/core/unknown_event/test/event.json | 4 ++++ tests/core/unknown_event/test/statements.json | 1 + tests/core/unknown_event/test/test.php | 24 +++++++++++++++++++ 5 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 tests/core/unknown_event/test/data.json create mode 100644 tests/core/unknown_event/test/event.json create mode 100644 tests/core/unknown_event/test/statements.json create mode 100644 tests/core/unknown_event/test/test.php diff --git a/src/transformer/handler.php b/src/transformer/handler.php index 828a08ba5..c1e6881e2 100644 --- a/src/transformer/handler.php +++ b/src/transformer/handler.php @@ -23,12 +23,16 @@ function handler(array $config, array $events) { $eventobj = (object) $event; try { $eventname = $eventobj->eventname; - $eventfunctionname = $eventfunctionmap[$eventname]; - $eventfunction = '\src\transformer\events\\' . $eventfunctionname; - $eventconfig = array_merge([ - 'event_function' => $eventfunction, - ], $config); - $eventstatements = $eventfunction($eventconfig, $eventobj); + if (isset($eventfunctionmap[$eventname])) { + $eventfunctionname = $eventfunctionmap[$eventname]; + $eventfunction = '\src\transformer\events\\' . $eventfunctionname; + $eventconfig = array_merge([ + 'event_function' => $eventfunction, + ], $config); + $eventstatements = $eventfunction($eventconfig, $eventobj); + } else { + $eventstatements = []; + } $transformedevent = [ 'eventid' => $eventobj->id, 'statements' => $eventstatements, diff --git a/tests/core/unknown_event/test/data.json b/tests/core/unknown_event/test/data.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/tests/core/unknown_event/test/data.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/tests/core/unknown_event/test/event.json b/tests/core/unknown_event/test/event.json new file mode 100644 index 000000000..142c28c88 --- /dev/null +++ b/tests/core/unknown_event/test/event.json @@ -0,0 +1,4 @@ +{ + "id": 1, + "eventname": "unknown_event" +} \ No newline at end of file diff --git a/tests/core/unknown_event/test/statements.json b/tests/core/unknown_event/test/statements.json new file mode 100644 index 000000000..0637a088a --- /dev/null +++ b/tests/core/unknown_event/test/statements.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tests/core/unknown_event/test/test.php b/tests/core/unknown_event/test/test.php new file mode 100644 index 000000000..4355561de --- /dev/null +++ b/tests/core/unknown_event/test/test.php @@ -0,0 +1,24 @@ +. + +namespace tests\core\unknown_event\test; +defined('MOODLE_INTERNAL') || die(); + +class test extends \tests\xapi_test_case { + protected function get_test_dir() { + return __DIR__; + } +} \ No newline at end of file