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 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  /**
  18   * Unit Tests for the Moodle Content Writer.
  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\writer;
  31  
  32  /**
  33   * Tests for the \core_privacy API's moodle_content_writer functionality.
  34   *
  35   * Note: The \core_privacy\tests\request\content_writer will be used for these tests.
  36   * This content writer has additional sugar methods for fetching infromation which are not part of the standard
  37   * content_writer interface.
  38   *
  39   * @copyright   2018 Andrew Nicols <andrew@nicols.co.uk>
  40   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   * @coversDefaultClass \core_privacy\local\request\writer
  42   */
  43  class writer_test extends advanced_testcase {
  44      /**
  45       * Ensure that the writer is cleared away as appropriate after each
  46       * test.
  47       */
  48      public function tearDown(): void {
  49          writer::reset();
  50      }
  51  
  52      /**
  53       * Test that calling with_context multiple times will return the same write instance.
  54       *
  55       * @covers ::with_context
  56       */
  57      public function test_with_context() {
  58          $writer = writer::with_context(\context_system::instance());
  59  
  60          $this->assertSame($writer, writer::with_context(\context_system::instance()));
  61      }
  62  
  63      /**
  64       * Test that calling with_context multiple times will return the same write instance.
  65       *
  66       * @covers ::with_context
  67       */
  68      public function test_with_context_different_context_same_instance() {
  69          $writer = writer::with_context(\context_system::instance());
  70  
  71          $this->assertSame($writer, writer::with_context(\context_user::instance(\core_user::get_user_by_username('admin')->id)));
  72      }
  73  
  74      /**
  75       * Test that calling writer::reset() causes a new copy of the writer to be returned.
  76       *
  77       * @covers ::reset
  78       */
  79      public function test_reset() {
  80          $writer = writer::with_context(\context_system::instance());
  81          writer::reset();
  82  
  83          $this->assertNotSame($writer, writer::with_context(\context_system::instance()));
  84      }
  85  
  86      /**
  87       * Test that the export_user_preference calls the writer against the system context.
  88       *
  89       * @covers ::export_user_preference
  90       */
  91      public function test_export_user_preference_sets_system_context() {
  92          $writer = writer::with_context(\context_user::instance(\core_user::get_user_by_username('admin')->id));
  93  
  94          writer::export_user_preference('core_test', 'key', 'value', 'description');
  95  
  96          $this->assertSame(\context_system::instance(), $writer->get_current_context());
  97      }
  98  }