MDL-64412 core_message: get_member_info() respects provided ordering
[moodle.git] / message / tests / helper_test.php
CommitLineData
9e6734a7
JD
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Contains a test class for the message helper.
19 *
20 * @package core_message
21 * @category test
22 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
31
32/**
33 * Tests for the message helper class.
34 *
35 * @package core_message
36 * @category test
37 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class core_message_helper_testcase extends advanced_testcase {
41
42 public function setUp() {
43 $this->resetAfterTest(true);
44 }
45
46 public function test_get_member_info_ordering() {
47 // Create a conversation with several users.
48 $user1 = self::getDataGenerator()->create_user();
49 $user2 = self::getDataGenerator()->create_user();
50 $user3 = self::getDataGenerator()->create_user();
51 $user4 = self::getDataGenerator()->create_user();
52
53 \core_message\api::create_conversation(
54 \core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP,
55 [
56 $user1->id,
57 $user2->id,
58 $user3->id,
59 $user4->id,
60 ],
61 'Group conversation'
62 );
63
64 // Verify that the member information comes back in the same order that we specified in the input array.
65 $memberinfo = \core_message\helper::get_member_info($user1->id, [$user3->id, $user4->id, $user2->id]);
66 $this->assertEquals($user3->id, array_shift($memberinfo)->id);
67 $this->assertEquals($user4->id, array_shift($memberinfo)->id);
68 $this->assertEquals($user2->id, array_shift($memberinfo)->id);
69 }
70}