CYS > Register PTK testimonial patterns as reviews (#48674)

* Register PTK testimonial patterns as reviews

* Add changefile(s) from automation for the following project(s): woocommerce

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-06-21 08:12:50 +00:00 committed by GitHub
parent 52d4c7bb84
commit 4424860d03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
CYS - Register PTK "Testimonials" patterns as "Reviews"

View File

@ -16,6 +16,10 @@ class PTKPatternsStore {
// would see an error when trying to insert them in the editor.
const EXCLUDED_PATTERNS = array( '13923', '14781', '14779', '13666', '13664', '13660', '13588', '14922', '14880', '13596', '13967', '13958', '15050', '15027' );
const CATEGORY_MAPPING = array(
'testimonials' => 'reviews',
);
/**
* PatternsToolkit instance.
*
@ -216,6 +220,7 @@ class PTKPatternsStore {
$patterns = array_merge( $dotcom_patterns, $woo_patterns );
$patterns = $this->filter_patterns( $patterns, self::EXCLUDED_PATTERNS );
$patterns = $this->map_categories( $patterns );
set_transient( self::TRANSIENT_NAME, $patterns );
}
@ -228,4 +233,30 @@ class PTKPatternsStore {
private function allowed_tracking_is_enabled(): bool {
return 'yes' === get_option( 'woocommerce_allow_tracking' );
}
/**
* Change the categories of the patterns to match the ones used in the CYS flow
*
* @param array $patterns The patterns to map categories for.
* @return array The patterns with the categories mapped.
*/
private function map_categories( array $patterns ) {
return array_map(
function ( $pattern ) {
if ( isset( $pattern['categories'] ) ) {
foreach ( $pattern['categories'] as $key => $category ) {
if ( isset( $category['slug'] ) && isset( self::CATEGORY_MAPPING[ $key ] ) ) {
$new_category = self::CATEGORY_MAPPING[ $key ];
unset( $pattern['categories'][ $key ] );
$pattern['categories'][ $new_category ]['slug'] = $new_category;
$pattern['categories'][ $new_category ]['title'] = ucfirst( $new_category );
}
}
}
return $pattern;
},
$patterns
);
}
}

View File

@ -201,6 +201,50 @@ class PTKPatternsStoreTest extends \WP_UnitTestCase {
$this->assertEquals( $expected_patterns, get_transient( PTKPatternsStore::TRANSIENT_NAME ) );
}
/**
* Test fetch_patterns should register testimonials category as reviews.
*/
public function test_fetch_patterns_should_register_testimonials_category_as_reviews() {
update_option( 'woocommerce_allow_tracking', 'yes' );
$ptk_patterns = array(
array(
'title' => 'My pattern',
'slug' => 'my-pattern',
'categories' => array(
'testimonials' => array(
'slug' => 'testimonials',
'title' => 'Testimonials',
),
),
),
);
$expected_patterns = array(
array(
'title' => 'My pattern',
'slug' => 'my-pattern',
'categories' => array(
'reviews' => array(
'slug' => 'reviews',
'title' => 'Reviews',
),
),
),
);
$this->ptk_client
->expects( $this->once() )
->method( 'fetch_patterns' )
->willReturn( $ptk_patterns );
$this->pattern_store->fetch_patterns();
$patterns = get_transient( PTKPatternsStore::TRANSIENT_NAME );
$this->assertEquals( $expected_patterns, $patterns );
$this->assertEquals( $expected_patterns, get_transient( PTKPatternsStore::TRANSIENT_NAME ) );
}
/**
* Asserts that the response is an error with the expected error code and message.
*