|
| 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