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 * Helper for privacy tests.
19 *
20 * @package    core_question
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
25defined('MOODLE_INTERNAL') || die();
26
27use \core_privacy\local\request\writer;
28
29/**
30 * Helper for privacy tests.
31 *
32 * @package    core_question
33 * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
34 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36trait core_question_privacy_helper {
37    /**
38     * Assert that the question usage in the supplied slot matches the expected format
39     * and usage for a question.
40     *
41     * @param   \question_usage_by_activity $quba The Question Usage to test against.
42     * @param   int                         $slotno The slot number to compare
43     * @param   \question_display_options   $options    The display options used for formatting.
44     * @param   \stdClass                   $data The data to check.
45     */
46    public function assert_question_slot_equals(
47            \question_usage_by_activity $quba,
48            $slotno,
49            \question_display_options $options,
50            $data
51        ) {
52        $attempt = $quba->get_question_attempt($slotno);
53        $question = $attempt->get_question(false);
54
55        // Check the question data exported.
56        $this->assertEquals($data->name, $question->name);
57        $this->assertEquals($data->question, $question->questiontext);
58
59        // Check the answer exported.
60        $this->assertEquals($attempt->get_response_summary(), $data->answer);
61
62        if ($options->marks != \question_display_options::HIDDEN) {
63            $this->assertEquals($attempt->get_mark(), $data->mark);
64        } else {
65            $this->assertFalse(isset($data->mark));
66        }
67
68        if ($options->flags != \question_display_options::HIDDEN) {
69            $this->assertEquals($attempt->is_flagged(), (int) $data->flagged);
70        } else {
71            $this->assertFalse(isset($data->flagged));
72        }
73
74        if ($options->generalfeedback != \question_display_options::HIDDEN) {
75            $this->assertEquals($question->format_generalfeedback($attempt), $data->generalfeedback);
76        } else {
77            $this->assertFalse(isset($data->generalfeedback));
78        }
79    }
80
81    /**
82     * Assert that a question attempt was exported.
83     *
84     * @param   \context    $context The context which the attempt should be in
85     * @param   array       $subcontext The base of the export
86     * @param   question_usage_by_activity  $quba The question usage expected
87     * @param   \question_display_options   $options    The display options used for formatting.
88     * @param   \stdClass   $user The user exported
89     */
90    public function assert_question_attempt_exported(\context $context, array $subcontext, $quba, $options, $user) {
91        $usagecontext = array_merge(
92            $subcontext,
93            [get_string('questions', 'core_question')]
94        );
95
96        $writer = writer::with_context($context);
97
98        foreach ($quba->get_slots() as $slotno) {
99            $data = $writer->get_data(array_merge($usagecontext, [$slotno]));
100            $this->assert_question_slot_equals($quba, $slotno, $options, $data);
101        }
102    }
103}
104