Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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   * Unit Tests for the request transform helper.
  19   *
  20   * @package     core_privacy
  21   * @category    test
  22   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  23   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  use \core_privacy\local\request\transform;
  31  
  32  /**
  33   * Tests for the \core_privacy API's request transform helper functionality.
  34   *
  35   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  36   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   * @coversDefaultClass \core_privacy\local\request\transform
  38   */
  39  class request_transform_test extends advanced_testcase {
  40      /**
  41       * Test that user translation currently does nothing.
  42       *
  43       * We have not determined if we will do this or not, but we provide the functionality and encourgae people to use
  44       * it so that it can be retrospectively fitted if required.
  45       *
  46       * @covers ::user
  47       */
  48      public function test_user() {
  49          // Note: This test currently sucks, but there's no point creating users just to test this.
  50          for ($i = 0; $i < 10; $i++) {
  51              $this->assertEquals($i, transform::user($i));
  52          }
  53      }
  54  
  55      /**
  56       * Test that the datetime is translated into a string.
  57       *
  58       * @covers ::datetime
  59       */
  60      public function test_datetime() {
  61          $time = 1;
  62  
  63          $datestr = transform::datetime($time);
  64  
  65          // Assert it is a string.
  66          $this->assertInternalType('string', $datestr);
  67  
  68          // To prevent failures on MAC where we are returned with a lower-case 'am' we want to convert this to 'AM'.
  69          $datestr = str_replace('am', 'AM', $datestr);
  70  
  71          // Assert the formatted date is correct.
  72          $dateobj = new DateTime();
  73          $dateobj->setTimestamp($time);
  74          $this->assertEquals($dateobj->format('l, j F Y, g:i A'), $datestr);
  75      }
  76  
  77      /**
  78       * Test that the date is translated into a string.
  79       *
  80       * @covers ::date
  81       */
  82      public function test_date() {
  83          $time = 1;
  84  
  85          $datestr = transform::date($time);
  86  
  87          // Assert it is a string.
  88          $this->assertInternalType('string', $datestr);
  89  
  90          // Assert the formatted date is correct.
  91          $dateobj = new DateTime();
  92          $dateobj->setTimestamp($time);
  93          $this->assertEquals($dateobj->format('j F Y'), $datestr);
  94      }
  95  
  96      /**
  97       * Ensure that the yesno function translates correctly.
  98       *
  99       * @dataProvider yesno_provider
 100       * @param   mixed   $input The input to test
 101       * @param   string  $expected The expected value
 102       * @covers ::yesno
 103       */
 104      public function test_yesno($input, $expected) {
 105          $this->assertEquals($expected, transform::yesno($input));
 106      }
 107  
 108      /**
 109       * Data provider for tests of the yesno transformation.
 110       *
 111       * @return  array
 112       */
 113      public function yesno_provider() {
 114          return [
 115              'Bool False' => [
 116                  false,
 117                  get_string('no'),
 118              ],
 119              'Bool true' => [
 120                  true,
 121                  get_string('yes'),
 122              ],
 123              'Int 0' => [
 124                  0,
 125                  get_string('no'),
 126              ],
 127              'Int 1' => [
 128                  1,
 129                  get_string('yes'),
 130              ],
 131              'String 0' => [
 132                  '0',
 133                  get_string('no'),
 134              ],
 135              'String 1' => [
 136                  '1',
 137                  get_string('yes'),
 138              ],
 139          ];
 140      }
 141  }