Skip to content

Commit 0e25826

Browse files
dd32claude
andcommitted
Add Plugin Directory to wp-env CI and add unit test coverage
Add Plugin Directory to the WordPress-dependent CI matrix using wp-env. Add .wp-env.json, enhance bootstrap.php with wp-env test framework detection, and add comprehensive unit tests for templates, trademarks, readme parsing, markdown, readme validation, and plugin registration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5cd00ba commit 0e25826

11 files changed

Lines changed: 1423 additions & 6 deletions

File tree

.github/workflows/unit-tests.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ jobs:
6161
include:
6262
- name: Handbook Plugin
6363
plugin-directory: wordpress.org/public_html/wp-content/plugins/handbook
64+
phpunit-args: "--no-configuration --bootstrap phpunit/bootstrap.php phpunit/tests/"
65+
- name: Plugin Directory
66+
plugin-directory: wordpress.org/public_html/wp-content/plugins/plugin-directory
67+
phpunit-args: "--no-configuration --bootstrap tests/bootstrap.php tests/phpunit/tests/"
6468
steps:
6569
- uses: actions/checkout@v4
6670

@@ -82,4 +86,4 @@ jobs:
8286

8387
- name: Run PHPUnit
8488
working-directory: ${{ matrix.plugin-directory }}
85-
run: wp-env run tests-cli --env-cwd=wp-content/plugins/$(basename ${{ matrix.plugin-directory }}) phpunit --no-configuration --bootstrap phpunit/bootstrap.php phpunit/tests/
89+
run: wp-env run tests-cli --env-cwd=wp-content/plugins/$(basename ${{ matrix.plugin-directory }}) phpunit ${{ matrix.phpunit-args }}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"core": "WordPress/WordPress#master",
3+
"plugins": [
4+
".",
5+
"https://downloads.wordpress.org/plugin/jetpack.latest-stable.zip"
6+
]
7+
}

wordpress.org/public_html/wp-content/plugins/plugin-directory/tests/bootstrap.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,31 @@
66
return;
77
}
88

9+
// Find the WordPress PHPUnit test framework.
10+
$_tests_dir = getenv( 'WP_TESTS_DIR' );
11+
12+
if ( ! $_tests_dir ) {
13+
// wp-env test directory.
14+
if ( file_exists( '/wordpress-phpunit/includes/functions.php' ) ) {
15+
$_tests_dir = '/wordpress-phpunit/';
16+
} else {
17+
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib/tests/phpunit/';
18+
}
19+
}
20+
21+
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
22+
echo "Could not find {$_tests_dir}/includes/functions.php\n";
23+
exit( 1 );
24+
}
25+
26+
// Set polyfills path if available (required by WP test suite).
27+
if ( ! defined( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' ) && file_exists( $_tests_dir . '/vendor/yoast/phpunit-polyfills' ) ) {
28+
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_tests_dir . '/vendor/yoast/phpunit-polyfills' );
29+
}
30+
31+
// Give access to tests_add_filter() function.
32+
require_once $_tests_dir . '/includes/functions.php';
33+
934
/**
1035
* Manually load the plugin being tested.
1136
*/
@@ -14,3 +39,6 @@ function manually_load_plugin() {
1439
}
1540

