Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 400 and 402] [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 entity name
 119       */
 120      public function test_set_entity_name(): void {
 121          $entity = new base_test_entity();
 122  
 123          $this->assertEquals('base_test_entity', $entity->get_entity_name());
 124  
 125          $entity->set_entity_name('newentityname');
 126          $this->assertEquals('newentityname', $entity->get_entity_name());
 127      }
 128  
 129      /**
 130       * Test invalid entity name
 131       */
 132      public function test_set_entity_name_invalid(): void {
 133          $entity = new base_test_entity();
 134  
 135          $this->expectException(coding_exception::class);
 136          $this->expectExceptionMessage('Entity name must be comprised of alphanumeric character, underscore or dash');
 137          $entity->set_entity_name('');
 138      }
 139  
 140      /**
 141       * Test entity title
 142       */
 143      public function test_set_entity_title(): void {
 144          $entity = new base_test_entity();
 145  
 146          $this->assertEquals(new lang_string('yes'), $entity->get_entity_title());
 147  
 148          $newtitle = new lang_string('fullname');
 149          $entity->set_entity_title($newtitle);
 150          $this->assertEquals($newtitle, $entity->get_entity_title());
 151      }
 152  
 153      /**
 154       * Test adding single join
 155       */
 156      public function test_add_join(): void {
 157          $entity = new base_test_entity();
 158  
 159          $tablejoin = "JOIN {course} c2 ON c2.id = c1.id";
 160          $entity->add_join($tablejoin);
 161  
 162          $this->assertEquals([$tablejoin], $entity->get_joins());
 163      }
 164  
 165      /**
 166       * Test adding multiple joins
 167       */
 168      public function test_add_joins(): void {
 169          $entity = new base_test_entity();
 170  
 171          $tablejoins = [
 172              "JOIN {course} c2 ON c2.id = c1.id",
 173              "JOIN {course} c3 ON c3.id = c1.id",
 174          ];
 175          $entity->add_joins($tablejoins);
 176  
 177          $this->assertEquals($tablejoins, $entity->get_joins());
 178      }
 179  
 180      /**
 181       * Test adding duplicate joins
 182       */
 183      public function test_add_duplicate_joins(): void {
 184          $entity = new base_test_entity();
 185  
 186          $tablejoins = [
 187              "JOIN {course} c2 ON c2.id = c1.id",
 188              "JOIN {course} c3 ON c3.id = c1.id",
 189          ];
 190          $entity
 191              ->add_joins($tablejoins)
 192              ->add_joins($tablejoins);
 193  
 194          $this->assertEquals($tablejoins, $entity->get_joins());
 195      }
 196  
 197      /**
 198       * Test getting column
 199       */
 200      public function test_get_column(): void {
 201          $entity = (new base_test_entity())->initialise();
 202  
 203          $column = $entity->get_column('test');
 204          $this->assertEquals('base_test_entity:test', $column->get_unique_identifier());
 205      }
 206  
 207      /**
 208       * Test for invalid get column
 209       */
 210      public function test_get_column_invalid(): void {
 211          $entity = (new base_test_entity())->initialise();
 212  
 213          $this->expectException(coding_exception::class);
 214          $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
 215              'Invalid column name (nonexistingcolumn)');
 216          $entity->get_column('nonexistingcolumn');
 217      }
 218  
 219      /**
 220       * Test getting columns
 221       */
 222      public function test_get_columns(): void {
 223          $entity = (new base_test_entity())->initialise();
 224  
 225          $columns = $entity->get_columns();
 226          $this->assertCount(1, $columns);
 227          $this->assertContainsOnlyInstancesOf(column::class, $columns);
 228      }
 229  
 230      /**
 231       * Test getting filter
 232       */
 233      public function test_get_filter(): void {
 234          $entity = (new base_test_entity())->initialise();
 235  
 236          $filter = $entity->get_filter('test');
 237          $this->assertEquals('base_test_entity:test', $filter->get_unique_identifier());
 238      }
 239  
 240      /**
 241       * Test for invalid get filter
 242       */
 243      public function test_get_filter_invalid(): void {
 244          $entity = (new base_test_entity())->initialise();
 245  
 246          $this->expectException(coding_exception::class);
 247          $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
 248              'Invalid filter name (nonexistingfilter)');
 249          $entity->get_filter('nonexistingfilter');
 250      }
 251  
 252      /**
 253       * Test getting filters
 254       */
 255      public function test_get_filters(): void {
 256          $entity = (new base_test_entity())->initialise();
 257  
 258          $filters = $entity->get_filters();
 259          $this->assertCount(1, $filters);
 260          $this->assertContainsOnlyInstancesOf(filter::class, $filters);
 261      }
 262  
 263      /**
 264       * Test getting condition
 265       */
 266      public function test_get_condition(): void {
 267          $entity = (new base_test_entity())->initialise();
 268  
 269          $condition = $entity->get_condition('test');
 270          $this->assertEquals('base_test_entity:test', $condition->get_unique_identifier());
 271      }
 272  
 273      /**
 274       * Test for invalid get condition
 275       */
 276      public function test_get_condition_invalid(): void {
 277          $entity = (new base_test_entity())->initialise();
 278  
 279          $this->expectException(coding_exception::class);
 280          $this->expectExceptionMessage('Coding error detected, it must be fixed by a programmer: ' .
 281              'Invalid condition name (nonexistingcondition)');
 282          $entity->get_condition('nonexistingcondition');
 283      }
 284  
 285      /**
 286       * Test getting conditions
 287       */
 288      public function test_get_conditions(): void {
 289          $entity = (new base_test_entity())->initialise();
 290  
 291          $conditions = $entity->get_conditions();
 292          $this->assertCount(1, $conditions);
 293          $this->assertContainsOnlyInstancesOf(filter::class, $conditions);
 294      }
 295  }
 296  
 297  /**
 298   * Simple implementation of the base entity
 299   */
 300  class base_test_entity extends base {
 301  
 302      /**
 303       * Table aliases
 304       *
 305       * @return array
 306       */
 307      protected function get_default_table_aliases(): array {
 308          return [
 309              'mytable' => 'm',
 310              'myothertable' => 'o',
 311          ];
 312      }
 313  
 314      /**
 315       * Entity title
 316       *
 317       * @return lang_string
 318       */
 319      protected function get_default_entity_title(): lang_string {
 320          return new lang_string('yes');
 321      }
 322  
 323      /**
 324       * Initialise entity
 325       *
 326       * @return base
 327       */
 328      public function initialise(): base {
 329          $column = (new column(
 330              'test',
 331              new lang_string('no'),
 332              $this->get_entity_name()
 333          ))
 334              ->add_field('no');
 335  
 336          $filter = (new filter(
 337              text::class,
 338              'test',
 339              new lang_string('no'),
 340              $this->get_entity_name(),
 341          ))
 342              ->set_field_sql('no');
 343  
 344          return $this
 345              ->add_column($column)
 346              ->add_filter($filter)
 347              ->add_condition($filter);
 348      }
 349  }