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 |
/**
|
|
|
18 |
* Helpers for the core_rating subsystem implementation of privacy.
|
|
|
19 |
*
|
|
|
20 |
* @package core_rating
|
|
|
21 |
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace core_rating\phpunit;
|
|
|
26 |
|
|
|
27 |
defined('MOODLE_INTERNAL') || die();
|
|
|
28 |
|
|
|
29 |
use \core_privacy\tests\request\content_writer;
|
|
|
30 |
|
|
|
31 |
global $CFG;
|
|
|
32 |
|
|
|
33 |
/**
|
|
|
34 |
* Helpers for the core_rating subsystem implementation of privacy.
|
|
|
35 |
*
|
|
|
36 |
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
|
|
|
37 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
38 |
*/
|
|
|
39 |
trait privacy_helper {
|
|
|
40 |
/**
|
|
|
41 |
* Fetch all ratings on a subcontext.
|
|
|
42 |
*
|
|
|
43 |
* @param \context $context The context being stored.
|
|
|
44 |
* @param array $subcontext The subcontext path to check.
|
|
|
45 |
* @return \stdClass|array
|
|
|
46 |
*/
|
|
|
47 |
protected function get_ratings_on_subcontext(\context $context, array $subcontext) {
|
|
|
48 |
$writer = \core_privacy\local\request\writer::with_context($context);
|
|
|
49 |
return $writer->get_related_data($subcontext, 'rating');
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* Check that all included ratings belong to the specified user.
|
|
|
54 |
*
|
|
|
55 |
* @param int $userid The ID of the user being stored.
|
|
|
56 |
* @param \context $context The context being stored.
|
|
|
57 |
* @param array $subcontext The subcontext path to check.
|
|
|
58 |
* @param string $component The component being stored.
|
|
|
59 |
* @param string $ratingarea The rating area to store results for.
|
|
|
60 |
* @param int $itemid The itemid to store.
|
|
|
61 |
*/
|
|
|
62 |
protected function assert_all_own_ratings_on_context(
|
|
|
63 |
int $userid,
|
|
|
64 |
\context $context,
|
|
|
65 |
array $subcontext,
|
|
|
66 |
$component,
|
|
|
67 |
$ratingarea,
|
|
|
68 |
$itemid
|
|
|
69 |
) {
|
|
|
70 |
$writer = \core_privacy\local\request\writer::with_context($context);
|
|
|
71 |
$rm = new \rating_manager();
|
|
|
72 |
$dbratings = $rm->get_all_ratings_for_item((object) [
|
|
|
73 |
'context' => $context,
|
|
|
74 |
'component' => $component,
|
|
|
75 |
'ratingarea' => $ratingarea,
|
|
|
76 |
'itemid' => $itemid,
|
|
|
77 |
]);
|
|
|
78 |
|
|
|
79 |
$exportedratings = $this->get_ratings_on_subcontext($context, $subcontext);
|
|
|
80 |
|
|
|
81 |
foreach ($exportedratings as $ratingid => $rating) {
|
|
|
82 |
$this->assertTrue(isset($dbratings[$ratingid]));
|
|
|
83 |
$this->assertEquals($userid, $rating->author);
|
|
|
84 |
$this->assert_rating_matches($dbratings[$ratingid], $rating);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
foreach ($dbratings as $rating) {
|
|
|
88 |
if ($rating->userid == $userid) {
|
|
|
89 |
$this->assertEquals($rating->id, $ratingid);
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Check that all included ratings are valid. They may belong to any user.
|
|
|
96 |
*
|
|
|
97 |
* @param \context $context The context being stored.
|
|
|
98 |
* @param array $subcontext The subcontext path to check.
|
|
|
99 |
* @param string $component The component being stored.
|
|
|
100 |
* @param string $ratingarea The rating area to store results for.
|
|
|
101 |
* @param int $itemid The itemid to store.
|
|
|
102 |
*/
|
|
|
103 |
protected function assert_all_ratings_on_context(\context $context, array $subcontext, $component, $ratingarea, $itemid) {
|
|
|
104 |
$writer = \core_privacy\local\request\writer::with_context($context);
|
|
|
105 |
$rm = new \rating_manager();
|
|
|
106 |
$dbratings = $rm->get_all_ratings_for_item((object) [
|
|
|
107 |
'context' => $context,
|
|
|
108 |
'component' => $component,
|
|
|
109 |
'ratingarea' => $ratingarea,
|
|
|
110 |
'itemid' => $itemid,
|
|
|
111 |
]);
|
|
|
112 |
|
|
|
113 |
$exportedratings = $this->get_ratings_on_subcontext($context, $subcontext);
|
|
|
114 |
|
|
|
115 |
foreach ($exportedratings as $ratingid => $rating) {
|
|
|
116 |
$this->assertTrue(isset($dbratings[$ratingid]));
|
|
|
117 |
$this->assert_rating_matches($dbratings[$ratingid], $rating);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
foreach ($dbratings as $rating) {
|
|
|
121 |
$this->assertTrue(isset($exportedratings[$rating->id]));
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Assert that the rating matches.
|
|
|
127 |
*
|
|
|
128 |
* @param \stdClass $expected The expected rating structure
|
|
|
129 |
* @param \stdClass $stored The actual rating structure
|
|
|
130 |
*/
|
|
|
131 |
protected function assert_rating_matches($expected, $stored) {
|
|
|
132 |
$this->assertEquals($expected->rating, $stored->rating);
|
|
|
133 |
$this->assertEquals($expected->userid, $stored->author);
|
|
|
134 |
}
|
|
|
135 |
}
|