MDL-63658 core_favourites: adding paging support to the service layer
[moodle.git] / favourites / tests / privacy_test.php
CommitLineData
ac9138db
JD
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/**
18 * Privacy tests for core_favourites.
19 *
20 * @package core_favourites
21 * @category test
22 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28use \core_privacy\tests\provider_testcase;
29use \core_favourites\privacy\provider;
30
31/**
32 * Unit tests for favourites/classes/privacy/provider
33 *
34 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class privacy_test extends provider_testcase {
38
39 public function setUp() {
40 $this->resetAfterTest(true);
41 }
42
43 /**
44 * Helper to set up some sample users and courses.
45 */
46 protected function set_up_courses_and_users() {
47 $user1 = self::getDataGenerator()->create_user();
48 $user1context = \context_user::instance($user1->id);
49 $user2 = self::getDataGenerator()->create_user();
50 $user2context = \context_user::instance($user2->id);
51 $course1 = self::getDataGenerator()->create_course();
52 $course2 = self::getDataGenerator()->create_course();
53 $course1context = context_course::instance($course1->id);
54 $course2context = context_course::instance($course2->id);
55 return [$user1, $user2, $user1context, $user2context, $course1context, $course2context];
56 }
57
58 /**
59 * Test confirming that contexts of favourited items can be added to the contextlist.
60 */
61 public function test_add_contexts_for_userid() {
62 list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();
63
64 // Favourite 2 courses for user1 and 1 course for user2, all at the site context.
65 $ufservice1 = \core_favourites\services::get_service_for_user_context($user1context);
66 $ufservice2 = \core_favourites\services::get_service_for_user_context($user2context);
67 $systemcontext = context_system::instance();
68 $ufservice1->create_favourite('core_course', 'course', $course1context->instanceid, $systemcontext);
69 $ufservice1->create_favourite('core_course', 'course', $course2context->instanceid, $systemcontext);
70 $ufservice2->create_favourite('core_course', 'course', $course2context->instanceid, $systemcontext);
71 $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'course'));
72 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'course'));
73
74 // Now, just for variety, let's assume you can favourite a course at user context, and do so for user1.
75 $ufservice1->create_favourite('core_course', 'course', $course1context->instanceid, $user1context);
76
77 // Now, ask the favourites privacy api to export contexts for favourites of the type we just created, for user1.
78 $contextlist = new \core_privacy\local\request\contextlist();
79 \core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $user1->id, 'core_course', 'course');
80
81 // Verify we have two contexts in the list for user1.
82 $this->assertCount(2, $contextlist->get_contextids());
83
84 // And verify we only have the system context returned for user2.
85 $contextlist = new \core_privacy\local\request\contextlist();
86 \core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $user2->id, 'core_course', 'course');
87 $this->assertCount(1, $contextlist->get_contextids());
88 }
89
90 /**
91 * Test deletion of user favourites based on an approved_contextlist and component area.
92 */
93 public function test_delete_favourites_for_user() {
94 list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();
95
96 // Favourite 2 courses for user1 and 1 course for user2, all at the user context.
97 $ufservice1 = \core_favourites\services::get_service_for_user_context($user1context);
98 $ufservice2 = \core_favourites\services::get_service_for_user_context($user2context);
99 $ufservice1->create_favourite('core_course', 'course', $course1context->instanceid, $user1context);
100 $ufservice1->create_favourite('core_course', 'course', $course2context->instanceid, $user1context);
101 $ufservice2->create_favourite('core_course', 'course', $course2context->instanceid, $user2context);
102 $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'course'));
103 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'course'));
104
105 // Now, delete the favourites for user1 only.
106 $approvedcontextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_course', [$user1context->id]);
107 provider::delete_favourites_for_user($approvedcontextlist, 'core_course', 'course');
108
109 // Verify that we have no favourite courses for user1 but that the records are in tact for user2.
110 $this->assertCount(0, $ufservice1->find_favourites_by_type('core_course', 'course'));
111 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'course'));
112 }
113
114 public function test_delete_favourites_for_all_users() {
115 list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();
116
117 // Favourite 2 course modules for user1 and 1 course module for user2 all in course 1 context.
118 $ufservice1 = \core_favourites\services::get_service_for_user_context($user1context);
119 $ufservice2 = \core_favourites\services::get_service_for_user_context($user2context);
120 $ufservice1->create_favourite('core_course', 'modules', 1, $course1context);
121 $ufservice1->create_favourite('core_course', 'modules', 2, $course1context);
122 $ufservice2->create_favourite('core_course', 'modules', 3, $course1context);
123
124 // Now, favourite a different course module for user2 in course 2.
125 $ufservice2->create_favourite('core_course', 'modules', 5, $course2context);
126
127 $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'modules'));
128 $this->assertCount(2, $ufservice2->find_favourites_by_type('core_course', 'modules'));
129
130 // Now, delete all course module favourites in the 'course1' context only.
131 provider::delete_favourites_for_all_users($course1context, 'core_course', 'modules');
132
133 // Verify that only a single favourite for user1 in course 1 remains.
134 $this->assertCount(0, $ufservice1->find_favourites_by_type('core_course', 'modules'));
135 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'modules'));
136 }
137}