| 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 use_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 | 
                   $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
                   $feedbackgenerator = $this->getDataGenerator()->get_plugin_generator('mod_feedback');
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
                   // Create at least one page.
  | 
        
        
            | 
            | 
           44 | 
                   $feedbackgenerator->create_item_multichoice($feedback, ['values' => "y\nn"]);
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
                   feedback_save_as_template($feedback, 'my template', 0);
  | 
        
        
            | 
            | 
           47 | 
                   $feedbackgenerator->create_item_multichoice($feedback, ['values' => "0\n1"]);
  | 
        
        
            | 
            | 
           48 | 
                   feedback_save_as_template($feedback, 'mytemplate2', 1);
  | 
        
        
            | 
            | 
           49 | 
                   $records = array_keys($DB->get_records('feedback_template', null, 'id ASC'));
  | 
        
        
            | 
            | 
           50 | 
                   $feedbackparams = [
  | 
        
        
            | 
            | 
           51 | 
                       'id' => $cm->id,
  | 
        
        
            | 
            | 
           52 | 
                       'privatetemplate' => $records[0],
  | 
        
        
            | 
            | 
           53 | 
                       'publictemplate' => $records[1],
  | 
        
        
            | 
            | 
           54 | 
                   ];
  | 
        
        
            | 
            | 
           55 | 
                   $PAGE->set_cm($cm);
  | 
        
        
            | 
            | 
           56 | 
                   $PAGE->set_activity_record($feedback);
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
                   return [$user, $feedbackparams];
  | 
        
        
            | 
            | 
           59 | 
               }
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
               /**
  | 
        
        
            | 
            | 
           62 | 
                * Test the form
  | 
        
        
            | 
            | 
           63 | 
                *
  | 
        
        
            | 
            | 
           64 | 
                * @param string $loginas Which user to log in as
  | 
        
        
            | 
            | 
           65 | 
                * @param bool $private Whether we are creating a private template
  | 
        
        
            | 
            | 
           66 | 
                * @param bool $expected Whether or not the form should be validated
  | 
        
        
            | 
            | 
           67 | 
                * @dataProvider usetemplate_form_provider
  | 
        
        
            | 
            | 
           68 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           69 | 
               public function test_usetemplate_form(string $loginas, bool $private, bool $expected): void {
  | 
        
        
           | 1 | 
           efrain | 
           70 | 
                   [$user, $feedback] = $this->setup_instance();
  | 
        
        
            | 
            | 
           71 | 
                   switch($loginas) {
  | 
        
        
            | 
            | 
           72 | 
                       case 'admin':
  | 
        
        
            | 
            | 
           73 | 
                           $this->setAdminUser();
  | 
        
        
            | 
            | 
           74 | 
                           break;
  | 
        
        
            | 
            | 
           75 | 
                       case 'student':
  | 
        
        
            | 
            | 
           76 | 
                           $this->setUser($user);
  | 
        
        
            | 
            | 
           77 | 
                           break;
  | 
        
        
            | 
            | 
           78 | 
                   }
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
                   $data = [
  | 
        
        
            | 
            | 
           81 | 
                       'id' => $feedback['id'],
  | 
        
        
            | 
            | 
           82 | 
                       'templateid' => $private ? $feedback['privatetemplate'] : $feedback['publictemplate'],
  | 
        
        
            | 
            | 
           83 | 
                   ];
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
                   $submitdata = use_template_form::mock_ajax_submit($data);
  | 
        
        
            | 
            | 
           86 | 
                   if (!$expected) {
  | 
        
        
            | 
            | 
           87 | 
                       $this->expectException(\moodle_exception::class);
  | 
        
        
            | 
            | 
           88 | 
                   }
  | 
        
        
            | 
            | 
           89 | 
                   $form = new use_template_form(null, null, 'post', '', null, true,
  | 
        
        
            | 
            | 
           90 | 
                       $submitdata, true);
  | 
        
        
            | 
            | 
           91 | 
                   $form->set_data_for_dynamic_submission();
  | 
        
        
            | 
            | 
           92 | 
                   if ($expected) {
  | 
        
        
            | 
            | 
           93 | 
                       $this->assertTrue($form->is_validated());
  | 
        
        
            | 
            | 
           94 | 
                   }
  | 
        
        
            | 
            | 
           95 | 
                   $form->process_dynamic_submission();
  | 
        
        
            | 
            | 
           96 | 
               }
  | 
        
        
            | 
            | 
           97 | 
              | 
        
        
            | 
            | 
           98 | 
               /**
  | 
        
        
            | 
            | 
           99 | 
                * Provider for the test_usetemplate_form test
  | 
        
        
            | 
            | 
           100 | 
                *
  | 
        
        
            | 
            | 
           101 | 
                * @return array
  | 
        
        
            | 
            | 
           102 | 
                */
  | 
        
        
            | 
            | 
           103 | 
               public function usetemplate_form_provider() {
  | 
        
        
            | 
            | 
           104 | 
                   return [
  | 
        
        
            | 
            | 
           105 | 
                       'Test submission with a private template as an admin' => [
  | 
        
        
            | 
            | 
           106 | 
                           'admin', true, true
  | 
        
        
            | 
            | 
           107 | 
                       ],
  | 
        
        
            | 
            | 
           108 | 
                       'Test submission with a public template as an admin' => [
  | 
        
        
            | 
            | 
           109 | 
                           'admin', false, true
  | 
        
        
            | 
            | 
           110 | 
                       ],
  | 
        
        
            | 
            | 
           111 | 
                       'Test submission with a public template as a student' => [
  | 
        
        
            | 
            | 
           112 | 
                           'student', false, false
  | 
        
        
            | 
            | 
           113 | 
                       ],
  | 
        
        
            | 
            | 
           114 | 
                       'Test submission with a private template as a student' => [
  | 
        
        
            | 
            | 
           115 | 
                           'student', true, false
  | 
        
        
            | 
            | 
           116 | 
                       ],
  | 
        
        
            | 
            | 
           117 | 
                   ];
  | 
        
        
            | 
            | 
           118 | 
               }
  | 
        
        
            | 
            | 
           119 | 
           }
  |