Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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 core_course;
  18  
  19  use core_course\external\course_summary_exporter;
  20  use context_user;
  21  use context_course;
  22  
  23  /**
  24   * Functional test for class course_summary_exporter
  25   *
  26   * @package    core
  27   * @subpackage course
  28   * @author     Dmitrii Metelkin <dmitriim@catalyst-au.net>
  29   * @copyright  2021 Catalyst IT
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class course_summary_exporter_test extends \advanced_testcase {
  33  
  34      /**
  35       * Test that if no course overview images uploaded get_course_image returns false.
  36       */
  37      public function test_get_course_image_when_no_overview_images_uploaded() {
  38          $this->resetAfterTest(true);
  39          $this->setAdminUser();
  40          $course = $this->getDataGenerator()->create_course();
  41  
  42          $this->assertFalse(course_summary_exporter::get_course_image($course));
  43      }
  44  
  45      /**
  46       * Test that if course overview images uploaded get_course_image returns an image URL.
  47       */
  48      public function test_get_course_image_when_overview_images_are_uploaded() {
  49          global $USER;
  50  
  51          $this->resetAfterTest(true);
  52          $this->setAdminUser();
  53  
  54          $draftid = file_get_unused_draft_itemid();
  55          $filerecord = [
  56              'component' => 'user',
  57              'filearea' => 'draft',
  58              'contextid' => context_user::instance($USER->id)->id,
  59              'itemid' => $draftid,
  60              'filename' => 'image.jpg',
  61              'filepath' => '/',
  62          ];
  63          $fs = get_file_storage();
  64          $fs->create_file_from_string($filerecord, file_get_contents(__DIR__ . '/fixtures/image.jpg'));
  65          $course = $this->getDataGenerator()->create_course(['overviewfiles_filemanager' => $draftid]);
  66          $coursecontext = context_course::instance($course->id);
  67  
  68          $expected = 'https://www.example.com/moodle/pluginfile.php/' . $coursecontext->id . '/course/overviewfiles/image.jpg';
  69          $actual = course_summary_exporter::get_course_image($course);
  70          $this->assertSame($expected, $actual);
  71      }
  72  
  73  }