Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions includes/content-distribution/class-incoming-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,9 @@ protected function update_taxonomy_terms() {
if ( is_wp_error( $result ) ) {
self::log( 'Failed to set terms for taxonomy ' . $taxonomy . ' with message: ' . $result->get_error_message() );
}
} elseif ( empty( $terms ) ) {
// If there are no terms, remove all terms from the taxonomy.
wp_set_object_terms( $this->ID, [], $taxonomy );
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion includes/content-distribution/class-taxonomy-terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public static function get_post_taxonomy_terms( \WP_Post $post ) {
}
$terms = get_the_terms( $post->ID, $taxonomy->name );
if ( ! $terms ) {
continue;
$terms = [];
}

$data[ $taxonomy->name ] = array_map(
Expand Down
79 changes: 79 additions & 0 deletions tests/unit-tests/content-distribution/test-incoming-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -747,4 +747,83 @@ public function test_toggle_jetpack_photon() {
// Assert that the thumbnail is unchanged.
$this->assertSame( $thumbnail_id, get_post_thumbnail_id( $post_id ) );
}

/**
* Test removing all terms from a taxonomy with empty array.
*/
public function test_remove_all_terms_with_empty_array() {
$payload = $this->get_sample_payload();

// Insert the linked post with categories.
$post_id = $this->incoming_post->insert( $payload );

// Assert that the post has categories.
$terms = wp_get_post_terms( $post_id, 'category' );
$this->assertNotEmpty( $terms );
$this->assertCount( 2, $terms );

// Update the payload to have an empty array for categories.
$payload['post_data']['taxonomy']['category'] = [];

// Insert the updated linked post.
$this->incoming_post->insert( $payload );

// Assert that all categories have been removed.
$terms = wp_get_post_terms( $post_id, 'category' );
$this->assertEmpty( $terms );

// Assert that tags are still present.
$tags = wp_get_post_terms( $post_id, 'post_tag' );
$this->assertNotEmpty( $tags );
$this->assertCount( 2, $tags );
}

/**
* Test removing all terms from multiple taxonomies with empty arrays.
*/
public function test_remove_all_terms_from_multiple_taxonomies() {
$payload = $this->get_sample_payload();

// Insert the linked post with categories and tags.
$post_id = $this->incoming_post->insert( $payload );

// Assert that the post has categories and tags.
$categories = wp_get_post_terms( $post_id, 'category' );
$tags = wp_get_post_terms( $post_id, 'post_tag' );
$this->assertNotEmpty( $categories );
$this->assertNotEmpty( $tags );

// Update the payload to have empty arrays for both taxonomies.
$payload['post_data']['taxonomy']['category'] = [];
$payload['post_data']['taxonomy']['post_tag'] = [];

// Insert the updated linked post.
$this->incoming_post->insert( $payload );

// Assert that all categories and tags have been removed.
$categories = wp_get_post_terms( $post_id, 'category' );
$tags = wp_get_post_terms( $post_id, 'post_tag' );
$this->assertEmpty( $categories );
$this->assertEmpty( $tags );
}

/**
* Test empty taxonomy array creates post without terms.
*/
public function test_insert_post_with_empty_taxonomy_array() {
$payload = $this->get_sample_payload();

// Set taxonomies to empty arrays before inserting.
$payload['post_data']['taxonomy']['category'] = [];
$payload['post_data']['taxonomy']['post_tag'] = [];

// Insert the linked post.
$post_id = $this->incoming_post->insert( $payload );

// Assert that the post was created without any terms.
$categories = wp_get_post_terms( $post_id, 'category' );
$tags = wp_get_post_terms( $post_id, 'post_tag' );
$this->assertEmpty( $categories );
$this->assertEmpty( $tags );
}
}
50 changes: 50 additions & 0 deletions tests/unit-tests/content-distribution/test-outgoing-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,56 @@ public function test_ignored_taxonomies() {
$this->assertTrue( empty( $payload['post_data']['taxonomy'][ $taxonomy ] ) );
}

/**
* Test empty taxonomies are included in the payload.
*/
public function test_empty_taxonomies_included_in_payload() {
// Create a post without any terms.
$post = $this->factory->post->create_and_get(
[
'post_type' => 'post',
'post_author' => $this->some_editor->ID,
]
);
$outgoing_post = new Outgoing_Post( $post );
$outgoing_post->set_distribution( [ $this->network[0]['url'] ] );

$payload = $outgoing_post->get_payload();

// Assert that category and post_tag exist in the payload as empty arrays.
$this->assertArrayHasKey( 'taxonomy', $payload['post_data'] );
$this->assertArrayHasKey( 'post_tag', $payload['post_data']['taxonomy'] );
$this->assertIsArray( $payload['post_data']['taxonomy']['post_tag'] );
$this->assertEmpty( $payload['post_data']['taxonomy']['post_tag'] );
}

/**
* Test empty custom taxonomy is included in the payload.
*/
public function test_empty_custom_taxonomy_included_in_payload() {
// Register a custom taxonomy.
$custom_taxonomy = 'custom_taxonomy';
register_taxonomy( $custom_taxonomy, 'post', [ 'public' => true ] );

// Create a post without any terms in the custom taxonomy.
$post = $this->factory->post->create_and_get(
[
'post_type' => 'post',
'post_author' => $this->some_editor->ID,
]
);
$outgoing_post = new Outgoing_Post( $post );
$outgoing_post->set_distribution( [ $this->network[0]['url'] ] );

$payload = $outgoing_post->get_payload();

// Assert that the custom taxonomy exists in the payload as an empty array.
$this->assertArrayHasKey( 'taxonomy', $payload['post_data'] );
$this->assertArrayHasKey( $custom_taxonomy, $payload['post_data']['taxonomy'] );
$this->assertIsArray( $payload['post_data']['taxonomy'][ $custom_taxonomy ] );
$this->assertEmpty( $payload['post_data']['taxonomy'][ $custom_taxonomy ] );
}

/**
* Test get partial payload.
*/
Expand Down
69 changes: 69 additions & 0 deletions tests/unit-tests/content-distribution/test-taxonomy-terms.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,73 @@ public function test_get_or_create_term_ids_mixed() {
$this->assertNotFalse( $new_term );
$this->assertContains( $new_term->term_id, $term_ids );
}

/**
* Test get_post_taxonomy_terms with no terms returns empty array.
*/
public function test_get_post_taxonomy_terms_no_terms_returns_empty_array() {
// Create a post without any terms.
$post_id = $this->factory->post->create( [ 'post_title' => 'Test Post' ] );
$post = get_post( $post_id );

// Get taxonomy terms.
$terms = Taxonomy_Terms::get_post_taxonomy_terms( $post );

// Assert that categories and tags exist in the array as empty arrays.
$this->assertArrayHasKey( 'post_tag', $terms );
$this->assertIsArray( $terms['post_tag'] );
$this->assertEmpty( $terms['post_tag'] );
}

/**
* Test get_post_taxonomy_terms with mixed terms (some empty, some not).
*/
public function test_get_post_taxonomy_terms_mixed_empty_and_populated() {
// Create a post.
$post_id = $this->factory->post->create( [ 'post_title' => 'Test Post' ] );
$post = get_post( $post_id );

// Create and assign categories but not tags.
$category_1 = $this->factory->term->create(
[
'taxonomy' => 'category',
'name' => 'Category 1',
]
);
wp_set_post_terms( $post_id, [ $category_1 ], 'category' );

// Get taxonomy terms.
$terms = Taxonomy_Terms::get_post_taxonomy_terms( $post );

// Assert that categories are populated.
$this->assertArrayHasKey( 'category', $terms );
$this->assertNotEmpty( $terms['category'] );
$this->assertCount( 1, $terms['category'] );
$this->assertSame( 'Category 1', $terms['category'][0]['name'] );

// Assert that tags are present but empty.
$this->assertArrayHasKey( 'post_tag', $terms );
$this->assertIsArray( $terms['post_tag'] );
$this->assertEmpty( $terms['post_tag'] );
}

/**
* Test get_post_taxonomy_terms with custom taxonomy having no terms.
*/
public function test_get_post_taxonomy_terms_custom_taxonomy_no_terms() {
// Register a custom taxonomy.
register_taxonomy( 'custom_taxonomy', 'post', [ 'public' => true ] );

// Create a post without any custom taxonomy terms.
$post_id = $this->factory->post->create( [ 'post_title' => 'Test Post' ] );
$post = get_post( $post_id );

// Get taxonomy terms.
$terms = Taxonomy_Terms::get_post_taxonomy_terms( $post );

// Assert that custom taxonomy exists in the array as an empty array.
$this->assertArrayHasKey( 'custom_taxonomy', $terms );
$this->assertIsArray( $terms['custom_taxonomy'] );
$this->assertEmpty( $terms['custom_taxonomy'] );
}
}