Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 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   * Helpers for the core_tag subsystem implementation of privacy.
  19   *
  20   * @package    core_tag
  21   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_tag\tests;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use \core_privacy\tests\request\content_writer;
  30  
  31  global $CFG;
  32  
  33  /**
  34   * Helpers for the core_tag subsystem implementation of privacy.
  35   *
  36   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  trait privacy_helper {
  40      /**
  41       * Fetch all tags on a subcontext.
  42       *
  43       * @param   \context            $context    The context being stored.
  44       * @param   array               $subcontext The subcontext path to check.
  45       * @return  array
  46       */
  47      protected function get_tags_on_subcontext(\context $context, array $subcontext) {
  48          $writer = \core_privacy\local\request\writer::with_context($context);
  49          return $writer->get_related_data($subcontext, 'tags');
  50      }
  51  
  52      /**
  53       * Check that all tags match on the specified context.
  54       *
  55       * @param   int                 $userid     The ID of the user being stored.
  56       * @param   \context            $context    The context being stored.
  57       * @param   array               $subcontext The subcontext path to check.
  58       * @param   string              $component  The component being stored.
  59       * @param   string              $itemtype    The tag area to store results for.
  60       * @param   int                 $itemid     The itemid to store.
  61       */
  62      protected function assert_all_tags_match_on_context(
  63          int $userid,
  64          \context $context,
  65          array $subcontext,
  66          $component,
  67          $itemtype,
  68          $itemid
  69      ) {
  70          $writer = \core_privacy\local\request\writer::with_context($context);
  71  
  72          $dbtags = \core_tag_tag::get_item_tags($component, $itemtype, $itemid);
  73          $exportedtags = $this->get_tags_on_subcontext($context, $subcontext);
  74  
  75          $this->assertCount(count($dbtags), $exportedtags);
  76  
  77          foreach ($dbtags as $tag) {
  78              $this->assertContains($tag->rawname, $exportedtags);
  79          }
  80      }
  81  }