| 1441 |
ariadna |
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 |
namespace message_sms;
|
|
|
18 |
|
|
|
19 |
use core_sms\message;
|
|
|
20 |
use core_sms\message_status;
|
|
|
21 |
use core_sms\task\send_sms_task;
|
|
|
22 |
use mod_assign\notification_helper;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* SMS processor tests.
|
|
|
26 |
*
|
|
|
27 |
* @package message_sms
|
|
|
28 |
* @copyright 2025 Safat Shahin <safat.shahin@moodle.com>
|
|
|
29 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
30 |
* @covers \message_output_sms
|
|
|
31 |
*/
|
|
|
32 |
final class message_output_sms_test extends \advanced_testcase {
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Test the SMS output for the SMS processor.
|
|
|
36 |
*/
|
|
|
37 |
public function test_sms_output(): void {
|
|
|
38 |
global $CFG;
|
|
|
39 |
require_once($CFG->dirroot . "/sms/tests/fixtures/dummy_gateway.php");
|
|
|
40 |
|
|
|
41 |
$this->preventResetByRollback();
|
|
|
42 |
$this->resetAfterTest();
|
|
|
43 |
|
|
|
44 |
$config = new \stdClass();
|
|
|
45 |
$config->priority = 50;
|
|
|
46 |
|
|
|
47 |
$manager = \core\di::get(\core_sms\manager::class);
|
|
|
48 |
$manager->create_gateway_instance(
|
|
|
49 |
classname: \smsgateway_dummy\gateway::class,
|
|
|
50 |
name: 'dummy',
|
|
|
51 |
enabled: true,
|
|
|
52 |
config: $config,
|
|
|
53 |
);
|
|
|
54 |
|
|
|
55 |
// Set the configs and enable SMS.
|
|
|
56 |
\core\plugininfo\message::enable_plugin(
|
|
|
57 |
pluginname: 'sms',
|
|
|
58 |
enabled: 1,
|
|
|
59 |
);
|
|
|
60 |
// We have to use assign because this is the only component supports sms at the moment.
|
|
|
61 |
set_config('message_provider_mod_assign_assign_due_digest_enabled', 'sms', 'message');
|
|
|
62 |
|
|
|
63 |
$user = self::getDataGenerator()->create_user();
|
|
|
64 |
$user->phone2 = '+61000000000';
|
|
|
65 |
|
|
|
66 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
67 |
$CFG->noreplyuser = $user2->id;
|
|
|
68 |
|
|
|
69 |
// Send a notification.
|
|
|
70 |
$message = new \core\message\message();
|
|
|
71 |
$message->component = 'mod_assign';
|
|
|
72 |
$message->name = notification_helper::TYPE_DUE_DIGEST;
|
|
|
73 |
$message->userfrom = \core_user::get_noreply_user();
|
|
|
74 |
$message->userto = $user;
|
|
|
75 |
$message->subject = 'Hello';
|
|
|
76 |
$message->fullmessageformat = FORMAT_PLAIN;
|
|
|
77 |
$message->fullmessage = 'Hello message';
|
|
|
78 |
$message->fullmessagehtml = 'Hello message';
|
|
|
79 |
$message->smallmessage = 'Hello message';
|
|
|
80 |
$message->fullmessagesms = 'Hello sms message';
|
|
|
81 |
$message->notification = 1;
|
|
|
82 |
|
|
|
83 |
message_send($message);
|
|
|
84 |
|
|
|
85 |
$messagedbrecords = $manager->get_messages();
|
|
|
86 |
$this->assertInstanceOf(\Generator::class, $messagedbrecords);
|
|
|
87 |
$messages = iterator_to_array($messagedbrecords);
|
|
|
88 |
$this->assertCount(1, $messages);
|
|
|
89 |
|
|
|
90 |
$message = $messages[0];
|
|
|
91 |
$this->assertInstanceOf(message::class, $message);
|
|
|
92 |
$this->assertIsInt($message->id);
|
|
|
93 |
$this->assertEquals(message_status::GATEWAY_QUEUED, $message->status);
|
|
|
94 |
$this->assertEquals('Hello sms message', $message->content);
|
|
|
95 |
|
|
|
96 |
$storedmessage = $manager->get_message(['id' => $message->id]);
|
|
|
97 |
$this->assertEquals($message, $storedmessage);
|
|
|
98 |
|
|
|
99 |
$adhoctask = \core\task\manager::get_adhoc_tasks(send_sms_task::class);
|
|
|
100 |
$this->assertCount(1, $adhoctask);
|
|
|
101 |
|
|
|
102 |
// Now lets run the task and check if SMS is sent.
|
|
|
103 |
$this->run_all_adhoc_tasks();
|
|
|
104 |
|
|
|
105 |
$message = $manager->get_message(['id' => $message->id]);
|
|
|
106 |
$this->assertEquals(message_status::GATEWAY_SENT, $message->status);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
}
|