Skip to content

Commit

Permalink
Clear out cron validation queue whenever validated environment changes
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Oct 15, 2021
1 parent f0c7b3b commit 55c0ea7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Validation/URLValidationCron.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use AmpProject\AmpWP\BackgroundTask\BackgroundTaskDeactivator;
use AmpProject\AmpWP\BackgroundTask\RecurringBackgroundTask;
use AMP_Validated_URL_Post_Type;
use AMP_Validation_Manager;

/**
Expand Down Expand Up @@ -77,14 +78,24 @@ protected function dequeue() {
if ( ! is_array( $data ) ) {
$data = [];
}

$data = array_merge(
[
'timestamp' => 0,
'urls' => [],
'timestamp' => 0,
'env' => [],
],
$data
);

$current_env = AMP_Validated_URL_Post_Type::get_validated_environment();

// When the validated environment changes, make sure the URLs and timestamp are reset so that new URLs are obtained.
if ( $data['timestamp'] && $data['env'] !== $current_env ) {
$data['urls'] = [];
$data['timestamp'] = 0;
}

// If there are no URLs queued, then obtain a new set.
if ( empty( $data['urls'] ) ) {

Expand All @@ -93,14 +104,14 @@ protected function dequeue() {
return null;
}

// @todo The URLs should be contextual based on the selected template mode, in particular only singular URLs should be included if using legacy Reader mode.
$data['urls'] = wp_list_pluck( $this->scannable_url_provider->get_urls(), 'url' );
$data['timestamp'] = time();
}

// If there is not a queued URL, then enqueue a new set of URLs.
$url = array_shift( $data['urls'] );

$data['env'] = $current_env;
update_option( self::OPTION_KEY, $data );

return $url ?: null;
Expand Down
9 changes: 9 additions & 0 deletions tests/php/src/Validation/URLValidationCronTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace AmpProject\AmpWP\Tests\Validation;

use AMP_Options_Manager;
use AMP_Theme_Support;
use AmpProject\AmpWP\BackgroundTask\CronBasedBackgroundTask;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;
Expand Down Expand Up @@ -142,6 +144,8 @@ public function test_schedule_event_with_different_recurrence() {
* @covers ::dequeue()
*/
public function test_process_and_dequeue() {
AMP_Options_Manager::update_option( Option::THEME_SUPPORT, AMP_Theme_Support::STANDARD_MODE_SLUG );

/** @var ScannableURLProvider $scannable_url_provider */
$scannable_url_provider = $this->get_private_property( $this->test_instance, 'scannable_url_provider' );

Expand Down Expand Up @@ -182,6 +186,11 @@ public function test_process_and_dequeue() {
$this->assertEquals( $before_request_count + 1, $this->request_count );
$data = get_option( URLValidationCron::OPTION_KEY );
$this->assertCount( $initial_url_count - 1, $data['urls'] );

// Now test once the validated environment has changed, that the URLs are re-queued.
AMP_Options_Manager::update_option( Option::THEME_SUPPORT, AMP_Theme_Support::TRANSITIONAL_MODE_SLUG );
$this->test_instance->process();
$this->assertCount( $initial_url_count - 1, $data['urls'] );
}

/** @covers ::get_event_name() */
Expand Down

0 comments on commit 55c0ea7

Please sign in to comment.