| 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_feedback\form;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | /**
 | 
        
           |  |  | 20 |  * Tests the confirm use template form
 | 
        
           |  |  | 21 |  *
 | 
        
           |  |  | 22 |  * @author Peter Dias
 | 
        
           |  |  | 23 |  * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 | 
        
           |  |  | 24 |  * @package mod_feedback
 | 
        
           |  |  | 25 |  */
 | 
        
           |  |  | 26 | class create_template_form_test extends \advanced_testcase {
 | 
        
           |  |  | 27 |     /**
 | 
        
           |  |  | 28 |      * Run the basic setup for the test
 | 
        
           |  |  | 29 |      */
 | 
        
           |  |  | 30 |     public function setup_instance(): array {
 | 
        
           |  |  | 31 |         global $DB, $PAGE;
 | 
        
           |  |  | 32 |         $this->resetAfterTest();
 | 
        
           |  |  | 33 |         $this->setAdminUser();
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 |         $course = $this->getDataGenerator()->create_course();
 | 
        
           |  |  | 36 |         $feedback = $this->getDataGenerator()->create_module('feedback', ['course' => $course->id]);
 | 
        
           |  |  | 37 |         $cm = get_coursemodule_from_instance('feedback', $feedback->id, $course->id);
 | 
        
           |  |  | 38 |         $user = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 39 |         $teacher = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 40 |         $manager = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 41 |   | 
        
           |  |  | 42 |         // Enrol a student and teacher.
 | 
        
           |  |  | 43 |         $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
 | 
        
           |  |  | 44 |         $this->getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher');
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 46 |         // Setup the site wide manager role.
 | 
        
           |  |  | 47 |         $managerrole = $DB->get_record('role', ['shortname' => 'manager']);
 | 
        
           |  |  | 48 |         role_assign($managerrole->id, $manager->id, SYSCONTEXTID);
 | 
        
           |  |  | 49 |   | 
        
           |  |  | 50 |         $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
 | 
        
           |  |  | 51 |   | 
        
           |  |  | 52 |         // Create at least one page.
 | 
        
           |  |  | 53 |         $feedbackgenerator->create_item_multichoice($feedback, ['values' => "y\nn"]);
 | 
        
           |  |  | 54 |         $feedbackgenerator->create_item_multichoice($feedback, ['values' => "0\n1"]);
 | 
        
           |  |  | 55 |         $feedbackparams = [
 | 
        
           |  |  | 56 |             'id' => $cm->id,
 | 
        
           |  |  | 57 |         ];
 | 
        
           |  |  | 58 |         $PAGE->set_cm($cm);
 | 
        
           |  |  | 59 |         $PAGE->set_activity_record($feedback);
 | 
        
           |  |  | 60 |   | 
        
           |  |  | 61 |         return [$manager, $teacher, $user, $managerrole, $feedbackparams];
 | 
        
           |  |  | 62 |     }
 | 
        
           |  |  | 63 |   | 
        
           |  |  | 64 |     /**
 | 
        
           |  |  | 65 |      * Test the create template for when capabilities have been modified
 | 
        
           |  |  | 66 |      *
 | 
        
           |  |  | 67 |      * @param array $unassignedroles
 | 
        
           |  |  | 68 |      * @param bool $accessallowed
 | 
        
           |  |  | 69 |      * @param bool $public
 | 
        
           |  |  | 70 |      * @param bool $expectedispublicvalue
 | 
        
           |  |  | 71 |      * @dataProvider createtemplate_form_with_modified_capabilities_provider
 | 
        
           |  |  | 72 |      */
 | 
        
           |  |  | 73 |     public function test_createtemplate_form_with_modified_capabilities(array $unassignedroles, bool $accessallowed,
 | 
        
           | 11 | efrain | 74 |             bool $public = false, bool $expectedispublicvalue = false): void {
 | 
        
           | 1 | efrain | 75 |         global $DB;
 | 
        
           |  |  | 76 |         [$manager, $teacher, $user, $managerrole, $feedback] = $this->setup_instance();
 | 
        
           |  |  | 77 |         $this->setAdminUser();
 | 
        
           |  |  | 78 |         foreach ($unassignedroles as $role) {
 | 
        
           |  |  | 79 |             unassign_capability($role, $managerrole->id);
 | 
        
           |  |  | 80 |         }
 | 
        
           |  |  | 81 |         $data = [
 | 
        
           |  |  | 82 |             'id' => $feedback['id'],
 | 
        
           |  |  | 83 |             'templatename' => 'mytemplate',
 | 
        
           |  |  | 84 |             'ispublic' => $public
 | 
        
           |  |  | 85 |         ];
 | 
        
           |  |  | 86 |         $this->setUser($manager);
 | 
        
           |  |  | 87 |         $submitdata = create_template_form::mock_ajax_submit($data);
 | 
        
           |  |  | 88 |         if (!$accessallowed) {
 | 
        
           |  |  | 89 |             $this->expectException(\moodle_exception::class);
 | 
        
           |  |  | 90 |         }
 | 
        
           |  |  | 91 |         $form = new create_template_form(null, null, 'post', '', null, true,
 | 
        
           |  |  | 92 |             $submitdata, true);
 | 
        
           |  |  | 93 |         $form->set_data_for_dynamic_submission();
 | 
        
           |  |  | 94 |         $this->assertTrue($form->is_validated());
 | 
        
           |  |  | 95 |         $form->process_dynamic_submission();
 | 
        
           |  |  | 96 |         $records = array_values($DB->get_records('feedback_template', null, 'id ASC'));
 | 
        
           |  |  | 97 |         $this->assertEquals($expectedispublicvalue, (bool) $records[0]->ispublic);
 | 
        
           |  |  | 98 |     }
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |     /**
 | 
        
           |  |  | 101 |      * Provider for the test_createtemplate_form_with_modified_capabilities
 | 
        
           |  |  | 102 |      *
 | 
        
           |  |  | 103 |      * @return array
 | 
        
           |  |  | 104 |      */
 | 
        
           |  |  | 105 |     public function createtemplate_form_with_modified_capabilities_provider(): array {
 | 
        
           |  |  | 106 |         return [
 | 
        
           |  |  | 107 |             "Manager without edititems permission cannot create any templates" => [
 | 
        
           |  |  | 108 |                 ['mod/feedback:edititems'], false
 | 
        
           |  |  | 109 |             ],
 | 
        
           |  |  | 110 |             "Manager without createprivatetemplate permission creating public template" => [
 | 
        
           |  |  | 111 |                 ['mod/feedback:createprivatetemplate'], true, true, true
 | 
        
           |  |  | 112 |             ],
 | 
        
           |  |  | 113 |             "Manager without createprivatetemplate permission creating private template" => [
 | 
        
           |  |  | 114 |                 ['mod/feedback:createprivatetemplate'], true
 | 
        
           |  |  | 115 |             ],
 | 
        
           |  |  | 116 |             "Manager without createpublictemplate permission creating private template" => [
 | 
        
           |  |  | 117 |                 ['mod/feedback:createpublictemplate'], true
 | 
        
           |  |  | 118 |             ],
 | 
        
           |  |  | 119 |             "Manager without createpublictemplate permission creating public template" => [
 | 
        
           |  |  | 120 |                 ['mod/feedback:createpublictemplate'], true, true
 | 
        
           |  |  | 121 |             ],
 | 
        
           |  |  | 122 |             "Manager without createprivatetemplate,createpublictemplate permission cannot create templates" => [
 | 
        
           |  |  | 123 |                 ['mod/feedback:createpublictemplate', 'mod/feedback:createprivatetemplate'], false
 | 
        
           |  |  | 124 |             ]
 | 
        
           |  |  | 125 |         ];
 | 
        
           |  |  | 126 |     }
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 |     /**
 | 
        
           |  |  | 129 |      * Test the form
 | 
        
           |  |  | 130 |      *
 | 
        
           |  |  | 131 |      * @param string $loginas
 | 
        
           |  |  | 132 |      * @param bool $public
 | 
        
           |  |  | 133 |      * @param bool $accessallowed
 | 
        
           |  |  | 134 |      * @dataProvider createtemplate_form_provider
 | 
        
           |  |  | 135 |      */
 | 
        
           |  |  | 136 |     public function test_createtemplate_form(string $loginas, bool $public,
 | 
        
           | 11 | efrain | 137 |             bool $accessallowed = true): void {
 | 
        
           | 1 | efrain | 138 |         global $DB;
 | 
        
           |  |  | 139 |         [$manager, $teacher, $user, $managerrole, $feedback] = $this->setup_instance();
 | 
        
           |  |  | 140 |         switch($loginas) {
 | 
        
           |  |  | 141 |             case 'admin':
 | 
        
           |  |  | 142 |                 $this->setAdminUser();
 | 
        
           |  |  | 143 |                 break;
 | 
        
           |  |  | 144 |             case 'student':
 | 
        
           |  |  | 145 |                 $this->setUser($user);
 | 
        
           |  |  | 146 |                 break;
 | 
        
           |  |  | 147 |             case 'teacher':
 | 
        
           |  |  | 148 |                 $this->setUser($teacher);
 | 
        
           |  |  | 149 |                 break;
 | 
        
           |  |  | 150 |             case 'manager':
 | 
        
           |  |  | 151 |                 $this->setUser($manager);
 | 
        
           |  |  | 152 |                 break;
 | 
        
           |  |  | 153 |         }
 | 
        
           |  |  | 154 |   | 
        
           |  |  | 155 |         $data = [
 | 
        
           |  |  | 156 |             'id' => $feedback['id'],
 | 
        
           |  |  | 157 |             'templatename' => 'mytemplate',
 | 
        
           |  |  | 158 |             'ispublic' => $public
 | 
        
           |  |  | 159 |         ];
 | 
        
           |  |  | 160 |   | 
        
           |  |  | 161 |         $submitdata = create_template_form::mock_ajax_submit($data);
 | 
        
           |  |  | 162 |         if (!$accessallowed) {
 | 
        
           |  |  | 163 |             $this->expectException(\moodle_exception::class);
 | 
        
           |  |  | 164 |         }
 | 
        
           |  |  | 165 |         $form = new create_template_form(null, null, 'post', '', null, true,
 | 
        
           |  |  | 166 |             $submitdata, true);
 | 
        
           |  |  | 167 |         $form->set_data_for_dynamic_submission();
 | 
        
           |  |  | 168 |         $this->assertTrue($form->is_validated());
 | 
        
           |  |  | 169 |         $form->process_dynamic_submission();
 | 
        
           |  |  | 170 |   | 
        
           |  |  | 171 |         // A teacher can access the form but cannot create public templates.
 | 
        
           |  |  | 172 |         if ($loginas == 'teacher' && $public) {
 | 
        
           |  |  | 173 |             $records = array_values($DB->get_records('feedback_template', null, 'id ASC'));
 | 
        
           |  |  | 174 |             $this->assertFalse((bool) $records[0]->ispublic);
 | 
        
           |  |  | 175 |         }
 | 
        
           |  |  | 176 |     }
 | 
        
           |  |  | 177 |   | 
        
           |  |  | 178 |     /**
 | 
        
           |  |  | 179 |      * Provider for the test_createtemplate_form
 | 
        
           |  |  | 180 |      *
 | 
        
           |  |  | 181 |      * @return array
 | 
        
           |  |  | 182 |      */
 | 
        
           |  |  | 183 |     public function createtemplate_form_provider(): array {
 | 
        
           |  |  | 184 |         return [
 | 
        
           |  |  | 185 |             'Create a private template as an admin' => [
 | 
        
           |  |  | 186 |                 'admin', false
 | 
        
           |  |  | 187 |             ],
 | 
        
           |  |  | 188 |             'Create a public template as an admin' => [
 | 
        
           |  |  | 189 |                 'admin', true
 | 
        
           |  |  | 190 |             ],
 | 
        
           |  |  | 191 |             'Create a private template as a manager' => [
 | 
        
           |  |  | 192 |                 'manager', false
 | 
        
           |  |  | 193 |             ],
 | 
        
           |  |  | 194 |             'Create a public template as a manager' => [
 | 
        
           |  |  | 195 |                 'manager', true
 | 
        
           |  |  | 196 |             ],
 | 
        
           |  |  | 197 |             'Create a private template as a teacher' => [
 | 
        
           |  |  | 198 |                 'teacher', false
 | 
        
           |  |  | 199 |             ],
 | 
        
           |  |  | 200 |             'Create a public template as a teacher' => [
 | 
        
           |  |  | 201 |                 'teacher', true
 | 
        
           |  |  | 202 |             ],
 | 
        
           |  |  | 203 |             'Create a public template as a student' => [
 | 
        
           |  |  | 204 |                 'student', true, false
 | 
        
           |  |  | 205 |             ],
 | 
        
           |  |  | 206 |             'Create a private template as a student' => [
 | 
        
           |  |  | 207 |                 'student', false, false
 | 
        
           |  |  | 208 |             ],
 | 
        
           |  |  | 209 |         ];
 | 
        
           |  |  | 210 |     }
 | 
        
           |  |  | 211 | }
 |