xref: /plugin/deletepageguard/tests/plugin_test_adapter.php (revision c176b8b34e4ca9181ac383002f75ffe5441c68b3)
1<?php
2/**
3 * Test Adapter for Delete Page Guard Plugin
4 *
5 * This file provides a testable version of the plugin by extending the
6 * actual plugin class and making protected methods accessible for testing.
7 *
8 * @license GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html) - see LICENSE.md
9 * @author  Johann Duscher <jonny.dee@posteo.net>
10 * @copyright 2025 Johann Duscher
11 */
12
13// Mock the DokuWiki ActionPlugin class for testing
14if (!class_exists('dokuwiki\Extension\ActionPlugin')) {
15    class ActionPlugin {
16        protected $config = [
17            'patterns' => "^start$\n^sidebar$\n^users:[^:]+:start$",
18            'match_target' => 'id',
19            'exempt_groups' => 'editors,moderators',
20            'trim_mode' => true
21        ];
22
23        protected $lang = [
24            'pattern_redos_warning' => 'Pattern "%s" may cause performance issues',
25            'pattern_too_long' => 'Pattern "%s" is too long (max 1000 chars)',
26            'pattern_invalid_syntax' => 'Pattern "%s" has invalid syntax: %s',
27            'deny_msg' => 'Deleting this page is not allowed.',
28            'config_validation_errors' => 'Some regex patterns have validation errors.'
29        ];
30
31        public function getConf($key) {
32            return isset($this->config[$key]) ? $this->config[$key] : null;
33        }
34
35        public function getLang($key) {
36            return isset($this->lang[$key]) ? $this->lang[$key] : "[$key]";
37        }
38    }
39
40    // Create the namespace alias
41    class_alias('ActionPlugin', 'dokuwiki\Extension\ActionPlugin');
42}
43
44// Mock DokuWiki Event classes
45if (!class_exists('dokuwiki\Extension\Event')) {
46    class Event {
47        public $data = [];
48        public $canPreventDefault = true;
49    }
50    class_alias('Event', 'dokuwiki\Extension\Event');
51}
52
53if (!class_exists('dokuwiki\Extension\EventHandler')) {
54    class EventHandler {
55        public function register_hook($event, $when, $obj, $method) {
56            // Mock implementation
57        }
58    }
59    class_alias('EventHandler', 'dokuwiki\Extension\EventHandler');
60}
61
62// Mock DokuWiki constants and functions
63if (!defined('DOKU_INC')) define('DOKU_INC', dirname(__DIR__) . '/');
64
65// Include the actual plugin file
66require_once dirname(__DIR__) . '/action.php';
67
68/**
69 * Testable version of the Delete Page Guard plugin
70 *
71 * Extends the actual plugin class and exposes protected methods for testing.
72 */
73class TestableDeletePageGuard extends action_plugin_deletepageguard {
74
75    /**
76     * Override getConf to use mock configuration
77     */
78    public function getConf($key) {
79        $config = [
80            'patterns' => "^start$\n^sidebar$\n^users:[^:]+:start$",
81            'match_target' => 'id',
82            'exempt_groups' => 'editors,moderators',
83            'trim_mode' => true
84        ];
85        return isset($config[$key]) ? $config[$key] : null;
86    }
87
88    /**
89     * Override getLang to use mock language strings
90     */
91    public function getLang($key) {
92        $lang = [
93            'pattern_redos_warning' => 'Pattern "%s" may cause performance issues',
94            'pattern_too_long' => 'Pattern "%s" is too long (max 1000 chars)',
95            'pattern_invalid_syntax' => 'Pattern "%s" has invalid syntax: %s',
96            'deny_msg' => 'Deleting this page is not allowed.',
97            'config_validation_errors' => 'Some regex patterns have validation errors.'
98        ];
99        return isset($lang[$key]) ? $lang[$key] : "[$key]";
100    }
101
102    /**
103     * Expose protected validateRegexPattern method for testing
104     */
105    public function validateRegexPattern($pattern, $lineNumber = 0) {
106        return parent::validateRegexPattern($pattern, $lineNumber);
107    }
108
109    /**
110     * Expose protected matchesPattern method for testing
111     */
112    public function matchesPattern($pattern, $target) {
113        return parent::matchesPattern($pattern, $target);
114    }
115
116    /**
117     * Expose protected getRelativeFilePath method for testing
118     */
119    public function getRelativeFilePath($fullPath, $dataDir) {
120        return parent::getRelativeFilePath($fullPath, $dataDir);
121    }
122}