| 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 | 
           declare(strict_types=1);
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           namespace core_reportbuilder\local\helpers;
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           use advanced_testcase;
  | 
        
        
            | 
            | 
           22 | 
           use stdClass;
  | 
        
        
            | 
            | 
           23 | 
              | 
        
        
            | 
            | 
           24 | 
           /**
  | 
        
        
            | 
            | 
           25 | 
            * Unit tests for the format helper
  | 
        
        
            | 
            | 
           26 | 
            *
  | 
        
        
            | 
            | 
           27 | 
            * @package     core_reportbuilder
  | 
        
        
            | 
            | 
           28 | 
            * @covers      \core_reportbuilder\local\helpers\format
  | 
        
        
            | 
            | 
           29 | 
            * @copyright   2021 Paul Holden <paulh@moodle.com>
  | 
        
        
            | 
            | 
           30 | 
            * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           31 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           32 | 
           final class format_test extends advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
               /**
  | 
        
        
            | 
            | 
           35 | 
                * Test userdate method
  | 
        
        
            | 
            | 
           36 | 
                */
  | 
        
        
            | 
            | 
           37 | 
               public function test_userdate(): void {
  | 
        
        
            | 
            | 
           38 | 
                   $now = time();
  | 
        
        
            | 
            | 
           39 | 
              | 
        
        
            | 
            | 
           40 | 
                   $userdate = format::userdate($now, new stdClass());
  | 
        
        
            | 
            | 
           41 | 
                   $this->assertEquals(userdate($now), $userdate);
  | 
        
        
            | 
            | 
           42 | 
               }
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
               /**
  | 
        
        
           | 1441 | 
           ariadna | 
           45 | 
                * Data provider for {@see test_format_time}
  | 
        
        
            | 
            | 
           46 | 
                *
  | 
        
        
            | 
            | 
           47 | 
                * @return array[]
  | 
        
        
            | 
            | 
           48 | 
                */
  | 
        
        
            | 
            | 
           49 | 
               public static function format_time_provider(): array {
  | 
        
        
            | 
            | 
           50 | 
                   return [
  | 
        
        
            | 
            | 
           51 | 
                       [null, 0, ''],
  | 
        
        
            | 
            | 
           52 | 
                       [0, 0, '0 secs'],
  | 
        
        
            | 
            | 
           53 | 
                       [2.456, 1, '2.5 secs'],
  | 
        
        
            | 
            | 
           54 | 
                       [3.2, null, '3 secs'],
  | 
        
        
            | 
            | 
           55 | 
                   ];
  | 
        
        
            | 
            | 
           56 | 
               }
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
               /**
  | 
        
        
            | 
            | 
           59 | 
                * Test format time
  | 
        
        
            | 
            | 
           60 | 
                *
  | 
        
        
            | 
            | 
           61 | 
                * @param float|null $value
  | 
        
        
            | 
            | 
           62 | 
                * @param int|null $precision
  | 
        
        
            | 
            | 
           63 | 
                * @param string $expected
  | 
        
        
            | 
            | 
           64 | 
                *
  | 
        
        
            | 
            | 
           65 | 
                * @dataProvider format_time_provider
  | 
        
        
            | 
            | 
           66 | 
                */
  | 
        
        
            | 
            | 
           67 | 
               public function test_format_time(?float $value, ?int $precision, string $expected): void {
  | 
        
        
            | 
            | 
           68 | 
                   $this->assertEquals($expected, format::format_time($value, (object) [], $precision));
  | 
        
        
            | 
            | 
           69 | 
               }
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
               /**
  | 
        
        
           | 1 | 
           efrain | 
           72 | 
                * Data provider for {@see test_boolean_as_text}
  | 
        
        
            | 
            | 
           73 | 
                *
  | 
        
        
           | 1441 | 
           ariadna | 
           74 | 
                * @return array[]
  | 
        
        
           | 1 | 
           efrain | 
           75 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           76 | 
               public static function boolean_as_text_provider(): array {
  | 
        
        
           | 1 | 
           efrain | 
           77 | 
                   return [
  | 
        
        
            | 
            | 
           78 | 
                       [false, get_string('no')],
  | 
        
        
            | 
            | 
           79 | 
                       [true, get_string('yes')],
  | 
        
        
            | 
            | 
           80 | 
                   ];
  | 
        
        
            | 
            | 
           81 | 
               }
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
               /**
  | 
        
        
            | 
            | 
           84 | 
                * Test boolean as text
  | 
        
        
            | 
            | 
           85 | 
                *
  | 
        
        
            | 
            | 
           86 | 
                * @param bool $value
  | 
        
        
            | 
            | 
           87 | 
                * @param string $expected
  | 
        
        
            | 
            | 
           88 | 
                *
  | 
        
        
            | 
            | 
           89 | 
                * @dataProvider boolean_as_text_provider
  | 
        
        
            | 
            | 
           90 | 
                */
  | 
        
        
            | 
            | 
           91 | 
               public function test_boolean_as_text(bool $value, string $expected): void {
  | 
        
        
            | 
            | 
           92 | 
                   $this->assertEquals($expected, format::boolean_as_text($value));
  | 
        
        
            | 
            | 
           93 | 
               }
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
               /**
  | 
        
        
            | 
            | 
           96 | 
                * Test percentage formatting of a float
  | 
        
        
            | 
            | 
           97 | 
                */
  | 
        
        
            | 
            | 
           98 | 
               public function test_percent(): void {
  | 
        
        
            | 
            | 
           99 | 
                   $this->assertEquals('33.3%', format::percent(1 / 3 * 100));
  | 
        
        
            | 
            | 
           100 | 
               }
  | 
        
        
            | 
            | 
           101 | 
           }
  |