Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [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   * Unit tests for core_table\external\dynamic\get;
  19   *
  20   * @package   core_table
  21   * @category  test
  22   * @copyright  2020 Simey Lameze <simey@moodle.com>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
  24   */
  25  
  26  declare(strict_types = 1);
  27  
  28  namespace core_table\external\dynamic;
  29  
  30  use core_table\local\filter\filter;
  31  use advanced_testcase;
  32  
  33  /**
  34   * Unit tests for core_table\external\dynamic\get;
  35   *
  36   * @package   core_table
  37   * @category  test
  38   * @copyright  2020 Simey Lameze <simey@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class get_test extends advanced_testcase {
  42      /**
  43       * Test execute invalid component format.
  44       */
  45      public function test_execute_invalid_component_format(): void {
  46          $this->resetAfterTest();
  47  
  48          $this->expectException(\invalid_parameter_exception::class);
  49          get::execute(
  50              "core-user",
  51              "participants",
  52              "",
  53              $this->get_sort_array(['email' => SORT_ASC]),
  54              [],
  55              (string) filter::JOINTYPE_ANY,
  56              null,
  57              null,
  58              null,
  59              null,
  60              [],
  61              null
  62  
  63          );
  64      }
  65  
  66      /**
  67       * Test execute invalid component.
  68       */
  69      public function test_execute_invalid_component(): void {
  70          $this->resetAfterTest();
  71  
  72          $this->expectException(\UnexpectedValueException::class);
  73          get::execute(
  74              "core_users",
  75              "participants",
  76              "",
  77              $this->get_sort_array(['email' => SORT_ASC]),
  78              [],
  79              (string) filter::JOINTYPE_ANY,
  80              null,
  81              null,
  82              null,
  83              null,
  84              [],
  85              null
  86          );
  87      }
  88  
  89      /**
  90       * Test execute invalid handler.
  91       */
  92      public function test_execute_invalid_handler(): void {
  93          $this->resetAfterTest();
  94  
  95          $this->expectException('UnexpectedValueException');
  96          $handler = "\\core_user\\table\\users_participants_table";
  97          $this->expectExceptionMessage("Table handler class {$handler} not found. Please make sure that your table handler class is under the \\core_user\\table namespace.");
  98  
  99          // Tests that invalid users_participants_table class gets an exception.
 100          get::execute(
 101              "core_user",
 102              "users_participants_table",
 103              "",
 104              $this->get_sort_array(['email' => SORT_ASC]),
 105              [],
 106              (string) filter::JOINTYPE_ANY,
 107              null,
 108              null,
 109              null,
 110              null,
 111              [],
 112              null
 113  
 114          );
 115      }
 116  
 117      /**
 118       * Test execute invalid filter.
 119       */
 120      public function test_execute_invalid_filter(): void {
 121          $this->resetAfterTest();
 122  
 123          $course = $this->getDataGenerator()->create_course();
 124  
 125          // Filter with an invalid name.
 126          $filter = [
 127              [
 128                  'fullname' => 'courseid',
 129                  'jointype' => filter::JOINTYPE_ANY,
 130                  'values' => [(int) $course->id]
 131              ]
 132          ];
 133          $this->expectException(\invalid_parameter_exception::class);
 134          $this->expectExceptionMessage("Invalid parameter value detected (filters => Invalid parameter value detected " .
 135          "(Missing required key in single structure: name): Missing required key in single structure: name");
 136  
 137          get::execute(
 138              "core_user",
 139              "participants", "user-index-participants-{$course->id}",
 140              $this->get_sort_array(['firstname' => SORT_ASC]),
 141              $filter,
 142              (string) filter::JOINTYPE_ANY
 143          );
 144      }
 145  
 146      /**
 147       * Test execute method.
 148       */
 149      public function test_table_get_execute(): void {
 150          $this->resetAfterTest();
 151  
 152          $course = $this->getDataGenerator()->create_course();
 153          $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['email' => 's1@moodle.com']);
 154          $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['email' => 's2@moodle.com']);
 155          $user3 = $this->getDataGenerator()->create_user(['email' => 's3@moodle.com']);
 156          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher', ['email' => 't1@moodle.com']);
 157  
 158          $this->setUser($teacher);
 159  
 160          $this->get_sort_array(['email' => SORT_ASC]);
 161  
 162          $filter = [
 163              [
 164                  'name' => 'courseid',
 165                  'jointype' => filter::JOINTYPE_ANY,
 166                  'values' => [(int) $course->id]
 167              ]
 168          ];
 169  
 170          $participantstable = get::execute(
 171              "core_user",
 172              "participants",
 173              "user-index-participants-{$course->id}",
 174              $this->get_sort_array(['firstname' => SORT_ASC]),
 175              $filter,
 176              (string) filter::JOINTYPE_ANY,
 177              null,
 178              null,
 179              null,
 180              null,
 181              [],
 182              null
 183          );
 184          $html = $participantstable['html'];
 185  
 186          $this->assertStringContainsString($user1->email, $html);
 187          $this->assertStringContainsString($user2->email, $html);
 188          $this->assertStringContainsString($teacher->email, $html);
 189          $this->assertStringNotContainsString($user3->email, $html);
 190      }
 191  
 192  
 193      /**
 194       * Convert a traditional sort order into a sortorder for the web service.
 195       *
 196       * @param array $sortdata
 197       * @return array
 198       */
 199      protected function get_sort_array(array $sortdata): array {
 200          $newsortorder = [];
 201          foreach ($sortdata as $sortby => $sortorder) {
 202              $newsortorder[] = [
 203                  'sortby' => $sortby,
 204                  'sortorder' => $sortorder,
 205              ];
 206          }
 207  
 208          return $newsortorder;
 209      }
 210  }