. namespace mod_wiki; use wiki_parser_proxy; defined('MOODLE_INTERNAL') || die; global $CFG; require_once($CFG->dirroot . '/mod/wiki/parser/parser.php'); /** * Unit tests for the wiki parser * * @package mod_wiki * @category test * @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu * @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu * * @author Jordi Piguillem * @author Marc Alier * @author David Jimenez * @author Josep Arus * @author Kenneth Riba * * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class wikiparser_test extends \advanced_testcase { /** * URL inside the clickable text of some link should not be turned into a new link via the url_tag_rule. * * @dataProvider urls_inside_link_text_provider * @param string $markup Markup of the Wiki page the text is part of. * @param string $input The input text. * @param string $output The expected output HTML as a result of the parsed input text. */ public function test_urls_inside_link_text(string $markup, string $input, string $output): void { $parsingresult = wiki_parser_proxy::parse($input, $markup, [ 'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link', 'link_callback_args' => ['swid' => 1], ]); $this->assertStringContainsString($output, $parsingresult['parsed_text']); } /** * Provides data sets for {@see self::test_urls_inside_link_text()}. * * @return array */ public function urls_inside_link_text_provider() { return [ 'creole implicit link' => [ 'markup' => 'creole', 'input' => 'Visit https://site.url for more information.', 'output' => 'Visit https://site.url for more information.', ], 'creole explicit link' => [ 'markup' => 'creole', 'input' => 'Visit [[https://site.url]] for more information.', 'output' => 'Visit https://site.url for more information.', ], 'creole explicit link with text' => [ 'markup' => 'creole', 'input' => 'Visit [[https://site.url|http://www.site.url]] for more information.', 'output' => 'Visit http://www.site.url for more information.', ], 'nwiki implicit link' => [ 'markup' => 'nwiki', 'input' => 'Visit https://site.url for more information.', 'output' => 'Visit https://site.url for more information.', ], 'nwiki explicit link' => [ 'markup' => 'nwiki', 'input' => 'Visit [https://site.url] for more information.', 'output' => 'Visit https://site.url for more information.', ], 'nwiki explicit link with space separated text' => [ 'markup' => 'nwiki', 'input' => 'Visit [https://site.url http://www.site.url] for more information.', 'output' => 'Visit http://www.site.url for more information.', ], 'nwiki explicit link with pipe separated text' => [ 'markup' => 'nwiki', 'input' => 'Visit [https://site.url|http://www.site.url] for more information.', 'output' => 'Visit http://www.site.url for more information.', ], 'html implicit link' => [ 'markup' => 'html', 'input' => 'Visit https://site.url for more information.', 'output' => 'Visit https://site.url for more information.', ], 'html explicit link with text' => [ 'markup' => 'html', 'input' => 'Visit http://www.site.url for more information.', 'output' => 'Visit http://www.site.url for more information.', ], 'html wiki link to non-existing page' => [ 'markup' => 'html', 'input' => 'Visit [[Another page]] for more information.', 'output' => 'Visit ' . 'Another page for more information.', ], 'html wiki link inside an explicit link' => [ // The explicit href URL takes precedence here, the [[...]] is not turned into a wiki link. 'markup' => 'html', 'input' => 'Visit [[Another page]] for more information.', 'output' => 'Visit [[Another page]] for more information.', ], ]; } function testCreoleMarkup() { $this->assertTestFiles('creole'); } function testNwikiMarkup() { $this->assertTestFiles('nwiki'); } function testHtmlMarkup() { $this->assertTestFiles('html'); } private function assertTestFile($num, $markup) { if(!file_exists(__DIR__."/fixtures/input/$markup/$num") || !file_exists(__DIR__."/fixtures/output/$markup/$num")) { return false; } $input = file_get_contents(__DIR__."/fixtures/input/$markup/$num"); $output = file_get_contents(__DIR__."/fixtures/output/$markup/$num"); $result = wiki_parser_proxy::parse($input, $markup, array('pretty_print' => true)); //removes line breaks to avoid line break encoding causing tests to fail. $result['parsed_text'] = preg_replace('~[\r\n]~', '', $result['parsed_text']); $output = preg_replace('~[\r\n]~', '', $output); $this->assertEquals($output, $result['parsed_text'], 'Failed asserting that two strings are equal. Markup = '.$markup.", num = $num"); return true; } private function assertTestFiles($markup) { $i = 1; while($this->assertTestFile($i, $markup)) { $i++; } } /** * Check that headings with special characters work as expected with HTML. * * - The heading itself is well displayed, * - The TOC heading is well display, * - The edit link points to the right page, * - The links properly works with get_section. */ public function test_special_headings(): void { // First testing HTML markup. // Test section name using HTML entities. $input = '
Table of contents
Table of contents
Table of contents
Table of contents
Table of contents
Table of contents
Table of contents
Table of contents
Table of contents