Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - https://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_workshop\task;
18
 
19
use workshop;
20
 
21
defined('MOODLE_INTERNAL') || die();
22
 
23
global $CFG;
24
 
25
require_once($CFG->dirroot.'/mod/workshop/lib.php');
26
 
27
/**
28
 * Test the functionality provided by  the {@link mod_workshop\task\cron_task} scheduled task.
29
 *
30
 * @package     mod_workshop
31
 * @category    test
32
 * @copyright 2019 David Mudrák <david@moodle.com>
33
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class cron_task_test extends \advanced_testcase {
36
 
37
    /**
38
     * Test that the phase is automatically switched after the submissions deadline.
39
     */
40
    public function test_phase_switching() {
41
        global $DB;
42
 
43
        $this->resetAfterTest();
44
        $this->setAdminUser();
45
 
46
        // Set up a test workshop with 'Switch to the next phase after the submissions deadline' enabled.
47
        $generator = $this->getDataGenerator();
48
        $course = $generator->create_course();
49
        $workshop = $generator->create_module('workshop', [
50
            'course' => $course,
51
            'name' => 'Test Workshop',
52
        ]);
53
 
54
        $DB->update_record('workshop', [
55
            'id' => $workshop->id,
56
            'phase' => workshop::PHASE_SUBMISSION,
57
            'phaseswitchassessment' => 1,
58
            'submissionend' => time() - 1,
59
        ]);
60
 
61
        // Execute the cron.
62
        ob_start();
63
        \core\cron::setup_user();
64
        $cron = new \mod_workshop\task\cron_task();
65
        $cron->execute();
66
        $output = ob_get_contents();
67
        ob_end_clean();
68
 
69
        // Assert that the phase has been switched.
70
        $this->assertStringContainsString('Processing automatic assessment phase switch', $output);
71
        $this->assertEquals(workshop::PHASE_ASSESSMENT, $DB->get_field('workshop', 'phase', ['id' => $workshop->id]));
72
    }
73
 
74
    public function test_that_phase_automatically_switched_event_is_triggerd_when_phase_switchassesment_is_active(): void {
75
 
76
        global $DB;
77
 
78
        $this->resetAfterTest();
79
        $this->setAdminUser();
80
 
81
        // Set up a test workshop with 'Switch to the next phase after the submissions deadline' enabled.
82
        $generator = $this->getDataGenerator();
83
        $course = $generator->create_course();
84
        $workshop = $generator->create_module('workshop', [
85
            'course' => $course,
86
            'name' => 'Test Workshop',
87
        ]);
88
 
89
        $DB->update_record('workshop', [
90
            'id' => $workshop->id,
91
            'phase' => workshop::PHASE_SUBMISSION,
92
            'phaseswitchassessment' => 1,
93
            'submissionend' => time() - 1,
94
        ]);
95
 
96
        // Execute the cron.
97
        $eventsink = $this->redirectEvents();
98
        ob_start();
99
        \core\cron::setup_user();
100
        $cron = new \mod_workshop\task\cron_task();
101
        $cron->execute();
102
        ob_end_clean();
103
 
104
        $events = array_filter($eventsink->get_events(), function ($event) {
105
            return $event instanceof \mod_workshop\event\phase_automatically_switched;
106
        });
107
 
108
        $this->assertCount(1, $events);
109
 
110
        $phaseswitchedevent = array_pop($events);
111
        $this->assertArrayHasKey('previousworkshopphase', $phaseswitchedevent->other);
112
        $this->assertArrayHasKey('targetworkshopphase', $phaseswitchedevent->other);
113
 
114
        $this->assertEquals($phaseswitchedevent->other['previousworkshopphase'], \workshop::PHASE_SUBMISSION);
115
        $this->assertEquals($phaseswitchedevent->other['targetworkshopphase'], \workshop::PHASE_ASSESSMENT);
116
    }
117
}