Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/integrations/admin/removable-query-args-integration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Yoast\WP\SEO\Integrations\Admin;

use Yoast\WP\SEO\Conditionals\Admin_Conditional;
use Yoast\WP\SEO\Integrations\Integration_Interface;

/**
* Registers Yoast's admin-only transient query args with WordPress core so
* `wp_removable_query_args()` can strip them from the address bar after the
* request that introduced them.
*/
class Removable_Query_Args_Integration implements Integration_Interface {

/**
* The query args that should be stripped by core after the first request.
*
* @return string[]
*/
public static function get_removable_query_args(): array {
return [
'redirected_from_site_kit',
'wpseo_tracked_action',
'wpseo_tracking_nonce',
'start-myyoast-connection',
'_wpnonce',
'install',
'from_tools',
];
}

/**
* Returns the conditionals based in which this loadable should be active.
*
* @return array<string>
*/
public static function get_conditionals(): array {
return [ Admin_Conditional::class ];
}

/**
* Initializes the integration.
*
* @return void
*/
public function register_hooks() {
\add_filter( 'removable_query_args', [ $this, 'add_removable_query_args' ] );
}

/**
* Adds the Yoast transient query args to the list stripped by core.
*
* @param array<string> $args The existing removable query args.
* @return array<string>
*/
public static function add_removable_query_args( array $args ): array {
return \array_merge( $args, self::get_removable_query_args() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ public function test_register_cleanup_routine_no_running() {

Monkey\Functions\expect( 'wp_schedule_single_event' )
->once()
->with( ( \time() + \DAY_IN_SECONDS ), Cleanup_Integration::START_HOOK );
// Use a type matcher instead of a captured timestamp: \time() in source runs after mock setup,
// so a strict equality match flaked across second boundaries. The hook name is the meaningful contract.
->with( Mockery::type( 'int' ), Cleanup_Integration::START_HOOK );

$this->indexable_helper->expects( 'should_index_indexables' )
->once()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Yoast\WP\SEO\Tests\Unit\Integrations\Admin;

use Brain\Monkey;
use Yoast\WP\SEO\Conditionals\Admin_Conditional;
use Yoast\WP\SEO\Integrations\Admin\Removable_Query_Args_Integration;
use Yoast\WP\SEO\Tests\Unit\TestCase;

/**
* Class Removable_Query_Args_Integration_Test.
*
* @group integrations
*
* @coversDefaultClass \Yoast\WP\SEO\Integrations\Admin\Removable_Query_Args_Integration
*/
final class Removable_Query_Args_Integration_Test extends TestCase {

/**
* The instance under test.
*
* @var Removable_Query_Args_Integration
*/
protected $instance;

/**
* Sets up the test fixtures.
*
* @return void
*/
protected function set_up() {
parent::set_up();

$this->instance = new Removable_Query_Args_Integration();
}

/**
* Tests if the expected conditionals are given.
*
* @covers ::get_conditionals
*
* @return void
*/
public function test_get_conditionals() {
$this->assertEquals( [ Admin_Conditional::class ], Removable_Query_Args_Integration::get_conditionals() );
}

/**
* Tests that all seven Yoast transient query args are returned.
*
* @covers ::get_removable_query_args
*
* @return void
*/
public function test_get_removable_query_args() {
$this->assertEquals(
[
'redirected_from_site_kit',
'wpseo_tracked_action',
'wpseo_tracking_nonce',
'start-myyoast-connection',
'_wpnonce',
'install',
'from_tools',
],
Removable_Query_Args_Integration::get_removable_query_args(),
);
}

/**
* Tests that register_hooks registers the removable_query_args filter.
*
* @covers ::register_hooks
*
* @return void
*/
public function test_register_hooks() {
Monkey\Filters\expectAdded( 'removable_query_args' )
->with( [ $this->instance, 'add_removable_query_args' ] );

$this->instance->register_hooks();
}

/**
* Tests that the filter callback merges the Yoast args into the existing list.
*
* @covers ::add_removable_query_args
*
* @return void
*/
public function test_add_removable_query_args() {
$existing = [ 'existing_arg' ];
$result = Removable_Query_Args_Integration::add_removable_query_args( $existing );

$this->assertContains( 'existing_arg', $result );
$this->assertContains( 'redirected_from_site_kit', $result );
$this->assertContains( 'from_tools', $result );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,20 @@ public function test_enqueue_assets() {
->once()
->with( $user_id, '_yoast_wpseo_introductions', true )
->andReturn( [] );
$expected_meta = [
'foo' => [
'is_seen' => true,
'seen_on' => \time(),
],
];
$this->user_helper->expects( 'update_meta' )
->once()
->with( $user_id, '_yoast_wpseo_introductions', $expected_meta );
->with(
$user_id,
'_yoast_wpseo_introductions',
Mockery::on(
static function ( $meta ) {
return \is_array( $meta )
&& isset( $meta['foo'] )
&& $meta['foo']['is_seen'] === true
&& \is_int( $meta['foo']['seen_on'] );
},
),
);

// Enqueueing.
$this->admin_asset_manager->expects( 'enqueue_script' )->once()->with( 'introductions' );
Expand Down Expand Up @@ -315,15 +320,20 @@ public function test_update_first_user_introductions() {
->with( $user_id, '_yoast_wpseo_introductions', true )
// Point of this test: returning false results in using an empty array as default.
->andReturn( false );
$expected_meta = [
'foo' => [
'is_seen' => true,
'seen_on' => \time(),
],
];
$this->user_helper->expects( 'update_meta' )
->once()
->with( $user_id, '_yoast_wpseo_introductions', $expected_meta );
->with(
$user_id,
'_yoast_wpseo_introductions',
Mockery::on(
static function ( $meta ) {
return \is_array( $meta )
&& isset( $meta['foo'] )
&& $meta['foo']['is_seen'] === true
&& \is_int( $meta['foo']['seen_on'] );
},
),
);

// Enqueueing.
$this->admin_asset_manager->expects( 'enqueue_script' )->once()->with( 'introductions' );
Expand All @@ -337,8 +347,8 @@ public function test_update_first_user_introductions() {
/**
* Sets the expectations surrounding the localized data.
*
* @param array $introductions The introductions.
* @param int $user_id The user ID.
* @param array<string, string|int>[] $introductions The introductions.
* @param int $user_id The user ID.
*
* @return void
*/
Expand Down
Loading