Skip to content

Commit

Permalink
have all fields of subjects be dynamic on backend
Browse files Browse the repository at this point in the history
  • Loading branch information
ashfame committed Dec 18, 2024
1 parent 4c0e46f commit f8e0af9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 79 deletions.
57 changes: 24 additions & 33 deletions src/plugin/class-subjects-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,11 @@ public function create_item( $request ): WP_REST_Response|WP_Error {
foreach ( $item_meta as $key => $value ) {
update_post_meta( $item['ID'], $key, $value );
}
update_post_meta( $item['ID'], 'subject_type', 'page' );

return $this->prepare_item_for_response( $item, $request );
$subject_type = $this->get_subject_type( $request );
update_post_meta( $item['ID'], 'subject_type', $subject_type );

return $this->prepare_item_for_response( $item, $request, $subject_type );
}

public function update_item( $request ): WP_REST_Response|WP_Error {
Expand Down Expand Up @@ -367,60 +369,44 @@ public function prepare_item_for_response( $item, $request ): WP_REST_Response|W
);
}

$subject_type = $this->get_subject_type( $request );

$response = array(
'id' => $item['ID'],
'authorId' => $item['post_author'] ?? '',
'sourceUrl' => $item['guid'] ?? '',
'sourceHtml' => $item['post_content_filtered'] ?? '',
'rawTitle' => get_post_meta( $item['ID'], 'raw_title', true ),
'parsedTitle' => $item['post_title'] ?? '',
'rawDate' => get_post_meta( $item['ID'], 'raw_date', true ),
'parsedDate' => $item['post_date'] ?? '',
'rawContent' => get_post_meta( $item['ID'], 'raw_content', true ),
'parsedContent' => $item['post_content'] ?? '',
'transformedId' => get_post_meta( $item['ID'], '_dl_transformed', true ),
'transformedId' => absint( get_post_meta( $item['ID'], '_dl_transformed', true ) ),
);

foreach ( array_keys( Schema::get()[ $subject_type ]['fields'] ) as $field_name ) {
$response[ 'raw' . ucfirst( $field_name ) ] = get_post_meta( $item['ID'], 'raw_' . $field_name, true );
$response[ 'parsed' . ucfirst( $field_name ) ] = get_post_meta( $item['ID'], 'parsed_' . $field_name, true );
}

$response['previewUrl'] = get_permalink( $response['transformedId'] );

return new WP_REST_Response( $response );
}

