Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 mod_forum;
18
 
19
use mod_forum_tests_cron_trait;
20
use mod_forum_tests_generator_trait;
21
 
22
defined('MOODLE_INTERNAL') || die();
23
 
24
global $CFG;
25
require_once($CFG->dirroot . '/mod/forum/lib.php');
26
require_once(__DIR__ . '/cron_trait.php');
27
require_once(__DIR__ . '/generator_trait.php');
28
 
29
/**
30
 * The forum module mail generation tests for groups.
31
 *
32
 * @package    mod_forum
33
 * @copyright  2013 Andrew Nicols
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class qanda_test extends \advanced_testcase {
1 efrain 37
    // Make use of the cron tester trait.
38
    use mod_forum_tests_cron_trait;
39
 
40
    // Make use of the test generator trait.
41
    use mod_forum_tests_generator_trait;
42
 
43
    /**
44
     * @var \phpunit_message_sink
45
     */
46
    protected $messagesink;
47
 
48
    /**
49
     * @var \phpunit_mailer_sink
50
     */
51
    protected $mailsink;
52
 
53
    public function setUp(): void {
54
        global $CFG;
1441 ariadna 55
        parent::setUp();
1 efrain 56
 
57
        // We must clear the subscription caches. This has to be done both before each test, and after in case of other
58
        // tests using these functions.
59
        \mod_forum\subscriptions::reset_forum_cache();
60
        \mod_forum\subscriptions::reset_discussion_cache();
61
 
62
        // Messaging is not compatible with transactions...
63
        $this->preventResetByRollback();
64
 
65
        // Catch all messages.
66
        $this->messagesink = $this->redirectMessages();
67
        $this->mailsink = $this->redirectEmails();
68
 
69
        // Forcibly reduce the maxeditingtime to a second in the past to
70
        // ensure that messages are sent out.
71
        $CFG->maxeditingtime = -1;
72
    }
73
 
74
    public function tearDown(): void {
75
        // We must clear the subscription caches. This has to be done both before each test, and after in case of other
76
        // tests using these functions.
77
        \mod_forum\subscriptions::reset_forum_cache();
78
 
79
        $this->messagesink->clear();
80
        $this->messagesink->close();
81
        unset($this->messagesink);
82
 
83
        $this->mailsink->clear();
84
        $this->mailsink->close();
85
        unset($this->mailsink);
1441 ariadna 86
        parent::tearDown();
1 efrain 87
    }
88
 
89
    /**
90
     * Test that a user who has not posted in a q&a forum does not receive
91
     * notificatinos.
92
     */
11 efrain 93
    public function test_user_has_not_posted(): void {
1 efrain 94
        global $CFG, $DB;
95
 
96
        $this->resetAfterTest(true);
97
 
98
        // Create a course, with a forum.
99
        $course = $this->getDataGenerator()->create_course();
100
 
101
        $forum = $this->getDataGenerator()->create_module('forum', [
102
            'course' => $course->id,
103
            'forcesubscribe' => FORUM_INITIALSUBSCRIBE,
104
            'groupmode' => SEPARATEGROUPS,
105
            'type' => 'qanda',
106
        ]);
107
 
108
        // Create three students:
109
        // - author, enrolled in group A; and
110
        // - recipient, enrolled in group B; and
111
        // - other, enrolled in the course, but no groups.
112
        list($author, $recipient, $otheruser) = $this->helper_create_users($course, 3);
113
 
114
        // Create one editing teacher, not in any group but with accessallgroups capability.
115
        list($editingteacher) = $this->helper_create_users($course, 1, 'editingteacher');
116
 
117
        // Post a discussion to the forum.
118
        list($discussion, $post) = $this->helper_post_to_forum($forum, $editingteacher);
119
        $reply = $this->helper_reply_to_post($post, $author);
120
        $otherreply = $this->helper_reply_to_post($post, $recipient);
121
        $DB->execute("UPDATE {forum_posts} SET modified = modified - 1");
122
        $DB->execute("UPDATE {forum_posts} SET created = created - 1");
123
        $DB->execute("UPDATE {forum_discussions} SET timemodified = timemodified - 1");
124
 
125
        // Only the author, recipient, and teachers should receive.
126
        $expect = [
127
            'author' => (object) [
128
                'userid' => $author->id,
129
                'messages' => 3,
130
            ],
131
            'recipient' => (object) [
132
                'userid' => $recipient->id,
133
                'messages' => 3,
134
            ],
135
            'otheruser' => (object) [
136
                'userid' => $otheruser->id,
137
                'messages' => 3,
138
            ],
139
            'editingteacher' => (object) [
140
                'userid' => $editingteacher->id,
141
                'messages' => 3,
142
            ],
143
        ];
144
        $this->queue_tasks_and_assert($expect);
145
        $posts = [$post, $reply, $otherreply];
146
 
147
        // No notifications should be queued.
148
        $this->send_notifications_and_assert($author, $posts);
149
        $this->send_notifications_and_assert($recipient, $posts);
150
        $this->send_notifications_and_assert($otheruser, [$post]);
151
        $this->send_notifications_and_assert($editingteacher, $posts);
152
    }
153
}