Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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   * This file contains unit test related to xAPI library.
  19   *
  20   * @package    core_xapi
  21   * @copyright  2020 Ferran Recio
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_xapi\local\statement;
  26  
  27  use advanced_testcase;
  28  use core_xapi\iri;
  29  use core_xapi\xapi_exception;
  30  
  31  /**
  32   * Contains test cases for testing statement attachment class.
  33   *
  34   * @package    core_xapi
  35   * @since      Moodle 3.9
  36   * @copyright  2020 Ferran Recio
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class item_attachment_test extends advanced_testcase {
  40  
  41      /**
  42       * Test item creation.
  43       */
  44      public function test_create() {
  45  
  46          $data = $this->get_generic_data();
  47          $item = item_attachment::create_from_data($data);
  48  
  49          $this->assertEquals(json_encode($item), json_encode($data));
  50      }
  51  
  52      /**
  53       * return a generic data to create a valid item.
  54       *
  55       * @return sdtClass the creation data
  56       */
  57      private function get_generic_data(): \stdClass {
  58          return (object) [
  59              'usageType' => iri::generate('example', 'attachment'),
  60              'display' => (object) [
  61                  'en-US' => 'Example',
  62              ],
  63              'description' => (object) [
  64                  'en-US' => 'Description example',
  65              ],
  66              "contentType" => "image/jpg",
  67              "length" => 1234,
  68              "sha2" => "b94c0f1cffb77475c6f1899111a0181efe1d6177"
  69          ];
  70      }
  71  
  72      /**
  73       * Test for invalid values.
  74       *
  75       * @dataProvider invalid_values_data
  76       * @param string $attr attribute to modify
  77       * @param mixed $newvalue new value (null means unset)
  78       */
  79      public function test_invalid_values(string $attr, $newvalue): void {
  80  
  81          $data = $this->get_generic_data();
  82          if ($newvalue === null) {
  83              unset($data->$attr);
  84          } else {
  85              $data->$attr = $newvalue;
  86          }
  87  
  88          $this->expectException(xapi_exception::class);
  89          $item = item_attachment::create_from_data($data);
  90      }
  91  
  92      /**
  93       * Data provider for the test_invalid_values tests.
  94       *
  95       * @return  array
  96       */
  97      public function invalid_values_data() : array {
  98          return [
  99              'No usageType attachment' => [
 100                  'usageType', null
 101              ],
 102              'Invalid usageType attachment' => [
 103                  'usageType', 'Invalid IRI'
 104              ],
 105              'No display attachment' => [
 106                  'display', null
 107              ],
 108              'No contentType attachment' => [
 109                  'contentType', null
 110              ],
 111              'No length attachment' => [
 112                  'length', null
 113              ],
 114              'Invalid length attachment' => [
 115                  'length', 'Invalid'
 116              ],
 117              'No sha2 attachment' => [
 118                  'sha2', null
 119              ],
 120          ];
 121      }
 122  }