1641
tests_add_filter( 'muplugins_loaded', __NAMESPACE__ . '\manually_load_plugin' );
42+
43+
// Start up the WP testing environment.
44+
require $_tests_dir . '/includes/bootstrap.php';
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
3+
use WordPressdotorg\Plugin_Directory\Markdown;
4+
5+
/**
6+
* @group plugin-directory
7+
* @group markdown
8+
*/
9+
class Tests_Markdown extends WP_UnitTestCase {
10+
11+
function test_transform_basic_paragraph(): void {
12+
$md = new Markdown();
13+
$result = $md->transform( 'Hello World' );
14+
$this->assertSame( '<p>Hello World</p>', $result );
15+
}
16+
17+
function test_transform_empty_string(): void {
18+
$md = new Markdown();
19+
$result = $md->transform( '' );
20+
$this->assertSame( '', $result );
21+
}
22+
23+
function test_transform_trims_output(): void {
24+
$md = new Markdown();
25+
$result = $md->transform( " \n Hello \n " );
26+
$this->assertSame( $result, trim( $result ) );
27+
}
28+
29+
/**
30+
* Test the custom `= Section Title =` header syntax.
31+
*
32+
* This is WordPress plugin readme specific — converts `= Title =` to <h4>.
33+
*/
34+
function test_transform_equals_header(): void {
35+
$md = new Markdown();
36+
$result = $md->transform( '= Section Title =' );
37+
$this->assertStringContainsString( '<h4>Section Title</h4>', $result );
38+
}
39+
40+
function test_transform_multiple_equals_headers(): void {
41+
$md = new Markdown();
42+
$result = $md->transform( "= First =\n\nContent\n\n= Second =" );
43+
$this->assertStringContainsString( '<h4>First</h4>', $result );
44+
$this->assertStringContainsString( '<h4>Second</h4>', $result );
45+
}
46+
47+
function test_equals_header_with_leading_whitespace(): void {
48+
$md = new Markdown();
49+
$result = $md->transform( ' = Indented Header =' );
50+
$this->assertStringContainsString( '<h4>Indented Header</h4>', $result );
51+
}
52+
53+
/**
54+
* Test code_trick: <pre><code> blocks preserve underscores in code.
55+
*
56+
* This is custom logic in Markdown::code_trick() — pre-existing HTML code blocks
57+
* are converted to backtick format before markdown processing, so markdown
58+
* does not mangle underscores and other special characters inside code.
59+
*
60+
* The block needs surrounding content so trim() does not strip indentation.
61+
*/
62+
function test_code_trick_preserves_underscores_in_pre_code(): void {
63+
$md = new Markdown();
64+
$input = "Some text before.\n\n<pre><code>\$my_var = some_function();\n\$other_var = 1;</code></pre>\n\nSome text after.";
65+
$result = $md->transform( $input );
66+
67+
// Underscores should NOT be converted to <em> tags inside code blocks.
68+
$this->assertStringNotContainsString( '<em>', $result );
69+
$this->assertStringContainsString( 'my_var', $result );
70+
$this->assertStringContainsString( 'some_function', $result );
71+
}
72+
73+
function test_code_trick_inline_code_preserves_underscores(): void {
74+
$md = new Markdown();
75+
$input = "Use <code>my_var_name</code> for the setting.";
76+
$result = $md->transform( $input );
77+
78+
// Inline code should also preserve underscores.
79+
$this->assertStringNotContainsString( '<em>', $result );
80+
$this->assertStringContainsString( 'my_var_name', $result );
81+
}
82+
83+
/**
84+
* Test code_trick: bbPress-style backtick code blocks at line start are
85+
* converted to indented code (4 spaces) for markdown processing.
86+
*/
87+
function test_code_trick_bbpress_backtick_block(): void {
88+
$md = new Markdown();
89+
$input = "Some text.\n\n`some_code_here`\n\nMore text.";
90+
$result = $md->transform( $input );
91+
92+
$this->assertStringContainsString( 'some_code_here', $result );
93+
}
94+
95+
/**
96+
* Test that inline markdown code (backticks) in mid-line is preserved.
97+
*/
98+
function test_inline_backtick_code_preserved(): void {
99+
$md = new Markdown();
100+
$result = $md->transform( 'Use `add_filter()` to modify output.' );
101+
$this->assertStringContainsString( '<code>add_filter()</code>', $result );
102+
}
103+
104+
/**
105+
* Test standard markdown features (these verify the upstream MarkdownExtra
106+
* library works correctly through our transform() wrapper).
107+
*/
108+
function test_transform_bold(): void {
109+
$md = new Markdown();
110+
$result = $md->transform( '**bold text**' );
111+
$this->assertStringContainsString( '<strong>bold text</strong>', $result );
112+
}
113+
114+
function test_transform_italic(): void {
115+
$md = new Markdown();
116+
$result = $md->transform( '*italic text*' );
117+
$this->assertStringContainsString( '<em>italic text</em>', $result );
118+
}
119+
120+
function test_transform_link(): void {
121+
$md = new Markdown();
122+
$result = $md->transform( '[Example](https://example.com)' );
123+
$this->assertStringContainsString( '<a href="https://example.com">Example</a>', $result );
124+
}
125+
126+
function test_transform_unordered_list(): void {
127+
$md = new Markdown();
128+
$result = $md->transform( "* Item 1\n* Item 2\n* Item 3" );
129+
$this->assertStringContainsString( '<li>Item 1</li>', $result );
130+
$this->assertStringContainsString( '<ul>', $result );
131+
}
132+
}

0 commit comments

Comments
 (0)