Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
11 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 mod_glossary\external;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
23
 
24
use core_external\external_api;
25
use externallib_advanced_testcase;
26
 
27
/**
28
 * External function test for delete_entry.
29
 *
30
 * @package    mod_glossary
31
 * @category   external
32
 * @covers     \mod_glossary\external\delete_entry
33
 * @since      Moodle 3.10
34
 * @copyright  2020 Juan Leyva <juan@moodle.com>
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36
 */
37
final class delete_entry_test extends externallib_advanced_testcase {
38
 
39
    /**
40
     * Test the behaviour of delete_entry().
41
     */
42
    public function test_delete_entry() {
43
        global $DB;
44
        $this->resetAfterTest();
45
 
46
        // Create required data.
47
        $course = $this->getDataGenerator()->create_course();
48
        $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
49
        $anotherstudent = $this->getDataGenerator()->create_and_enrol($course, 'student');
50
        $glossary = $this->getDataGenerator()->create_module('glossary', ['course' => $course->id]);
51
        $gg = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
52
 
53
        $this->setUser($student);
54
        $entry = $gg->create_content($glossary);
55
 
56
        // Test entry creator can delete.
57
        $result = delete_entry::execute($entry->id);
58
        $result = external_api::clean_returnvalue(delete_entry::execute_returns(), $result);
59
        $this->assertTrue($result['result']);
60
        $this->assertEquals(0, $DB->count_records('glossary_entries', ['id' => $entry->id]));
61
 
62
        // Test admin can delete.
63
        $this->setAdminUser();
64
        $entry = $gg->create_content($glossary);
65
        $result = delete_entry::execute($entry->id);
66
        $result = external_api::clean_returnvalue(delete_entry::execute_returns(), $result);
67
        $this->assertTrue($result['result']);
68
        $this->assertEquals(0, $DB->count_records('glossary_entries', ['id' => $entry->id]));
69
 
70
        $entry = $gg->create_content($glossary);
71
        // Test a different student is not able to delete.
72
        $this->setUser($anotherstudent);
73
        $this->expectExceptionMessage(get_string('nopermissiontodelentry', 'error'));
74
        delete_entry::execute($entry->id);
75
    }
76
}