Skip to content

Commit

Permalink
Linty McGinty
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Oct 22, 2024
1 parent 0e5cda8 commit 2ae5273
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function register_routes() {
$this->namespace,
'/' . $this->rest_base . '/(?P<post_type>[\w-]+)',
array(
'args' => array(
'args' => array(
'post_type' => array(
'description' => __( 'An alphanumeric identifier for the post type.' ),
'type' => 'string',
Expand Down Expand Up @@ -96,7 +96,7 @@ public function get_item_permissions_check( $request ) {
*/
public function get_item( $request ) {
$post_type = $request['post_type'];
$counts = wp_count_posts( $post_type );
$counts = wp_count_posts( $post_type );

if ( ! $counts ) {
return new WP_Error(
Expand All @@ -120,7 +120,7 @@ public function get_item( $request ) {
* @return WP_REST_Response Response object.
*/
public function prepare_item_for_response( $item, $request ) {
$data = array();
$data = array();
$fields = $this->get_fields_for_response( $request );

foreach ( $fields as $field ) {
Expand All @@ -130,8 +130,8 @@ public function prepare_item_for_response( $item, $request ) {
}

$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );
$data = $this->add_additional_fields_to_object( $data, $request );
$data = $this->filter_response_by_context( $data, $context );

$response = rest_ensure_response( $data );

Expand Down
24 changes: 13 additions & 11 deletions phpunit/class-gutenberg-rest-post-counts-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Gutenberg_Test_REST_Post_Counts_Controller extends WP_Test_REST_Controller
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$admin_id = $factory->user->create(
array(
'role' => 'administrator',
'role' => 'administrator',
)
);

Expand Down Expand Up @@ -74,9 +74,9 @@ public function test_context_param() {
* @covers Gutenberg_REST_Post_Counts_Controller::et_item_schema
*/
public function test_get_item_schema() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/counts/post' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/counts/post' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];

$this->assertCount( 7, $properties );
Expand All @@ -94,9 +94,9 @@ public function test_get_item_schema() {
*/
public function test_get_item_response() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/counts/post' );
$request = new WP_REST_Request( 'GET', '/wp/v2/counts/post' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$data = $response->get_data();

$this->assertSame( 200, $response->get_status() );
$this->assertArrayHasKey( 'publish', $data );
Expand All @@ -115,18 +115,20 @@ public function test_get_item() {
wp_set_current_user( self::$admin_id );

$published = self::factory()->post->create( array( 'post_status' => 'publish' ) );
$future = self::factory()->post->create( array(
'post_status' => 'future',
'post_date' => date('Y-m-d H:i:s', strtotime('+1 day'))
) );
$future = self::factory()->post->create(
array(
'post_status' => 'future',
'post_date' => date( 'Y-m-d H:i:s', strtotime( '+1 day' ) ),

Check failure on line 121 in phpunit/class-gutenberg-rest-post-counts-controller-test.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

date() is affected by runtime timezone changes which can cause date/time to be incorrectly displayed. Use gmdate() instead.
)
);
$draft = self::factory()->post->create( array( 'post_status' => 'draft' ) );
$pending = self::factory()->post->create( array( 'post_status' => 'pending' ) );
$private = self::factory()->post->create( array( 'post_status' => 'private' ) );
$trashed = self::factory()->post->create( array( 'post_status' => 'trash' ) );

$request = new WP_REST_Request( 'GET', '/wp/v2/counts/post' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$data = $response->get_data();

$this->assertSame( 1, $data['publish'], 'Published post count mismatch.' );
$this->assertSame( 1, $data['future'], 'Future post count mismatch.' );
Expand Down

0 comments on commit 2ae5273

Please sign in to comment.