@@ -379,4 +379,115 @@ public function testConfluenceUploadWithLinksToOtherPages(): void
379379 $ this ->testLogger ->logMessages ,
380380 );
381381 }
382+
383+ public function testConfluenceUploadReplacesCodeBlocks (): void
384+ {
385+ $ guzzleLog = [];
386+
387+ $ handlerStack = HandlerStack::create (new MockHandler ([
388+ // GET /{pageId}
389+ 0 => new Response (200 , [], json_encode ([
390+ 'id ' => '123456789 ' ,
391+ 'type ' => 'page type ' ,
392+ 'title ' => 'page title ' ,
393+ 'space ' => ['key ' => 'space key ' ],
394+ 'version ' => ['number ' => '1 ' ],
395+ ], JSON_THROW_ON_ERROR )),
396+ // GET /{pageId}/child/attachment
397+ 1 => new Response (200 , [], json_encode ([
398+ 'results ' => [
399+ ['title ' => 'attachment ' ],
400+ ],
401+ ], JSON_THROW_ON_ERROR )),
402+ // PUT /{pageId}
403+ 2 => new Response (200 , [], json_encode ([], JSON_THROW_ON_ERROR )),
404+ ]));
405+ $ handlerStack ->push (Middleware::history ($ guzzleLog ));
406+
407+ $ confluence = new ConfluenceWriter (
408+ new Client (['handler ' => $ handlerStack ]),
409+ 'https://fake-confluence-url ' ,
410+ 'Something ' ,
411+ $ this ->testLogger ,
412+ true ,
413+ );
414+
415+ $ confluence ->__invoke ([
416+ DocbookPage::fromSlugAndContent (
417+ '/fake/path ' ,
418+ 'page-slug ' ,
419+ <<<'HTML'
420+ <p>Regular text before.</p>
421+ <pre><code>just some plaintext code</code></pre>
422+ <pre><code class="lang-json">{
423+ "some": true,
424+ "json": 123
425+ }
426+ </code></pre>
427+ <pre><code class="lang-shell">make build</code></pre>
428+ <pre><code class="lang-gobbledygook">this gobbledygook language is not a supported Confluence language</code></pre>
429+ <pre><code class="lang-html"><html>
430+ <body>
431+ <pre><code>Example of some HTML to trip up the regex</code></pre>
432+ </body>
433+ </html>
434+ </code></pre>
435+ <p>Regular text after.</p>
436+ HTML,
437+ )->withFrontMatter (['confluencePageId ' => 123456789 ]),
438+ ]);
439+
440+ /** @psalm-var array<self::*,array{request:RequestInterface}> $guzzleLog */
441+
442+ $ postedPageContent = $ guzzleLog [2 ]['request ' ];
443+ assert ($ postedPageContent instanceof RequestInterface);
444+ $ this ->assertPostContentRequestWasCorrect (
445+ $ postedPageContent ,
446+ 123456789 ,
447+ <<<'HTML'
448+ <p>Regular text before.</p>
449+ <ac:structured-macro ac:name="code" ac:schema-version="1">
450+ <ac:parameter ac:name="language">none</ac:parameter>
451+ <ac:plain-text-body><![CDATA[just some plaintext code]]>
452+ </ac:plain-text-body>
453+ </ac:structured-macro>
454+ <ac:structured-macro ac:name="code" ac:schema-version="1">
455+ <ac:parameter ac:name="language">javascript</ac:parameter>
456+ <ac:plain-text-body><![CDATA[{
457+ "some": true,
458+ "json": 123
459+ }
460+ ]]>
461+ </ac:plain-text-body>
462+ </ac:structured-macro>
463+ <ac:structured-macro ac:name="code" ac:schema-version="1">
464+ <ac:parameter ac:name="language">bash</ac:parameter>
465+ <ac:plain-text-body><![CDATA[make build]]>
466+ </ac:plain-text-body>
467+ </ac:structured-macro>
468+ <ac:structured-macro ac:name="code" ac:schema-version="1">
469+ <ac:parameter ac:name="language">none</ac:parameter>
470+ <ac:plain-text-body><![CDATA[this gobbledygook language is not a supported Confluence language]]>
471+ </ac:plain-text-body>
472+ </ac:structured-macro>
473+ <ac:structured-macro ac:name="code" ac:schema-version="1">
474+ <ac:parameter ac:name="language">html</ac:parameter>
475+ <ac:plain-text-body><![CDATA[<html>
476+ <body>
477+ <pre><code>Example of some HTML to trip up the regex</code></pre>
478+ </body>
479+ </html>
480+ ]]>
481+ </ac:plain-text-body>
482+ </ac:structured-macro>
483+ <p>Regular text after.</p>
484+ HTML,
485+ 2 ,
486+ );
487+
488+ self ::assertContains (
489+ sprintf ('[%s] - OK! Successfully updated confluence page 123456789 with page-slug ... ' , ConfluenceWriter::class),
490+ $ this ->testLogger ->logMessages ,
491+ );
492+ }
382493}
0 commit comments