132 lines
3.7 KiB
PHP
132 lines
3.7 KiB
PHP
<?php
|
|
/**
|
|
* Tests for WordPress content stubs.
|
|
*
|
|
* @package WPContentSync
|
|
*/
|
|
|
|
namespace WPContentSync\Tests\Unit\Content;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class WordPressContentStubTest extends TestCase {
|
|
protected function tearDown(): void {
|
|
unset(
|
|
$GLOBALS['wpcs_test_posts'],
|
|
$GLOBALS['wpcs_test_next_post_id'],
|
|
$GLOBALS['wpcs_test_post_meta'],
|
|
$GLOBALS['wpcs_test_terms'],
|
|
$GLOBALS['wpcs_test_next_term_id'],
|
|
$GLOBALS['wpcs_test_object_terms'],
|
|
$GLOBALS['wpcs_test_attachment_files'],
|
|
$GLOBALS['wpcs_test_attachment_metadata'],
|
|
$GLOBALS['wpcs_test_post_filter'],
|
|
$GLOBALS['wpcs_test_force_delete']
|
|
);
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_post_stubs_insert_update_and_read_posts(): void {
|
|
$post_id = wp_insert_post(
|
|
array(
|
|
'post_title' => 'Hello',
|
|
'post_type' => 'post',
|
|
),
|
|
true
|
|
);
|
|
|
|
wp_update_post(
|
|
array(
|
|
'ID' => $post_id,
|
|
'post_title' => 'Updated',
|
|
),
|
|
true
|
|
);
|
|
|
|
self::assertSame( 'Updated', get_post( $post_id )['post_title'] );
|
|
}
|
|
|
|
public function test_meta_stubs_replace_values(): void {
|
|
update_post_meta( 10, '_source_url', 'https://source.test/page' );
|
|
update_post_meta( 10, '_source_url', 'https://destination.test/page' );
|
|
|
|
self::assertSame( array( 'https://destination.test/page' ), get_post_meta( 10, '_source_url', false ) );
|
|
self::assertSame( 'https://destination.test/page', get_post_meta( 10, '_source_url', true ) );
|
|
}
|
|
|
|
public function test_term_stubs_insert_update_and_read_terms(): void {
|
|
$result = wp_insert_term( 'News', 'category', array( 'slug' => 'news' ) );
|
|
|
|
wp_update_term( $result['term_id'], 'category', array( 'name' => 'Latest News' ) );
|
|
|
|
$term = get_term_by( 'slug', 'news', 'category' );
|
|
self::assertSame( 'Latest News', $term['name'] );
|
|
}
|
|
|
|
public function test_attachment_stubs_store_metadata(): void {
|
|
$attachment_id = wp_insert_attachment(
|
|
array(
|
|
'post_title' => 'Image',
|
|
'post_mime_type' => 'image/jpeg',
|
|
),
|
|
false,
|
|
44,
|
|
true
|
|
);
|
|
|
|
wp_update_attachment_metadata(
|
|
$attachment_id,
|
|
array(
|
|
'width' => 1200,
|
|
)
|
|
);
|
|
|
|
self::assertSame( 44, get_post( $attachment_id )['post_parent'] );
|
|
self::assertSame( array( 'width' => 1200 ), wp_get_attachment_metadata( $attachment_id ) );
|
|
}
|
|
|
|
public function test_query_delete_and_object_term_stubs(): void {
|
|
$first_post_id = wp_insert_post(
|
|
array(
|
|
'post_title' => 'First',
|
|
'post_type' => 'post',
|
|
),
|
|
true
|
|
);
|
|
$second_post_id = wp_insert_post(
|
|
array(
|
|
'post_title' => 'Second',
|
|
'post_type' => 'page',
|
|
),
|
|
true
|
|
);
|
|
|
|
update_post_meta( $first_post_id, '_wpcs_source_id', 10 );
|
|
update_post_meta( $second_post_id, '_wpcs_source_id', 20 );
|
|
wp_set_object_terms( $first_post_id, array( 'news', 'updates' ), 'category' );
|
|
delete_post_meta( $second_post_id, '_wpcs_source_id' );
|
|
wp_delete_post( $second_post_id, true );
|
|
|
|
$posts = get_posts(
|
|
array(
|
|
'post_type' => 'post',
|
|
// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- Verifies the unit-test get_posts meta query stub.
|
|
'meta_key' => '_wpcs_source_id',
|
|
'meta_value' => 10,
|
|
// phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value
|
|
)
|
|
);
|
|
|
|
self::assertCount( 1, $posts );
|
|
self::assertSame( $first_post_id, $posts[0]['ID'] );
|
|
self::assertSame( array(), get_post_meta( $second_post_id, '_wpcs_source_id', false ) );
|
|
self::assertNull( get_post( $second_post_id ) );
|
|
self::assertTrue( $GLOBALS['wpcs_test_force_delete'][ $second_post_id ] );
|
|
self::assertSame(
|
|
array( 'news', 'updates' ),
|
|
$GLOBALS['wpcs_test_object_terms'][ $first_post_id ]['category']
|
|
);
|
|
}
|
|
}
|