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 400 and 403] [Versions 401 and 403] [Versions 402 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 base entity
  19   *
  20   * @package     core_reportbuilder
  21   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  declare(strict_types=1);
  26  
  27  namespace core_reportbuilder\local\entities;
  28  
  29  use advanced_testcase;
  30  use coding_exception;
  31  use lang_string;
  32  use core_reportbuilder\local\filters\text;
  33  use core_reportbuilder\local\report\column;
  34  use core_reportbuilder\local\report\filter;
  35  
  36  defined('MOODLE_INTERNAL') || die();
  37  
  38  /**
  39   * Unit tests for base entity
  40   *
  41   * @package     core_reportbuilder
  42   * @covers      \core_reportbuilder\local\entities\base
  43   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  44   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class base_test extends advanced_testcase {
  47  
  48      /**
  49       * Test entity table alias
  50       */
  51      public function test_get_table_alias(): void {
  52          $entity = new base_test_entity();
  53          $this->assertEquals('m', $entity->get_table_alias('mytable'));
  54      }
  55  
  56      /**
  57       * Test for invalid get table alias
  58       */
  59      public function test_get_table_alias_invalid(): void {
  60          $entity = new base_test_entity();
  61  
  62          $this->expectException(coding_exception::class);
  63          $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
  64              'Invalid table name (nonexistingalias)');
  65          $entity->get_table_alias('nonexistingalias');
  66      }
  67  
  68      /**
  69       * Test setting table alias
  70       */
  71      public function test_set_table_alias(): void {
  72          $entity = new base_test_entity();
  73  
  74          $entity->set_table_alias('mytable', 'newalias');
  75          $this->assertEquals('newalias', $entity->get_table_alias('mytable'));
  76      }
  77  
  78      /**
  79       * Test invalid entity set table alias
  80       */
  81      public function test_set_table_alias_invalid(): void {
  82          $entity = new base_test_entity();
  83  
  84          $this->expectException(coding_exception::class);
  85          $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: Invalid table name (nonexistent)');
  86          $entity->set_table_alias('nonexistent', 'newalias');
  87      }
  88  
  89      /**
  90       * Test setting multiple table aliases
  91       */
  92      public function test_set_table_aliases(): void {
  93          $entity = new base_test_entity();
  94  
  95          $entity->set_table_aliases([
  96              'mytable' => 'newalias',
  97              'myothertable' => 'newalias2',
  98          ]);
  99          $this->assertEquals('newalias', $entity->get_table_alias('mytable'));
 100          $this->assertEquals('newalias2', $entity->get_table_alias('myothertable'));
 101      }
 102  
 103      /**
 104       * Test setting multiple table aliases, containing an invalid table
 105       */
 106      public function test_set_table_aliases_invalid(): void {
 107          $entity = new base_test_entity();
 108  
 109          $this->expectException(coding_exception::class);
 110          $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: Invalid table name (nonexistent)');
 111          $entity->set_table_aliases([
 112              'mytable' => 'newalias',
 113              'nonexistent' => 'newalias2',
 114          ]);
 115      }
 116  
 117      /**
 118       * Test setting table join alias
 119       */
 120      public function test_set_table_join_alias(): void {
 121          $entity = new base_test_entity();
 122  
 123          $entity->set_table_join_alias('mytable', 'newalias');
 124          $this->assertTrue($entity->has_table_join_alias('mytable'));
 125          $this->assertEquals('newalias', $entity->get_table_alias('mytable'));
 126      }
 127  
 128      /**
 129       * Test that entity doesn't have table join alias by default
 130       *
 131       * {@see test_set_table_join_alias} for assertion where it does
 132       */
 133      public function test_has_table_join_alias(): void {
 134          $entity = new base_test_entity();
 135          $this->assertFalse($entity->has_table_join_alias('mytable'));
 136      }
 137  
 138      /**
 139       * Test entity name
 140       */
 141      public function test_set_entity_name(): void {
 142          $entity = new base_test_entity();
 143  
 144          $this->assertEquals('base_test_entity', $entity->get_entity_name());
 145  
 146          $entity->set_entity_name('newentityname');
 147          $this->assertEquals('newentityname', $entity->get_entity_name());
 148      }
 149  
 150      /**
 151       * Test entity title
 152       */
 153      public function test_set_entity_title(): void {
 154          $entity = new base_test_entity();
 155  
 156          $this->assertEquals(new lang_string('yes'), $entity->get_entity_title());
 157  
 158          $newtitle = new lang_string('fullname');
 159          $entity->set_entity_title($newtitle);
 160          $this->assertEquals($newtitle, $entity->get_entity_title());
 161      }
 162  
 163      /**
 164       * Test adding single join
 165       */
 166      public function test_add_join(): void {
 167          $entity = new base_test_entity();
 168  
 169          $tablejoin = "JOIN {course} c2 ON c2.id = c1.id";
 170          $entity->add_join($tablejoin);
 171  
 172          $this->assertEquals([$tablejoin], $entity->get_joins());
 173      }
 174  
 175      /**
 176       * Test adding multiple joins
 177       */
 178      public function test_add_joins(): void {
 179          $entity = new base_test_entity();
 180  
 181          $tablejoins = [
 182              "JOIN {course} c2 ON c2.id = c1.id",
 183              "JOIN {course} c3 ON c3.id = c1.id",
 184          ];
 185          $entity->add_joins($tablejoins);
 186  
 187          $this->assertEquals($tablejoins, $entity->get_joins());
 188      }
 189  
 190      /**
 191       * Test adding duplicate joins
 192       */
 193      public function test_add_duplicate_joins(): void {
 194          $entity = new base_test_entity();
 195  
 196          $tablejoins = [
 197              "JOIN {course} c2 ON c2.id = c1.id",
 198              "JOIN {course} c3 ON c3.id = c1.id",
 199          ];
 200          $entity
 201              ->add_joins($tablejoins)
 202              ->add_joins($tablejoins);
 203  
 204          $this->assertEquals($tablejoins, $entity->get_joins());
 205      }
 206  
 207      /**
 208       * Test getting column
 209       */
 210      public function test_get_column(): void {
 211          $entity = (new base_test_entity())->initialise();
 212  
 213          $column = $entity->get_column('test');
 214          $this->assertEquals('base_test_entity:test', $column->get_unique_identifier());
 215      }
 216  
 217      /**
 218       * Test for invalid get column
 219       */
 220      public function test_get_column_invalid(): void {
 221          $entity = (new base_test_entity())->initialise();
 222  
 223          $this->expectException(coding_exception::class);
 224          $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
 225              'Invalid column name (nonexistingcolumn)');
 226          $entity->get_column('nonexistingcolumn');
 227      }
 228  
 229      /**
 230       * Test getting columns
 231       */
 232      public function test_get_columns(): void {
 233          $entity = (new base_test_entity())->initialise();
 234  
 235          $columns = $entity->get_columns();
 236          $this->assertCount(1, $columns);
 237          $this->assertContainsOnlyInstancesOf(column::class, $columns);
 238      }
 239  
 240      /**
 241       * Test getting filter
 242       */
 243      public function test_get_filter(): void {
 244          $entity = (new base_test_entity())->initialise();
 245  
 246          $filter = $entity->get_filter('test');
 247          $this->assertEquals('base_test_entity:test', $filter->get_unique_identifier());
 248      }
 249  
 250      /**
 251       * Test for invalid get filter
 252       */
 253      public function test_get_filter_invalid(): void {
 254          $entity = (new base_test_entity())->initialise();
 255  
 256          $this->expectException(coding_exception::class);
 257          $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
 258              'Invalid filter name (nonexistingfilter)');
 259          $entity->get_filter('nonexistingfilter');
 260      }
 261  
 262      /**
 263       * Test getting filters
 264       */
 265      public function test_get_filters(): void {
 266          $entity = (new base_test_entity())->initialise();
 267  
 268          $filters = $entity->get_filters();
 269          $this->assertCount(1, $filters);
 270          $this->assertContainsOnlyInstancesOf(filter::class, $filters);
 271      }
 272  
 273      /**
 274       * Test getting condition
 275       */
 276      public function test_get_condition(): void {
 277          $entity = (new base_test_entity())->initialise();
 278  
 279          $condition = $entity->get_condition('test');
 280          $this->assertEquals('base_test_entity:test', $condition->get_unique_identifier());
 281      }
 282  
 283      /**
 284       * Test for invalid get condition
 285       */
 286      public function test_get_condition_invalid(): void {
 287          $entity = (new base_test_entity())->initialise();
 288  
 289          $this->expectException(coding_exception::class);
 290          $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
 291              'Invalid condition name (nonexistingcondition)');
 292          $entity->get_condition('nonexistingcondition');
 293      }
 294  
 295      /**
 296       * Test getting conditions
 297       */
 298      public function test_get_conditions(): void {
 299          $entity = (new base_test_entity())->initialise();
 300  
 301          $conditions = $entity->get_conditions();
 302          $this->assertCount(1, $conditions);
 303          $this->assertContainsOnlyInstancesOf(filter::class, $conditions);
 304      }
 305  }
 306  
 307  /**
 308   * Simple implementation of the base entity
 309   */
 310  class base_test_entity extends base {
 311  
 312      /**
 313       * Table aliases
 314       *
 315       * @return array
 316       */
 317      protected function get_default_table_aliases(): array {
 318          return [
 319              'mytable' => 'm',
 320              'myothertable' => 'o',
 321          ];
 322      }
 323  
 324      /**
 325       * Entity title
 326       *
 327       * @return lang_string
 328       */
 329      protected function get_default_entity_title(): lang_string {
 330          return new lang_string('yes');
 331      }
 332  
 333      /**
 334       * Initialise entity
 335       *
 336       * @return base
 337       */
 338      public function initialise(): base {
 339          $column = (new column(
 340              'test',
 341              new lang_string('no'),
 342              $this->get_entity_name()
 343          ))
 344              ->add_field('no');
 345  
 346          $filter = (new filter(
 347              text::class,
 348              'test',
 349              new lang_string('no'),
 350              $this->get_entity_name(),
 351          ))
 352              ->set_field_sql('no');
 353  
 354          return $this
 355              ->add_column($column)
 356              ->add_filter($filter)
 357              ->add_condition($filter);
 358      }
 359  }