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.
   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 tool_brickfield;
  18  
  19  use tool_brickfield\local\tool\filter;
  20  use tool_brickfield\local\tool\tool;
  21  
  22  /**
  23   * Unit tests for {@tool tool_brickfield\local\tool\tool}.
  24   *
  25   * @package   tool_brickfield
  26   * @copyright  2020 onward: Brickfield Education Labs, www.brickfield.ie
  27   * @author     Jay Churchward (jay.churchward@poetopensource.org)
  28   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  29   */
  30  class tool_test extends \advanced_testcase {
  31  
  32      /** @var string base 64 image */
  33      protected $base64img = <<<EOF
  34  <img src="data:image/gif;base64,R0lGODlhPQBEAPeoAJosM//AwO/AwHVYZ/z595kzAP/s7P+goOXMv8+fhw/v739/f+8PD98fH/
  35  8mJl+fn/9ZWb8/PzWlwv///6wWGbImAPgTEMImIN9gUFCEm/gDALULDN8PAD6atYdCTX9gUNKlj8wZAKUsAOzZz+UMAOsJAP/Z2ccMDA8PD/95eX5N
  36  WvsJCOVNQPtfX/8zM8+QePLl38MGBr8JCP+zs9myn/8GBqwpAP/GxgwJCPny78l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qNWtFiF+/gA95Gly2CJLDhwEHMOUAAuOpLYDEgBxZ4GRTlC1fDnpkM+fOqD6DDj1aZpITp0dt
  46  GCDhr+fVuCu3zlg49ijaokTZTo27uG7Gjn2P+hI8+PDPERoUB318bWbfAJ5sUNFcuGRTYUqV/3ogfXp1rWlMc6awJjiAAd2fm4ogXjz56aypOoIde4O
  47  E5u/F9x199dlXnnGiHZWEYbGpsAEA3QXYnHwEFliKAgswgJ8LPeiUXGwedCAKABACCN+EA1pYIIYaFlcDhytd51sGAJbo3onOpajiihlO92KHGaUXGw
  48  WjUBChjSPiWJuOO/LYIm4v1tXfE6J4gCSJEZ7YgRYUNrkji9P55sF/ogxw5ZkSqIDaZBV6aSGYq/lGZplndkckZ98xoICbTcIJGQAZcNmdmUc210hs3
  49  5nCyJ58fgmIKX5RQGOZowxaZwYA+JaoKQwswGijBV4C6SiTUmpphMspJx9unX4KaimjDv9aaXOEBteBqmuuxgEHoLX6Kqx+yXqqBANsgCtit4FWQAEk
  50  rNbpq7HSOmtwag5w57GrmlJBASEU18ADjUYb3ADTinIttsgSB1oJFfA63bduimuqKB1keqwUhoCSK374wbujvOSu4QG6UvxBRydcpKsav++Ca6G8A6P
  51  r1x2kVMyHwsVxUALDq/krnrhPSOzXG1lUTIoffqGR7Goi2MAxbv6O2kEG56I7CSlRsEFKFVyovDJoIRTg7sugNRDGqCJzJgcKE0ywc0ELm6KBCCJo8D
  52  IPFeCWNGcyqNFE06ToAfV0HBRgxsvLThHn1oddQMrXj5DyAQgjEHSAJMWZwS3HPxT/QMbabI/iBCliMLEJKX2EEkomBAUCxRi42VDADxyTYDVogV+wS
  53  ChqmKxEKCDAYFDFj4OmwbY7bDGdBhtrnTQYOigeChUmc1K3QTnAUfEgGFgAWt88hKA6aCRIXhxnQ1yg3BCayK44EWdkUQcBByEQChFXfCB776aQsG0B
  54  IlQgQgE8qO26X1h8cEUep8ngRBnOy74E9QgRgEAC8SvOfQkh7FDBDmS43PmGoIiKUUEGkMEC/PJHgxw0xH74yx/3XnaYRJgMB8obxQW6kL9QYEJ0FIF
  55  gByfIL7/IQAlvQwEpnAC7DtLNJCKUoO/w45c44GwCXiAFB/OXAATQryUxdN4LfFiwgjCNYg+kYMIEFkCKDs6PKAIJouyGWMS1FSKJOMRB/BoIxYJIUX
  56  FUxNwoIkEKPAgCBZSQHQ1A2EWDfDEUVLyADj5AChSIQW6gu10bE/JG2VnCZGfo4R4d0sdQoBAHhPjhIB94v/wRoRKQWGRHgrhGSQJxCS+0pCZbEhAAO
  57  w==" alt="This is a bus." />';
  58  EOF;
  59  
  60  
  61      public function test_build_all_accessibilitytools() {
  62          $tools = tool::build_all_accessibilitytools();
  63  
  64          $this->assertEquals($tools['errors']::toolshortname(), 'Error list');
  65          $this->assertEquals($tools['activityresults']::toolshortname(), 'Activity breakdown');
  66          $this->assertEquals($tools['checktyperesults']::toolshortname(), 'Content types');
  67          $this->assertEquals($tools['printable']::toolshortname(), 'Summary report');
  68          $this->assertEquals($tools['advanced']::toolshortname(), 'Advanced');
  69      }
  70  
  71      public function test_data_is_valid() {
  72          $object = $this->getMockForAbstractClass(tool::class);
  73          $object->set_filter(new filter());
  74          $output = $object->data_is_valid();
  75          $this->assertFalse($output);
  76      }
  77  
  78      public function test_can_access() {
  79          $this->resetAfterTest();
  80          $category = $this->getDataGenerator()->create_category();
  81          $filter = new filter(1, $category->id, 'tab', 3, 4);
  82  
  83          $tool = $this->getMockForAbstractClass(tool::class);
  84  
  85          $output = $tool->can_access($filter);
  86          $this->assertFalse($output);
  87      }
  88  
  89      public function test_get_error_message() {
  90          $tool = $this->getMockForAbstractClass(tool::class);
  91  
  92          $output = $tool->get_error_message();
  93          $this->assertEquals($output, '');
  94      }
  95  
  96      public function test_get_module_label() {
  97          $output = tool::get_module_label('core_course');
  98          $this->assertEquals($output, 'Course');
  99  
 100          $output = tool::get_module_label('mod_book');
 101          $this->assertEquals($output, 'Book');
 102      }
 103  
 104      public function test_toplevel_arguments() {
 105          $this->resetAfterTest();
 106          $category = $this->getDataGenerator()->create_category();
 107          $filter = new filter(1, $category->id, 'tab', 3, 4);
 108  
 109          $output = tool::toplevel_arguments();
 110          $this->assertEmpty($output);
 111  
 112          $output = tool::toplevel_arguments($filter);
 113          $this->assertEquals($output['courseid'], 1);
 114          $this->assertEquals($output['categoryid'], $category->id);
 115      }
 116  
 117      /**
 118       * Base64 image provider.
 119       * @return array
 120       */
 121      public function base64_img_provider(): array {
 122          $img = '<img src="myimage.jpg" />';
 123          return [
 124              'Image tag alone (base64)' => [
 125                  $this->base64img,
 126                  true,
 127              ],
 128              'Image tag alone (link)' => [
 129                  $img,
 130                  false,
 131              ],
 132              'Image tag in string (base64)' => [
 133                  "This is my image {$this->base64img}.",
 134                  true,
 135              ],
 136              'Image tag in string (link)' => [
 137                  "This is my image {$img}.",
 138                  false,
 139              ],
 140              'Image tag with string base64 in alt' => [
 141                  "<img src='myimage.jpg' alt='base64'/>",
 142                  false,
 143              ],
 144              'base64 string in text' => [
 145                  "An example base 64 format string is 'data:image/gif;base64'./>",
 146                  false,
 147              ],
 148          ];
 149      }
 150  
 151      /**
 152       * Test base 64 image provider.
 153       * @dataProvider base64_img_provider
 154       * @param string $content
 155       * @param bool $expectation
 156       */
 157      public function test_base64_img_detected(string $content, bool $expectation) {
 158          $this->assertEquals(
 159              $expectation,
 160              tool::base64_img_detected($content)
 161          );
 162      }
 163  
 164      public function test_truncate_base64() {
 165          $truncated = tool::truncate_base64($this->base64img);
 166          $this->assertStringContainsString('<img src="data:image/gif;base64..."', $truncated);
 167      }
 168  }