| 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 | 
           /**
  | 
        
        
            | 
            | 
           18 | 
            * Contains unit tests for mod_feedback\dates.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package   mod_feedback
  | 
        
        
            | 
            | 
           21 | 
            * @category  test
  | 
        
        
            | 
            | 
           22 | 
            * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
  | 
        
        
            | 
            | 
           23 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           24 | 
            */
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           declare(strict_types=1);
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           namespace mod_feedback;
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           use advanced_testcase;
  | 
        
        
            | 
            | 
           31 | 
           use cm_info;
  | 
        
        
            | 
            | 
           32 | 
           use core\activity_dates;
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           /**
  | 
        
        
            | 
            | 
           35 | 
            * Class for unit testing mod_feedback\dates.
  | 
        
        
            | 
            | 
           36 | 
            *
  | 
        
        
            | 
            | 
           37 | 
            * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
  | 
        
        
            | 
            | 
           38 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           39 | 
            */
  | 
        
        
            | 
            | 
           40 | 
           class dates_test extends advanced_testcase {
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               /**
  | 
        
        
            | 
            | 
           43 | 
                * Data provider for get_dates_for_module().
  | 
        
        
            | 
            | 
           44 | 
                * @return array[]
  | 
        
        
            | 
            | 
           45 | 
                */
  | 
        
        
            | 
            | 
           46 | 
               public function get_dates_for_module_provider(): array {
  | 
        
        
            | 
            | 
           47 | 
                   $now = time();
  | 
        
        
            | 
            | 
           48 | 
                   $before = $now - DAYSECS;
  | 
        
        
            | 
            | 
           49 | 
                   $earlier = $before - DAYSECS;
  | 
        
        
            | 
            | 
           50 | 
                   $after = $now + DAYSECS;
  | 
        
        
            | 
            | 
           51 | 
                   $later = $after + DAYSECS;
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
                   return [
  | 
        
        
            | 
            | 
           54 | 
                       'without any dates' => [
  | 
        
        
            | 
            | 
           55 | 
                           null, null, []
  | 
        
        
            | 
            | 
           56 | 
                       ],
  | 
        
        
            | 
            | 
           57 | 
                       'only with opening time' => [
  | 
        
        
            | 
            | 
           58 | 
                           $after, null, [
  | 
        
        
            | 
            | 
           59 | 
                               ['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeopen'],
  | 
        
        
            | 
            | 
           60 | 
                           ]
  | 
        
        
            | 
            | 
           61 | 
                       ],
  | 
        
        
            | 
            | 
           62 | 
                       'only with closing time' => [
  | 
        
        
            | 
            | 
           63 | 
                           null, $after, [
  | 
        
        
            | 
            | 
           64 | 
                               ['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeclose'],
  | 
        
        
            | 
            | 
           65 | 
                           ]
  | 
        
        
            | 
            | 
           66 | 
                       ],
  | 
        
        
            | 
            | 
           67 | 
                       'with both times' => [
  | 
        
        
            | 
            | 
           68 | 
                           $after, $later, [
  | 
        
        
            | 
            | 
           69 | 
                               ['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeopen'],
  | 
        
        
            | 
            | 
           70 | 
                               ['label' => 'Closes:', 'timestamp' => $later, 'dataid' => 'timeclose'],
  | 
        
        
            | 
            | 
           71 | 
                           ]
  | 
        
        
            | 
            | 
           72 | 
                       ],
  | 
        
        
            | 
            | 
           73 | 
                       'between the dates' => [
  | 
        
        
            | 
            | 
           74 | 
                           $before, $after, [
  | 
        
        
            | 
            | 
           75 | 
                               ['label' => 'Opened:', 'timestamp' => $before, 'dataid' => 'timeopen'],
  | 
        
        
            | 
            | 
           76 | 
                               ['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeclose'],
  | 
        
        
            | 
            | 
           77 | 
                           ]
  | 
        
        
            | 
            | 
           78 | 
                       ],
  | 
        
        
            | 
            | 
           79 | 
                       'dates are past' => [
  | 
        
        
            | 
            | 
           80 | 
                           $earlier, $before, [
  | 
        
        
            | 
            | 
           81 | 
                               ['label' => 'Opened:', 'timestamp' => $earlier, 'dataid' => 'timeopen'],
  | 
        
        
            | 
            | 
           82 | 
                               ['label' => 'Closed:', 'timestamp' => $before, 'dataid' => 'timeclose'],
  | 
        
        
            | 
            | 
           83 | 
                           ]
  | 
        
        
            | 
            | 
           84 | 
                       ],
  | 
        
        
            | 
            | 
           85 | 
                   ];
  | 
        
        
            | 
            | 
           86 | 
               }
  | 
        
        
            | 
            | 
           87 | 
              | 
        
        
            | 
            | 
           88 | 
               /**
  | 
        
        
            | 
            | 
           89 | 
                * Test for get_dates_for_module().
  | 
        
        
            | 
            | 
           90 | 
                *
  | 
        
        
            | 
            | 
           91 | 
                * @dataProvider get_dates_for_module_provider
  | 
        
        
            | 
            | 
           92 | 
                * @param int|null $timeopen The "allow answers from" time in the feedback activity.
  | 
        
        
            | 
            | 
           93 | 
                * @param int|null $timeclose The "allow answers to" time in the feedback activity.
  | 
        
        
            | 
            | 
           94 | 
                * @param array $expected The expected value of calling get_dates_for_module()
  | 
        
        
            | 
            | 
           95 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           96 | 
               public function test_get_dates_for_module(?int $timeopen, ?int $timeclose, array $expected): void {
  | 
        
        
           | 1 | 
           efrain | 
           97 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           100 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           101 | 
                   $this->getDataGenerator()->enrol_user($user->id, $course->id);
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
                   $data = ['course' => $course->id];
  | 
        
        
            | 
            | 
           104 | 
                   if ($timeopen) {
  | 
        
        
            | 
            | 
           105 | 
                       $data['timeopen'] = $timeopen;
  | 
        
        
            | 
            | 
           106 | 
                   }
  | 
        
        
            | 
            | 
           107 | 
                   if ($timeclose) {
  | 
        
        
            | 
            | 
           108 | 
                       $data['timeclose'] = $timeclose;
  | 
        
        
            | 
            | 
           109 | 
                   }
  | 
        
        
            | 
            | 
           110 | 
                   $feedback = $this->getDataGenerator()->create_module('feedback', $data);
  | 
        
        
            | 
            | 
           111 | 
              | 
        
        
            | 
            | 
           112 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           113 | 
              | 
        
        
            | 
            | 
           114 | 
                   $cm = get_coursemodule_from_instance('feedback', $feedback->id);
  | 
        
        
            | 
            | 
           115 | 
                   // Make sure we're using a cm_info object.
  | 
        
        
            | 
            | 
           116 | 
                   $cm = cm_info::create($cm);
  | 
        
        
            | 
            | 
           117 | 
              | 
        
        
            | 
            | 
           118 | 
                   $dates = activity_dates::get_dates_for_module($cm, (int) $user->id);
  | 
        
        
            | 
            | 
           119 | 
              | 
        
        
            | 
            | 
           120 | 
                   $this->assertEquals($expected, $dates);
  | 
        
        
            | 
            | 
           121 | 
               }
  | 
        
        
            | 
            | 
           122 | 
           }
  |