| 1 | 
           efrain | 
           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 | 
            * Class defining resuable tests methods for external functions
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package   qbank_columnsortorder
  | 
        
        
            | 
            | 
           21 | 
            * @copyright 2023 Catalyst IT Europe Ltd.
  | 
        
        
            | 
            | 
           22 | 
            * @author    Mark Johnson <mark.johnson@catalyst-eu.net>
  | 
        
        
            | 
            | 
           23 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           24 | 
            */
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           namespace qbank_columnsortorder\tests;
  | 
        
        
            | 
            | 
           27 | 
              | 
        
        
            | 
            | 
           28 | 
           /**
  | 
        
        
            | 
            | 
           29 | 
            * Class defining resuable tests methods for external functions
  | 
        
        
            | 
            | 
           30 | 
            */
  | 
        
        
            | 
            | 
           31 | 
           abstract class external_function_testcase extends \advanced_testcase {
  | 
        
        
            | 
            | 
           32 | 
               /**
  | 
        
        
            | 
            | 
           33 | 
                * @var string Fully-qualified external function class to test.
  | 
        
        
            | 
            | 
           34 | 
                */
  | 
        
        
            | 
            | 
           35 | 
               protected $testclass;
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
               /**
  | 
        
        
            | 
            | 
           38 | 
                * @var string The name of the setting used to store the data.
  | 
        
        
            | 
            | 
           39 | 
                */
  | 
        
        
            | 
            | 
           40 | 
               protected $setting;
  | 
        
        
            | 
            | 
           41 | 
              | 
        
        
            | 
            | 
           42 | 
               /**
  | 
        
        
            | 
            | 
           43 | 
                * @var bool Whether the data is stored as a comma-separated list.
  | 
        
        
            | 
            | 
           44 | 
                */
  | 
        
        
            | 
            | 
           45 | 
               protected $csv = true;
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
               /**
  | 
        
        
            | 
            | 
           48 | 
                * A function that returns the data to be passed to the external function.
  | 
        
        
            | 
            | 
           49 | 
                *
  | 
        
        
            | 
            | 
           50 | 
                * The data returned will depend on the testclass.
  | 
        
        
            | 
            | 
           51 | 
                *
  | 
        
        
            | 
            | 
           52 | 
                * @return mixed
  | 
        
        
            | 
            | 
           53 | 
                */
  | 
        
        
            | 
            | 
           54 | 
               abstract protected function generate_test_data(): mixed;
  | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
               /**
  | 
        
        
            | 
            | 
           57 | 
                * Test that execute() method sets the correct config setting.
  | 
        
        
            | 
            | 
           58 | 
                */
  | 
        
        
            | 
            | 
           59 | 
               public function test_execute(): void {
  | 
        
        
            | 
            | 
           60 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           61 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   $testdata = $this->generate_test_data();
  | 
        
        
            | 
            | 
           64 | 
                   $this->testclass::execute($testdata, true);
  | 
        
        
            | 
            | 
           65 | 
              | 
        
        
            | 
            | 
           66 | 
                   $currentconfig = get_config('qbank_columnsortorder', $this->setting);
  | 
        
        
            | 
            | 
           67 | 
                   if ($this->csv) {
  | 
        
        
            | 
            | 
           68 | 
                       $currentconfig = explode(',', $currentconfig);
  | 
        
        
            | 
            | 
           69 | 
                   }
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
                   $this->assertEqualsCanonicalizing($testdata, $currentconfig);
  | 
        
        
            | 
            | 
           72 | 
               }
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
               /**
  | 
        
        
            | 
            | 
           75 | 
                * Test that execute() method sets user preference when a component is passed.
  | 
        
        
            | 
            | 
           76 | 
                */
  | 
        
        
            | 
            | 
           77 | 
               public function test_execute_user(): void {
  | 
        
        
            | 
            | 
           78 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           79 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           80 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
                   $testdata = $this->generate_test_data();
  | 
        
        
            | 
            | 
           83 | 
                   $this->testclass::execute($testdata);
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
                   $userpreference = get_user_preferences('qbank_columnsortorder_' . $this->setting);
  | 
        
        
            | 
            | 
           86 | 
                   if ($this->csv) {
  | 
        
        
            | 
            | 
           87 | 
                       $userpreference = explode(',', $userpreference);
  | 
        
        
            | 
            | 
           88 | 
                   }
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                   $this->assertEqualsCanonicalizing($testdata, $userpreference);
  | 
        
        
            | 
            | 
           91 | 
               }
  | 
        
        
            | 
            | 
           92 | 
           }
  |