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
namespace filter_mathjaxloader;
18
 
1441 ariadna 19
use core\context\system as context_system;
1 efrain 20
 
21
/**
22
 * Unit tests for the MathJax loader filter.
23
 *
24
 * @package   filter_mathjaxloader
25
 * @category  test
26
 * @copyright 2018 Markku Riekkinen
27
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 28
 * @covers \filter_mathjaxloader\text_filter
1 efrain 29
 */
1441 ariadna 30
final class filtermath_test extends \advanced_testcase {
1 efrain 31
    /**
1441 ariadna 32
     * Test the functionality of {@see text_filter::filter()}.
1 efrain 33
     *
34
     * @param string $inputtext The text given by the user.
35
     * @param string $expected The expected output after filtering.
36
     * @dataProvider math_filtering_inputs
37
     */
11 efrain 38
    public function test_math_filtering($inputtext, $expected): void {
1441 ariadna 39
        $filter = new text_filter(context_system::instance(), []);
1 efrain 40
        $this->assertEquals($expected, $filter->filter($inputtext));
41
    }
42
 
43
    /**
44
     * Data provider for {@link self::test_math_filtering()}.
45
     *
46
     * @return array of [inputtext, expectedoutput] tuples.
47
     */
1441 ariadna 48
    public static function math_filtering_inputs(): array {
49
        // phpcs:disable moodle.Files.LineLength.TooLong
1 efrain 50
        return [
51
            // One inline formula.
1441 ariadna 52
            [
53
                'Some inline math \\( y = x^2 \\).',
54
                '<span class="filter_mathjaxloader_equation">Some inline math <span class="nolink">\\( y = x^2 \\)</span>.</span>',
55
            ],
1 efrain 56
 
57
            // One inline and one display.
1441 ariadna 58
            [
59
                'Some inline math \\( y = x^2 \\) and display formula \\[ S = \\sum_{n=1}^{\\infty} 2^n \\]',
60
                '<span class="filter_mathjaxloader_equation">Some inline math <span class="nolink">\\( y = x^2 \\)</span> and '
61
                    . 'display formula <span class="nolink">\\[ S = \\sum_{n=1}^{\\infty} 2^n \\]</span></span>',
62
            ],
1 efrain 63
 
64
            // One display and one inline.
1441 ariadna 65
            [
66
                'Display formula \\[ S = \\sum_{n=1}^{\\infty} 2^n \\] and some inline math \\( y = x^2 \\).',
67
                '<span class="filter_mathjaxloader_equation">Display formula <span class="nolink">\\[ S = \\sum_{n=1}^{\\infty} 2^n \\]</span> and '
68
                    . 'some inline math <span class="nolink">\\( y = x^2 \\)</span>.</span>',
69
            ],
1 efrain 70
 
71
            // One inline and one display (with dollars).
1441 ariadna 72
            [
73
                'Some inline math \\( y = x^2 \\) and display formula $$ S = \\sum_{n=1}^{\\infty} 2^n $$',
74
                '<span class="filter_mathjaxloader_equation">Some inline math <span class="nolink">\\( y = x^2 \\)</span> and '
75
                    . 'display formula <span class="nolink">$$ S = \\sum_{n=1}^{\\infty} 2^n $$</span></span>',
76
            ],
1 efrain 77
 
78
            // One display (with dollars) and one inline.
1441 ariadna 79
            [
80
                'Display formula $$ S = \\sum_{n=1}^{\\infty} 2^n $$ and some inline math \\( y = x^2 \\).',
81
                '<span class="filter_mathjaxloader_equation">Display formula <span class="nolink">$$ S = \\sum_{n=1}^{\\infty} 2^n $$</span> and '
82
                    . 'some inline math <span class="nolink">\\( y = x^2 \\)</span>.</span>',
83
            ],
1 efrain 84
 
85
            // Inline math environment nested inside display environment (using a custom LaTex macro).
1441 ariadna 86
            [
87
                '\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\] '
88
                    . 'Text with inline formula using the custom LaTex macro \\( a = \\NullF \\).',
89
                '<span class="filter_mathjaxloader_equation"><span class="nolink">'
90
                    . '\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\]</span> '
91
                    . 'Text with inline formula using the custom LaTex macro <span class="nolink">\\( a = \\NullF \\)</span>.</span>',
92
            ],
1 efrain 93
 
94
            // Nested environments and some more content.
1441 ariadna 95
            [
96
                '\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\] '
97
                    . 'Text with inline formula using the custom LaTex macro \\( a = \\NullF \\). Finally, a display formula '
98
                    . '$$ b = \\NullF $$',
99
                '<span class="filter_mathjaxloader_equation"><span class="nolink">'
100
                    . '\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\]</span> '
101
                    . 'Text with inline formula using the custom LaTex macro <span class="nolink">\\( a = \\NullF \\)</span>. '
102
                    . 'Finally, a display formula <span class="nolink">$$ b = \\NullF $$</span></span>',
103
            ],
1 efrain 104
 
105
            // Broken math: the delimiters ($$) are not closed.
1441 ariadna 106
            [
107
                'Writing text and starting display math. $$ k = i^3 \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} '
108
                    . 'More text and inline math \\( x = \\NullF \\).',
109
                'Writing text and starting display math. $$ k = i^3 \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} '
110
                    . 'More text and inline math \\( x = \\NullF \\).',
111
            ],
1 efrain 112
        ];
113
    }
114
}