now tests are green again

This commit is contained in:
Matthias Pfefferle 2023-06-16 11:40:26 +02:00
parent 46f376e05e
commit 793214cea2
4 changed files with 44 additions and 18 deletions

View file

@ -6,11 +6,19 @@ services:
environment: environment:
MYSQL_DATABASE: activitypub-test MYSQL_DATABASE: activitypub-test
MYSQL_ROOT_PASSWORD: activitypub-test MYSQL_ROOT_PASSWORD: activitypub-test
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3306"]
interval: 5s
timeout: 2s
retries: 5
test-php: test-php:
build: build:
context: . context: .
dockerfile: Dockerfile dockerfile: Dockerfile
depends_on:
test-db:
condition: service_healthy
links: links:
- test-db - test-db
volumes: volumes:

View file

@ -217,7 +217,7 @@ class Followers {
$follower->from_meta( $meta ); $follower->from_meta( $meta );
$follower->upsert(); $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' ); wp_cache_delete( sprintf( self::CACHE_KEY_INBOXES, $user_id ), 'activitypub' );
@ -339,14 +339,13 @@ class Followers {
/** /**
* Get all 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. * @return array The Term list of Followers.
*/ */
public static function get_all_followers( $user_id = null ) { public static function get_all_followers( $user_id = null ) {
$args = array( $args = array(
'author' => null, 'meta_query' => array(),
'nopaging' => true,
); );
return self::get_followers( $user_id, null, null, $args ); return self::get_followers( $user_id, null, null, $args );
} }
@ -358,10 +357,10 @@ class Followers {
* *
* @return int The number of 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? // todo: rethink this. Don't we already get a total_posts count out of WP_Query?
// in the absence of that: caching. // 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; global $wpdb;
$results = $wpdb->get_col( $results = $wpdb->get_col(
$wpdb->prepare( $wpdb->prepare(
"SELECT DISTINCT meta_value FROM {$wpdb->posts} "SELECT DISTINCT meta_value FROM {$wpdb->postmeta}
WHERE term_id IN (" . implode( ', ', array_fill( 0, count( $terms ), '%d' ) ) . ") WHERE post_id IN (" . implode( ', ', array_fill( 0, count( $posts ), '%d' ) ) . ")
AND meta_key = 'shared_inbox' AND meta_key = 'shared_inbox'
AND meta_value IS NOT NULL", AND meta_value IS NOT NULL",
$terms $posts
) )
); );
@ -435,10 +434,11 @@ class Followers {
'posts_per_page' => $number, 'posts_per_page' => $number,
'orderby' => 'modified', 'orderby' => 'modified',
'order' => 'DESC', 'order' => 'DESC',
'post_status' => 'any', // 'any' includes 'trash
'date_query' => array( 'date_query' => array(
array( array(
'column' => 'post_modified_gmt', 'column' => 'post_modified_gmt',
'before' => 604800, 'before' => gmdate( 'Y-m-d', \time() - $older_than ),
), ),
), ),
); );

View file

@ -367,10 +367,7 @@ class Follower {
* @return void * @return void
*/ */
public function update() { public function update() {
if ( ! $this->updated_at ) {
$this->updated_at = \time(); $this->updated_at = \time();
}
$this->save(); $this->save();
} }
@ -387,6 +384,7 @@ class Follower {
'post_author' => 0, 'post_author' => 0,
'post_type' => Followers::POST_TYPE, 'post_type' => Followers::POST_TYPE,
'post_content' => wp_json_encode( $this->meta ), 'post_content' => wp_json_encode( $this->meta ),
'post_status' => 'publish',
'post_modified' => gmdate( 'Y-m-d H:i:s', $this->updated_at ), 'post_modified' => gmdate( 'Y-m-d H:i:s', $this->updated_at ),
'meta_input' => $this->get_post_meta_input(), 'meta_input' => $this->get_post_meta_input(),
); );

View file

@ -65,7 +65,7 @@ class Test_Db_Activitypub_Followers extends WP_UnitTestCase {
$db_followers $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() { 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 = new \Activitypub\Model\Follower( 'https://example.com/author/jon' );
$follower->set_updates_at( \time() - 804800 ); global $wpdb;
$follower->update();
//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(); $followers = \Activitypub\Collection\Followers::get_outdated_followers();
$this->assertEquals( 1, count( $followers ) ); $this->assertEquals( 1, count( $followers ) );