Proyectos de Subversion Moodle

Rev

Rev 1 | | 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
declare(strict_types=1);
18
 
19
/**
20
 * Badges test generator for Behat
21
 *
22
 * @package     core_badges
23
 * @copyright   2023 Paul Holden <paulh@moodle.com>
24
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
class behat_core_badges_generator extends behat_generator_base {
27
 
28
    /**
29
     * Get a list of the entities that can be created for this component
30
     *
31
     * @return array[]
32
     */
33
    protected function get_creatable_entities(): array {
34
        return [
35
            'Badges' => [
36
                'singular' => 'Badge',
37
                'datagenerator' => 'badge',
38
                'required' => [
39
                    'name',
1441 ariadna 40
                    'image',
1 efrain 41
                ],
42
                'switchids' => [
43
                    'course' => 'courseid',
44
                ],
45
            ],
46
            'Criterias' => [
47
                'singular' => 'Criteria',
48
                'datagenerator' => 'criteria',
49
                'required' => [
50
                    'badge',
51
                    'role',
52
                ],
53
                'switchids' => [
54
                    'badge' => 'badgeid',
55
                    'role' => 'roleid',
56
                ],
57
            ],
58
            'Issued badges' => [
59
                'singular' => 'Issued badge',
60
                'datagenerator' => 'issued_badge',
61
                'required' => [
62
                    'badge',
63
                    'user',
64
                ],
65
                'switchids' => [
66
                    'badge' => 'badgeid',
67
                    'user' => 'userid',
68
                ],
69
            ],
70
        ];
71
    }
72
 
73
    /**
74
     * Look up badge ID from given name
75
     *
76
     * @param string $name
77
     * @return int
78
     */
79
    protected function get_badge_id(string $name): int {
80
        global $DB;
81
 
82
        return (int) $DB->get_field('badge', 'id', ['name' => $name], MUST_EXIST);
83
    }
84
 
85
    /**
86
     * Pre-process badge entity
87
     *
88
     * @param array $badge
89
     * @return array
90
     */
91
    protected function preprocess_badge(array $badge): array {
92
        global $CFG;
93
 
94
        require_once("{$CFG->libdir}/badgeslib.php");
95
 
96
        // Allow text status' that correspond to badge constants.
97
        if (array_key_exists('status', $badge) && !is_numeric($badge['status'])) {
98
            $badge['status'] = constant('BADGE_STATUS_' . strtoupper($badge['status']));
99
        }
100
 
101
        return $badge;
102
    }
103
}