MDL-45445 events: added missing 'relateduserid' validation
authorMark Nelson <markn@moodle.com>
Wed, 7 May 2014 05:47:21 +0000 (22:47 -0700)
committerMark Nelson <markn@moodle.com>
Fri, 13 Jun 2014 03:46:58 +0000 (20:46 -0700)
30 files changed:
blog/tests/bloglib_test.php
lib/classes/event/blog_association_created.php
lib/classes/event/blog_entry_created.php
lib/classes/event/blog_entry_deleted.php
lib/classes/event/blog_entry_updated.php
lib/classes/event/cohort_member_added.php
lib/classes/event/cohort_member_removed.php
lib/classes/event/course_completed.php
lib/classes/event/email_failed.php
lib/classes/event/group_member_added.php
lib/classes/event/group_member_removed.php
lib/classes/event/note_created.php
lib/classes/event/note_deleted.php
lib/classes/event/note_updated.php
lib/classes/event/role_assigned.php
lib/classes/event/role_unassigned.php
lib/classes/event/user_graded.php
lib/classes/event/user_loggedinas.php
lib/classes/event/user_profile_viewed.php
mod/feedback/classes/event/response_deleted.php
mod/feedback/classes/event/response_submitted.php
mod/feedback/tests/events_test.php
mod/lesson/classes/event/essay_attempt_viewed.php
mod/scorm/classes/event/interactions_viewed.php
mod/scorm/classes/event/tracks_viewed.php
mod/scorm/classes/event/user_report_viewed.php
mod/workshop/classes/event/submission_assessed.php
mod/workshop/classes/event/submission_reassessed.php
mod/workshop/classes/event/submission_viewed.php
report/completion/classes/event/user_report_viewed.php