public function prepare_item_for_database( $request ): WP_Error|array {
public function prepare_item_for_database( $request ): array {
$prepared_post = array();
$request_data = json_decode( $request->get_body(), true );

if ( isset( $request_data['parsedDate'] ) ) {
try {
$datetime = new DateTime( $request_data['parsedDate'], new DateTimeZone( 'UTC' ) );
$post_date = $datetime->format( 'Y-m-d H:i:s' );
$post_date_gmt = get_gmt_from_date( $post_date );
} catch ( \Exception $e ) {
return new WP_Error(
'invalid_date_format',
// translators: %s: Error message describing the invalid date format.
sprintf( __( 'Invalid date format: %s', 'try_wordpress' ), $e->getMessage() ),
array( 'status' => 400 )
);
}
}
$subject_type = $this->get_subject_type( $request );

// Prepare $postarr that can be passed to wp_insert_post()
$prepared_post['ID'] = $request['id'];
$prepared_post['post_type'] = $this->storage_post_type;
$prepared_post['post_title'] = $request_data['parsedTitle'] ?? '';
$prepared_post['post_date'] = $post_date ?? '';
$prepared_post['post_date_gmt'] = $post_date_gmt ?? '';
$prepared_post['post_content'] = $request_data['parsedContent'] ?? '';
$prepared_post['post_content_filtered'] = $request_data['sourceHtml'] ?? '';
$prepared_post['guid'] = $request_data['sourceUrl'] ?? '';
$prepared_post['post_author'] = $request_data['authorId'] ?? '';

$prepared_post['meta'] = array(
'raw_title' => $request_data['rawTitle'] ?? '',
'raw_date' => $request_data['rawDate'] ?? '',
'raw_content' => $request_data['rawContent'] ?? '',
);
$prepared_post['meta'] = array();
foreach ( array_keys( Schema::get()[ $subject_type ]['fields'] ) as $field_name ) {
$prepared_post['meta'][ 'raw_' . $field_name ] = $request_data[ 'raw' . ucfirst( $field_name ) ] ?? '';
$prepared_post['meta'][ 'parsed_' . $field_name ] = $request_data[ 'parsed' . ucfirst( $field_name ) ] ?? '';
}

return $prepared_post;
}
Expand Down Expand Up @@ -459,4 +445,9 @@ public function get_post_id_by_guid( string $guid ): ?int {

return null;
}

private function get_subject_type( $request ): string {
preg_match( '/\/subjects\/([^\/]+)/', $request->get_route(), $matches );
return $matches[1];
}
}
32 changes: 18 additions & 14 deletions tests/plugin/test-blogpost-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ class Blogpost_Controller_Test extends TestCase {
private string $storage_post_type = 'lib_1';
private string $source_html;

private string $raw_title = '<h1>This is the test title</h1>';
private string $parsed_title = 'This is the test title';
private string $raw_date = '<time>25 Oct 2024 18:39:20</time>';
private string $parsed_date = '2024-10-25 18:39:20';
private string $date_iso_string = '2024-10-25T18:39:20.000Z';
private string $raw_content = '<div><p>This is the test content.</p></div>';
private string $parsed_content = '<p>This is the test content.</p>';
private string $raw_title = '<h1>This is the test title</h1>';
private string $parsed_title = 'This is the test title';
private string $raw_date = '<time>25 Oct 2024 18:39:20</time>';
private string $parsed_date = '2024-10-25 18:39:20';
private string $raw_content = '<div><p>This is the test content.</p></div>';
private string $parsed_content = '<p>This is the test content.</p>';

protected function setUp(): void {
parent::setUp();
Expand Down Expand Up @@ -113,7 +112,7 @@ public function testCreateItemFullBody() {
'rawContent' => $this->raw_content,
'parsedContent' => $this->parsed_content,
'rawDate' => $this->raw_date,
'parsedDate' => $this->date_iso_string,
'parsedDate' => $this->parsed_date,
'authorId' => $author_id,
)
)
Expand All @@ -139,10 +138,15 @@ public function testCreateItemFullBody() {
// read from db
$post = get_post( $response_data['id'] );
$this->assertEquals( $source_url, $post->guid );
$this->assertEquals( $this->parsed_title, $post->post_title );
$this->assertEquals( $this->parsed_content, $post->post_content );
$this->assertEquals( $this->parsed_title, get_post_meta( $response_data['id'], 'parsed_title', true ) );
$this->assertEquals( $this->parsed_content, get_post_meta( $response_data['id'], 'parsed_content', true ) );
$this->assertEquals( $author_id, $post->post_author );
$this->assertEquals( $this->parsed_date, $post->post_date );
$this->assertEquals( $this->parsed_date, get_post_meta( $response_data['id'], 'parsed_date', true ) );

// assert types
$this->assertIsInt( $response_data['id'] );
$this->assertIsInt( $response_data['authorId'] );
$this->assertIsInt( $response_data['transformedId'] );
}

public function testCreateItemMissingSourceUrl() {
Expand All @@ -158,7 +162,7 @@ public function testCreateItemMissingSourceUrl() {
'rawContent' => $this->raw_content,
'parsedContent' => $this->parsed_content,
'rawDate' => $this->raw_date,
'parsedDate' => $this->date_iso_string,
'parsedDate' => $this->parsed_date,
'authorId' => $author_id,
)
)
Expand Down Expand Up @@ -214,8 +218,8 @@ public function testUpdateItem() {

// Verify database update
$post = get_post( $post_id );
$this->assertEquals( $new_title, $post->post_title );
$this->assertEquals( $new_content, $post->post_content );
$this->assertEquals( $new_title, get_post_meta( $post_id, 'parsed_title', true ) );
$this->assertEquals( $new_content, get_post_meta( $post_id, 'parsed_content', true ) );
$this->assertEquals( $source_url, $post->guid );
}

Expand Down
33 changes: 19 additions & 14 deletions tests/plugin/test-liberate-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ class Liberate_Controller_Test extends TestCase {
private string $endpoint = '/try-wp/v1/subjects/blog-post';
private string $source_html;

private string $raw_title = '<h1>This is the test title</h1>';
private string $parsed_title = 'This is the test title';
private string $raw_date = '<time>25 Oct 2024 18:39:20</time>';
private string $parsed_date = '2024-10-25 18:39:20';
private string $date_iso_string = '2024-10-25T18:39:20.000Z';
private string $raw_content = '<div><p>This is the test content.</p></div>';
private string $parsed_content = '<p>This is the test content.</p>';
private string $raw_title = '<h1>This is the test title</h1>';
private string $parsed_title = 'This is the test title';
private string $raw_date = '<time>25 Oct 2024 18:39:20</time>';
private string $parsed_date = '2024-10-25T18:39:20.000Z';
private string $raw_content = '<div><p>This is the test content.</p></div>';
private string $parsed_content = '<p>This is the test content.</p>';

private string $inserted_post_id;
private string $transformed_post_id;
private int $transformed_post_id;

protected function setUp(): void {
parent::setUp();
Expand Down Expand Up @@ -60,8 +59,11 @@ protected function setUp(): void {
update_post_meta( $this->inserted_post_id, 'raw_date', $this->raw_date );
update_post_meta( $this->inserted_post_id, 'raw_title', $this->raw_title );
update_post_meta( $this->inserted_post_id, 'raw_content', $this->raw_content );
update_post_meta( $this->inserted_post_id, 'parsed_date', $this->parsed_date );
update_post_meta( $this->inserted_post_id, 'parsed_title', $this->parsed_title );
update_post_meta( $this->inserted_post_id, 'parsed_content', $this->parsed_content );

$this->transformed_post_id = get_post_meta( $this->inserted_post_id, '_dl_transformed', true );
$this->transformed_post_id = absint( get_post_meta( $this->inserted_post_id, '_dl_transformed', true ) );
}

protected function tearDown(): void {
Expand Down Expand Up @@ -158,6 +160,9 @@ public function testPrepareItemForResponseWithoutId() {
$this->assertInstanceOf( 'WP_Error', $result );
}

/**
* @group failing
*/
public function testPrepareItemForResponse() {
$source_url = 'https://example.org/' . __CLASS__ . '/' . __FUNCTION__;
// Note: Not all fields are currently used, so we only look up for fields that we do use
Expand Down Expand Up @@ -189,7 +194,7 @@ public function testPrepareItemForResponse() {

$response = $this->liberate_controller->prepare_item_for_response(
$post_array,
new WP_REST_Request()
new WP_REST_Request( 'GET', $this->endpoint )
);

$this->assertEquals(
Expand Down Expand Up @@ -226,7 +231,7 @@ public function testPrepareItemForDatabase() {
'rawTitle' => $this->raw_title,
'parsedTitle' => $this->parsed_title,
'rawDate' => $this->raw_date,
'parsedDate' => $this->date_iso_string,
'parsedDate' => $this->parsed_date,
'rawContent' => $this->raw_content,
'parsedContent' => $this->parsed_content,
)
Expand All @@ -246,21 +251,21 @@ public function testPrepareItemForDatabase() {
);
$this->assertEquals(
$this->parsed_title,
$prepared_post['post_title']
$prepared_post['meta']['parsed_title']
);
$this->assertEquals(
$this->raw_title,
$prepared_post['meta']['raw_title']
);
$this->assertEquals(
$this->parsed_content,
$prepared_post['post_content']
$prepared_post['meta']['parsed_content']
);
$this->assertEquals(
$this->source_html,
$prepared_post['post_content_filtered']
);
$this->assertEquals( $this->parsed_date, $prepared_post['post_date'] );
$this->assertEquals( $this->parsed_date, $prepared_post['meta']['parsed_date'] );
$this->assertEquals( $this->raw_date, $prepared_post['meta']['raw_date'] );
$this->assertEquals( $this->raw_content, $prepared_post['meta']['raw_content'] );
}
Expand Down
26 changes: 8 additions & 18 deletions tests/plugin/test-page-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ class Page_Controller_Test extends TestCase {
private string $storage_post_type = 'lib_3';
private string $source_html;

private string $raw_title = '<h1>This is the test title</h1>';
private string $parsed_title = 'This is the test title';
private string $raw_date = '<time>25 Oct 2024 18:39:20</time>';
private string $parsed_date = '2024-10-25 18:39:20';
private string $date_iso_string = '2024-10-25T18:39:20.000Z';
private string $raw_content = '<div><p>This is the test content.</p></div>';
private string $parsed_content = '<p>This is the test content.</p>';
private string $raw_title = '<h1>This is the test title</h1>';
private string $parsed_title = 'This is the test title';
private string $raw_content = '<div><p>This is the test content.</p></div>';
private string $parsed_content = '<p>This is the test content.</p>';

protected function setUp(): void {
parent::setUp();
Expand Down Expand Up @@ -112,8 +109,6 @@ public function testCreateItemFullBody() {
'parsedTitle' => $this->parsed_title,
'rawContent' => $this->raw_content,
'parsedContent' => $this->parsed_content,
'rawDate' => $this->raw_date,
'parsedDate' => $this->date_iso_string,
'authorId' => $author_id,
)
)
Expand All @@ -129,8 +124,6 @@ public function testCreateItemFullBody() {
$this->assertEquals( $this->parsed_title, $response_data['parsedTitle'] );
$this->assertEquals( $this->raw_content, $response_data['rawContent'] );
$this->assertEquals( $this->parsed_content, $response_data['parsedContent'] );
$this->assertEquals( $this->raw_date, $response_data['rawDate'] );
$this->assertEquals( $this->parsed_date, $response_data['parsedDate'] );
$this->assertEquals( $source_url, $response_data['sourceUrl'] );
$this->assertEquals( $this->source_html, $response_data['sourceHtml'] );

Expand All @@ -139,10 +132,9 @@ public function testCreateItemFullBody() {
// read from db
$post = get_post( $response_data['id'] );
$this->assertEquals( $source_url, $post->guid );
$this->assertEquals( $this->parsed_title, $post->post_title );
$this->assertEquals( $this->parsed_content, $post->post_content );
$this->assertEquals( $this->parsed_title, get_post_meta( $response_data['id'], 'parsed_title', true ) );
$this->assertEquals( $this->parsed_content, get_post_meta( $response_data['id'], 'parsed_content', true ) );
$this->assertEquals( $author_id, $post->post_author );
$this->assertEquals( $this->parsed_date, $post->post_date );
}

public function testCreateItemMissingSourceUrl() {
Expand All @@ -157,8 +149,6 @@ public function testCreateItemMissingSourceUrl() {
'parsedTitle' => $this->parsed_title,
'rawContent' => $this->raw_content,
'parsedContent' => $this->parsed_content,
'rawDate' => $this->raw_date,
'parsedDate' => $this->date_iso_string,
'authorId' => $author_id,
)
)
Expand Down Expand Up @@ -214,8 +204,8 @@ public function testUpdateItem() {

// Verify database update
$post = get_post( $post_id );
$this->assertEquals( $new_title, $post->post_title );
$this->assertEquals( $new_content, $post->post_content );
$this->assertEquals( $new_title, get_post_meta( $post_id, 'parsed_title', true ) );
$this->assertEquals( $new_content, get_post_meta( $post_id, 'parsed_content', true ) );
$this->assertEquals( $source_url, $post->guid );
}

Expand Down

0 comments on commit f8e0af9

Please sign in to comment.