diff --git a/filter/mathjaxloader/tests/filtermath_test.php b/filter/mathjaxloader/tests/filtermath_test.php new file mode 100644 index 00000000000..31bd1ee0677 --- /dev/null +++ b/filter/mathjaxloader/tests/filtermath_test.php @@ -0,0 +1,102 @@ +. +/** + * Provides the {@link filter_mathjaxloader_filtermath_testcase} class. + * + * @package filter_mathjaxloader + * @category test + * @copyright 2018 Markku Riekkinen + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->dirroot.'/filter/mathjaxloader/filter.php'); +/** + * Unit tests for the MathJax loader filter. + * + * @copyright 2018 Markku Riekkinen + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class filter_mathjaxloader_filtermath_testcase extends advanced_testcase { + + /** + * Test the functionality of {@link filter_mathjaxloader::filter()}. + * + * @param string $inputtext The text given by the user. + * @param string $expected The expected output after filtering. + * + * @dataProvider test_math_filtering_inputs + */ + public function test_math_filtering($inputtext, $expected) { + $filter = new filter_mathjaxloader(context_system::instance(), []); + $this->assertEquals($expected, $filter->filter($inputtext)); + } + + /** + * Data provider for {@link self::test_math_filtering()}. + * + * @return array of [inputtext, expectedoutput] tuples. + */ + public function test_math_filtering_inputs() { + return [ + // One inline formula. + ['Some inline math \\( y = x^2 \\).', + 'Some inline math \\( y = x^2 \\).'], + + // One inline and one display. + ['Some inline math \\( y = x^2 \\) and display formula \\[ S = \\sum_{n=1}^{\\infty} 2^n \\]', + 'Some inline math \\( y = x^2 \\) and ' + . 'display formula \\[ S = \\sum_{n=1}^{\\infty} 2^n \\]'], + + // One display and one inline. + ['Display formula \\[ S = \\sum_{n=1}^{\\infty} 2^n \\] and some inline math \\( y = x^2 \\).', + 'Display formula \\[ S = \\sum_{n=1}^{\\infty} 2^n \\] and ' + . 'some inline math \\( y = x^2 \\).'], + + // One inline and one display (with dollars). + ['Some inline math \\( y = x^2 \\) and display formula $$ S = \\sum_{n=1}^{\\infty} 2^n $$', + 'Some inline math \\( y = x^2 \\) and ' + . 'display formula $$ S = \\sum_{n=1}^{\\infty} 2^n $$'], + + // One display (with dollars) and one inline. + ['Display formula $$ S = \\sum_{n=1}^{\\infty} 2^n $$ and some inline math \\( y = x^2 \\).', + 'Display formula $$ S = \\sum_{n=1}^{\\infty} 2^n $$ and ' + . 'some inline math \\( y = x^2 \\).'], + + // Inline math environment nested inside display environment (using a custom LaTex macro). + ['\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\] ' + . 'Text with inline formula using the custom LaTex macro \\( a = \\NullF \\).', + '' + . '\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\] ' + . 'Text with inline formula using the custom LaTex macro \\( a = \\NullF \\).'], + + // Nested environments and some more content. + ['\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\] ' + . 'Text with inline formula using the custom LaTex macro \\( a = \\NullF \\). Finally, a display formula ' + . '$$ b = \\NullF $$', + '' + . '\\[ \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} \\] ' + . 'Text with inline formula using the custom LaTex macro \\( a = \\NullF \\). ' + . 'Finally, a display formula $$ b = \\NullF $$'], + + // Broken math: the delimiters ($$) are not closed. + ['Writing text and starting display math. $$ k = i^3 \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} ' + . 'More text and inline math \\( x = \\NullF \\).', + 'Writing text and starting display math. $$ k = i^3 \\newcommand{\\False}{\\mathsf{F}} \\newcommand{\\NullF}{\\fbox{\\(\\False\\)}} ' + . 'More text and inline math \\( x = \\NullF \\).'], + ]; + } +}