Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea -... Línea 16...
-
 
16
 
-
 
17
use core_badges\helper;
-
 
18
use core_badges\tests\badges_testcase;
-
 
19
use core\task\manager;
16
 
20
 
17
/**
21
/**
18
 * Unit tests for badges
22
 * Unit tests for badges
19
 *
23
 *
20
 * @package    core
-
 
21
 * @subpackage badges
24
 * @package    core_badges
22
 * @copyright  2013 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
25
 * @copyright  2013 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
27
 * @author     Yuliya Bozhko <yuliya.bozhko@totaralms.com>
25
 */
-
 
26
 
-
 
27
defined('MOODLE_INTERNAL') || die();
-
 
28
 
-
 
29
global $CFG;
-
 
30
require_once($CFG->libdir . '/badgeslib.php');
-
 
31
require_once($CFG->dirroot . '/badges/lib.php');
-
 
32
 
-
 
33
use core_badges\helper;
-
 
34
use core\task\manager;
-
 
35
 
28
 */
36
class badgeslib_test extends advanced_testcase {
29
final class badgeslib_test extends badges_testcase {
37
    protected $badgeid;
30
    protected $badgeid;
38
    protected $course;
31
    protected $course;
39
    protected $user;
32
    protected $user;
40
    protected $module;
33
    protected $module;
41
    protected $coursebadge;
34
    protected $coursebadge;
Línea 42... Línea 35...
42
    protected $assertion;
35
    protected $assertion;
43
 
36
 
Línea 44... Línea -...
44
    /** @var $assertion2 to define json format for Open badge version 2 */
-
 
45
    protected $assertion2;
-
 
46
 
-
 
47
    protected function setUp(): void {
-
 
48
        global $DB, $CFG;
-
 
49
        $this->resetAfterTest(true);
-
 
50
        $CFG->enablecompletion = true;
-
 
51
        $user = $this->getDataGenerator()->create_user();
-
 
52
        $fordb = new stdClass();
-
 
53
        $fordb->id = null;
-
 
54
        $fordb->name = "Test badge with 'apostrophe' and other friends (<>&@#)";
-
 
55
        $fordb->description = "Testing badges";
-
 
56
        $fordb->timecreated = time();
-
 
57
        $fordb->timemodified = time();
-
 
58
        $fordb->usercreated = $user->id;
-
 
59
        $fordb->usermodified = $user->id;
-
 
60
        $fordb->issuername = "Test issuer";
-
 
61
        $fordb->issuerurl = "http://issuer-url.domain.co.nz";
-
 
62
        $fordb->issuercontact = "issuer@example.com";
-
 
63
        $fordb->expiredate = null;
-
 
64
        $fordb->expireperiod = null;
-
 
65
        $fordb->type = BADGE_TYPE_SITE;
-
 
66
        $fordb->version = 1;
-
 
67
        $fordb->language = 'en';
-
 
68
        $fordb->courseid = null;
-
 
69
        $fordb->messagesubject = "Test message subject";
-
 
70
        $fordb->message = "Test message body";
-
 
71
        $fordb->attachment = 1;
-
 
72
        $fordb->notification = 0;
-
 
73
        $fordb->imageauthorname = "Image Author 1";
-
 
74
        $fordb->imageauthoremail = "author@example.com";
-
 
75
        $fordb->imageauthorurl = "http://author-url.example.com";
-
 
76
        $fordb->imagecaption = "Test caption image";
-
 
77
        $fordb->status = BADGE_STATUS_INACTIVE;
-
 
78
 
-
 
79
        $this->badgeid = $DB->insert_record('badge', $fordb, true);
-
 
80
 
-
 
81
        // Set the default Issuer (because OBv2 needs them).
-
 
82
        set_config('badges_defaultissuername', $fordb->issuername);
-
 
83
        set_config('badges_defaultissuercontact', $fordb->issuercontact);
-
 
84
 
-
 
85
        // Create a course with activity and auto completion tracking.
-
 
86
        $this->course = $this->getDataGenerator()->create_course(array('enablecompletion' => true));
-
 
87
        $this->user = $this->getDataGenerator()->create_user();
-
 
88
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
-
 
89
        $this->assertNotEmpty($studentrole);
-
 
90
 
-
 
91
        // Get manual enrolment plugin and enrol user.
-
 
92
        require_once($CFG->dirroot.'/enrol/manual/locallib.php');
-
 
93
        $manplugin = enrol_get_plugin('manual');
-
 
94
        $maninstance = $DB->get_record('enrol', array('courseid' => $this->course->id, 'enrol' => 'manual'), '*', MUST_EXIST);
-
 
95
        $manplugin->enrol_user($maninstance, $this->user->id, $studentrole->id);
-
 
96
        $this->assertEquals(1, $DB->count_records('user_enrolments'));
-
 
97
        $completionauto = array('completion' => COMPLETION_TRACKING_AUTOMATIC);
-
 
98
        $this->module = $this->getDataGenerator()->create_module('forum', array('course' => $this->course->id), $completionauto);
-
 
99
 
-
 
100
        // Build badge and criteria.
-
 
101
        $fordb->type = BADGE_TYPE_COURSE;
-
 
102
        $fordb->courseid = $this->course->id;
-
 
103
        $fordb->status = BADGE_STATUS_ACTIVE;
-
 
104
        $this->coursebadge = $DB->insert_record('badge', $fordb, true);
-
 
105
 
-
 
106
        // Insert Endorsement.
-
 
107
        $endorsement = new stdClass();
-
 
108
        $endorsement->badgeid = $this->coursebadge;
-
 
109
        $endorsement->issuername = "Issuer 123";
-
 
110
        $endorsement->issueremail = "issuer123@email.com";
-
 
111
        $endorsement->issuerurl = "https://example.org/issuer-123";
-
 
112
        $endorsement->dateissued = 1524567747;
-
 
113
        $endorsement->claimid = "https://example.org/robotics-badge.json";
-
 
114
        $endorsement->claimcomment = "Test endorser comment";
-
 
115
        $DB->insert_record('badge_endorsement', $endorsement, true);
-
 
116
 
-
 
117
        // Insert related badges.
-
 
118
        $badge = new badge($this->coursebadge);
-
 
119
        $clonedid = $badge->make_clone();
-
 
120
        $badgeclone = new badge($clonedid);
-
 
121
        $badgeclone->status = BADGE_STATUS_ACTIVE;
-
 
122
        $badgeclone->save();
-
 
123
 
-
 
124
        $relatebadge = new stdClass();
-
 
125
        $relatebadge->badgeid = $this->coursebadge;
-
 
126
        $relatebadge->relatedbadgeid = $clonedid;
-
 
127
        $relatebadge->relatedid = $DB->insert_record('badge_related', $relatebadge, true);
-
 
128
 
-
 
129
        // Insert a aligment.
-
 
130
        $alignment = new stdClass();
-
 
131
        $alignment->badgeid = $this->coursebadge;
-
 
132
        $alignment->targetname = 'CCSS.ELA-Literacy.RST.11-12.3';
-
 
133
        $alignment->targeturl = 'http://www.corestandards.org/ELA-Literacy/RST/11-12/3';
-
 
134
        $alignment->targetdescription = 'Test target description';
-
 
135
        $alignment->targetframework = 'CCSS.RST.11-12.3';
-
 
136
        $alignment->targetcode = 'CCSS.RST.11-12.3';
-
 
137
        $DB->insert_record('badge_alignment', $alignment, true);
-
 
138
 
-
 
139
        // Insert tags.
-
 
140
        core_tag_tag::set_item_tags('core_badges', 'badge', $badge->id, $badge->get_context(), ['tag1', 'tag2']);
-
 
141
 
-
 
142
        $this->assertion = new stdClass();
-
 
143
        $this->assertion->badge = '{"uid":"%s","recipient":{"identity":"%s","type":"email","hashed":true,"salt":"%s"},' .
-
 
144
            '"badge":"%s","verify":{"type":"hosted","url":"%s"},"issuedOn":"%d","evidence":"%s","tags":%s}';
-
 
145
        $this->assertion->class = '{"name":"%s","description":"%s","image":"%s","criteria":"%s","issuer":"%s","tags":%s}';
-
 
146
        $this->assertion->issuer = '{"name":"%s","url":"%s","email":"%s"}';
-
 
147
        // Format JSON-LD for Openbadge specification version 2.0.
-
 
148
        $this->assertion2 = new stdClass();
-
 
149
        $this->assertion2->badge = '{"recipient":{"identity":"%s","type":"email","hashed":true,"salt":"%s"},' .
-
 
150
            '"badge":{"name":"%s","description":"%s","image":"%s",' .
-
 
151
            '"criteria":{"id":"%s","narrative":"%s"},"issuer":{"name":"%s","url":"%s","email":"%s",' .
-
 
152
            '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"},' .
-
 
153
            '"tags":%s,"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"BadgeClass","version":"%s",' .
-
 
154
            '"@language":"en","related":[{"id":"%s","version":"%s","@language":"%s"}],"endorsement":"%s",' .
-
 
155
            '"alignments":[{"targetName":"%s","targetUrl":"%s","targetDescription":"%s","targetFramework":"%s",' .
-
 
156
            '"targetCode":"%s"}]},"verify":{"type":"hosted","url":"%s"},"issuedOn":"%s","evidence":"%s","tags":%s,' .
-
 
157
            '"@context":"https:\/\/w3id.org\/openbadges\/v2","type":"Assertion","id":"%s"}';
-
 
158
 
-
 
159
        $this->assertion2->class = '{"name":"%s","description":"%s","image":"%s",' .
-
 
160
            '"criteria":{"id":"%s","narrative":"%s"},"issuer":{"name":"%s","url":"%s","email":"%s",' .
-
 
161
            '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"},' .
-
 
162
            '"tags":%s,"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"BadgeClass","version":"%s",' .
-
 
163
            '"@language":"%s","related":[{"id":"%s","version":"%s","@language":"%s"}],"endorsement":"%s",' .
-
 
164
            '"alignments":[{"targetName":"%s","targetUrl":"%s","targetDescription":"%s","targetFramework":"%s",' .
-
 
165
            '"targetCode":"%s"}]}';
-
 
166
        $this->assertion2->issuer = '{"name":"%s","url":"%s","email":"%s",' .
-
 
167
            '"@context":"https:\/\/w3id.org\/openbadges\/v2","id":"%s","type":"Issuer"}';
37
    /** @var $assertion2 to define json format for Open badge version 2 */
168
    }
38
    protected $assertion2;
Línea 169... Línea 39...
169
 
39
 
170
    public function test_create_badge(): void {
40
    public function test_create_badge(): void {
Línea 193... Línea 63...
193
        $this->assertEquals($badge->attachment, $clonedbadge->attachment);
63
        $this->assertEquals($badge->attachment, $clonedbadge->attachment);
194
        $this->assertEquals($badge->notification, $clonedbadge->notification);
64
        $this->assertEquals($badge->notification, $clonedbadge->notification);
195
        $this->assertEquals($badge->version, $clonedbadge->version);
65
        $this->assertEquals($badge->version, $clonedbadge->version);
196
        $this->assertEquals($badge->language, $clonedbadge->language);
66
        $this->assertEquals($badge->language, $clonedbadge->language);
197
        $this->assertEquals($badge->imagecaption, $clonedbadge->imagecaption);
67
        $this->assertEquals($badge->imagecaption, $clonedbadge->imagecaption);
198
        $this->assertEquals($badge->imageauthorname, $clonedbadge->imageauthorname);
-
 
199
        $this->assertEquals($badge->imageauthoremail, $clonedbadge->imageauthoremail);
-
 
200
        $this->assertEquals($badge->imageauthorurl, $clonedbadge->imageauthorurl);
-
 
201
    }
68
    }
Línea 202... Línea 69...
202
 
69
 
203
    public function test_badge_status(): void {
70
    public function test_badge_status(): void {
204
        $badge = new badge($this->badgeid);
71
        $badge = new badge($this->badgeid);
Línea 379... Línea 246...
379
            $badge->notification = 0;
246
            $badge->notification = 0;
380
            $badge->status = BADGE_STATUS_INACTIVE;
247
            $badge->status = BADGE_STATUS_INACTIVE;
381
            $badge->version = "Version $i";
248
            $badge->version = "Version $i";
382
            $badge->language = "en";
249
            $badge->language = "en";
383
            $badge->imagecaption = "Image caption $i";
250
            $badge->imagecaption = "Image caption $i";
384
            $badge->imageauthorname = "Image author's name $i";
-
 
385
            $badge->imageauthoremail = "author$i@example.com";
-
 
386
            $badge->imageauthorname = "Image author's name $i";
-
 
Línea 387... Línea 251...
387
 
251
 
388
            $badgeid = $DB->insert_record('badge', $badge, true);
252
            $badgeid = $DB->insert_record('badge', $badge, true);
389
            $badges[$badgeid] = new badge($badgeid);
253
            $badges[$badgeid] = new badge($badgeid);
390
            $badges[$badgeid]->issue($user2->id, true);
254
            $badges[$badgeid]->issue($user2->id, true);
Línea 460... Línea 324...
460
        $badge->notification = 0;
324
        $badge->notification = 0;
461
        $badge->status = BADGE_STATUS_ACTIVE;
325
        $badge->status = BADGE_STATUS_ACTIVE;
462
        $badge->version = "Version $i";
326
        $badge->version = "Version $i";
463
        $badge->language = "en";
327
        $badge->language = "en";
464
        $badge->imagecaption = "Image caption";
328
        $badge->imagecaption = "Image caption";
465
        $badge->imageauthorname = "Image author's name";
-
 
466
        $badge->imageauthoremail = "author@example.com";
-
 
467
        $badge->imageauthorname = "Image author's name";
-
 
Línea 468... Línea 329...
468
 
329
 
469
        $badgeid = $DB->insert_record('badge', $badge, true);
330
        $badgeid = $DB->insert_record('badge', $badge, true);
470
        $badges[$badgeid] = new badge($badgeid);
331
        $badges[$badgeid] = new badge($badgeid);
Línea 480... Línea 341...
480
        $result = badges_get_user_badges($this->user->id);
341
        $result = badges_get_user_badges($this->user->id);
481
        $this->assertCount(2, $result);
342
        $this->assertCount(2, $result);
Línea 482... Línea 343...
482
 
343
 
Línea 483... Línea 344...
483
    }
344
    }
484
 
345
 
485
    public function data_for_message_from_template() {
346
    public static function data_for_message_from_template(): array {
486
        return array(
347
        return array(
487
            array(
348
            array(
488
                'This is a message with no variables',
349
                'This is a message with no variables',
Línea 904... Línea 765...
904
    public function test_badges_assertion(): void {
765
    public function test_badges_assertion(): void {
905
        $this->preventResetByRollback(); // Messaging is not compatible with transactions.
766
        $this->preventResetByRollback(); // Messaging is not compatible with transactions.
906
        $badge = new badge($this->coursebadge);
767
        $badge = new badge($this->coursebadge);
907
        $this->assertFalse($badge->is_issued($this->user->id));
768
        $this->assertFalse($badge->is_issued($this->user->id));
Línea 908... Línea 769...
908
 
769
 
909
        $criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
770
        $criteriaoverall = award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id]);
910
        $criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ANY));
771
        $criteriaoverall->save(['agg' => BADGE_CRITERIA_AGGREGATION_ANY]);
911
        $criteria_overall1 = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id));
772
        $criteriaoverall1 = award_criteria::build(['criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $badge->id]);
Línea 912... Línea 773...
912
        $criteria_overall1->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address'));
773
        $criteriaoverall1->save(['agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address']);
913
 
774
 
914
        $this->user->address = 'Test address';
775
        $this->user->address = 'Test address';
915
        $sink = $this->redirectEmails();
776
        $sink = $this->redirectEmails();
Línea 919... Línea 780...
919
        // Check if badge is awarded.
780
        // Check if badge is awarded.
920
        $this->assertDebuggingCalled('Error baking badge image!');
781
        $this->assertDebuggingCalled('Error baking badge image!');
921
        $awards = $badge->get_awards();
782
        $awards = $badge->get_awards();
922
        $this->assertCount(1, $awards);
783
        $this->assertCount(1, $awards);
Línea 923... Línea -...
923
 
-
 
924
        // Get assertion.
-
 
925
        $award = reset($awards);
-
 
926
        $assertion = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V1);
-
 
927
        $testassertion = $this->assertion;
-
 
928
 
-
 
929
        // Make sure JSON strings have the same structure.
-
 
930
        $this->assertStringMatchesFormat($testassertion->badge, json_encode($assertion->get_badge_assertion()));
-
 
931
        $this->assertStringMatchesFormat($testassertion->class, json_encode($assertion->get_badge_class()));
-
 
932
        $this->assertStringMatchesFormat($testassertion->issuer, json_encode($assertion->get_issuer()));
-
 
933
 
784
 
934
        // Test Openbadge specification version 2.
785
        // Test Openbadge specification version 2.0.
935
        // Get assertion version 2.
786
        // Get assertion version 2.
936
        $award = reset($awards);
787
        $award = reset($awards);
937
        $assertion2 = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V2);
788
        $assertion2 = new core_badges_assertion($award->uniquehash, OPEN_BADGES_V2);
Línea 1240... Línea 1091...
1240
    /**
1091
    /**
1241
     * Data provider for test_create_backpack_credentials().
1092
     * Data provider for test_create_backpack_credentials().
1242
     *
1093
     *
1243
     * @return array
1094
     * @return array
1244
     */
1095
     */
1245
    public function save_backpack_credentials_provider(): array {
1096
    public static function save_backpack_credentials_provider(): array {
1246
        return [
1097
        return [
1247
            'Empty fields' => [
1098
            'Empty fields' => [
1248
                false,
1099
                false,
1249
            ],
1100
            ],
1250
            'No backpack mail or password are defined' => [
1101
            'No backpack mail or password are defined' => [
Línea 1308... Línea 1159...
1308
    /**
1159
    /**
1309
     * Provider for test_badges_save_external_backpack
1160
     * Provider for test_badges_save_external_backpack
1310
     *
1161
     *
1311
     * @return array
1162
     * @return array
1312
     */
1163
     */
1313
    public function badges_save_external_backpack_provider() {
1164
    public static function badges_save_external_backpack_provider(): array {
1314
        $data = [
1165
        $data = [
1315
            'apiversion' => 2,
1166
            'apiversion' => 2,
1316
            'backpackapiurl' => 'https://api.ca.badgr.io/v2',
1167
            'backpackapiurl' => 'https://api.ca.badgr.io/v2',
1317
            'backpackweburl' => 'https://ca.badgr.io',
1168
            'backpackweburl' => 'https://ca.badgr.io',
1318
        ];
1169
        ];
Línea 1425... Línea 1276...
1425
    }
1276
    }
Línea 1426... Línea 1277...
1426
 
1277
 
1427
    /**
1278
    /**
1428
     * Provider for test_badges_(create/update)_site_backpack
1279
     * Provider for test_badges_(create/update)_site_backpack
1429
     */
1280
     */
1430
    public function badges_create_site_backpack_provider() {
1281
    public static function badges_create_site_backpack_provider(): array {
1431
        return [
1282
        return [
1432
            "Test as admin user - creation test" => [true, true],
1283
            "Test as admin user - creation test" => [true, true],
1433
            "Test as admin user - update test" => [true, false],
1284
            "Test as admin user - update test" => [true, false],
1434
            "Test as normal user - creation test" => [false, true],
1285
            "Test as normal user - creation test" => [false, true],
Línea 1596... Línea 1447...
1596
    /**
1447
    /**
1597
     * Test the test_badges_get_site_primary_backpack function.
1448
     * Test the test_badges_get_site_primary_backpack function.
1598
     *
1449
     *
1599
     * @return array
1450
     * @return array
1600
     */
1451
     */
1601
    public function badges_get_site_primary_backpack_provider() {
1452
    public static function badges_get_site_primary_backpack_provider(): array {
1602
        return [
1453
        return [
1603
            "Test with auth details" => [true],
1454
            "Test with auth details" => [true],
1604
            "Test without auth details" => [false],
1455
            "Test without auth details" => [false],
1605
        ];
1456
        ];
1606
    }
1457
    }
Línea 1651... Línea 1502...
1651
    /**
1502
    /**
1652
     * Provider for test_badges_change_sortorder_backpacks.
1503
     * Provider for test_badges_change_sortorder_backpacks.
1653
     *
1504
     *
1654
     * @return array
1505
     * @return array
1655
     */
1506
     */
1656
    public function badges_change_sortorder_backpacks_provider(): array {
1507
    public static function badges_change_sortorder_backpacks_provider(): array {
-
 
1508
        static::load_requirements();
1657
        return [
1509
        return [
1658
            "Test up" => [
1510
            "Test up" => [
1659
                'backpacktomove' => 1,
1511
                'backpacktomove' => 1,
1660
                'direction' => BACKPACK_MOVE_UP,
1512
                'direction' => BACKPACK_MOVE_UP,
1661
                'expectedsortorder' => 1,
1513
                'expectedsortorder' => 1,
Línea 1705... Línea 1557...
1705
 
1557
 
1706
    /**
1558
    /**
1707
     * Data provider for test_badges_generate_badgr_open_url
1559
     * Data provider for test_badges_generate_badgr_open_url
1708
     * @return array
1560
     * @return array
1709
     */
1561
     */
-
 
1562
    public static function badgr_open_url_generator(): array {
-
 
1563
        static::load_requirements();
1710
    public function badgr_open_url_generator() {
1564
 
1711
        return [
1565
        return [
1712
            'Badgr Assertion URL test' => [
1566
            'Badgr Assertion URL test' => [
1713
                OPEN_BADGES_V2_TYPE_ASSERTION, "https://api.ca.badgr.io/public/assertions/123455"
1567
                OPEN_BADGES_V2_TYPE_ASSERTION, "https://api.ca.badgr.io/public/assertions/123455"
1714
            ],
1568
            ],
Línea 1752... Línea 1606...
1752
    /**
1606
    /**
1753
     * Data provider for badges_external_get_mapping_provider
1607
     * Data provider for badges_external_get_mapping_provider
1754
     *
1608
     *
1755
     * @return array
1609
     * @return array
1756
     */
1610
     */
1757
    public function badges_external_get_mapping_provider() {
1611
    public static function badges_external_get_mapping_provider(): array {
1758
        return [
1612
        return [
1759
            "Get the site backpack value" => [
1613
            "Get the site backpack value" => [
1760
                1234, 4321, 'id', 'sitebackpackid'
1614
                1234, 4321, 'id', 'sitebackpackid'
1761
            ],
1615
            ],
1762
            "Get the type of the mapping" => [
1616
            "Get the type of the mapping" => [