Proyectos de Subversion Moodle

Rev

| 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 tool_generator\local\testscenario;
18
 
19
/**
20
 * Tests for runner class.
21
 *
22
 * @package tool_generator
23
 * @copyright 2023 Ferran Recio <ferran@moodel.com>
24
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @coversDefaultClass \tool_generator\local\testscenario\runner
26
 */
27
class runner_test extends \advanced_testcase {
28
 
29
    /**
30
     * Test for parse_feature.
31
     * @covers ::parse_feature
32
     * @covers ::execute
33
     */
34
    public function test_parse_and_execute_feature(): void {
35
        global $CFG, $DB;
36
 
37
        $this->resetAfterTest();
38
        $this->setAdminUser();
39
 
40
        // Call the init method to include all behat libraries and attributes.
41
        $runner = new runner();
42
        $runner->init();
43
 
44
        $featurefile = $CFG->dirroot . '/admin/tool/generator/tests/fixtures/testscenario/scenario.feature';
45
        $contents = file_get_contents($featurefile);
46
        $feature = $runner->parse_feature($contents);
47
 
48
        $this->assertEquals(2, count($feature->get_scenarios()));
49
        $this->assertEquals(6, count($feature->get_all_steps()));
50
        $this->assertTrue($feature->is_valid());
51
 
52
        $result = $runner->execute($feature);
53
        $this->assertTrue($result);
54
 
55
        // Validate everything is created.
56
        $this->assertEquals(
57
            1,
58
            $DB->count_records('course', ['shortname' => 'C1'])
59
        );
60
        $course = $DB->get_record('course', ['shortname' => 'C1']);
61
        $this->assertEquals(
62
            2,
63
            $DB->count_records('course_modules', ['course' => $course->id])
64
        );
65
        $this->assertEquals(
66
            1,
67
            $DB->count_records('user', ['firstname' => 'Teacher'])
68
        );
69
        $this->assertEquals(
70
            5,
71
            $DB->count_records('user', ['firstname' => 'Student'])
72
        );
73
        $context = \context_course::instance($course->id);
74
        $this->assertEquals(
75
            6,
76
            $DB->count_records('role_assignments', ['contextid' => $context->id])
77
        );
78
    }
79
 
80
    /**
81
     * Test for parse_feature.
82
     * @covers ::parse_feature
83
     * @covers ::execute
84
     */
85
    public function test_parse_and_execute_wrong_feature(): void {
86
        global $CFG, $DB;
87
 
88
        $this->resetAfterTest();
89
        $this->setAdminUser();
90
 
91
        // Call the init method to include all behat libraries and attributes.
92
        $runner = new runner();
93
        $runner->init();
94
 
95
        $featurefile = $CFG->dirroot . '/admin/tool/generator/tests/fixtures/testscenario/scenario_wrongstep.feature';
96
        $contents = file_get_contents($featurefile);
97
        $feature = $runner->parse_feature($contents);
98
 
99
        $this->assertEquals(1, count($feature->get_scenarios()));
100
        $this->assertEquals(3, count($feature->get_all_steps()));
101
        $this->assertFalse($feature->is_valid());
102
 
103
        $result = $runner->execute($feature);
104
        $this->assertFalse($result);
105
        $this->assertEquals(0, $DB->count_records('course', ['shortname' => 'C1']));
106
    }
107
}