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