Skip to content

Commit

Permalink
Add unit test to ensure that attachments are included
Browse files Browse the repository at this point in the history
  • Loading branch information
nate-allen committed Nov 29, 2023
1 parent ef57520 commit 218821c
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions tests/phpunit/tests/export/attachments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* @group export
* @group attachments
* @ticket 17379
*/
class Test_Export_Includes_Attachments extends WP_UnitTestCase {
protected static $post_1;
protected static $attachment_1;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$post_1 = self::factory()->post->create_and_get(
array(
'post_title' => 'Test Post 1',
'post_author' => $factory->user->create( array( 'role' => 'editor' ) ),
)
);

$post_2 = self::factory()->post->create_and_get(
array(
'post_title' => 'Test Post 2',
'post_author' => $factory->user->create( array( 'role' => 'editor' ) ),
)
);

$file = DIR_TESTDATA . '/images/test-image.jpg';

self::$attachment_1 = $factory->attachment->create_upload_object( $file );

set_post_thumbnail( self::$post_1->ID, self::$attachment_1 );
set_post_thumbnail( $post_2, $factory->attachment->create_upload_object( $file ) );
}

/**
* Tests the export function to ensure that attachments are included.
*
* Runs in a separate process to prevent "headers already sent" error.
*
* This test does not preserve global state to prevent the exception
* "Serialization of 'Closure' is not allowed" when running in
* a separate process.
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_export_includes_attachments_for_specific_author() {
require_once ABSPATH . 'wp-admin/includes/export.php';

ob_start();
export_wp(
array(
'content' => 'post',
'author' => self::$post_1->post_author,
)
);
$xml = simplexml_load_string( ob_get_clean() );

$this->assertNotEmpty( $xml->channel->item->title );
$this->assertEquals( self::$post_1->ID, (int) $xml->channel->item[0]->children( 'wp', true )->post_id );
$this->assertEquals( self::$attachment_1, (int) $xml->channel->item[1]->children( 'wp', true )->post_id );

// Test that the post and attachment by the other author are not included by asserting that there are only two items.
$this->assertCount( 2, $xml->channel->item );
}
}

Check failure on line 66 in tests/phpunit/tests/export/attachments.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 newline at end of file; 0 found

0 comments on commit 218821c

Please sign in to comment.