Skip to content

Commit

Permalink
Add unit test and integration test to safeguard caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
thijsoo committed Mar 13, 2024
1 parent 2142717 commit 43ffa8f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 35 deletions.
8 changes: 5 additions & 3 deletions src/helpers/image-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,10 +333,12 @@ public function get_attachment_by_url( $url, $use_link_table = true ) {
if ( ! $use_link_table ) {
return WPSEO_Image_Utils::get_attachment_by_url( $url );
}
$cache_key = 'attachment_seo_link_object_' . $url;
$link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link' );
$cache_key = 'attachment_seo_link_object_' . \md5( $url );

if ( $link === false ) {
$found = false;
$link = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link', false, $found );

if ( $found === false ) {
$link = $this->seo_links_repository->find_one_by_url( $url );
\wp_cache_set( $cache_key, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS );
}
Expand Down
35 changes: 3 additions & 32 deletions tests/Unit/Helpers/Image_Helper_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,10 @@ public function test_get_attachment_by_url_with_existing_link() {
]
);

$url = \md5( 'a_dir/something' );
Monkey\Functions\expect( 'wp_cache_get' )
->once()
->with( 'attachment_seo_link_object_a_dir/something', 'yoast-seo-attachment-link' )
->with( 'attachment_seo_link_object_' . $url, 'yoast-seo-attachment-link', false, false )
->andReturn( false );

$link = new SEO_Links_Mock();
Expand All @@ -617,37 +618,7 @@ public function test_get_attachment_by_url_with_existing_link() {

Monkey\Functions\expect( 'wp_cache_set' )
->once()
->with( 'attachment_seo_link_object_a_dir/something', $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS );
$this->assertEquals( 17, $this->actual_instance->get_attachment_by_url( 'a_dir/something' ) );
}

/**
* Tests the get_attachment_by_url function without using the SEO links table. Because the query result is cached
*
* @covers ::get_attachment_by_url
* @return void
*/
public function test_get_attachment_by_url_with_existing_link_and_cache() {
Monkey\Functions\expect( 'wp_parse_url' )
->once()
->with( 'a_dir/something' )
->andReturn(
[
'scheme' => 'https',
'host' => 'example.com',
]
);

$link = new SEO_Links_Mock();
$link->target_post_id = 17;
$this->url_helper->expects( 'get_link_type' )->andReturn( SEO_Links::TYPE_INTERNAL );
$this->options_helper->expects( 'get' )->with( 'disable-attachment' )->andReturn( true );
$this->indexable_seo_links_repository->expects( 'find_one_by_url' )->never();
Monkey\Functions\expect( 'wp_cache_get' )
->once()
->with( 'attachment_seo_link_object_a_dir/something', 'yoast-seo-attachment-link' )
->andReturn( $link );

->with( 'attachment_seo_link_object_' . $url, $link, 'yoast-seo-attachment-link', \MINUTE_IN_SECONDS );
$this->assertEquals( 17, $this->actual_instance->get_attachment_by_url( 'a_dir/something' ) );
}
}
68 changes: 68 additions & 0 deletions tests/WP/Helpers/Get_Attachment_By_Url_Image_Helper_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Yoast\WP\SEO\Tests\WP\Helpers;

use Yoast\WP\SEO\Helpers\Image_Helper;
use Yoast\WP\SEO\Repositories\Indexable_Repository;
use Yoast\WP\SEO\Repositories\SEO_Links_Repository;
use Yoast\WP\SEO\Tests\WP\TestCase;

/**
* Integration Test Class for the Image_Helper class.
*
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded -- Needed to show specific method being tested in this file.
*
* @coversDefaultClass \Yoast\WP\SEO\Helpers\Image_Helper
*/
final class Get_Attachment_By_Url_Image_Helper_Test extends TestCase {

/**
* The instance.
*
* @var Image_Helper
*/
private $instance;

/**
* Sets up the test class.
*
* @return void
*/
public function set_up(): void {
parent::set_up();
$this->instance = new Image_Helper( \YoastSEO()->classes->get( Indexable_Repository::class ), \YoastSEO()->classes->get( SEO_Links_Repository::class ), \YoastSEO()->helpers->options, \YoastSEO()->helpers->url );
\YoastSEO()->helpers->options->set( 'disable-attachment', true );

global $wpdb;

$wpdb->insert(
$wpdb->prefix . 'yoast_seo_links',
[
'url' => 'a_dir/something',
'type' => 'internal',
'indexable_id' => '101',
'post_id' => '110',
'target_post_id' => '112',
'target_indexable_id' => '344',
'id' => '113',
]
);
}

/**
* Tests if the query cached is filled when `get_attachment_by_url` is called multiple times.
*
* @covers ::get_attachment_by_url
* @return void
*/
public function test_get_attachment_by_url_with_existing_link_and_cache() {
$url = 'a_dir/something';
$cache_key = 'attachment_seo_link_object_' . \md5( $url );
$link = $this->instance->get_attachment_by_url( $url );

$link_cache = \wp_cache_get( $cache_key, 'yoast-seo-attachment-link' );
$this->assertSame( $link, $link_cache->target_post_id );

$this->assertSame( 112, $this->instance->get_attachment_by_url( 'a_dir/something' ) );
}
}

0 comments on commit 43ffa8f

Please sign in to comment.