MDL-59890 calendar: Allow event creation in behat
authorAndrew Nicols <andrew@nicols.co.uk>
Thu, 28 Sep 2017 05:50:26 +0000 (13:50 +0800)
committerAndrew Nicols <andrew@nicols.co.uk>
Wed, 4 Oct 2017 03:16:06 +0000 (11:16 +0800)
lib/testing/generator/data_generator.php
lib/tests/behat/behat_data_generators.php

index 5b0bc69..e3d42a8 100644 (file)
@@ -1103,4 +1103,59 @@ EOD;
         // Get the tool associated with this instance.
         return $DB->get_record('enrol_lti_tools', array('enrolid' => $instanceid));
     }
+
+    /**
+     * Helper function used to create an event.
+     *
+     * @param   array   $data
+     * @return  stdClass
+     */
+    public function create_event($data = []) {
+        global $CFG;
+
+        require_once($CFG->dirroot . '/calendar/lib.php');
+        $record = new \stdClass();
+        $record->name = 'event name';
+        $record->eventtype = 'global';
+        $record->repeat = 0;
+        $record->repeats = 0;
+        $record->timestart = time();
+        $record->timeduration = 0;
+        $record->timesort = 0;
+        $record->eventtype = 'user';
+        $record->courseid = 0;
+
+        foreach ($data as $key => $value) {
+            $record->$key = $value;
+        }
+
+        switch ($record->eventtype) {
+            case 'user':
+                unset($record->categoryid);
+                unset($record->courseid);
+                unset($record->groupid);
+                break;
+            case 'group':
+                unset($record->categoryid);
+                break;
+            case 'course':
+                unset($record->categoryid);
+                unset($record->groupid);
+                break;
+            case 'category':
+                unset($record->courseid);
+                unset($record->groupid);
+                break;
+            case 'global':
+                unset($record->categoryid);
+                unset($record->courseid);
+                unset($record->groupid);
+                break;
+        }
+
+        $event = new calendar_event($record);
+        $event->create($record);
+
+        return $event->properties();
+    }
 }
index b54e3d6..d0bdb8e 100644 (file)
@@ -174,6 +174,15 @@ class behat_data_generators extends behat_base {
             'datagenerator' => 'tag',
             'required' => array('name')
         ),
+        'events' => array(
+            'datagenerator' => 'event',
+            'required' => array('name', 'eventtype'),
+            'switchids' => array(
+                'user' => 'userid',
+                'course' => 'courseid',
+                'category' => 'categoryid',
+            )
+        ),
     );
 
     /**