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.
   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  declare(strict_types=1);
  18  
  19  namespace core_reportbuilder\local\filters;
  20  
  21  use advanced_testcase;
  22  use lang_string;
  23  use core_reportbuilder\local\report\filter;
  24  
  25  /**
  26   * Unit tests for text report filter
  27   *
  28   * @package     core_reportbuilder
  29   * @covers      \core_reportbuilder\local\filters\base
  30   * @covers      \core_reportbuilder\local\filters\text
  31   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class text_test extends advanced_testcase {
  35  
  36      /**
  37       * Data provider for {@see test_get_sql_filter_simple}
  38       *
  39       * @return array
  40       */
  41      public function get_sql_filter_simple_provider(): array {
  42          return [
  43              [text::ANY_VALUE, null, true],
  44              [text::CONTAINS, 'looking', true],
  45              [text::CONTAINS, 'sky', false],
  46              [text::DOES_NOT_CONTAIN, 'sky', true],
  47              [text::DOES_NOT_CONTAIN, 'looking', false],
  48              [text::IS_EQUAL_TO, "Hello, is it me you're looking for?", true],
  49              [text::IS_EQUAL_TO, 'I can see it in your eyes', false],
  50              [text::IS_NOT_EQUAL_TO, "Hello, is it me you're looking for?", false],
  51              [text::IS_NOT_EQUAL_TO, 'I can see it in your eyes', true],
  52              [text::STARTS_WITH, 'Hello', true],
  53              [text::STARTS_WITH, 'sunlight', false],
  54              [text::ENDS_WITH, 'looking for?', true],
  55              [text::ENDS_WITH, 'your heart', false],
  56          ];
  57      }
  58  
  59      /**
  60       * Test getting filter SQL
  61       *
  62       * @param int $operator
  63       * @param string|null $value
  64       * @param bool $expectmatch
  65       *
  66       * @dataProvider get_sql_filter_simple_provider
  67       */
  68      public function test_get_sql_filter_simple(int $operator, ?string $value, bool $expectmatch): void {
  69          global $DB;
  70  
  71          $this->resetAfterTest();
  72  
  73          $course = $this->getDataGenerator()->create_course([
  74              'fullname' => "Hello, is it me you're looking for?",
  75          ]);
  76  
  77          $filter = new filter(
  78              text::class,
  79              'test',
  80              new lang_string('course'),
  81              'testentity',
  82              'fullname'
  83          );
  84  
  85          // Create instance of our filter, passing given operator.
  86          [$select, $params] = text::create($filter)->get_sql_filter([
  87              $filter->get_unique_identifier() . '_operator' => $operator,
  88              $filter->get_unique_identifier() . '_value' => $value,
  89          ]);
  90  
  91          $fullnames = $DB->get_fieldset_select('course', 'fullname', $select, $params);
  92          if ($expectmatch) {
  93              $this->assertContains($course->fullname, $fullnames);
  94          } else {
  95              $this->assertNotContains($course->fullname, $fullnames);
  96          }
  97      }
  98  
  99      /**
 100       * Data provider for {@see test_get_sql_filter_empty}
 101       *
 102       * @return array
 103       */
 104      public function get_sql_filter_empty_provider(): array {
 105          return [
 106              [text::IS_EMPTY, null, true],
 107              [text::IS_EMPTY, '', true],
 108              [text::IS_EMPTY, 'hola', false],
 109              [text::IS_NOT_EMPTY, null, false],
 110              [text::IS_NOT_EMPTY, '', false],
 111              [text::IS_NOT_EMPTY, 'hola', true],
 112          ];
 113      }
 114  
 115      /**
 116       * Test getting filter SQL using the {@see text::IS_EMPTY} and {@see text::IS_NOT_EMPTY} operators
 117       *
 118       * @param int $operator
 119       * @param string|null $profilefieldvalue
 120       * @param bool $expectmatch
 121       *
 122       * @dataProvider get_sql_filter_empty_provider
 123       */
 124      public function test_get_sql_filter_empty(int $operator, ?string $profilefieldvalue, bool $expectmatch): void {
 125          global $DB;
 126  
 127          $this->resetAfterTest();
 128  
 129          // We are using the user.moodlenetprofile field because it is nullable.
 130          $user = $this->getDataGenerator()->create_user([
 131              'moodlenetprofile' => $profilefieldvalue,
 132          ]);
 133  
 134          $filter = new filter(
 135              text::class,
 136              'test',
 137              new lang_string('user'),
 138              'testentity',
 139              'moodlenetprofile'
 140          );
 141  
 142          // Create instance of our filter, passing given operator.
 143          [$select, $params] = text::create($filter)->get_sql_filter([
 144              $filter->get_unique_identifier() . '_operator' => $operator,
 145          ]);
 146  
 147          $usernames = $DB->get_fieldset_select('user', 'username', $select, $params);
 148          if ($expectmatch) {
 149              $this->assertContains($user->username, $usernames);
 150          } else {
 151              $this->assertNotContains($user->username, $usernames);
 152          }
 153      }
 154  }