Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * Tests for mod/assign/submission/onlinetext/locallib.php
  19   *
  20   * @package   assignsubmission_onlinetext
  21   * @copyright 2016 Cameron Ball
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/mod/assign/tests/generator.php');
  29  
  30  /**
  31   * Unit tests for mod/assign/submission/onlinetext/locallib.php
  32   *
  33   * @copyright  2016 Cameron Ball
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class assignsubmission_onlinetext_locallib_testcase extends advanced_testcase {
  37  
  38      // Use the generator helper.
  39      use mod_assign_test_generator;
  40  
  41      /**
  42       * Test submission_is_empty
  43       *
  44       * @dataProvider submission_is_empty_testcases
  45       * @param string $submissiontext The online text submission text
  46       * @param bool $expected The expected return value
  47       */
  48      public function test_submission_is_empty($submissiontext, $expected) {
  49          $this->resetAfterTest();
  50  
  51          $course = $this->getDataGenerator()->create_course();
  52          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  53          $assign = $this->create_instance($course, [
  54                  'assignsubmission_onlinetext_enabled' => true,
  55              ]);
  56  
  57          $this->setUser($student->id);
  58  
  59          $plugin = $assign->get_submission_plugin_by_type('onlinetext');
  60          $result = $plugin->submission_is_empty((object) [
  61                  'onlinetext_editor' => [
  62                      'text' => $submissiontext,
  63                  ],
  64              ]);
  65          $this->assertTrue($result === $expected);
  66      }
  67  
  68      /**
  69       * Test new_submission_empty
  70       *
  71       * @dataProvider submission_is_empty_testcases
  72       * @param string $submissiontext The file submission data
  73       * @param bool $expected The expected return value
  74       */
  75      public function test_new_submission_empty($submissiontext, $expected) {
  76          $this->resetAfterTest();
  77  
  78          $course = $this->getDataGenerator()->create_course();
  79          $student = $this->getDataGenerator()->create_and_enrol($course, 'student');
  80          $assign = $this->create_instance($course, [
  81                  'assignsubmission_onlinetext_enabled' => true,
  82              ]);
  83  
  84          $this->setUser($student->id);
  85  
  86          $result = $assign->new_submission_empty((object) [
  87                  'onlinetext_editor' => [
  88                      'text' => $submissiontext,
  89                  ],
  90              ]);
  91  
  92          $this->assertTrue($result === $expected);
  93      }
  94  
  95      /**
  96       * Dataprovider for the test_submission_is_empty testcase
  97       *
  98       * @return array of testcases
  99       */
 100      public function submission_is_empty_testcases() {
 101          return [
 102              'Empty submission string' => ['', true],
 103              'Empty submission null' => [null, true],
 104              'Value 0' => [0, false],
 105              'String 0' => ['0', false],
 106              'Text' => ['Ai! laurië lantar lassi súrinen, yéni únótimë ve rámar aldaron!', false],
 107              'Image' => ['<img src="test.jpg" />', false],
 108              'Video' => ['<video controls="true"><source src="test.mp4"></video>', false],
 109              'Audio' => ['<audio controls="true"><source src="test.mp3"></audio>', false],
 110          ];
 111      }
 112  }