MDL-64666 core_message: Favourite private conversations data generator
authorAmaia Anabitarte <amaia@moodle.com>
Wed, 3 Apr 2019 16:05:55 +0000 (18:05 +0200)
committerAmaia Anabitarte <amaia@moodle.com>
Wed, 24 Apr 2019 07:55:17 +0000 (09:55 +0200)
lib/tests/behat/behat_data_generators.php

index 78c72e1..8be665f 100644 (file)
@@ -198,6 +198,16 @@ class behat_data_generators extends behat_base {
             'required' => array('user', 'contact'),
             'switchids' => array('user' => 'userid', 'contact' => 'contactid')
         ),
+        'private messages' => array(
+            'datagenerator' => 'private_messages',
+            'required' => array('user', 'contact', 'message'),
+            'switchids' => array('user' => 'userid', 'contact' => 'contactid')
+        ),
+        'favourite conversations' => array(
+            'datagenerator' => 'favourite_conversations',
+            'required' => array('user', 'contact'),
+            'switchids' => array('user' => 'userid', 'contact' => 'contactid')
+        ),
     );
 
     /**
@@ -876,4 +886,38 @@ class behat_data_generators extends behat_base {
         }
         return $id;
     }
+
+    /**
+     * Send a new message from user to contact in a private conversation
+     *
+     * @param array $data
+     * @return void
+     */
+    protected function process_private_messages(array $data) {
+        if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
+            $conversation = \core_message\api::create_conversation(
+                \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+                [$data['userid'], $data['contactid']]
+            );
+            $conversationid = $conversation->id;
+        }
+        \core_message\api::send_message_to_conversation($data['userid'], $conversationid, $data['message'], FORMAT_PLAIN);
+    }
+
+    /**
+     * Mark a private conversation as favourite for user
+     *
+     * @param array $data
+     * @return void
+     */
+    protected function process_favourite_conversations(array $data) {
+        if (!$conversationid = \core_message\api::get_conversation_between_users([$data['userid'], $data['contactid']])) {
+            $conversation = \core_message\api::create_conversation(
+                \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
+                [$data['userid'], $data['contactid']]
+            );
+            $conversationid = $conversation->id;
+        }
+        \core_message\api::set_favourite_conversation($conversationid, $data['userid']);
+    }
 }