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.

Differences Between: [Versions 310 and 401] [Versions 39 and 401]

   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  namespace mod_forum;
  18  
  19  use mod_forum\local\entities\sorter as sorter_entity;
  20  
  21  /**
  22   * The discussion entity tests.
  23   *
  24   * @package    mod_forum
  25   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  class entities_sorter_test extends \advanced_testcase {
  29      /**
  30       * Test the entity returns expected values.
  31       */
  32      public function test_entity_sort_into_children() {
  33          $this->resetAfterTest();
  34          $sorter = new sorter_entity(
  35              function($entity) {
  36                  return $entity['id'];
  37              },
  38              function($entity) {
  39                  return $entity['parent'];
  40              }
  41          );
  42  
  43          $a = ['id' => 1, 'parent' => 0];
  44          $b = ['id' => 2, 'parent' => 1];
  45          $c = ['id' => 3, 'parent' => 1];
  46          $d = ['id' => 4, 'parent' => 2];
  47          $e = ['id' => 5, 'parent' => 0];
  48  
  49          $expected = [
  50              [$e, []],
  51              [$a, [[$b, [[$d, []]]], [$c, []]]],
  52          ];
  53  
  54          $actual = $sorter->sort_into_children([$d, $b, $e, $a, $c]);
  55  
  56          $this->assertEquals($expected, $actual);
  57      }
  58  
  59      /**
  60       * Test the entity returns expected values.
  61       */
  62      public function test_entity_flatten_children() {
  63          $this->resetAfterTest();
  64          $sorter = new sorter_entity(
  65              function($entity) {
  66                  return $entity['id'];
  67              },
  68              function($entity) {
  69                  return $entity['parent'];
  70              }
  71          );
  72  
  73          $a = ['id' => 1, 'parent' => 0];
  74          $b = ['id' => 2, 'parent' => 1];
  75          $c = ['id' => 3, 'parent' => 1];
  76          $d = ['id' => 4, 'parent' => 3];
  77  
  78          $sorted = [
  79              [$a, [[$b, [[$d, []]]], [$c, []]]]
  80          ];
  81  
  82          $expected = [$a, $b, $d, $c];
  83          $actual = $sorter->flatten_children($sorted);
  84  
  85          $this->assertEquals($expected, $actual);
  86      }
  87  }