Proyectos de Subversion Moodle

Rev

Rev 11 | | 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
namespace core_badges\external;
18
 
1441 ariadna 19
use core_badges\tests\external_helper;
1 efrain 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_user_badge_by_hash.
30
 *
31
 * @package    core_badges
32
 * @category   external
33
 * @copyright  2023 Rodrigo Mady <rodrigo.mady@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @since      Moodle 4.3
36
 * @coversDefaultClass \core_badges\external\get_user_badge_by_hash
37
 */
1441 ariadna 38
final class get_user_badge_by_hash_test extends externallib_advanced_testcase {
39
    use external_helper;
1 efrain 40
 
41
    /**
42
     * Test get user badge by hash.
43
     * These are a basic tests since the badges_get_my_user_badges used by the external function already has unit tests.
44
     * @covers ::execute
45
     */
11 efrain 46
    public function test_get_user_badge_by_hash(): void {
1 efrain 47
        $data = $this->prepare_test_data();
48
 
1441 ariadna 49
        // Site badge fetched by recipient.
50
        $this->setUser($data['student']);
51
        $result = get_user_badge_by_hash::execute($data['sitebadge']['uniquehash']);
1 efrain 52
        $result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
1441 ariadna 53
        $this->assert_issued_badge($data['sitebadge'], $result['badge'][0], true, false);
1 efrain 54
        $this->assertEmpty($result['warnings']);
55
 
1441 ariadna 56
        // Site badge fetched by user without "moodle/badges:configuredetails" capability.
57
        $this->setGuestUser();
58
        $result = get_user_badge_by_hash::execute($data['sitebadge']['uniquehash']);
1 efrain 59
        $result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
1441 ariadna 60
        $this->assert_issued_badge($data['sitebadge'], $result['badge'][0], false, false);
1 efrain 61
        $this->assertEmpty($result['warnings']);
62
 
1441 ariadna 63
        // Site badge fetched by user with "moodle/badges:configuredetails" capability.
64
        $this->setAdminUser();
65
        $result = get_user_badge_by_hash::execute($data['sitebadge']['uniquehash']);
1 efrain 66
        $result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
1441 ariadna 67
        $this->assert_issued_badge($data['sitebadge'], $result['badge'][0], false, true);
68
        $this->assertEmpty($result['warnings']);
1 efrain 69
 
1441 ariadna 70
        // Course badge fetched by recipient.
71
        $this->setUser($data['student']);
72
        $result = get_user_badge_by_hash::execute($data['coursebadge']['uniquehash']);
73
        $result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
74
        $this->assert_issued_badge($data['coursebadge'], $result['badge'][0], true, false);
75
        $this->assertEmpty($result['warnings']);
1 efrain 76
 
1441 ariadna 77
        // Course badge fetched by user without "moodle/badges:configuredetails" capability.
78
        $this->setGuestUser();
79
        $result = get_user_badge_by_hash::execute($data['coursebadge']['uniquehash']);
1 efrain 80
        $result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
1441 ariadna 81
        $this->assert_issued_badge($data['coursebadge'], $result['badge'][0], false, false);
1 efrain 82
        $this->assertEmpty($result['warnings']);
83
 
1441 ariadna 84
        // Course badge fetched by user with "moodle/badges:configuredetails" capability.
85
        $this->setAdminUser();
86
        $result = get_user_badge_by_hash::execute($data['coursebadge']['uniquehash']);
1 efrain 87
        $result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
1441 ariadna 88
        $this->assert_issued_badge($data['coursebadge'], $result['badge'][0], false, true);
1 efrain 89
        $this->assertEmpty($result['warnings']);
90
 
1441 ariadna 91
        // Wrong hash.
92
        $result = get_user_badge_by_hash::execute('1234');
93
        $result = \core_external\external_api::clean_returnvalue(get_user_badge_by_hash::execute_returns(), $result);
94
        $this->assertEmpty($result['badge']);
95
        $this->assertNotEmpty($result['warnings']);
96
        $this->assertEquals('badgeawardnotfound', $result['warnings'][0]['warningcode']);
1 efrain 97
    }
98
}