Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 11
Línea 22... Línea 22...
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
23
 */
Línea 24... Línea 24...
24
 
24
 
Línea 25... Línea 25...
25
namespace core_message\tests;
25
namespace core_message\tests;
Línea 26... Línea 26...
26
 
26
 
27
defined('MOODLE_INTERNAL') || die();
27
use stdClass;
28
 
28
 
29
/**
29
/**
30
 * The helper class providing util methods for testing.
30
 * The helper class providing util methods for testing.
31
 *
31
 *
32
 * @copyright  2018 Jake Dallimore <markn@moodle.com>
32
 * @copyright  2018 Jake Dallimore <markn@moodle.com>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
34
 */
-
 
35
class helper {
-
 
36
    /**
-
 
37
     * Send a fake message.
-
 
38
     *
-
 
39
     * {@link message_send()} does not support transaction, this function will simulate a message
-
 
40
     * sent from a user to another. We should stop using it once {@link message_send()} will support
-
 
41
     * transactions. This is not clean at all, this is just used to add rows to the table.
-
 
42
     *
-
 
43
     * @param \stdClass $userfrom user object of the one sending the message.
-
 
44
     * @param \stdClass $userto user object of the one receiving the message.
-
 
45
     * @param string $message message to send.
-
 
46
     * @param int $notification if the message is a notification.
-
 
47
     * @param int $time the time the message was sent
-
 
48
     * @return int the id of the message
-
 
49
     */
-
 
50
    public static function send_fake_message(
-
 
51
        stdClass $userfrom,
-
 
52
        stdClass $userto,
-
 
53
        string $message = 'Hello world!',
-
 
54
        int $notification = 0,
-
 
55
        int $time = 0,
-
 
56
    ): int {
-
 
57
        global $DB;
-
 
58
 
-
 
59
        if (empty($time)) {
-
 
60
            $time = time();
-
 
61
        }
-
 
62
 
-
 
63
        if ($notification) {
-
 
64
            $record = new \stdClass();
-
 
65
            $record->useridfrom = $userfrom->id;
-
 
66
            $record->useridto = $userto->id;
-
 
67
            $record->subject = 'No subject';
-
 
68
            $record->fullmessage = $message;
-
 
69
            $record->smallmessage = $message;
-
 
70
            $record->timecreated = $time;
-
 
71
 
-
 
72
            return $DB->insert_record('notifications', $record);
-
 
73
        }
-
 
74
 
-
 
75
        if ($userfrom->id == $userto->id) {
-
 
76
            // It's a self conversation.
-
 
77
            $conversation = \core_message\api::get_self_conversation($userfrom->id);
-
 
78
            if (empty($conversation)) {
-
 
79
                $conversation = \core_message\api::create_conversation(
-
 
80
                    \core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
-
 
81
                    [$userfrom->id],
-
 
82
                );
-
 
83
            }
-
 
84
            $conversationid = $conversation->id;
-
 
85
        } else if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
-
 
86
            // It's an individual conversation between two different users.
-
 
87
            $conversation = \core_message\api::create_conversation(
-
 
88
                \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
-
 
89
                [
-
 
90
                    $userfrom->id,
-
 
91
                    $userto->id,
-
 
92
                ]
-
 
93
            );
-
 
94
            $conversationid = $conversation->id;
-
 
95
        }
-
 
96
 
-
 
97
        // Ok, send the message.
-
 
98
        $record = (object) [
-
 
99
            'useridfrom' => $userfrom->id,
-
 
100
            'conversationid' => $conversationid,
-
 
101
            'subject' => 'No subject',
-
 
102
            'fullmessage' => $message,
-
 
103
            'smallmessage' => $message,
-
 
104
            'timecreated' => $time,
-
 
105
        ];
-
 
106
 
-
 
107
        return $DB->insert_record('messages', $record);
34
 */
108
    }
35
class helper {
109
 
36
    /**
110
    /**
37
     * Sends a message to a conversation.
111
     * Sends a message to a conversation.
38
     *
112
     *