Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 402] [Versions 400 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      /**
  44       * Setup before class.
  45       */
  46      public static function setUpBeforeClass(): void {
  47          global $CFG;
  48          require_once("{$CFG->libdir}/externallib.php");
  49      }
  50  
  51      /**
  52       * Test execute invalid component format.
  53       */
  54      public function test_execute_invalid_component_format(): void {
  55          $this->resetAfterTest();
  56  
  57          $this->expectException(\invalid_parameter_exception::class);
  58          get::execute(
  59              "core-user",
  60              "participants",
  61              "",
  62              $this->get_sort_array(['email' => SORT_ASC]),
  63              [],
  64              (string) filter::JOINTYPE_ANY,
  65              null,
  66              null,
  67              null,
  68              null,
  69              [],
  70              null
  71  
  72          );
  73      }
  74  
  75      /**
  76       * Test execute invalid component.
  77       */
  78      public function test_execute_invalid_component(): void {
  79          $this->resetAfterTest();
  80  
  81          $this->expectException(\UnexpectedValueException::class);
  82          get::execute(
  83              "core_users",
  84              "participants",
  85              "",
  86              $this->get_sort_array(['email' => SORT_ASC]),
  87              [],
  88              (string) filter::JOINTYPE_ANY,
  89              null,
  90              null,
  91              null,
  92              null,
  93              [],
  94              null
  95          );
  96      }
  97  
  98      /**
  99       * Test execute invalid handler.
 100       */
 101      public function test_execute_invalid_handler(): void {
 102          $this->resetAfterTest();
 103  
 104          $this->expectException('UnexpectedValueException');
 105          $handler = "\\core_user\\table\\users_participants_table";
 106          $this->expectExceptionMessage("Table handler class {$handler} not found. Please make sure that your table handler class is under the \\core_user\\table namespace.");
 107  
 108          // Tests that invalid users_participants_table class gets an exception.
 109          get::execute(
 110              "core_user",
 111              "users_participants_table",
 112              "",
 113              $this->get_sort_array(['email' => SORT_ASC]),
 114              [],
 115              (string) filter::JOINTYPE_ANY,
 116              null,
 117              null,
 118              null,
 119              null,
 120              [],
 121              null
 122  
 123          );
 124      }
 125  
 126      /**
 127       * Test execute invalid filter.
 128       */
 129      public function test_execute_invalid_filter(): void {
 130          $this->resetAfterTest();
 131  
 132          $course = $this->getDataGenerator()->create_course();
 133  
 134          // Filter with an invalid name.
 135          $filter = [
 136              [
 137                  'fullname' => 'courseid',
 138                  'jointype' => filter::JOINTYPE_ANY,
 139                  'values' => [(int) $course->id]
 140              ]
 141          ];
 142          $this->expectException(\invalid_parameter_exception::class);
 143          $this->expectExceptionMessage("Invalid parameter value detected (filters => Invalid parameter value detected " .
 144          "(Missing required key in single structure: name): Missing required key in single structure: name");
 145  
 146          get::execute(
 147              "core_user",
 148              "participants", "user-index-participants-{$course->id}",
 149              $this->get_sort_array(['firstname' => SORT_ASC]),
 150              $filter,
 151              (string) filter::JOINTYPE_ANY
 152          );
 153      }
 154  
 155      /**
 156       * Test execute method.
 157       */
 158      public function test_table_get_execute(): void {
 159          $this->resetAfterTest();
 160  
 161          $course = $this->getDataGenerator()->create_course();
 162          $user1 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['email' => 's1@moodle.com']);
 163          $user2 = $this->getDataGenerator()->create_and_enrol($course, 'student', ['email' => 's2@moodle.com']);
 164          $user3 = $this->getDataGenerator()->create_user(['email' => 's3@moodle.com']);
 165          $teacher = $this->getDataGenerator()->create_and_enrol($course, 'teacher', ['email' => 't1@moodle.com']);
 166  
 167          $this->setUser($teacher);
 168  
 169          $this->get_sort_array(['email' => SORT_ASC]);
 170  
 171          $filter = [
 172              [
 173                  'name' => 'courseid',
 174                  'jointype' => filter::JOINTYPE_ANY,
 175                  'values' => [(int) $course->id]
 176              ]
 177          ];
 178  
 179          $participantstable = get::execute(
 180              "core_user",
 181              "participants",
 182              "user-index-participants-{$course->id}",
 183              $this->get_sort_array(['firstname' => SORT_ASC]),
 184              $filter,
 185              (string) filter::JOINTYPE_ANY,
 186              null,
 187              null,
 188              null,
 189              null,
 190              [],
 191              null
 192          );
 193          $html = $participantstable['html'];
 194  
 195          $this->assertStringContainsString($user1->email, $html);
 196          $this->assertStringContainsString($user2->email, $html);
 197          $this->assertStringContainsString($teacher->email, $html);
 198          $this->assertStringNotContainsString($user3->email, $html);
 199      }
 200  
 201  
 202      /**
 203       * Convert a traditional sort order into a sortorder for the web service.
 204       *
 205       * @param array $sortdata
 206       * @return array
 207       */
 208      protected function get_sort_array(array $sortdata): array {
 209          $newsortorder = [];
 210          foreach ($sortdata as $sortby => $sortorder) {
 211              $newsortorder[] = [
 212                  'sortby' => $sortby,
 213                  'sortorder' => $sortorder,
 214              ];
 215          }
 216  
 217          return $newsortorder;
 218      }
 219  }