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 * Unit tests for mod_workshop_portfolio_caller class defined in mod/workshop/classes/portfolio_caller.php
19 *
20 * @package    mod_workshop
21 * @copyright  2016 An Pham Van <an.phamvan@harveynash.vn>
22 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
28
29require_once($CFG->dirroot . '/mod/workshop/locallib.php');
30require_once(__DIR__ . '/fixtures/testable.php');
31require_once($CFG->dirroot . '/mod/workshop/classes/portfolio_caller.php');
32
33/**
34 * Unit tests for mod_workshop_portfolio_caller class
35 *
36 * @package    mod_workshop
37 * @copyright  2016 An Pham Van <an.phamvan@harveynash.vn>
38 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class mod_workshop_porfolio_caller_testcase extends advanced_testcase {
41
42    /** @var stdClass $workshop Basic workshop data stored in an object. */
43    protected $workshop;
44    /** @var stdClass mod info */
45    protected $cm;
46
47    /**
48     * Setup testing environment.
49     */
50    protected function setUp(): void {
51        parent::setUp();
52        $this->setAdminUser();
53        $course = $this->getDataGenerator()->create_course();
54        $workshop = $this->getDataGenerator()->create_module('workshop', ['course' => $course]);
55        $this->cm = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST);
56        $this->workshop = new testable_workshop($workshop, $this->cm, $course);
57    }
58
59    /**
60     * Tear down.
61     */
62    protected function tearDown(): void {
63        $this->workshop = null;
64        $this->cm = null;
65        parent::tearDown();
66    }
67
68    /**
69     * Test the method mod_workshop_portfolio_caller::load_data()
70     */
71    public function test_load_data() {
72        $this->resetAfterTest(true);
73
74        $student1 = $this->getDataGenerator()->create_user();
75        $student2 = $this->getDataGenerator()->create_user();
76        $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
77        $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id);
78        $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
79        $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
80        $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
81
82        $portfoliocaller = new mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]);
83        $portfoliocaller->set_formats_from_button([]);
84        $portfoliocaller->load_data();
85
86        $reflector = new ReflectionObject($portfoliocaller);
87        $propertysubmission = $reflector->getProperty('submission');
88        $propertysubmission->setAccessible(true);
89        $submission = $propertysubmission->getValue($portfoliocaller);
90
91        $this->assertEquals($subid1, $submission->id);
92    }
93
94    /**
95     * Test the method mod_workshop_portfolio_caller::get_return_url()
96     */
97    public function test_get_return_url() {
98        $this->resetAfterTest(true);
99
100        $student1 = $this->getDataGenerator()->create_user();
101        $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
102        $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
103        $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
104
105        $portfoliocaller = new mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]);
106        $portfoliocaller->set_formats_from_button([]);
107        $portfoliocaller->load_data();
108
109        $expected = new moodle_url('/mod/workshop/submission.php', ['cmid' => $this->workshop->cm->id, 'id' => $subid1]);
110        $actual = new moodle_url($portfoliocaller->get_return_url());
111        $this->assertTrue($expected->compare($actual));
112    }
113
114    /**
115     * Test the method mod_workshop_portfolio_caller::get_navigation()
116     */
117    public function test_get_navigation() {
118        $this->resetAfterTest(true);
119
120        $student1 = $this->getDataGenerator()->create_user();
121        $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
122        $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
123        $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
124
125        $portfoliocaller = new mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]);
126        $portfoliocaller->set_formats_from_button([]);
127        $portfoliocaller->load_data();
128
129        $this->assertTrue(is_array($portfoliocaller->get_navigation()));
130    }
131
132    /**
133     * Test the method mod_workshop_portfolio_caller::check_permissions()
134     */
135    public function test_check_permissions_exportownsubmissionassessment() {
136        global $DB;
137        $this->resetAfterTest(true);
138
139        $context = context_module::instance($this->cm->id);
140        $student1 = $this->getDataGenerator()->create_user();
141        $student2 = $this->getDataGenerator()->create_user();
142        $roleids = $DB->get_records_menu('role', null, '', 'shortname, id');
143        $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id, $roleids['student']);
144        $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id, $roleids['student']);
145        $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
146        $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
147        $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
148        $this->setUser($student1);
149
150        $portfoliocaller = new mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]);
151
152        role_change_permission($roleids['student'], $context, 'mod/workshop:exportsubmissions', CAP_PREVENT);
153        $this->assertFalse($portfoliocaller->check_permissions());
154
155        role_change_permission($roleids['student'], $context, 'mod/workshop:exportsubmissions', CAP_ALLOW);
156        $this->assertTrue($portfoliocaller->check_permissions());
157    }
158
159    /**
160     * Test the method mod_workshop_portfolio_caller::get_sha1()
161     */
162    public function test_get_sha1() {
163        $this->resetAfterTest(true);
164
165        $student1 = $this->getDataGenerator()->create_user();
166        $student2 = $this->getDataGenerator()->create_user();
167        $this->getDataGenerator()->enrol_user($student1->id, $this->workshop->course->id);
168        $this->getDataGenerator()->enrol_user($student2->id, $this->workshop->course->id);
169        $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
170        $subid1 = $workshopgenerator->create_submission($this->workshop->id, $student1->id);
171        $asid1 = $workshopgenerator->create_assessment($subid1, $student2->id);
172
173        $portfoliocaller = new mod_workshop_portfolio_caller(['id' => $this->workshop->cm->id, 'submissionid' => $subid1]);
174        $portfoliocaller->set_formats_from_button([]);
175        $portfoliocaller->load_data();
176
177        $this->assertTrue(is_string($portfoliocaller->get_sha1()));
178    }
179
180    /**
181     * Test function display_name()
182     * Assert that this function can return the name of the module ('Workshop').
183     */
184    public function test_display_name() {
185        $this->resetAfterTest(true);
186
187        $name = mod_workshop_portfolio_caller::display_name();
188        $this->assertEquals(get_string('pluginname', 'mod_workshop'), $name);
189    }
190}
191