From 2d3b082e03125b861a8a10974df34bf6df645013 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Michael=20Gro=C3=9Fe?= <grosse@cosmocode.de>
Date: Mon, 11 Sep 2017 12:58:56 +0200
Subject: [PATCH] test: add tests for the section edit regex

---
 _test/tests/inc/html_secedit_pattern.test.php | 58 +++++++++++++++++++
 inc/html.php                                  | 10 ++--
 2 files changed, 64 insertions(+), 4 deletions(-)
 create mode 100644 _test/tests/inc/html_secedit_pattern.test.php

diff --git a/_test/tests/inc/html_secedit_pattern.test.php b/_test/tests/inc/html_secedit_pattern.test.php
new file mode 100644
index 000000000..1c0107801
--- /dev/null
+++ b/_test/tests/inc/html_secedit_pattern.test.php
@@ -0,0 +1,58 @@
+<?php
+
+class html_scedit_pattern_test extends DokuWikiTest {
+
+
+    public function dataProviderForTestSecEditPattern() {
+        return [
+            [
+                '<!-- EDIT5 SECTION "Plugins" "plugins" [1406-] -->',
+                [
+                    'secid' => '5',
+                    'target' => 'SECTION',
+                    'name' => 'Plugins',
+                    'hid' => 'plugins',
+                    'range' => '1406-',
+                ],
+                'basic section edit',
+            ],
+            [
+                '<!-- EDIT10 TABLE "" "table4" [11908-14014] -->',
+                [
+                    'secid' => '10',
+                    'target' => 'TABLE',
+                    'name' => '',
+                    'hid' => 'table4',
+                    'range' => '11908-14014',
+                ],
+                'table edit'
+            ],
+            [
+                '<!-- EDIT2 PLUGIN_DATA [27-432] -->',
+                [
+                    'secid' => '2',
+                    'target' => 'PLUGIN_DATA',
+                    'name' => '',
+                    'hid' => '',
+                    'range' => '27-432',
+                ],
+                'data plugin'
+            ],
+        ];
+    }
+
+    /**
+     * @dataProvider dataProviderForTestSecEditPattern
+     *
+     * @param $text
+     * @param $expectedMatches
+     * @param $msg
+     */
+    public function testSecEditPattern($text, $expectedMatches, $msg) {
+        preg_match(SEC_EDIT_PATTERN, $text, $matches);
+        foreach ($expectedMatches as $key => $expected_value) {
+            $this->assertSame($expected_value, $matches[$key], $msg);
+        }
+    }
+
+}
diff --git a/inc/html.php b/inc/html.php
index d370d48b2..350c65f07 100644
--- a/inc/html.php
+++ b/inc/html.php
@@ -8,6 +8,10 @@
 
 if(!defined('DOKU_INC')) die('meh.');
 if(!defined('NL')) define('NL',"\n");
+if (!defined('SEC_EDIT_PATTERN')) {
+    define('SEC_EDIT_PATTERN', '#<!-- EDIT(?<secid>\d+) (?<target>[A-Z_]+) (?:"(?<name>[^"]*)" )?(?:"(?<hid>[^"]*)" )?\[(?<range>\d+-\d*)\] -->#');
+}
+
 
 /**
  * Convenience function to quickly build a wikilink
@@ -91,13 +95,11 @@ function html_denied() {
 function html_secedit($text,$show=true){
     global $INFO;
 
-    $regexp = '#<!-- EDIT(?<secid>\d+) (?<target>[A-Z_]+) (?:"(?<name>[^"]*)" )?(?:"(?<hid>[^"]*)" )?\[(?<range>\d+-\d*)\] -->#';
-
     if(!$INFO['writable'] || !$show || $INFO['rev']){
-        return preg_replace($regexp,'',$text);
+        return preg_replace(SEC_EDIT_PATTERN,'',$text);
     }
 
-    return preg_replace_callback($regexp,
+    return preg_replace_callback(SEC_EDIT_PATTERN,
                 'html_secedit_button', $text);
 }
 
-- 
GitLab