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\external;
|
|
|
18 |
|
|
|
19 |
use core_badges\tests\external_helper;
|
|
|
20 |
use externallib_advanced_testcase;
|
|
|
21 |
|
|
|
22 |
defined('MOODLE_INTERNAL') || die();
|
|
|
23 |
|
|
|
24 |
global $CFG;
|
|
|
25 |
|
|
|
26 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Tests for external function get_badge.
|
|
|
30 |
*
|
|
|
31 |
* @package core_badges
|
|
|
32 |
* @category external
|
|
|
33 |
*
|
|
|
34 |
* @copyright 2024 Daniel Ureña <durenadev@gmail.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
* @since Moodle 4.5
|
|
|
37 |
* @coversDefaultClass \core_badges\external\get_badge
|
|
|
38 |
*/
|
|
|
39 |
final class get_badge_test extends externallib_advanced_testcase {
|
|
|
40 |
use external_helper;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* Test get badge by id without enablebadges active in moodle.
|
|
|
44 |
* @covers ::execute
|
|
|
45 |
*/
|
|
|
46 |
public function test_get_badge_without_enablebadges(): void {
|
|
|
47 |
$data = $this->prepare_test_data();
|
|
|
48 |
// Badges are not enabled on this site.
|
|
|
49 |
set_config('enablebadges', 0);
|
|
|
50 |
|
|
|
51 |
$this->expectException(\moodle_exception::class);
|
|
|
52 |
$this->expectExceptionMessage('Badges are not enabled on this site.');
|
|
|
53 |
get_badge::execute($data['sitebadge']['id']);
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Test get badge by id.
|
|
|
58 |
* @covers ::execute
|
|
|
59 |
*/
|
|
|
60 |
public function test_get_badge(): void {
|
|
|
61 |
$data = $this->prepare_test_data();
|
|
|
62 |
|
|
|
63 |
// Test with an existing site badge.
|
|
|
64 |
$result = get_badge::execute($data['sitebadge']['id']);
|
|
|
65 |
$result = \core_external\external_api::clean_returnvalue(get_badge::execute_returns(), $result);
|
|
|
66 |
$this->assert_badge_class($data['sitebadge'], $result['badge'], true);
|
|
|
67 |
$this->assertEmpty($result['warnings']);
|
|
|
68 |
|
|
|
69 |
// Test with an existing course badge.
|
|
|
70 |
$result = get_badge::execute($data['coursebadge']['id']);
|
|
|
71 |
$result = \core_external\external_api::clean_returnvalue(get_badge::execute_returns(), $result);
|
|
|
72 |
$this->assert_badge_class($data['coursebadge'], $result['badge'], true);
|
|
|
73 |
$this->assertEmpty($result['warnings']);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Test get badge by id with an unprivileged user.
|
|
|
78 |
* @covers ::execute
|
|
|
79 |
*/
|
|
|
80 |
public function test_get_badge_with_unprivileged_user(): void {
|
|
|
81 |
$data = $this->prepare_test_data();
|
|
|
82 |
$this->setUser($this->getDataGenerator()->create_user());
|
|
|
83 |
|
|
|
84 |
// Site badge.
|
|
|
85 |
$result = get_badge::execute($data['sitebadge']['id']);
|
|
|
86 |
$result = \core_external\external_api::clean_returnvalue(get_badge::execute_returns(), $result);
|
|
|
87 |
$this->assert_badge_class($data['sitebadge'], $result['badge'], false);
|
|
|
88 |
$this->assertEmpty($result['warnings']);
|
|
|
89 |
|
|
|
90 |
// Course badge.
|
|
|
91 |
$result = get_badge::execute($data['coursebadge']['id']);
|
|
|
92 |
$result = \core_external\external_api::clean_returnvalue(get_badge::execute_returns(), $result);
|
|
|
93 |
$this->assert_badge_class($data['coursebadge'], $result['badge'], false);
|
|
|
94 |
$this->assertEmpty($result['warnings']);
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Test get badge by id with an invalid badge id.
|
|
|
99 |
* @covers ::execute
|
|
|
100 |
*/
|
|
|
101 |
public function test_get_badge_with_invalid_badge_id(): void {
|
|
|
102 |
$this->prepare_test_data();
|
|
|
103 |
|
|
|
104 |
$this->expectException(\moodle_exception::class);
|
|
|
105 |
get_badge::execute(123);
|
|
|
106 |
}
|
|
|
107 |
}
|