Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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 core_privacy;
  18  
  19  use core_privacy\local\metadata\types\database_table;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  global $CFG;
  24  
  25  /**
  26   * Tests for the \core_privacy API's types\database_table functionality.
  27   *
  28   * @package     core_privacy
  29   * @category    test
  30   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   * @coversDefaultClass \core_privacy\local\metadata\types\database_table
  33   */
  34  class types_database_table_test extends \advanced_testcase {
  35  
  36      /**
  37       * Ensure that warnings are thrown if string identifiers contain invalid characters.
  38       *
  39       * @dataProvider invalid_string_provider
  40       * @param   string  $name Name
  41       * @param   array   $fields List of fields
  42       * @param   string  $summary Summary
  43       * @covers ::__construct
  44       */
  45      public function test_invalid_configs($name, $fields, $summary) {
  46          $record = new database_table($name, $fields, $summary);
  47          $this->assertDebuggingCalled();
  48      }
  49  
  50      /**
  51       * Ensure that warnings are not thrown if debugging is not enabled, even if string identifiers contain invalid characters.
  52       *
  53       * @dataProvider invalid_string_provider
  54       * @param   string  $name Name
  55       * @param   array   $fields List of fields
  56       * @param   string  $summary Summary
  57       * @covers ::__construct
  58       */
  59      public function test_invalid_configs_debug_normal($name, $fields, $summary) {
  60          global $CFG;
  61          $this->resetAfterTest();
  62  
  63          $CFG->debug = DEBUG_NORMAL;
  64          $record = new database_table($name, $fields, $summary);
  65          $this->assertDebuggingNotCalled();
  66      }
  67  
  68      /**
  69       * Ensure that no warnings are shown for valid combinations.
  70       *
  71       * @dataProvider valid_string_provider
  72       * @param   string  $name Name
  73       * @param   array   $fields List of fields
  74       * @param   string  $summary Summary
  75       * @covers ::__construct
  76       */
  77      public function test_valid_configs($name, $fields, $summary) {
  78          $record = new database_table($name, $fields, $summary);
  79          $this->assertDebuggingNotCalled();
  80      }
  81  
  82      /**
  83       * Data provider with a list of invalid string identifiers.
  84       *
  85       * @return  array
  86       */
  87      public function invalid_string_provider() {
  88          return [
  89              'Space in summary' => [
  90                  'example',
  91                  [
  92                      'field' => 'privacy:valid',
  93                  ],
  94                  'This table is used for purposes.',
  95              ],
  96              'Comma in summary' => [
  97                  'example',
  98                  [
  99                      'field' => 'privacy:valid',
 100                  ],
 101                  'privacy,foo',
 102              ],
 103              'Space in field name' => [
 104                  'example',
 105                  [
 106                      'field' => 'This field is used for purposes.',
 107                  ],
 108                  'privacy:valid',
 109              ],
 110              'Comma in field name' => [
 111                  'example',
 112                  [
 113                      'field' => 'invalid,name',
 114                  ],
 115                  'privacy:valid',
 116              ],
 117              'No fields specified' => [
 118                  'example',
 119                  [],
 120                  'privacy:example:valid',
 121              ],
 122  
 123          ];
 124      }
 125  
 126      /**
 127       * Data provider with a list of valid string identifiers.
 128       *
 129       * @return  array
 130       */
 131      public function valid_string_provider() {
 132          return [
 133              'Valid combination' => [
 134                  'example',
 135                  [
 136                      'field' => 'privacy:example:valid:field',
 137                      'field2' => 'privacy:example:valid:field2',
 138                  ],
 139                  'privacy:example:valid',
 140              ],
 141          ];
 142      }
 143  }