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
declare(strict_types=1);
18
 
19
namespace core\task;
20
 
21
/**
22
 * File containing tests for send_new_user_passwords_task
23
 *
24
 * @package     core
25
 * @category    test
26
 * @covers      \core\task\send_new_user_passwords_task
27
 * @copyright   2025 Moodle Pty Ltd <support@moodle.com>
28
 * @author      2025 Tasio Bertomeu Gomez <tasio.bertomeu@moodle.com>
29
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 */
31
final class send_new_user_passwords_task_test extends \advanced_testcase {
32
    /**
33
     * Validate the content of the email sent to new users
34
     */
35
    public function test_send_new_user_password_task(): void {
36
        global $DB;
37
        $this->resetAfterTest();
38
        $this->setAdminUser();
39
        $user1 = self::getDataGenerator()->create_user([
40
            'username' => 'student1',
41
            'firstname' => 'StudentA',
42
            'lastname' => 'One',
43
            'email' => 's1@example.com',
44
            ]);
45
        $user2 = self::getDataGenerator()->create_user([
46
            'username' => 'student2',
47
            'firstname' => 'StudentB',
48
            'lastname' => 'One',
49
            'email' => 's2@example.com',
50
            ]);
51
        set_user_preference('create_password', 1, $user1);
52
        set_user_preference('create_password', 1, $user2);
53
 
54
        $sink = $this->redirectEmails();
55
        $task = new \core\task\send_new_user_passwords_task();
56
        ob_start();
57
        $task->execute();
58
        ob_end_clean();
59
        $emails = $sink->get_messages();
60
        $sink->close();
61
 
62
        // Email for the new users.
63
        $emailonebody = quoted_printable_decode($emails[0]->body);
64
        $this->assertStringContainsString("Hi StudentA,", $emailonebody);
65
        $this->assertStringContainsString("An account has been created for you at 'PHPUnit test site'", $emailonebody);
66
        $this->assertStringContainsString("username: student1", $emailonebody);
67
        $this->assertStringContainsString('https://www.example.com/moodle/login/?lang=en', $emailonebody);
68
        $this->assertStringContainsString('https://www.example.com/moodle/user/contactsitesupport.php', $emailonebody);
69
        $this->assertStringContainsString('PHPUnit test site: New user account', $emails[0]->subject);
70
 
71
        $emailtwobody = quoted_printable_decode($emails[1]->body);
72
        $this->assertStringContainsString("Hi StudentB,", $emailtwobody);
73
        $this->assertStringContainsString("An account has been created for you at 'PHPUnit test site'", $emailtwobody);
74
        $this->assertStringContainsString("username: student2", $emailtwobody);
75
        $this->assertStringContainsString('https://www.example.com/moodle/login/?lang=en', $emailtwobody);
76
        $this->assertStringContainsString('https://www.example.com/moodle/user/contactsitesupport.php', $emailtwobody);
77
        $this->assertStringContainsString('PHPUnit test site: New user account', $emails[1]->subject);
78
    }
79
}