Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Fix tests and start using the Options constants
Browse files Browse the repository at this point in the history
  • Loading branch information
albarin committed Jun 17, 2022
1 parent e7f78bc commit e4c463c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 96 deletions.
16 changes: 8 additions & 8 deletions src/BlockTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,25 @@ protected function init() {
add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
add_filter( 'get_block_templates', array( $this, 'add_block_templates' ), 10, 3 );
add_filter( 'current_theme_supports-block-templates', array( $this, 'remove_block_template_support_for_shop_page' ) );
add_action( 'after_switch_theme', array( $this, 'check_should_use_blockified_templates' ), 10, 2 );
add_action( 'after_switch_theme', array( $this, 'check_should_use_blockified_product_grid_templates' ), 10, 2 );
}

/**
* Checks whether the new theme is a block one and the old is not, the "wc_blocks_use_blockified_templates" flag is
* set to true to start using the new blockified templates.
* Checks the old and current themes and determines if the "wc_blocks_use_blockified_product_grid_block_as_template"
* option need to be updated accordingly.
*
* @param string $old_name Old theme name.
* @param \WP_Theme $old_theme Instance of the old theme.
* @return void
*/
public function check_should_use_blockified_templates( $old_name, $old_theme ) {
public function check_should_use_blockified_product_grid_templates( $old_name, $old_theme ) {
if ( ! wc_current_theme_is_fse_theme() ) {
update_option( 'wc_blocks_use_blockified_templates', wc_bool_to_string( false ) );
update_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE, wc_bool_to_string( false ) );
return;
}

if ( ! $old_theme->is_block_theme() && wc_current_theme_is_fse_theme() ) {
update_option( 'wc_blocks_use_blockified_templates', wc_bool_to_string( true ) );
update_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE, wc_bool_to_string( true ) );
return;
}
}
Expand Down Expand Up @@ -260,7 +260,7 @@ public function get_block_templates_from_woocommerce( $slugs, $already_found_tem
$templates = array();

foreach ( $template_files as $template_file ) {
if ( ! BlockTemplateUtils::should_use_blockified_templates() && strpos( $template_file, 'blockified' ) !== false ) {
if ( ! BlockTemplateUtils::should_use_blockified_product_grid_templates() && strpos( $template_file, 'blockified' ) !== false ) {
continue;
}

Expand Down Expand Up @@ -330,7 +330,7 @@ protected function get_templates_directory( $template_type = 'wp_template' ) {
return $this->template_parts_directory;
}

if ( BlockTemplateUtils::should_use_blockified_templates() ) {
if ( BlockTemplateUtils::should_use_blockified_product_grid_templates() ) {
return $this->templates_directory . '/blockified';
}

Expand Down
4 changes: 0 additions & 4 deletions src/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,11 @@ public static function run_migrations() {
}
}


/**
* Set a flag to indicate if the blockified Product Grid Block should be rendered by default.
*/
public static function wc_blocks_update_710_blockified_product_grid_block() {
$use_blockified_templates = wc_current_theme_is_fse_theme() ? false : true;
update_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE, wc_bool_to_string( $use_blockified_templates ) );

}


}
8 changes: 4 additions & 4 deletions src/Utils/BlockTemplateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Automattic\WooCommerce\Blocks\Utils;

use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
use Automattic\WooCommerce\Blocks\Options;

/**
* Utility methods used for serving block templates from WooCommerce Blocks.
Expand Down Expand Up @@ -518,13 +519,12 @@ function( $template ) use ( $customised_template_slugs ) {
}

/**
* Returns whether the blockified templates should be used or not, depending on the
* "wc_blocks_use_blockified_templates" option.
* Returns whether the blockified templates should be used or not.
* Returns "true" ONLY if the option is present on the db and its value is "yes".
*
* @return boolean
*/
public static function should_use_blockified_templates() {
return wc_string_to_bool( get_option( 'wc_blocks_use_blockified_templates', 'no' ) );
public static function should_use_blockified_product_grid_templates() {
return wc_string_to_bool( get_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE, 'no' ) );
}
}
64 changes: 64 additions & 0 deletions tests/php/BlockifiedProductGridTemplatesOptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Automattic\WooCommerce\Blocks\Tests;

use Automattic\WooCommerce\Blocks\Migration;
use Automattic\WooCommerce\Blocks\Options;

class BlockifiedProductGridTemplatesOptionTest extends \WP_UnitTestCase {
public function setUp() {
parent::setUp();
delete_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE );
delete_option( Options::WC_BLOCK_VERSION );
}

public function test_switching_to_a_classic_theme_should_set_the_option_to_false() {
switch_theme( 'storefront' );
check_theme_switched();

$this->assertFalse( $this->get_use_blockified_product_grid_templates_option() );
}

public function test_switching_from_a_block_to_a_block_theme_should_set_the_option_to_false() {
switch_theme( 'twentytwentytwo' );

switch_theme( 'twentytwentytwo' );
check_theme_switched();

$this->assertFalse( $this->get_use_blockified_product_grid_templates_option() );
}

public function test_switching_from_a_classic_to_a_block_theme_should_set_the_option_to_true() {
switch_theme( 'twentytwentytwo' );
check_theme_switched();

$this->assertTrue( $this->get_use_blockified_product_grid_templates_option() );
}

public function test_running_the_migration_with_a_block_theme_should_set_the_option_to_false() {
switch_theme( 'twentytwentytwo' );

update_option( Options::WC_BLOCK_VERSION, 1 );
Migration::run_migrations();

$this->assertFalse( $this->get_use_blockified_product_grid_templates_option() );
}

public function test_running_the_migration_with_a_classic_theme_should_set_the_option_to_true() {
switch_theme( 'storefront' );

update_option( Options::WC_BLOCK_VERSION, 1 );
Migration::run_migrations();

$this->assertTrue( $this->get_use_blockified_product_grid_templates_option() );
}

/**
* Returns the value of the option.
*
* @return boolean
*/
private function get_use_blockified_product_grid_templates_option() {
return wc_string_to_bool( get_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE ) );
}
}
80 changes: 0 additions & 80 deletions tests/php/BlockifiedTemplatesOptionTest.php

This file was deleted.

0 comments on commit e4c463c

Please sign in to comment.