Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 smsgateway_modica;
18
 
19
use core_sms\message;
20
use core_sms\message_status;
21
use GuzzleHttp\Psr7\Response;
22
 
23
/**
24
 * Modica SMS gateway tests.
25
 *
26
 * @package    smsgateway_modica
27
 * @category   test
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     \smsgateway_modica\gateway
31
 */
32
final class gateway_test extends \advanced_testcase {
33
    public function test_send(): void {
34
        $this->resetAfterTest();
35
 
36
        $config = (object) [
37
            'modica_url' => gateway::MODICA_DEFAULT_API,
38
            'modica_application_name' => 'test_application',
39
            'modica_application_password' => 'test_password',
40
        ];
41
 
42
        $manager = \core\di::get(\core_sms\manager::class);
43
        $gw = $manager->create_gateway_instance(
44
            classname: gateway::class,
45
            name: 'modica',
46
            enabled: true,
47
            config: $config,
48
        );
49
 
50
        // Mock the http client to return a successful response.
51
        ['mock' => $mock] = $this->get_mocked_http_client();
52
 
53
        // Append the response status.
54
        $mock->append(new Response(
55
            status: 201,
56
        ));
57
 
58
        $message = $manager->send(
59
            recipientnumber: '+447123456789',
60
            content: 'Hello, world!',
61
            component: 'core',
62
            messagetype: 'test',
63
            recipientuserid: null,
64
            async: false,
65
        );
66
 
67
        $this->assertInstanceOf(message::class, $message);
68
        $this->assertIsInt($message->id);
69
        $this->assertEquals(message_status::GATEWAY_SENT, $message->status);
70
        $this->assertEquals($gw->id, $message->gatewayid);
71
        $this->assertEquals('Hello, world!', $message->content);
72
 
73
        $storedmessage = $manager->get_message(['id' => $message->id]);
74
        $this->assertEquals($message, $storedmessage);
75
 
76
        // Now let's try with failed status.
77
        $mock->append(new Response(
78
            status: 200,
79
        ));
80
 
81
        $message = $manager->send(
82
            recipientnumber: '+447123456789',
83
            content: 'Hello, world!',
84
            component: 'core',
85
            messagetype: 'test',
86
            recipientuserid: null,
87
            async: false,
88
        );
89
 
90
        $this->assertEquals(message_status::GATEWAY_FAILED, $message->status);
91
        $this->assertEquals($gw->id, $message->gatewayid);
92
    }
93
}