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
namespace core_badges\tests;
18
 
19
use badge;
20
use core_tag_tag;
21
use stdClass;
22
 
23
/**
24
 * Unit tests for badges
25
 *
26
 * @package    core_badges
27
 * @copyright  2013 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
30
 */
31
abstract class badges_testcase extends \advanced_testcase {
32
    protected $badgeid;
33
    protected $course;
34
    protected $user;
35
    protected $module;
36
    protected $coursebadge;
37
    protected $assertion;
38
 
39
    /** @var $assertion2 to define json format for Open badge version 2 */
40
    protected $assertion2;
41
 
42
    #[\Override]
43
    public static function setUpBeforeClass(): void {
44
        parent::setUpBeforeClass();
45
        static::load_requirements();
46
    }
47
 
48
    /**
49
     * Helper to load class dependencies.
50
     *
51
     * Note: This must be called in any data providers.
52
     */
53
    protected static function load_requirements(): void {
54
        global $CFG;
55
 
56
        require_once($CFG->libdir . '/badgeslib.php');
57
        require_once($CFG->dirroot . '/badges/lib.php');
58
    }
59
 
60
    #[\Override]
61
    protected function setUp(): void {
62
        global $DB, $CFG;
63
        parent::setUp();
64
        $this->resetAfterTest(true);
65
        $CFG->enablecompletion = true;
66
        $user = $this->getDataGenerator()->create_user();
67
        $fordb = new stdClass();
68
        $fordb->id = null;
69
        $fordb->name = "Test badge with 'apostrophe' and other friends (<>&@#)";
70
        $fordb->description = "Testing badges";
71
        $fordb->timecreated = time();
72
        $fordb->timemodified = time();
73
        $fordb->usercreated = $user->id;
74
        $fordb->usermodified = $user->id;
75
        $fordb->issuername = "Test issuer";
76
        $fordb->issuerurl = "http://issuer-url.domain.co.nz";
77
        $fordb->issuercontact = "issuer@example.com";
78
        $fordb->expiredate = null;
79
        $fordb->expireperiod = null;
80
        $fordb->type = BADGE_TYPE_SITE;
81
        $fordb->version = 1;
82
        $fordb->language = 'en';
83
        $fordb->courseid = null;
84
        $fordb->messagesubject = "Test message subject";
85
        $fordb->message = "Test message body";
86
        $fordb->attachment = 1;
87
        $fordb->notification = 0;
88
        $fordb->imagecaption = "Test caption image";
89
        $fordb->status = BADGE_STATUS_INACTIVE;
90
 
91
        $this->badgeid = $DB->insert_record('badge', $fordb, true);
92
 
93
        // Set the default Issuer (because OBv2 needs them).
94
        set_config('badges_defaultissuername', $fordb->issuername);
95
        set_config('badges_defaultissuercontact', $fordb->issuercontact);
96
 
97
        // Create a course with activity and auto completion tracking.
98
        $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
99
        $this->user = $this->getDataGenerator()->create_user();
100
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
101
        $this->assertNotEmpty($studentrole);
102
 
103
        // Get manual enrolment plugin and enrol user.
104
        require_once($CFG->dirroot.'/enrol/manual/locallib.php');
105
        $manplugin = enrol_get_plugin('manual');
106
        $maninstance = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
107
        $manplugin->enrol_user($maninstance, $this->user->id, $studentrole->id);
108
        $this->assertEquals(1, $DB->count_records('user_enrolments'));
109
        $completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
110
        $this->module = $this->getDataGenerator()->create_module('forum', array('course' => $this->course->id), $completionauto);
111
 
112
        // Build badge and criteria.
113
        $fordb->type = BADGE_TYPE_COURSE;
114
        $fordb->courseid = $this->course->id;
115
        $fordb->status = BADGE_STATUS_ACTIVE;
116
        $this->coursebadge = $DB->insert_record('badge', $fordb, true);
117
 
118
        // Insert Endorsement.
119
        $endorsement = new stdClass();
120
        $endorsement->badgeid = $this->coursebadge;
121
        $endorsement->issuername = "Issuer 123";
122
        $endorsement->issueremail = "issuer123@email.com";
123
        $endorsement->issuerurl = "https://example.org/issuer-123";
124
        $endorsement->dateissued = 1524567747;
125
        $endorsement->claimid = "https://example.org/robotics-badge.json";
126
        $endorsement->claimcomment = "Test endorser comment";
127
        $DB->insert_record('badge_endorsement', $endorsement, true);
128
 
129
        // Insert related badges.
130
        $badge = new badge($this->coursebadge);
131
        $clonedid = $badge->make_clone();
132
        $badgeclone = new badge($clonedid);
133
        $badgeclone->status = BADGE_STATUS_ACTIVE;
134
        $badgeclone->save();
135
 
136
        $relatebadge = new stdClass();
137
        $relatebadge->badgeid = $this->coursebadge;
138
        $relatebadge->relatedbadgeid = $clonedid;
139
        $relatebadge->relatedid = $DB->insert_record('badge_related', $relatebadge, true);
140
 
141
        // Insert a aligment.
142
        $alignment = new stdClass();
143
        $alignment->badgeid = $this->coursebadge;
144
        $alignment->targetname = 'CCSS.ELA-Literacy.RST.11-12.3';
145
        $alignment->targeturl = 'http://www.corestandards.org/ELA-Literacy/RST/11-12/3';
146
        $alignment->targetdescription = 'Test target description';
147
        $alignment->targetframework = 'CCSS.RST.11-12.3';
148
        $alignment->targetcode = 'CCSS.RST.11-12.3';
149
        $DB->insert_record('badge_alignment', $alignment, true);
150
 
151
        // Insert tags.
152
        core_tag_tag::set_item_tags('core_badges', 'badge', $badge->id, $badge->get_context(), ['tag1', 'tag2']);
153
 
154
        $this->assertion = new stdClass();
155
        $this->assertion->badge = '{"uid":"%s","recipient":{"identity":"%s","type":"email","hashed":true,"salt":"%s"},' .
156
            '"badge":"%s","verify":{"type":"hosted","url":"%s"},"issuedOn":"%d","evidence":"%s","tags":%s}';
157
        $this->assertion->class = '{"name":"%s","description":"%s","image":"%s","criteria":"%s","issuer":"%s","tags":%s}';
158
        $this->assertion->issuer = '{"name":"%s","url":"%s","email":"%s"}';
159
        // Format JSON-LD for Openbadge specification version 2.0.
160
        $this->assertion2 = new stdClass();
161
        $this->assertion2->badge = '{"recipient":{"identity":"%s","type":"email","hashed":true,"salt":"%s"},' .
162
            '"badge":{"name":"%s","description":"%s","image":"%s",' .
163
            '"criteria":{"id":"%s","narrative":"%s"},"issuer":{"name":"%s","url":"%s","email":"%s",' .
164
            '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"},' .
165
            '"tags":%s,"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"BadgeClass","version":"%s",' .
166
            '"@language":"en","related":[{"id":"%s","version":"%s","@language":"%s"}],' .
167
            '"alignments":[{"targetName":"%s","targetUrl":"%s","targetDescription":"%s","targetFramework":"%s",' .
168
            '"targetCode":"%s"}]},"verify":{"type":"hosted","url":"%s"},"issuedOn":"%s","evidence":"%s","tags":%s,' .
169
            '"@context":"https:\/\/w3id.org\/openbadges\/v2","type":"Assertion","id":"%s"}';
170
 
171
        $this->assertion2->class = '{"name":"%s","description":"%s","image":"%s",' .
172
            '"criteria":{"id":"%s","narrative":"%s"},"issuer":{"name":"%s","url":"%s","email":"%s",' .
173
            '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"},' .
174
            '"tags":%s,"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"BadgeClass","version":"%s",' .
175
            '"@language":"%s","related":[{"id":"%s","version":"%s","@language":"%s"}],' .
176
            '"alignments":[{"targetName":"%s","targetUrl":"%s","targetDescription":"%s","targetFramework":"%s",' .
177
            '"targetCode":"%s"}]}';
178
        $this->assertion2->issuer = '{"name":"%s","url":"%s","email":"%s",' .
179
            '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"}';
180
    }
181
}