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 311 and 401] [Versions 39 and 401] [Versions 401 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   * Coverage information for PHPUnit.
  19   *
  20   * @package    core
  21   * @category   phpunit
  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  /**
  29   * Coverage information for PHPUnit.
  30   *
  31   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class phpunit_coverage_info {
  35  
  36      /**
  37       * @var array The list of folders relative to the plugin root to whitelist in coverage generation.
  38       * @deprecated since Moodle 3.11 MDL-70745 - please don't use this property any more.
  39       * @todo MDL-71067 - remove in Moodle 4.3
  40       */
  41      protected $whitelistfolders = [];
  42  
  43      /**
  44       * @var array The list of files relative to the plugin root to whitelist in coverage generation.
  45       * @deprecated since Moodle 3.11 MDL-70745 - please don't use this property any more.
  46       * @todo MDL-71067 - remove in Moodle 4.3
  47       */
  48      protected $whitelistfiles = [];
  49  
  50      /** @var array The list of folders relative to the plugin root to include in coverage generation. */
  51      protected $includelistfolders = [];
  52  
  53      /** @var array The list of files relative to the plugin root to include in coverage generation. */
  54      protected $includelistfiles = [];
  55  
  56      /** @var array The list of folders relative to the plugin root to exclude from coverage generation. */
  57      protected $excludelistfolders = [];
  58  
  59      /** @var array The list of files relative to the plugin root to exclude from coverage generation. */
  60      protected $excludelistfiles = [];
  61  
  62      /**
  63       * Get the formatted XML list of files and folders to include.
  64       *
  65       * @param   string  $plugindir The root of the plugin, relative to the dataroot.
  66       * @return  array
  67       */
  68      final public function get_includelists(string $plugindir) : array {
  69          $coverages = [];
  70  
  71          $includelistfolders = array_merge([
  72              'classes',
  73              'tests/generator',
  74          ], $this->includelistfolders);;
  75  
  76          $includelistfiles = array_merge([
  77              'externallib.php',
  78              'lib.php',
  79              'locallib.php',
  80              'renderer.php',
  81              'rsslib.php',
  82          ], $this->includelistfiles);
  83  
  84          if (!empty($plugindir)) {
  85              $plugindir .= "/";
  86  
  87              // TODO: MDL-71067 - remove this whole block once these properties deprecation period ends.
  88              if (!empty($this->whitelistfolders) || !empty($this->whitelistfiles)) {
  89                  // Warning if the deprecated (whitelist) properties are found.
  90                  echo "Warning: \$whitelistfolders and \$whitelistfiles in " .
  91                      "coverage.php files are deprecated since Moodle 3.11. " .
  92                      "Please, replace them with \$includelistfolders and " .
  93                      "\$includelistfiles in {$plugindir}tests/coverage.php\n";
  94  
  95                  $includelistfolders = array_merge($includelistfolders, $this->whitelistfolders);
  96                  $includelistfiles = array_merge($includelistfiles, $this->whitelistfiles);
  97              }
  98          }
  99  
 100          foreach (array_unique($includelistfolders) as $folder) {
 101              $coverages[] = html_writer::tag('directory', "{$plugindir}{$folder}", ['suffix' => '.php']);
 102          }
 103  
 104          foreach (array_unique($includelistfiles) as $file) {
 105              $coverages[] = html_writer::tag('file', "{$plugindir}{$file}");
 106          }
 107  
 108          return $coverages;
 109      }
 110  
 111      /**
 112       * Get the formatted XML list of files and folders to exclude.
 113       *
 114       * @param   string  $plugindir The root of the plugin, relative to the dataroot.
 115       * @return  array
 116       */
 117      final public function get_excludelists(string $plugindir) : array {
 118          $coverages = [];
 119  
 120          if (!empty($plugindir)) {
 121              $plugindir .= "/";
 122          }
 123  
 124          foreach ($this->excludelistfolders as $folder) {
 125              $coverages[] = html_writer::tag('directory', "{$plugindir}{$folder}", ['suffix' => '.php']);
 126          }
 127  
 128          foreach ($this->excludelistfiles as $file) {
 129              $coverages[] = html_writer::tag('file', "{$plugindir}{$file}");
 130          }
 131  
 132          return $coverages;
 133      }
 134  }