Archived
108 lines
3.1 KiB
Python
108 lines
3.1 KiB
Python
import os
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from deliverable.utils.plugins import _get_plugins_from_dirs, _get_obfuscated_plugins
|
|
|
|
|
|
def write_file(path: Path, content: str):
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(content, encoding="utf-8")
|
|
|
|
|
|
class TestGetPluginsFromDirs(unittest.TestCase):
|
|
def test_single_file_plugin_detected(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
p = Path(td) / "plugins"
|
|
p.mkdir()
|
|
single = p / "hello.php"
|
|
write_file(
|
|
single,
|
|
"""<?php
|
|
/*
|
|
Plugin Name: Hello Plugin
|
|
*/
|
|
// plugin body
|
|
""",
|
|
)
|
|
|
|
res = _get_plugins_from_dirs([str(p)])
|
|
self.assertIn("hello", res)
|
|
self.assertEqual(
|
|
os.path.normpath(res["hello"]), os.path.normpath(str(single))
|
|
)
|
|
|
|
def test_directory_plugin_with_dir_named_file(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
p = Path(td) / "plugins"
|
|
plugin_dir = p / "akismet"
|
|
plugin_dir.mkdir(parents=True)
|
|
main = plugin_dir / "akismet.php"
|
|
write_file(
|
|
main,
|
|
"""<?php
|
|
/*
|
|
Plugin Name: Akismet Mock
|
|
*/
|
|
""",
|
|
)
|
|
|
|
res = _get_plugins_from_dirs([str(p)])
|
|
self.assertIn("akismet", res)
|
|
self.assertEqual(
|
|
os.path.normpath(res["akismet"]), os.path.normpath(str(main))
|
|
)
|
|
|
|
def test_directory_plugin_with_other_php_file(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
p = Path(td) / "plugins"
|
|
plugin_dir = p / "sample"
|
|
plugin_dir.mkdir(parents=True)
|
|
main = plugin_dir / "main.php"
|
|
write_file(
|
|
main,
|
|
"""<?php
|
|
/*
|
|
Plugin Name: Sample Plugin
|
|
*/
|
|
""",
|
|
)
|
|
|
|
res = _get_plugins_from_dirs([str(p)])
|
|
# slug is the directory name
|
|
self.assertIn("sample", res)
|
|
self.assertEqual(
|
|
os.path.normpath(res["sample"]), os.path.normpath(str(main))
|
|
)
|
|
|
|
def test_files_without_header_ignored(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
p = Path(td) / "plugins"
|
|
p.mkdir()
|
|
noheader = p / "noheader.php"
|
|
write_file(noheader, "<?php\n// no plugin header here\n")
|
|
|
|
res = _get_plugins_from_dirs([str(p)])
|
|
self.assertNotIn("noheader", res)
|
|
|
|
def test_mu_plugins_include_no_header(self):
|
|
with tempfile.TemporaryDirectory() as td:
|
|
p = Path(td) / "mu-plugins"
|
|
p.mkdir()
|
|
noheader = p / "bootstrap.php"
|
|
write_file(
|
|
noheader, "<?php\n// initialization file without plugin header\n"
|
|
)
|
|
|
|
res = _get_obfuscated_plugins([str(p)])
|
|
# mu-plugin should be included even without a header
|
|
self.assertIn("bootstrap", res)
|
|
self.assertEqual(
|
|
os.path.normpath(res["bootstrap"]), os.path.normpath(str(noheader))
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|