Skip to content

Commit

Permalink
Reduce duplication, make tests more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Feb 20, 2024
1 parent 04c6910 commit 99e955e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
41 changes: 20 additions & 21 deletions src/wp-includes/fonts/class-wp-font-collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,26 @@ private function load_from_json( $file_or_url ) {
return new WP_Error( 'font_collection_json_missing', $message );
}

return $url ? $this->load_from_url( $url ) : $this->load_from_file( $file );
$data = $url ? $this->load_from_url( $url ) : $this->load_from_file( $file );

if ( is_wp_error( $data ) ) {
return $data;
}

$data = array(
'name' => $this->data['name'],
'font_families' => $data['font_families'],
);

if ( isset( $this->data['description'] ) ) {
$data['description'] = $this->data['description'];
}

if ( isset( $this->data['categories'] ) ) {
$data['categories'] = $this->data['categories'];
}

return $data;
}

/**
Expand All @@ -142,16 +161,6 @@ private function load_from_file( $file ) {
return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection JSON file contents.' ) );
}

$data['name'] = $this->data['name'];

if ( isset( $this->data['description'] ) ) {
$data['description'] = $this->data['description'];
}

if ( isset( $this->data['categories'] ) ) {
$data['categories'] = $this->data['categories'];
}

return $this->sanitize_and_validate_data( $data, array( 'font_families' ) );
}

Expand Down Expand Up @@ -196,16 +205,6 @@ private function load_from_url( $url ) {
set_site_transient( $transient_key, $data, DAY_IN_SECONDS );
}

$data['name'] = $this->data['name'];

if ( isset( $this->data['description'] ) ) {
$data['description'] = $this->data['description'];
}

if ( isset( $this->data['categories'] ) ) {
$data['categories'] = $this->data['categories'];
}

return $data;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public function test_should_get_data_from_json_file( $slug, $config, $expected_d

$collection = new WP_Font_Collection(
$slug,
array(
'name' => $config['name'],
'font_families' => $mock_file,
array_merge(
$config,
array( 'font_families' => $mock_file )
)
);
$data = $collection->get_data();

$this->assertSame( $slug, $collection->slug, 'The slug should match.' );
$this->assertSame( $expected_data, $data, 'The collection data should match.' );
$this->assertEqualSetsWithIndex( $expected_data, $data, 'The collection data should match.' );
}

/**
Expand All @@ -66,17 +66,19 @@ public function test_should_get_data_from_json_url( $slug, $config, $expected_da
self::$mock_collection_data = $config;
$collection = new WP_Font_Collection(
$slug,
array(
'name' => 'My Collection',
'font_families' => 'https://example.com/fonts/mock-font-collection.json',
array_merge(
$config,
array(
'font_families' => 'https://example.com/fonts/mock-font-collection.json',
)
)
);
$data = $collection->get_data();

remove_filter( 'pre_http_request', array( $this, 'mock_request' ) );

$this->assertSame( $slug, $collection->slug, 'The slug should match.' );
$this->assertSame( $expected_data, $data, 'The collection data should match.' );
$this->assertEqualSetsWithIndex( $expected_data, $data, 'The collection data should match.' );
}

/**
Expand Down

0 comments on commit 99e955e

Please sign in to comment.