Skip to content

Commit

Permalink
test(content-distribution): post content (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpeixe authored Jan 10, 2025
1 parent 956219d commit a694795
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tests/unit-tests/content-distribution/post-content/classic.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h2>Heading 2</h2>
<img class="alignnone size-medium wp-image-123" src="https://picsum.photos/id/1/300/300.jpg" width="300" height="300" />
<strong>Strong paragraph</strong>
<p style="text-align: center;">Align middle</p>
<p style="text-align: right;">Align right</p>
<ul>
<li>List Item #1</li>
<li>List Item #2</li>
</ul>
<ol>
<li>Ordered List Item #1</li>
<li>Ordered List Item #2</li>
</ol>
<a href="https://newspack.com">Link</a>
20 changes: 20 additions & 0 deletions tests/unit-tests/content-distribution/post-content/cover.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- wp:cover {"url":"https://picsum.photos/id/2/300/300.jpg","id":123,"dimRatio":50,"customOverlayColor":"#706959","isUserOverlayColor":false,"layout":{"type":"constrained"}} -->
<div class="wp-block-cover">
<span
aria-hidden="true"
class="wp-block-cover__background has-background-dim"
style="background-color: #706959"
></span
><img
class="wp-block-cover__image-background wp-image-123"
alt=""
src="https://picsum.photos/id/2/300/300.jpg"
data-object-fit="cover"
/>
<div class="wp-block-cover__inner-container">
<!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size">Cover Title</p>
<!-- /wp:paragraph -->
</div>
</div>
<!-- /wp:cover -->
19 changes: 19 additions & 0 deletions tests/unit-tests/content-distribution/post-content/gallery.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!-- wp:gallery {"linkTo":"none"} -->
<figure class="wp-block-gallery has-nested-images columns-default is-cropped">
<!-- wp:image {"id":123,"sizeSlug":"large","linkDestination":"none","meta":{"_media_credit":"","_media_credit_url":"","_navis_media_credit_org":""}} -->
<figure class="wp-block-image size-large"><img src="https://picsum.photos/id/1/300/300.jpg" alt="" class="wp-image-123"/><figcaption class="wp-element-caption">Test 1</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":456,"sizeSlug":"large","linkDestination":"none","meta":{"_media_credit":"","_media_credit_url":"","_navis_media_credit_org":""}} -->
<figure class="wp-block-image size-large"><img src="https://picsum.photos/id/2/300/300.jpg" alt="" class="wp-image-456"/><figcaption class="wp-element-caption">Test 2</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":789,"sizeSlug":"large","linkDestination":"none","meta":{"_media_credit":"","_media_credit_url":"","_navis_media_credit_org":""}} -->
<figure class="wp-block-image size-large"><img src="https://picsum.photos/id/3/300/300.jpg" alt="" class="wp-image-789"/><figcaption class="wp-element-caption">Test 3</figcaption></figure>
<!-- /wp:image -->

<!-- wp:image {"id":012,"sizeSlug":"large","linkDestination":"none","meta":{"_media_credit":"","_media_credit_url":"","_navis_media_credit_org":""}} -->
<figure class="wp-block-image size-large"><img src="https://picsum.photos/id/4/300/300.jpg" alt="" class="wp-image-012"/><figcaption class="wp-element-caption">Test 4</figcaption></figure>
<!-- /wp:image -->
</figure>
<!-- /wp:gallery -->
121 changes: 121 additions & 0 deletions tests/unit-tests/content-distribution/test-post-content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/**
* Class TestPostContent
*
* @package Newspack_Network
*/

namespace Test\Content_Distribution;

use Newspack_Network\Content_Distribution\Outgoing_Post;
use Newspack_Network\Content_Distribution\Incoming_Post;

/**
* Test the Incoming_Post class.
*/
class TestPostContent extends \WP_UnitTestCase {
/**
* URL for node that distributes posts.
*
* @var string
*/
protected $node_1 = 'https://node1.test';

/**
* URL for node that receives posts.
*
* @var string
*/
protected $node_2 = 'https://node2.test';

/**
* Set up.
*/
public function set_up() {
parent::set_up();

// Set the site URL for the node that receives posts.
update_option( 'siteurl', $this->node_2 );
update_option( 'home', $this->node_2 );
}

/**
* Get outgoing post payload with content.
*
* @param string $content The post content.
*
* @return array The outgoing post payload.
*/
private function get_outgoing_post_payload_with_content( $content ) {
$outgoing_post = $this->factory->post->create_and_get( [ 'post_content' => $content ] );
$payload = ( new Outgoing_Post( $outgoing_post->ID ) )->get_payload();

// Mock distribution for the post.
$payload['site_url'] = $this->node_1;
$payload['sites'] = [ $this->node_2 ];

return $payload;
}

/**
* Data provider for content.
*/
public function content() {
$files = scandir( __DIR__ . '/post-content' );
$files = array_diff( $files, [ '.', '..' ] );
return array_map(
function ( $file ) {
return [ pathinfo( $file, PATHINFO_FILENAME ) ];
},
$files
);
}

/**
* Assert that two contents are equal.
*
* @param string $expected The expected content.
* @param string $actual The actual content.
*/
private function assertEqualContent( $expected, $actual ) {
$expected = trim( $expected );
$actual = trim( $actual );

/**
* Remove classes from tags to make comparison easier for blocks that uses
* wp_unique_id().
*/
$expected = preg_replace( '/ class="[^"]+"/', '', $expected );
$actual = preg_replace( '/ class="[^"]+"/', '', $actual );

$this->assertEquals( $expected, $actual );
}

/**
* Test classic editor content.
*
* @param string $type The content type.
*
* @dataProvider content
*/
public function test_content( $type ) {
if ( 'classic' === $type ) {
add_filter( 'use_block_editor_for_post_type', '__return_false' );
}

$content = file_get_contents( __DIR__ . '/post-content/' . $type . '.html' );
$payload = $this->get_outgoing_post_payload_with_content( $content );

$incoming_post = new Incoming_Post( $payload );
$post_id = $incoming_post->insert();

$this->assertEqualContent(
apply_filters( 'the_content', get_post_field( 'post_content', $payload['post_id'] ) ),
apply_filters( 'the_content', get_post_field( 'post_content', $post_id ) )
);

if ( 'classic' === $type ) {
remove_filter( 'use_block_editor_for_post_type', '__return_false' );
}
}
}

0 comments on commit a694795

Please sign in to comment.