From 793214cea2a438805db0e28653d8b2c0c5eff131 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Fri, 16 Jun 2023 11:40:26 +0200 Subject: [PATCH] now tests are green again --- docker-compose-test.yml | 8 ++++++ includes/collection/class-followers.php | 22 ++++++++-------- includes/model/class-follower.php | 6 ++--- tests/test-class-db-activitypub-followers.php | 26 ++++++++++++++++--- 4 files changed, 44 insertions(+), 18 deletions(-) diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 4c69e44c..411f3fdd 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -6,11 +6,19 @@ services: environment: MYSQL_DATABASE: activitypub-test MYSQL_ROOT_PASSWORD: activitypub-test + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:3306"] + interval: 5s + timeout: 2s + retries: 5 test-php: build: context: . dockerfile: Dockerfile + depends_on: + test-db: + condition: service_healthy links: - test-db volumes: diff --git a/includes/collection/class-followers.php b/includes/collection/class-followers.php index 601d1291..ab5be32a 100644 --- a/includes/collection/class-followers.php +++ b/includes/collection/class-followers.php @@ -217,7 +217,7 @@ class Followers { $follower->from_meta( $meta ); $follower->upsert(); - add_post_meta( $follower->get_id(), 'user_id', $user_id ); + update_post_meta( $follower->get_id(), 'user_id', $user_id, $user_id ); wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' ); @@ -339,14 +339,13 @@ class Followers { /** * Get all Followers * - * @param array $args The WP_Term_Query arguments. + * @param array $args The WP_Query arguments. * * @return array The Term list of Followers. */ public static function get_all_followers( $user_id = null ) { $args = array( - 'author' => null, - 'nopaging' => true, + 'meta_query' => array(), ); return self::get_followers( $user_id, null, null, $args ); } @@ -358,10 +357,10 @@ class Followers { * * @return int The number of Followers */ - public static function count_followers( $user_id = null ) { + public static function count_followers( $user_id ) { // todo: rethink this. Don't we already get a total_posts count out of WP_Query? // in the absence of that: caching. - return count( self::get_all_followers( $user_id ) ); + return count( self::get_followers( $user_id ) ); } /** @@ -406,11 +405,11 @@ class Followers { global $wpdb; $results = $wpdb->get_col( $wpdb->prepare( - "SELECT DISTINCT meta_value FROM {$wpdb->posts} - WHERE term_id IN (" . implode( ', ', array_fill( 0, count( $terms ), '%d' ) ) . ") + "SELECT DISTINCT meta_value FROM {$wpdb->postmeta} + WHERE post_id IN (" . implode( ', ', array_fill( 0, count( $posts ), '%d' ) ) . ") AND meta_key = 'shared_inbox' AND meta_value IS NOT NULL", - $terms + $posts ) ); @@ -435,10 +434,11 @@ class Followers { 'posts_per_page' => $number, 'orderby' => 'modified', 'order' => 'DESC', - 'date_query' => array( + 'post_status' => 'any', // 'any' includes 'trash + 'date_query' => array( array( 'column' => 'post_modified_gmt', - 'before' => 604800, + 'before' => gmdate( 'Y-m-d', \time() - $older_than ), ), ), ); diff --git a/includes/model/class-follower.php b/includes/model/class-follower.php index 7b41bb0c..0a6a6ef6 100644 --- a/includes/model/class-follower.php +++ b/includes/model/class-follower.php @@ -367,10 +367,7 @@ class Follower { * @return void */ public function update() { - if ( ! $this->updated_at ) { - $this->updated_at = \time(); - } - + $this->updated_at = \time(); $this->save(); } @@ -387,6 +384,7 @@ class Follower { 'post_author' => 0, 'post_type' => Followers::POST_TYPE, 'post_content' => wp_json_encode( $this->meta ), + 'post_status' => 'publish', 'post_modified' => gmdate( 'Y-m-d H:i:s', $this->updated_at ), 'meta_input' => $this->get_post_meta_input(), ); diff --git a/tests/test-class-db-activitypub-followers.php b/tests/test-class-db-activitypub-followers.php index e660c009..87e00641 100644 --- a/tests/test-class-db-activitypub-followers.php +++ b/tests/test-class-db-activitypub-followers.php @@ -65,7 +65,7 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase { $db_followers ); - $this->assertSame( array( 'https://example.com/author/jon', 'https://example.org/author/doe', 'http://sally.example.org' ), $db_followers ); + $this->assertEquals( array( 'http://sally.example.org', 'https://example.org/author/doe', 'https://example.com/author/jon' ), $db_followers ); } public function test_add_follower() { @@ -109,8 +109,28 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase { $follower = new \Activitypub\Model\Follower( 'https://example.com/author/jon' ); - $follower->set_updates_at( \time() - 804800 ); - $follower->update(); + global $wpdb; + + //eg. time one year ago.. + $time = time() - 804800; + $mysql_time_format = 'Y-m-d H:i:s'; + + $post_modified = gmdate( $mysql_time_format, $time ); + $post_modified_gmt = gmdate( $mysql_time_format, ( $time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ); + $post_id = $follower->get_id(); + + $wpdb->query( + $wpdb->prepare( + "UPDATE $wpdb->posts SET post_modified = %s, post_modified_gmt = %s WHERE ID = %s", + array( + $post_modified, + $post_modified_gmt, + $post_id, + ) + ) + ); + + clean_post_cache( $post_id ); $followers = \Activitypub\Collection\Followers::get_outdated_followers(); $this->assertEquals( 1, count( $followers ) );