MDL-68991 core: delete core_get_userfeedback_url webservice
[moodle.git] / lib / tests / external / record_userfeedback_action_test.php
CommitLineData
1a4c8e02
SR
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/**
ce80716a 18 * External functions test for record_feedback_action.
1a4c8e02
SR
19 *
20 * @package core
21 * @category test
22 * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
ce80716a 26namespace core\external;
1a4c8e02
SR
27
28defined('MOODLE_INTERNAL') || die();
29
30use externallib_advanced_testcase;
31use context_system;
32
33global $CFG;
34
35require_once($CFG->dirroot . '/webservice/tests/helpers.php');
36
37/**
ce80716a 38 * Class record_userfeedback_action_testcase
1a4c8e02
SR
39 *
40 * @copyright 2020 Shamim Rezaie <shamim@moodle.com>
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
ce80716a 42 * @coversDefaultClass record_userfeedback_action
1a4c8e02 43 */
ce80716a 44class record_userfeedback_action_testcase extends externallib_advanced_testcase {
1a4c8e02
SR
45
46 /**
ce80716a 47 * Data provider for test_record_userfeedback_action.
1a4c8e02
SR
48 *
49 * @return array
50 */
ce80716a 51 public function record_userfeedback_action_provider() {
1a4c8e02
SR
52 return [
53 'give action' => ['give'],
54 'remind action' => ['remind'],
55 ];
56 }
57
58 /**
ce80716a 59 * Test the behaviour of record_userfeedback_action().
1a4c8e02 60 *
ce80716a 61 * @dataProvider record_userfeedback_action_provider
1a4c8e02
SR
62 * @param string $action The action taken by the user
63 *
64 * @covers ::execute
65 */
ce80716a 66 public function test_record_userfeedback_action(string $action) {
1a4c8e02
SR
67 $this->resetAfterTest();
68
69 $context = context_system::instance();
70 $user = $this->getDataGenerator()->create_user();
71 $this->setUser($user);
363aaf6e 72 $eventsink = $this->redirectEvents();
1a4c8e02
SR
73
74 $now = time();
75
76 // Call the WS and check the action is recorded as expected.
ce80716a 77 $result = record_userfeedback_action::execute($action, $context->id);
1a4c8e02
SR
78 $this->assertNull($result);
79
80 $preference = get_user_preferences('core_userfeedback_' . $action);
81 $this->assertGreaterThanOrEqual($now, $preference);
363aaf6e
SR
82
83 $events = $eventsink->get_events();
84 $this->assertCount(1, $events);
85 $this->assertInstanceOf('\core\event\userfeedback_' . $action, $events[0]);
86 $eventsink->clear();
1a4c8e02
SR
87 }
88}