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.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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   * Class test_css_text_has_contrast test
  19   *
  20   * @package    tool_brickfield
  21   * @copyright  2020 onward: Brickfield Education Labs, https://www.brickfield.ie
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_brickfield\local\htmlchecker\common\checks;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once ('all_checks.php');
  30  
  31  /**
  32   * Class test_css_text_has_contrast_test
  33   */
  34  class css_text_has_contrast_test extends all_checks {
  35      /** @var string The check type. */
  36      protected $checktype = 'css_text_has_contrast';
  37  
  38      /** @var string HTML that should get flagged. */
  39      private $htmlfail1 = <<<EOD
  40      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
  41      <html lang="en">
  42      <head>
  43      <title>OAC Testfile - Check #6 - Positive</title>
  44      </head>
  45      <body>
  46      <p style="color:#333333; background-color:#000000; font-weight: bold;">This is not contrasty enough.</p>
  47      </body>
  48      </html>
  49  EOD;
  50  
  51      /** @var string HTML that should get flagged. */
  52      private $htmlfail2 = <<<EOD
  53      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
  54      <html lang="en">
  55      <head>
  56      <title>OAC Testfile - Check #6 - Positive</title>
  57      </head>
  58      <body>
  59      <p style="color:#333333; background-color:#000000; font-size: 18px;">This is not contrasty enough.</p>
  60      </body>
  61      </html>
  62  EOD;
  63  
  64      /** @var string HTML that should get flagged. */
  65      private $htmlfail3 = <<<EOD
  66      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
  67      <html lang="en">
  68      <head>
  69      <title>OAC Testfile - Check #6 - Positive</title>
  70      </head>
  71      <body>
  72      <p style="color:#333333; background-color:#000000; font-size: 18%;">This is not contrasty enough.</p>
  73      </body>
  74      </html>
  75  EOD;
  76  
  77      /** @var string HTML that should get flagged. */
  78      private $htmlfail4 = <<<EOD
  79      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
  80      <html lang="en">
  81      <head>
  82      <title>OAC Testfile - Check #6 - Positive</title>
  83      </head>
  84      <body>
  85      <p style="color:#333333; background-color:#000000; font-size: 18em;">This is not contrasty enough.</p>
  86      </body>
  87      </html>
  88  EOD;
  89  
  90      /** @var string HTML that should get flagged. */
  91      private $htmlfail5 = <<<EOD
  92      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
  93      <html lang="en">
  94      <head>
  95      <title>OAC Testfile - Check #6 - Positive</title>
  96      </head>
  97      <body>
  98      <p style="color:#333333; background-color:#000000; font-size: 18ex;">This is not contrasty enough.</p>
  99      </body>
 100      </html>
 101  EOD;
 102  
 103      /** @var string HTML that should get flagged. */
 104      private $htmlfail6 = <<<EOD
 105      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
 106      <html lang="en">
 107      <head>
 108      <title>OAC Testfile - Check #6 - Positive</title>
 109      </head>
 110      <body>
 111      <p style="color:#333333; background-color:#000000;">This is not contrasty enough.</p>
 112      </body>
 113      </html>
 114  EOD;
 115  
 116      /** @var string HTML that should not get flagged. */
 117      private $htmlpass = <<<EOD
 118      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
 119      <html lang="en">
 120      <head>
 121      <title>OAC Testfile - Check #6 - Positive</title>
 122      </head>
 123      <body>
 124      <p style="color:#333333; background-color:#ffffff;">This is contrasty enough.</p>
 125      </body>
 126      </html>
 127  EOD;
 128  
 129      /** @var string HTML that should get flagged. */
 130      private $namecolours = <<<EOD
 131      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
 132      <html lang="en">
 133      <head>
 134      <title>OAC Testfile - Check #6 - Positive</title>
 135      </head>
 136      <body>
 137      <p style="color: red; background-color: blue;">This is not contrasty enough.</p>
 138      </body>
 139      </html>
 140  EOD;
 141  
 142      /** @var string HTML with invalid colour names. */
 143      private $invalidcolours = <<<EOD
 144      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
 145      <html lang="en">
 146      <head>
 147      <title>OAC Testfile - Check #6 - Positive</title>
 148      </head>
 149      <body>
 150      <p style="color: grog; background-color: numpi;">This is not contrasty enough.</p>
 151      </body>
 152      </html>
 153  EOD;
 154  
 155      /** @var string HTML with invalid colour numeric values. */
 156      private $invalidvalue = <<<EOD
 157      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
 158      <html lang="en">
 159      <head>
 160      <title>OAC Testfile - Check #6 - Positive</title>
 161      </head>
 162      <body>
 163      <p style="color: 10000500; background-color: -10234;">This is not contrasty enough.</p>
 164      </body>
 165      </html>
 166  EOD;
 167  
 168      /** @var string HTML with empty colour values. */
 169      private $emptyvalue = <<<EOD
 170      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN""http://www.w3.org/TR/REC-html40/loose.dtd">
 171      <html lang="en">
 172      <head>
 173      <title>OAC Testfile - Check #6 - Positive</title>
 174      </head>
 175      <body>
 176      <p style="color:; background-color:;">This is not contrasty enough.</p>
 177      </body>
 178      </html>
 179  EOD;
 180  
 181      /**
 182       * Test for the area assign intro
 183       */
 184      public function test_check() {
 185          $results = $this->get_checker_results($this->htmlfail1);
 186          $this->assertTrue($results[0]->element->tagName == 'p');
 187  
 188          $results = $this->get_checker_results($this->htmlfail2);
 189          $this->assertTrue($results[0]->element->tagName == 'p');
 190  
 191          $results = $this->get_checker_results($this->htmlfail3);
 192          $this->assertTrue($results[0]->element->tagName == 'p');
 193  
 194          $results = $this->get_checker_results($this->htmlfail4);
 195          $this->assertTrue($results[0]->element->tagName == 'p');
 196  
 197          $results = $this->get_checker_results($this->htmlfail5);
 198          $this->assertTrue($results[0]->element->tagName == 'p');
 199  
 200          $results = $this->get_checker_results($this->htmlfail6);
 201          $this->assertTrue($results[0]->element->tagName == 'p');
 202  
 203          $results = $this->get_checker_results($this->htmlpass);
 204          $this->assertEmpty($results);
 205      }
 206  
 207      /**
 208       * Test with valid colour names.
 209       */
 210      public function test_check_for_namedcolours() {
 211          $results = $this->get_checker_results($this->namecolours);
 212          $this->assertTrue($results[0]->element->tagName == 'p');
 213      }
 214  
 215      /**
 216       * Test with invalid colour names.
 217       */
 218      public function test_check_for_invalidcolours() {
 219          $results = $this->get_checker_results($this->invalidcolours);
 220          $this->assertTrue($results[0]->element->tagName == 'p');
 221      }
 222  
 223      /**
 224       * Test with invalid colour numeric values.
 225       */
 226      public function test_check_for_invalidvalues() {
 227          $results = $this->get_checker_results($this->invalidvalue);
 228          $this->assertTrue($results[0]->element->tagName == 'p');
 229      }
 230  
 231      /**
 232       * Test with empty colour values.
 233       */
 234      public function test_check_for_emptyvalues() {
 235          $results = $this->get_checker_results($this->emptyvalue);
 236          $this->assertEmpty($results);
 237      }
 238  }