index cf1c1de..2db2265 100644 (file)
@@ -326,6 +326,7 @@ class core_bloglib_testcase extends advanced_testcase {
             \core\event\blog_association_created::create(array(
                 'contextid' => 1,
                 'objectid' => 3,
+                'relateduserid' => 2,
                 'other' => array('associateid' => 2 , 'blogid' => 3, 'subject' => 'blog subject')));
         } catch (coding_exception $e) {
             $this->assertContains('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage());
@@ -334,6 +335,7 @@ class core_bloglib_testcase extends advanced_testcase {
             \core\event\blog_association_created::create(array(
                 'contextid' => 1,
                 'objectid' => 3,
+                'relateduserid' => 2,
                 'other' => array('associateid' => 2 , 'blogid' => 3, 'associatetype' => 'random', 'subject' => 'blog subject')));
         } catch (coding_exception $e) {
             $this->assertContains('The \'associatetype\' value must be set in other and be a valid type.', $e->getMessage());
@@ -343,6 +345,7 @@ class core_bloglib_testcase extends advanced_testcase {
             \core\event\blog_association_created::create(array(
                 'contextid' => 1,
                 'objectid' => 3,
+                'relateduserid' => 2,
                 'other' => array('blogid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
         } catch (coding_exception $e) {
             $this->assertContains('The \'associateid\' value must be set in other.', $e->getMessage());
@@ -352,6 +355,7 @@ class core_bloglib_testcase extends advanced_testcase {
             \core\event\blog_association_created::create(array(
                 'contextid' => 1,
                 'objectid' => 3,
+                'relateduserid' => 2,
                 'other' => array('associateid' => 3, 'associatetype' => 'course', 'subject' => 'blog subject')));
         } catch (coding_exception $e) {
             $this->assertContains('The \'blogid\' value must be set in other.', $e->getMessage());
@@ -361,6 +365,7 @@ class core_bloglib_testcase extends advanced_testcase {
             \core\event\blog_association_created::create(array(
                 'contextid' => 1,
                 'objectid' => 3,
+                'relateduserid' => 2,
                 'other' => array('blogid' => 3, 'associateid' => 3, 'associatetype' => 'course')));
         } catch (coding_exception $e) {
             $this->assertContains('The \'subject\' value must be set in other.', $e->getMessage());
index ba083cb..62ae7c9 100644 (file)
@@ -104,14 +104,24 @@ class blog_association_created extends base {
     protected function validate_data() {
         parent::validate_data();
 
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+
         if (empty($this->other['associatetype']) || ($this->other['associatetype'] !== 'course'
                 && $this->other['associatetype'] !== 'coursemodule')) {
             throw new \coding_exception('The \'associatetype\' value must be set in other and be a valid type.');
-        } else if (!isset($this->other['blogid'])) {
+        }
+
+        if (!isset($this->other['blogid'])) {
             throw new \coding_exception('The \'blogid\' value must be set in other.');
-        } else if (!isset($this->other['associateid'])) {
+        }
+
+        if (!isset($this->other['associateid'])) {
             throw new \coding_exception('The \'associateid\' value must be set in other.');
-        } else if (!isset($this->other['subject'])) {
+        }
+
+        if (!isset($this->other['subject'])) {
             throw new \coding_exception('The \'subject\' value must be set in other.');
         }
     }
index 36acac5..1f39bed 100644 (file)
@@ -126,4 +126,18 @@ class blog_entry_created extends base {
         return array (SITEID, 'blog', 'add', 'index.php?userid=' . $this->relateduserid . '&entryid=' . $this->objectid,
             $this->blogentry->subject);
     }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index c7c3a63..2321e42 100644 (file)
@@ -117,4 +117,18 @@ class blog_entry_deleted extends base {
         return array (SITEID, 'blog', 'delete', 'index.php?userid=' . $this->relateduserid, 'deleted blog entry with entry id# '.
                 $this->objectid);
     }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 893e830..4b92371 100644 (file)
@@ -124,5 +124,19 @@ class blog_entry_updated extends base {
         return array(SITEID, 'blog', 'update', 'index.php?userid=' . $this->relateduserid . '&entryid=' . $this->objectid,
                  $this->blogentry->subject);
     }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
 
index 6a944ea..018d854 100644 (file)
@@ -94,4 +94,18 @@ class cohort_member_added extends base {
         $data->userid = $this->relateduserid;
         return $data;
     }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 68b29e3..25c902a 100644 (file)
@@ -95,4 +95,18 @@ class cohort_member_removed extends base {
         $data->userid = $this->relateduserid;
         return $data;
     }
+
+    /**
+     * Custom validations.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index cd4bbdc..ed51fa9 100644 (file)
@@ -124,6 +124,14 @@ class course_completed extends base {
     protected function validate_data() {
         parent::validate_data();
 
-        // TODO: MDL-45445 add validation of relateduserid and other['relateduserid'].
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+
+        // Check that the 'relateduserid' value is set in other as well. This is because we introduced this in 2.6
+        // and some observers may be relying on this value to be present.
+        if (!isset($this->other['relateduserid'])) {
+            throw new \coding_exception('The \'relateduserid\' value must be set in other.');
+        }
     }
 }
index d62e2c7..461f458 100644 (file)
@@ -80,6 +80,10 @@ class email_failed extends base {
      */
     protected function validate_data() {
         parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
         if (!isset($this->other['subject'])) {
             throw new \coding_exception('The \'subject\' value must be set in other.');
         }
index 2cb197f..1da73bc 100644 (file)
@@ -113,6 +113,10 @@ class group_member_added extends base {
     protected function validate_data() {
         parent::validate_data();
 
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+
         if (!isset($this->other['component'])) {
             throw new \coding_exception('The \'component\' value must be set in other, even if empty.');
         }
index 6fca127..1acc778 100644 (file)
@@ -94,4 +94,18 @@ class group_member_removed extends base {
         $this->data['edulevel'] = self::LEVEL_OTHER;
         $this->data['objecttable'] = 'groups';
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 62ce5bd..dc303f4 100644 (file)
@@ -92,4 +92,18 @@ class note_created extends base {
         $logurl->set_anchor('note-' . $this->objectid);
         return array($this->courseid, 'notes', 'add', $logurl, 'add note');
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 8af710b..6f44605 100644 (file)
@@ -82,4 +82,18 @@ class note_deleted extends base {
         $logurl->set_anchor('note-' . $this->objectid);
         return array($this->courseid, 'notes', 'delete', $logurl, 'delete note');
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 43a0ed8..08b4dcd 100644 (file)
@@ -92,4 +92,18 @@ class note_updated extends base {
         $logurl->set_anchor('note-' . $this->objectid);
         return array($this->courseid, 'notes', 'update', $logurl, 'update note');
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 4b86c8c..85c6051 100644 (file)
@@ -109,4 +109,18 @@ class role_assigned extends base {
         return array($this->courseid, 'role', 'assign', 'admin/roles/assign.php?contextid='.$this->contextid.'&roleid='.$this->objectid,
                 $rolenames[$this->objectid], '', $this->userid);
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 31f00a0..f0888be 100644 (file)
@@ -106,4 +106,18 @@ class role_unassigned extends base {
         return array($this->courseid, 'role', 'unassign', 'admin/roles/assign.php?contextid='.$this->contextid.'&roleid='.$this->objectid,
                 $rolenames[$this->objectid], '', $this->userid);
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 157b90b..61c8980 100644 (file)
@@ -138,4 +138,18 @@ class user_graded extends base {
 
         return array($this->courseid, 'grade', 'update', $url, $info);
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception when validation does not pass.
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 2e6b4dd..0e32b29 100644 (file)
@@ -90,4 +90,18 @@ class user_loggedinas extends base {
     public function get_url() {
         return new \moodle_url('/user/view.php', array('id' => $this->objectid));
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception when validation does not pass.
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index d120303..277aa97 100644 (file)
@@ -97,4 +97,18 @@ class user_profile_viewed extends base {
         }
         return null;
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception when validation does not pass.
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 686f757..00a1105 100644 (file)
@@ -113,6 +113,9 @@ class response_deleted extends \core\event\base {
     protected function validate_data() {
         parent::validate_data();
 
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
         if (!isset($this->other['anonymous'])) {
             throw new \coding_exception('The \'anonymous\' value must be set in other.');
         }
index 8dc8d98..acd1dcc 100644 (file)
@@ -135,6 +135,9 @@ class response_submitted extends \core\event\base {
     protected function validate_data() {
         parent::validate_data();
 
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
         if (!isset($this->other['anonymous'])) {
             throw new \coding_exception('The \'anonymous\' value must be set in other.');
         }
index 300c231..8413388 100644 (file)
@@ -184,7 +184,8 @@ class mod_feedback_events_testcase extends advanced_testcase {
         try {
             \mod_feedback\event\response_submitted::create(array(
                 'context'  => $context,
-                'objectid' => $this->eventfeedbackcompleted->id
+                'objectid' => $this->eventfeedbackcompleted->id,
+                'relateduserid' => 2,
             ));
             $this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
                     other['anonymous']");
@@ -289,6 +290,7 @@ class mod_feedback_events_testcase extends advanced_testcase {
             \mod_feedback\event\response_submitted::create(array(
                 'context'  => $context,
                 'objectid' => $this->eventfeedbackcompleted->id,
+                'relateduserid' => 2,
                 'anonymous' => 0,
                 'other'    => array('cmid' => $this->eventcm->id, 'anonymous' => 2)
             ));
@@ -303,6 +305,7 @@ class mod_feedback_events_testcase extends advanced_testcase {
             \mod_feedback\event\response_submitted::create(array(
                 'context'  => $context,
                 'objectid' => $this->eventfeedbackcompleted->id,
+                'relateduserid' => 2,
                 'anonymous' => 0,
                 'other'    => array('instanceid' => $this->eventfeedback->id, 'anonymous' => 2)
             ));
@@ -317,6 +320,7 @@ class mod_feedback_events_testcase extends advanced_testcase {
             \mod_feedback\event\response_submitted::create(array(
                  'context'  => $context,
                  'objectid' => $this->eventfeedbackcompleted->id,
+                 'relateduserid' => 2,
                  'other'    => array('cmid' => $this->eventcm->id, 'instanceid' => $this->eventfeedback->id)
             ));
             $this->fail("Event validation should not allow \\mod_feedback\\event\\response_deleted to be triggered without
index 8d1ac19..5e17922 100644 (file)
@@ -83,4 +83,18 @@ class essay_attempt_viewed extends \core\event\base {
         return array($this->courseid, 'lesson', 'view grade', 'essay.php?id=' . $this->contextinstanceid . '&mode=grade&attemptid='
             . $this->objectid, get_string('manualgrading', 'lesson'), $this->contextinstanceid);
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index face571..91dcc8e 100644 (file)
@@ -103,6 +103,10 @@ class interactions_viewed extends \core\event\base {
     protected function validate_data() {
         parent::validate_data();
 
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+
         if (empty($this->other['attemptid'])) {
             throw new \coding_exception('The \'attemptid\' must be set in other.');
         }
index 1420095..d76812d 100644 (file)
@@ -105,6 +105,9 @@ class tracks_viewed extends \core\event\base {
     protected function validate_data() {
         parent::validate_data();
 
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
         if (empty($this->other['attemptid'])) {
             throw new \coding_exception('The \'attemptid\' value must be set in other.');
         }
index 9fece2d..b91ad55 100644 (file)
@@ -101,6 +101,10 @@ class user_report_viewed extends \core\event\base {
      */
     protected function validate_data() {
         parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
         if (empty($this->other['attemptid'])) {
             throw new \coding_exception('The \'attemptid\' value must be set in other.');
         }
index d878233..7c7e4dc 100644 (file)
@@ -90,4 +90,18 @@ class submission_assessed extends \core\event\base {
     public function get_url() {
         return new \moodle_url('/mod/workshop/assessment.php', array('asid' => $this->objectid));
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 5d4393d..eb05f03 100644 (file)
@@ -91,4 +91,18 @@ class submission_reassessed extends \core\event\base {
     public function get_url() {
         return new \moodle_url('/mod/workshop/assessment.php?', array('asid' => $this->objectid));
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index 5c2e312..ffe6013 100644 (file)
@@ -88,4 +88,18 @@ class submission_viewed extends \core\event\base {
             'submission.php?cmid=' . $this->contextinstanceid . '&id=' . $this->objectid,
             $this->objectid, $this->contextinstanceid);
     }
+
+    /**
+     * Custom validation.
+     *
+     * @throws \coding_exception
+     * @return void
+     */
+    protected function validate_data() {
+        parent::validate_data();
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
+    }
 }
index ca046c5..f8193a8 100644 (file)
@@ -94,5 +94,9 @@ class user_report_viewed extends \core\event\base {
         if ($this->contextlevel != CONTEXT_COURSE) {
             throw new \coding_exception('Context level must be CONTEXT_COURSE.');
         }
+
+        if (!isset($this->relateduserid)) {
+            throw new \coding_exception('The \'relateduserid\' must be set.');
+        }
     }
 }