| 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_scorm;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           defined('MOODLE_INTERNAL') || die;
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           // Make sure the code being tested is accessible.
  | 
        
        
            | 
            | 
           22 | 
           global $CFG;
  | 
        
        
            | 
            | 
           23 | 
           require_once($CFG->dirroot . '/mod/scorm/locallib.php'); // Include the code to test.
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           /**
  | 
        
        
            | 
            | 
           26 | 
            * Unit tests for scorm_formatduration function from locallib.php
  | 
        
        
            | 
            | 
           27 | 
            *
  | 
        
        
            | 
            | 
           28 | 
            * @package    mod_scorm
  | 
        
        
            | 
            | 
           29 | 
            * @category   test
  | 
        
        
            | 
            | 
           30 | 
            * @copyright  2009 Dan Marsden
  | 
        
        
            | 
            | 
           31 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           32 | 
            */
  | 
        
        
            | 
            | 
           33 | 
           class formatduration_test extends \basic_testcase {
  | 
        
        
           | 11 | 
           efrain | 
           34 | 
               public function test_scorm2004_format(): void {
  | 
        
        
           | 1 | 
           efrain | 
           35 | 
                   $stryears = get_string('years');
  | 
        
        
            | 
            | 
           36 | 
                   $strmonths = trim(get_string('nummonths'));
  | 
        
        
            | 
            | 
           37 | 
                   $strdays = get_string('days');
  | 
        
        
            | 
            | 
           38 | 
                   $strhours = get_string('hours');
  | 
        
        
            | 
            | 
           39 | 
                   $strminutes = get_string('minutes');
  | 
        
        
            | 
            | 
           40 | 
                   $strseconds = get_string('seconds');
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
                   $suts = array(1 => 'PT001H012M0043.12S', 2 => 'PT15.3S', 3 => 'P01Y02M5DT0H7M', 4 => 'P0Y0M0DT0H1M00.00S',
  | 
        
        
            | 
            | 
           43 | 
                       5 => 'P1YT15M00.01S', 6 => 'P0Y0M0DT0H0M0.0S', 7 => 'P1MT4M0.30S', 8 => 'PT', 9 => 'P1DT2H3S', 10 => 'P4M');
  | 
        
        
            | 
            | 
           44 | 
                   $validates = array(1 => "1 $strhours 12 $strminutes 43.12 $strseconds",
  | 
        
        
            | 
            | 
           45 | 
                                       2 => "15.3 $strseconds",
  | 
        
        
            | 
            | 
           46 | 
                                       3 => "1 $stryears 2 $strmonths 5 $strdays 7 $strminutes ",
  | 
        
        
            | 
            | 
           47 | 
                                       4 => "1 $strminutes ",
  | 
        
        
            | 
            | 
           48 | 
                                       5 => "1 $stryears 15 $strminutes 0.01 $strseconds",
  | 
        
        
            | 
            | 
           49 | 
                                       6 => '',
  | 
        
        
            | 
            | 
           50 | 
                                       7 => "1 $strmonths 4 $strminutes 0.30 $strseconds",
  | 
        
        
            | 
            | 
           51 | 
                                       8 => '',
  | 
        
        
            | 
            | 
           52 | 
                                       9 => "1 $strdays 2 $strhours 3 $strseconds",
  | 
        
        
            | 
            | 
           53 | 
                                       10 => "4 $strmonths ");
  | 
        
        
            | 
            | 
           54 | 
                   foreach ($suts as $key => $sut) {
  | 
        
        
            | 
            | 
           55 | 
                       $formatted = scorm_format_duration($sut);
  | 
        
        
            | 
            | 
           56 | 
                       $this->assertEquals($formatted, $validates[$key]);
  | 
        
        
            | 
            | 
           57 | 
                   }
  | 
        
        
            | 
            | 
           58 | 
               }
  | 
        
        
            | 
            | 
           59 | 
              | 
        
        
           | 11 | 
           efrain | 
           60 | 
               public function test_scorm12_format(): void {
  | 
        
        
           | 1 | 
           efrain | 
           61 | 
                   $stryears = get_string('years');
  | 
        
        
            | 
            | 
           62 | 
                   $strmonths = trim(get_string('nummonths'));
  | 
        
        
            | 
            | 
           63 | 
                   $strdays = get_string('days');
  | 
        
        
            | 
            | 
           64 | 
                   $strhours = get_string('hours');
  | 
        
        
            | 
            | 
           65 | 
                   $strminutes = get_string('minutes');
  | 
        
        
            | 
            | 
           66 | 
                   $strseconds = get_string('seconds');
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
                   $suts = array(1 => '00:00:00', 2 => '1:2:3', 3 => '12:34:56.78', 4 => '00:12:00.03', 5 => '01:00:23', 6 => '00:12:34.00',
  | 
        
        
            | 
            | 
           69 | 
                       7 => '00:01:02.03', 8 => '00:00:00.1', 9 => '1:23:00', 10 => '2:00:00');
  | 
        
        
            | 
            | 
           70 | 
                   $validates = array(1 => '',
  | 
        
        
            | 
            | 
           71 | 
                                       2 => "1 $strhours 2 $strminutes 3 $strseconds",
  | 
        
        
            | 
            | 
           72 | 
                                       3 => "12 $strhours 34 $strminutes 56.78 $strseconds",
  | 
        
        
            | 
            | 
           73 | 
                                       4 => "12 $strminutes 0.03 $strseconds",
  | 
        
        
            | 
            | 
           74 | 
                                       5 => "1 $strhours 23 $strseconds",
  | 
        
        
            | 
            | 
           75 | 
                                       6 => "12 $strminutes 34 $strseconds",
  | 
        
        
            | 
            | 
           76 | 
                                       7 => "1 $strminutes 2.03 $strseconds",
  | 
        
        
            | 
            | 
           77 | 
                                       8 => "0.1 $strseconds",
  | 
        
        
            | 
            | 
           78 | 
                                       9 => "1 $strhours 23 $strminutes ",
  | 
        
        
            | 
            | 
           79 | 
                                       10 => "2 $strhours ");
  | 
        
        
            | 
            | 
           80 | 
                   foreach ($suts as $key => $sut) {
  | 
        
        
            | 
            | 
           81 | 
                       $formatted = scorm_format_duration($sut);
  | 
        
        
            | 
            | 
           82 | 
                       $this->assertEquals($formatted, $validates[$key]);
  | 
        
        
            | 
            | 
           83 | 
                   }
  | 
        
        
            | 
            | 
           84 | 
               }
  | 
        
        
            | 
            | 
           85 | 
              | 
        
        
           | 11 | 
           efrain | 
           86 | 
               public function test_non_datetime(): void {
  | 
        
        
           | 1 | 
           efrain | 
           87 | 
               }
  | 
        
        
            | 
            | 
           88 | 
           }
  |