Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * tool_brickfield check 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
 
1441 ariadna 27
use tool_brickfield\local\htmlchecker\brickfield_accessibility;
28
 
1 efrain 29
defined('MOODLE_INTERNAL') || die();
30
 
31
require_once('all_checks.php');
32
 
33
/**
34
 * Class img_alt_is_too_long_testcase
1441 ariadna 35
 *
36
 * @covers \tool_brickfield\local\htmlchecker\common\checks\img_alt_is_too_long
1 efrain 37
 */
1441 ariadna 38
final class img_alt_is_too_long_test extends all_checks {
1 efrain 39
    /** @var string Check type */
40
    protected $checktype = 'img_alt_is_too_long';
41
 
1441 ariadna 42
    /**
43
     * Get test HTML with an image tag.
44
     *
45
     * @param string $alttext
46
     * @return string
47
     */
48
    protected function get_test_html(string $alttext): string {
49
        return <<<EOD
50
<!DOCTYPE HTML>
51
<html lang="en">
1 efrain 52
    <head>
53
        <title>Image alt attributes must not be too long</title>
54
    </head>
55
    <body>
1441 ariadna 56
    <img src="rex.jpg" alt="$alttext">
1 efrain 57
    </body>
58
</html>
59
EOD;
1441 ariadna 60
    }
1 efrain 61
 
62
    /**
1441 ariadna 63
     * Image alt text data provider.
64
     *
65
     * @return array
66
     */
67
    public static function img_alt_text_provider(): array {
68
        return [
69
            'Alt text <= 750 characters' => [
70
                true,
71
                str_repeat("Hello world!", 60),
72
            ],
73
            'Alt text > 750 characters' => [
74
                false,
75
                str_repeat("Hello world!", 65),
76
            ],
77
            'Multi-byte alt text <= 750 characters' => [
78
                true,
79
                str_repeat('こんにちは、世界!', 83),
80
            ],
81
            'Multi-byte alt text > 750 characters' => [
82
                false,
83
                str_repeat('こんにちは、世界!', 90),
84
            ],
85
        ];
86
    }
87
 
88
    /**
1 efrain 89
     * Test for image alt attributes being too long
1441 ariadna 90
     *
91
     * @dataProvider img_alt_text_provider
92
     * @param bool $expectedpass Whether the test is expected to pass or fail.
93
     * @param string $alttext The alt text to test.
1 efrain 94
     */
1441 ariadna 95
    public function test_check(bool $expectedpass, string $alttext): void {
96
        $html = $this->get_test_html($alttext);
97
        $results = $this->get_checker_results($html);
98
        if ($expectedpass) {
99
            $this->assertEmpty($results);
100
        } else {
101
            $this->assertEquals('img', $results[0]->element->tagName);
102
        }
103
    }
1 efrain 104
 
1441 ariadna 105
    /**
106
     * Test the severity of the {@see img_alt_is_too_long} check.
107
     *
108
     * @return void
109
     */
110
    public function test_severity(): void {
111
        $html = $this->get_test_html('Some alt text');
112
        $checker = new brickfield_accessibility($html, 'brickfield', 'string');
113
        $this->assertEquals(brickfield_accessibility::BA_TEST_SUGGESTION, $checker->get_test_severity(img_alt_is_too_long::class));
1 efrain 114
    }
115
}