MDL-69726 calendar: fix raw event fetcher under postgres
authorJenkins Automation <noreply@netspot.com.au>
Fri, 18 Sep 2020 01:04:10 +0000 (10:34 +0930)
committerJake Dallimore <jake@moodle.com>
Mon, 19 Oct 2020 23:46:00 +0000 (07:46 +0800)
Without this, there's a code path that under the right circumstances is hit and
results in a query with `FROM (mdl_event) ev` in it - which is not valid syntax
under postgres.

calendar/classes/local/event/strategies/raw_event_retrieval_strategy.php
calendar/tests/raw_event_retrieval_strategy_test.php

index f442a64..46b42d7 100644 (file)
@@ -281,7 +281,7 @@ class raw_event_retrieval_strategy implements raw_event_retrieval_strategy_inter
             $unionstartquery = "SELECT modulename, instance, eventtype, priority
                                   FROM {event} ev
                                  WHERE ";
-            $subqueryunion = $unionstartquery . implode(" UNION $unionstartquery ", $subqueryconditions);
+            $subqueryunion = '('.$unionstartquery . implode(" UNION $unionstartquery ", $subqueryconditions).')';
         } else {
             $subqueryunion = '{event}';
         }
@@ -296,7 +296,7 @@ class raw_event_retrieval_strategy implements raw_event_retrieval_strategy_inter
                             ev.instance,
                             ev.eventtype,
                             MIN(ev.priority) as priority
-                       FROM ($subqueryunion) ev
+                       FROM $subqueryunion ev
                    GROUP BY ev.modulename, ev.instance, ev.eventtype";
 
         // Build the main query.
index 82cd7a8..b195eea 100644 (file)
@@ -447,4 +447,15 @@ class core_calendar_raw_event_retrieval_strategy_testcase extends advanced_testc
                 array_column($events, 'name'),
                 '', 0.0, 10, true);
     }
+
+    /**
+     * Test retrieval strategy with empty filters.
+     * This covers a edge case not covered elsewhere to ensure its SQL is cross
+     * db compatible. The test is ensuring we don't get a DML Exception with
+     * the filters setup this way.
+     */
+    public function test_get_raw_events_with_empty_user_and_category_lists() {
+        $retrievalstrategy = new raw_event_retrieval_strategy;
+        $retrievalstrategy->get_raw_events([], null, null, []);
+    }
 }