woocommerce/plugins/woocommerce-blocks/tests/php/Utils/BlockTemplateUtilsTest.php

359 lines
12 KiB
PHP
Raw Normal View History

Add migration strategy and load blockified templates (https://github.com/woocommerce/woocommerce-blocks/pull/6538) * Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag If the schema version stored on the db is <= 260 means the plugin is being updated to the new version and we should keep using the old templates, not the blockified ones. * After the theme is switched we check if we need to update the flag to start loading the blockified templates or not. * Get the templates from the blockified folder if the flag is set to true on the db * Add temporary blockified template for testing purposes * Inline variable * Improve comment * Use blockified templates on new installs with block themes only * Don't use blockified templates when switching to a non FSE theme * Fix condition * Add tests for the option value * Move the check to use blockified templates * WIP * WIP * Add migration strategy * Move the blockified templates to the templates folder and filter them depending on the option * Fix tests and start using the Options constants * Fix migration, the `should_use_blockified_product_grid_templates` and tests * Rename tests and move to Utils folder * add Migration class test * try * remove PHP * add composer * Replace the blockified templates with a temporary version * Fix tests * add comment * Add feature gating for experimental builds * Inject the package to the controller * test * Change blocks.ini * debug * Remove debug info * fix test * fix tests * try now * using composer cache * install deps * test * Remove unnecessary extra key * Add cache actions * Undo tests change * Fix readme format Co-authored-by: Luigi <gigitux@gmail.com>
2022-07-06 07:51:39 +00:00
<?php
namespace Automattic\WooCommerce\Blocks\Tests\Utils;
use Automattic\WooCommerce\Blocks\Migration;
use Automattic\WooCommerce\Blocks\Options;
use Automattic\WooCommerce\Blocks\Utils\BlockTemplateUtils;
use WP_UnitTestCase;
Add migration strategy and load blockified templates (https://github.com/woocommerce/woocommerce-blocks/pull/6538) * Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag If the schema version stored on the db is <= 260 means the plugin is being updated to the new version and we should keep using the old templates, not the blockified ones. * After the theme is switched we check if we need to update the flag to start loading the blockified templates or not. * Get the templates from the blockified folder if the flag is set to true on the db * Add temporary blockified template for testing purposes * Inline variable * Improve comment * Use blockified templates on new installs with block themes only * Don't use blockified templates when switching to a non FSE theme * Fix condition * Add tests for the option value * Move the check to use blockified templates * WIP * WIP * Add migration strategy * Move the blockified templates to the templates folder and filter them depending on the option * Fix tests and start using the Options constants * Fix migration, the `should_use_blockified_product_grid_templates` and tests * Rename tests and move to Utils folder * add Migration class test * try * remove PHP * add composer * Replace the blockified templates with a temporary version * Fix tests * add comment * Add feature gating for experimental builds * Inject the package to the controller * test * Change blocks.ini * debug * Remove debug info * fix test * fix tests * try now * using composer cache * install deps * test * Remove unnecessary extra key * Add cache actions * Undo tests change * Fix readme format Co-authored-by: Luigi <gigitux@gmail.com>
2022-07-06 07:51:39 +00:00
/**
* Tests for the BlockTemplateUtils class.
*/
class BlockTemplateUtilsTest extends WP_UnitTestCase {
/**
* Setup test environment.
*/
Add PHP8 Unit Testing (https://github.com/woocommerce/woocommerce-blocks/pull/7528) * fixed method sig * Updated to @wordpress/env@5.5.0 and set default PHP 7.4 for wp-env. * updated Coding Standards flow to use PHP 8.0 * Added comment to E2E flows explaining what PHP version is used * Revert "Updated to @wordpress/env@5.5.0 and set default PHP 7.4 for wp-env." This reverts commit 696cd7f42edc9d9726b777cf4f83a501a6d63936. * Added comment to Unit test flows explaining what PHP version is used. Specified PHP version on .wp-env.json * Fixed composer-lock.json version. * Updated tests to run on PHP Unit 9.2.6 * Updated tests to run on PHP 8 * Reverted test, mismatched results between local and pipeline * Removed Todo * Updated platform overrides * Update Migrationb tests with Mockery for PHP8 compat * try at PHP unit flow matrix * Fix blocks.ini invalid config * Temp disable E2E * Downgraded woocommerce/woocommerce-sniffs as it introduced new sniffs we should be handling on a different PR * re-enable E2E tests * blocks.ini fix * revert blocks.ini fix * Update @wordpress/env * remove .htaccess mapping * Fix permissions for tests * Debug permissions * Attempt at perm fix * Attempt at perm fix * Downgraded @wordpress/env * Another attempt at upgrade @wordpress/env * Attempt at cleaning wp-env before run * Attempt at destroying wp-env before run. Disabled E2E. * Attempt at destroying wp-env before run. * debug wp-env data * attempt at deleting wp-env data (destroy won't work due to prompt) * re-enable E2E * Fix deprecation warnings * Cleaned wp-env data for E2E * Fix perms for E2E * Updated RateLimitsTests * debug * Force 7.4 for wp-env * Run sh outside of npm * Reverted E2E flow * reverted wp-env-config.sh debug test * reverted .wp-env.json phpVersion force * Update tests/php/StoreApi/Utilities/ProductQueryFilters.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartExtensions.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartItems.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Products.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/ProductCollectionData.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Batch.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Checkout.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartCoupons.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/ProductAttributes.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Cart.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * downgraded @wordpress/env to v4 * Reverted back to reflection class for pivate attribs manipulation on tests * reverted JS unit testing job name * Update tests/php/StoreApi/Formatters/TestMoneyFormatter.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Typo fix Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-11-09 15:28:08 +00:00
protected function setUp(): void {
Add migration strategy and load blockified templates (https://github.com/woocommerce/woocommerce-blocks/pull/6538) * Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag If the schema version stored on the db is <= 260 means the plugin is being updated to the new version and we should keep using the old templates, not the blockified ones. * After the theme is switched we check if we need to update the flag to start loading the blockified templates or not. * Get the templates from the blockified folder if the flag is set to true on the db * Add temporary blockified template for testing purposes * Inline variable * Improve comment * Use blockified templates on new installs with block themes only * Don't use blockified templates when switching to a non FSE theme * Fix condition * Add tests for the option value * Move the check to use blockified templates * WIP * WIP * Add migration strategy * Move the blockified templates to the templates folder and filter them depending on the option * Fix tests and start using the Options constants * Fix migration, the `should_use_blockified_product_grid_templates` and tests * Rename tests and move to Utils folder * add Migration class test * try * remove PHP * add composer * Replace the blockified templates with a temporary version * Fix tests * add comment * Add feature gating for experimental builds * Inject the package to the controller * test * Change blocks.ini * debug * Remove debug info * fix test * fix tests * try now * using composer cache * install deps * test * Remove unnecessary extra key * Add cache actions * Undo tests change * Fix readme format Co-authored-by: Luigi <gigitux@gmail.com>
2022-07-06 07:51:39 +00:00
parent::setUp();
delete_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE );
delete_option( Options::WC_BLOCK_VERSION );
}
/**
* Provides data for testing template_is_eligible_for_product_archive_fallback.
*/
public function provideFallbackData() {
return array(
array( 'taxonomy-product_cat', true ),
array( 'taxonomy-product_tag', true ),
array( 'taxonomy-product_attribute', true ),
array( 'single-product', false ),
);
}
/**
* Test template_is_eligible_for_product_archive_fallback.
*
* @param string $input The template slug.
* @param bool $expected The expected result.
*
* @dataProvider provideFallbackData
*/
public function test_template_is_eligible_for_product_archive_fallback( $input, $expected ) {
$this->assertEquals( $expected, BlockTemplateUtils::template_is_eligible_for_product_archive_fallback( $input ) );
}
/**
* Test template_is_eligible_for_product_archive_fallback_from_db when the template is not eligible.
*/
public function test_template_is_eligible_for_product_archive_fallback_from_db_no_eligible_template() {
$this->assertEquals( false, BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_db( 'single-product', array() ) );
}
/**
* Test template_is_eligible_for_product_archive_fallback_from_db when the template is eligible but not in the db.
*/
public function test_template_is_eligible_for_product_archive_fallback_from_db_eligible_template_empty_db() {
$this->assertEquals( false, BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_db( 'taxonomy-product_cat', array() ) );
}
/**
* Test template_is_eligible_for_product_archive_fallback_from_db when the template is eligible and in the db.
*/
public function test_template_is_eligible_for_product_archive_fallback_from_db_eligible_template_custom_in_the_db() {
$db_templates = array(
(object) array( 'slug' => 'archive-product' ),
);
$this->assertEquals( true, BlockTemplateUtils::template_is_eligible_for_product_archive_fallback_from_db( 'taxonomy-product_cat', $db_templates ) );
}
/**
* Test build_template_result_from_post.
*/
public function test_build_template_result_from_post() {
$theme = BlockTemplateUtils::PLUGIN_SLUG;
$post_fields = array(
'ID' => 'the_post_id',
'post_name' => 'the_post_name',
'post_content' => 'the_post_content',
'post_type' => 'the_post_type',
'post_excerpt' => 'the_post_excerpt',
'post_title' => 'the_post_title',
'post_status' => 'the_post_status',
);
$post = $this->createPost( $post_fields, $theme );
$template = BlockTemplateUtils::build_template_result_from_post( $post );
$this->assertEquals( $post->ID, $template->wp_id );
$this->assertEquals( $theme . '//' . $post_fields['post_name'], $template->id );
$this->assertEquals( $theme, $template->theme );
$this->assertEquals( $post_fields['post_content'], $template->content );
$this->assertEquals( $post_fields['post_name'], $template->slug );
$this->assertEquals( 'custom', $template->source );
$this->assertEquals( $post_fields['post_type'], $template->type );
$this->assertEquals( $post_fields['post_excerpt'], $template->description );
$this->assertEquals( $post_fields['post_title'], $template->title );
$this->assertEquals( $post_fields['post_status'], $template->status );
$this->assertEquals( 'plugin', $template->origin );
$this->assertTrue( $template->has_theme_file );
$this->assertFalse( $template->is_custom );
$this->assertEmpty( $template->post_types );
}
/**
* Test build_template_result_from_file.
*/
public function test_build_template_result_from_file() {
switch_theme( 'storefront' );
$template_file = array(
'slug' => 'single-product',
'id' => 'woocommerce/woocommerce//single-product',
'path' => __DIR__ . '/single-product.html',
'type' => 'wp_template',
'theme' => 'woocommerce/woocommerce',
'source' => 'plugin',
'title' => 'Single Product',
'description' => 'Displays a single product.',
);
$template = BlockTemplateUtils::build_template_result_from_file( $template_file, 'wp_template' );
$this->assertEquals( BlockTemplateUtils::PLUGIN_SLUG . '//' . $template_file['slug'], $template->id );
$this->assertEquals( BlockTemplateUtils::PLUGIN_SLUG, $template->theme );
$this->assertStringContainsString( '"theme":"storefront"', $template->content );
$this->assertEquals( $template_file['source'], $template->source );
$this->assertEquals( $template_file['slug'], $template->slug );
$this->assertEquals( 'wp_template', $template->type );
$this->assertEquals( $template_file['title'], $template->title );
$this->assertEquals( $template_file['description'], $template->description );
$this->assertEquals( 'publish', $template->status );
$this->assertTrue( $template->has_theme_file );
$this->assertEquals( $template_file['source'], $template->origin );
$this->assertFalse( $template->is_custom );
$this->assertEmpty( $template->post_types );
$this->assertEquals( 'uncategorized', $template->area );
}
/**
* Test set_has_theme_file_if_fallback_is_available when the template file has no fallback.
*/
public function test_set_has_theme_file_if_fallback_is_available_no_fallback() {
Add PHP 8.1/8.2 to unit testing matrix (https://github.com/woocommerce/woocommerce-blocks/pull/8757) * Added PHP 8.1 to unit testing matrix * Refactor unit testing to match Gutenberg repo 🤞 * Removed Gutenberg reference from debug information * Updated image running phpunit binary * Reverted image running phpunit binary * Remove composer platform override * Bump @wordpress/env and include phpunit * Update phpunit and tests * Change prefix * Try admin context when install wc * try wc install * adjust phpunit config file * Updated phpunit.xml.dist, composer.json and package.json * Added PHP 8.1 to unit testing matrix * Refactor unit testing to match Gutenberg repo 🤞 * Removed Gutenberg reference from debug information * Updated image running phpunit binary * Reverted image running phpunit binary * Remove composer platform override * Bump @wordpress/env and include phpunit * Update phpunit and tests * Change prefix * Try admin context when install wc * try wc install * adjust phpunit config file * Updated phpunit.xml.dist, composer.json and package.json * Revert install changes * updated npm packages * updated composer lock * updated check assets flow * Updated check assets flow. Yaml file auto formatted. * Updated check assets flow. * Attempt at fixing E2E * Reverted setup permissions step * Add logging information to E2E * Reverted E2E test flow * test playwright perms fix * test e2e perms fix * test e2e perms fix * test e2e perms fix * test e2e perms fix * languages update & debug * languages update & debug * revert * force "@wordpress/env": "^4.9.0" on E2E * update package lock * update package lock --------- Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Niels Lange <info@nielslange.de>
2023-04-21 11:07:37 +00:00
$query_result = array(
(object) array(
'slug' => 'single-product',
'theme' => 'twentytwentytwo',
),
);
Add PHP 8.1/8.2 to unit testing matrix (https://github.com/woocommerce/woocommerce-blocks/pull/8757) * Added PHP 8.1 to unit testing matrix * Refactor unit testing to match Gutenberg repo 🤞 * Removed Gutenberg reference from debug information * Updated image running phpunit binary * Reverted image running phpunit binary * Remove composer platform override * Bump @wordpress/env and include phpunit * Update phpunit and tests * Change prefix * Try admin context when install wc * try wc install * adjust phpunit config file * Updated phpunit.xml.dist, composer.json and package.json * Added PHP 8.1 to unit testing matrix * Refactor unit testing to match Gutenberg repo 🤞 * Removed Gutenberg reference from debug information * Updated image running phpunit binary * Reverted image running phpunit binary * Remove composer platform override * Bump @wordpress/env and include phpunit * Update phpunit and tests * Change prefix * Try admin context when install wc * try wc install * adjust phpunit config file * Updated phpunit.xml.dist, composer.json and package.json * Revert install changes * updated npm packages * updated composer lock * updated check assets flow * Updated check assets flow. Yaml file auto formatted. * Updated check assets flow. * Attempt at fixing E2E * Reverted setup permissions step * Add logging information to E2E * Reverted E2E test flow * test playwright perms fix * test e2e perms fix * test e2e perms fix * test e2e perms fix * test e2e perms fix * languages update & debug * languages update & debug * revert * force "@wordpress/env": "^4.9.0" on E2E * update package lock * update package lock --------- Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Niels Lange <info@nielslange.de>
2023-04-21 11:07:37 +00:00
$template_file = (object) array(
'slug' => 'archive-product',
'theme' => 'twentytwentytwo',
);
Add PHP 8.1/8.2 to unit testing matrix (https://github.com/woocommerce/woocommerce-blocks/pull/8757) * Added PHP 8.1 to unit testing matrix * Refactor unit testing to match Gutenberg repo 🤞 * Removed Gutenberg reference from debug information * Updated image running phpunit binary * Reverted image running phpunit binary * Remove composer platform override * Bump @wordpress/env and include phpunit * Update phpunit and tests * Change prefix * Try admin context when install wc * try wc install * adjust phpunit config file * Updated phpunit.xml.dist, composer.json and package.json * Added PHP 8.1 to unit testing matrix * Refactor unit testing to match Gutenberg repo 🤞 * Removed Gutenberg reference from debug information * Updated image running phpunit binary * Reverted image running phpunit binary * Remove composer platform override * Bump @wordpress/env and include phpunit * Update phpunit and tests * Change prefix * Try admin context when install wc * try wc install * adjust phpunit config file * Updated phpunit.xml.dist, composer.json and package.json * Revert install changes * updated npm packages * updated composer lock * updated check assets flow * Updated check assets flow. Yaml file auto formatted. * Updated check assets flow. * Attempt at fixing E2E * Reverted setup permissions step * Add logging information to E2E * Reverted E2E test flow * test playwright perms fix * test e2e perms fix * test e2e perms fix * test e2e perms fix * test e2e perms fix * languages update & debug * languages update & debug * revert * force "@wordpress/env": "^4.9.0" on E2E * update package lock * update package lock --------- Co-authored-by: Mike Jolley <mike.jolley@me.com> Co-authored-by: Niels Lange <info@nielslange.de>
2023-04-21 11:07:37 +00:00
$this->assertFalse( BlockTemplateUtils::set_has_theme_file_if_fallback_is_available( $query_result, $template_file ) );
}
/**
* Test set_has_theme_file_if_fallback_is_available when the template file has a fallback.
*/
public function test_set_has_theme_file_if_fallback_is_available_with_fallback() {
$template_file = (object) array(
'slug' => 'taxonomy-product_cat',
'theme' => 'twentytwentytwo',
);
$query_result = array(
(object) array(
'slug' => 'taxonomy-product_cat',
'theme' => 'twentytwentytwo',
),
);
$this->assertTrue( BlockTemplateUtils::set_has_theme_file_if_fallback_is_available( $query_result, $template_file ) );
}
/**
* Test create_new_block_template_object.
*/
public function test_create_new_block_template_object() {
$expected_template = (object) array(
'slug' => 'single-product',
'id' => 'woocommerce/woocommerce//single-product',
'path' => __DIR__ . '/single-product.html',
'type' => 'wp_template',
'theme' => 'woocommerce/woocommerce',
'source' => 'plugin',
'title' => 'Single Product',
'description' => 'Displays a single product.',
'post_types' => array(),
);
$template = BlockTemplateUtils::create_new_block_template_object(
__DIR__ . '/single-product.html',
'wp_template',
'single-product',
false
);
$this->assertEquals( $expected_template, $template );
}
/**
* Test remove_theme_templates_with_custom_alternative.
*/
public function test_remove_theme_templates_with_custom_alternative() {
$templates = array(
(object) array(
'slug' => 'single-product',
'source' => 'theme',
),
(object) array(
'slug' => 'taxonomy-product_tag',
'source' => 'theme',
),
(object) array(
'slug' => 'taxonomy-product_cat',
'source' => 'theme',
),
(object) array(
'slug' => 'taxonomy-product_cat',
'source' => 'custom',
),
);
$expected_templates = array(
(object) array(
'slug' => 'single-product',
'source' => 'theme',
),
(object) array(
'slug' => 'taxonomy-product_tag',
'source' => 'theme',
),
(object) array(
'slug' => 'taxonomy-product_cat',
'source' => 'custom',
),
);
$this->assertEquals( $expected_templates, BlockTemplateUtils::remove_theme_templates_with_custom_alternative( $templates ) );
}
/**
* Test inject_theme_attribute_in_content with no template part.
*/
public function test_inject_theme_attribute_in_content_with_no_template_part() {
$template_content = '<!-- wp:woocommerce/legacy-template {"template":"archive-product"} /-->';
$this->assertEquals( $template_content, BlockTemplateUtils::inject_theme_attribute_in_content( $template_content ) );
}
/**
* Test inject_theme_attribute_in_content with a template part.
*/
public function test_inject_theme_attribute_in_content_with_template_parts() {
switch_theme( 'storefront' );
$template_content = '<!-- wp:template-part {"slug":"header","tagName":"header"} /-->';
$expected_template_content = '<!-- wp:template-part {"slug":"header","tagName":"header","theme":"storefront"} /-->';
$this->assertEquals( $expected_template_content, BlockTemplateUtils::inject_theme_attribute_in_content( $template_content ) );
}
/**
* Test a new installation with a classic theme.
*/
Add migration strategy and load blockified templates (https://github.com/woocommerce/woocommerce-blocks/pull/6538) * Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag If the schema version stored on the db is <= 260 means the plugin is being updated to the new version and we should keep using the old templates, not the blockified ones. * After the theme is switched we check if we need to update the flag to start loading the blockified templates or not. * Get the templates from the blockified folder if the flag is set to true on the db * Add temporary blockified template for testing purposes * Inline variable * Improve comment * Use blockified templates on new installs with block themes only * Don't use blockified templates when switching to a non FSE theme * Fix condition * Add tests for the option value * Move the check to use blockified templates * WIP * WIP * Add migration strategy * Move the blockified templates to the templates folder and filter them depending on the option * Fix tests and start using the Options constants * Fix migration, the `should_use_blockified_product_grid_templates` and tests * Rename tests and move to Utils folder * add Migration class test * try * remove PHP * add composer * Replace the blockified templates with a temporary version * Fix tests * add comment * Add feature gating for experimental builds * Inject the package to the controller * test * Change blocks.ini * debug * Remove debug info * fix test * fix tests * try now * using composer cache * install deps * test * Remove unnecessary extra key * Add cache actions * Undo tests change * Fix readme format Co-authored-by: Luigi <gigitux@gmail.com>
2022-07-06 07:51:39 +00:00
public function test_new_installation_with_a_classic_theme_should_not_use_blockified_templates() {
switch_theme( 'storefront' );
$this->assertFalse( BlockTemplateUtils::should_use_blockified_product_grid_templates() );
}
/**
* Test a new installation with a block theme.
*/
Add migration strategy and load blockified templates (https://github.com/woocommerce/woocommerce-blocks/pull/6538) * Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag If the schema version stored on the db is <= 260 means the plugin is being updated to the new version and we should keep using the old templates, not the blockified ones. * After the theme is switched we check if we need to update the flag to start loading the blockified templates or not. * Get the templates from the blockified folder if the flag is set to true on the db * Add temporary blockified template for testing purposes * Inline variable * Improve comment * Use blockified templates on new installs with block themes only * Don't use blockified templates when switching to a non FSE theme * Fix condition * Add tests for the option value * Move the check to use blockified templates * WIP * WIP * Add migration strategy * Move the blockified templates to the templates folder and filter them depending on the option * Fix tests and start using the Options constants * Fix migration, the `should_use_blockified_product_grid_templates` and tests * Rename tests and move to Utils folder * add Migration class test * try * remove PHP * add composer * Replace the blockified templates with a temporary version * Fix tests * add comment * Add feature gating for experimental builds * Inject the package to the controller * test * Change blocks.ini * debug * Remove debug info * fix test * fix tests * try now * using composer cache * install deps * test * Remove unnecessary extra key * Add cache actions * Undo tests change * Fix readme format Co-authored-by: Luigi <gigitux@gmail.com>
2022-07-06 07:51:39 +00:00
public function test_new_installation_with_a_block_theme_should_use_blockified_templates() {
switch_theme( 'twentytwentytwo' );
$this->assertTrue( BlockTemplateUtils::should_use_blockified_product_grid_templates() );
}
/**
* Test a new installation with a classic theme switching to a block theme.
*/
Add migration strategy and load blockified templates (https://github.com/woocommerce/woocommerce-blocks/pull/6538) * Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag If the schema version stored on the db is <= 260 means the plugin is being updated to the new version and we should keep using the old templates, not the blockified ones. * After the theme is switched we check if we need to update the flag to start loading the blockified templates or not. * Get the templates from the blockified folder if the flag is set to true on the db * Add temporary blockified template for testing purposes * Inline variable * Improve comment * Use blockified templates on new installs with block themes only * Don't use blockified templates when switching to a non FSE theme * Fix condition * Add tests for the option value * Move the check to use blockified templates * WIP * WIP * Add migration strategy * Move the blockified templates to the templates folder and filter them depending on the option * Fix tests and start using the Options constants * Fix migration, the `should_use_blockified_product_grid_templates` and tests * Rename tests and move to Utils folder * add Migration class test * try * remove PHP * add composer * Replace the blockified templates with a temporary version * Fix tests * add comment * Add feature gating for experimental builds * Inject the package to the controller * test * Change blocks.ini * debug * Remove debug info * fix test * fix tests * try now * using composer cache * install deps * test * Remove unnecessary extra key * Add cache actions * Undo tests change * Fix readme format Co-authored-by: Luigi <gigitux@gmail.com>
2022-07-06 07:51:39 +00:00
public function test_new_installation_with_a_classic_theme_switching_to_a_block_should_use_blockified_templates() {
switch_theme( 'storefront' );
switch_theme( 'twentytwentytwo' );
check_theme_switched();
$this->assertTrue( BlockTemplateUtils::should_use_blockified_product_grid_templates() );
}
/**
* Test a plugin update with a classic theme.
*/
Add migration strategy and load blockified templates (https://github.com/woocommerce/woocommerce-blocks/pull/6538) * Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag If the schema version stored on the db is <= 260 means the plugin is being updated to the new version and we should keep using the old templates, not the blockified ones. * After the theme is switched we check if we need to update the flag to start loading the blockified templates or not. * Get the templates from the blockified folder if the flag is set to true on the db * Add temporary blockified template for testing purposes * Inline variable * Improve comment * Use blockified templates on new installs with block themes only * Don't use blockified templates when switching to a non FSE theme * Fix condition * Add tests for the option value * Move the check to use blockified templates * WIP * WIP * Add migration strategy * Move the blockified templates to the templates folder and filter them depending on the option * Fix tests and start using the Options constants * Fix migration, the `should_use_blockified_product_grid_templates` and tests * Rename tests and move to Utils folder * add Migration class test * try * remove PHP * add composer * Replace the blockified templates with a temporary version * Fix tests * add comment * Add feature gating for experimental builds * Inject the package to the controller * test * Change blocks.ini * debug * Remove debug info * fix test * fix tests * try now * using composer cache * install deps * test * Remove unnecessary extra key * Add cache actions * Undo tests change * Fix readme format Co-authored-by: Luigi <gigitux@gmail.com>
2022-07-06 07:51:39 +00:00
public function test_plugin_update_with_a_classic_theme_should_not_use_blockified_templates() {
switch_theme( 'storefront' );
$this->update_plugin();
$this->assertFalse( BlockTemplateUtils::should_use_blockified_product_grid_templates() );
}
/**
* Test a plugin update with a block theme.
*/
Add migration strategy and load blockified templates (https://github.com/woocommerce/woocommerce-blocks/pull/6538) * Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag If the schema version stored on the db is <= 260 means the plugin is being updated to the new version and we should keep using the old templates, not the blockified ones. * After the theme is switched we check if we need to update the flag to start loading the blockified templates or not. * Get the templates from the blockified folder if the flag is set to true on the db * Add temporary blockified template for testing purposes * Inline variable * Improve comment * Use blockified templates on new installs with block themes only * Don't use blockified templates when switching to a non FSE theme * Fix condition * Add tests for the option value * Move the check to use blockified templates * WIP * WIP * Add migration strategy * Move the blockified templates to the templates folder and filter them depending on the option * Fix tests and start using the Options constants * Fix migration, the `should_use_blockified_product_grid_templates` and tests * Rename tests and move to Utils folder * add Migration class test * try * remove PHP * add composer * Replace the blockified templates with a temporary version * Fix tests * add comment * Add feature gating for experimental builds * Inject the package to the controller * test * Change blocks.ini * debug * Remove debug info * fix test * fix tests * try now * using composer cache * install deps * test * Remove unnecessary extra key * Add cache actions * Undo tests change * Fix readme format Co-authored-by: Luigi <gigitux@gmail.com>
2022-07-06 07:51:39 +00:00
public function test_plugin_update_with_a_block_theme_should_not_use_blockified_templates() {
switch_theme( 'twentytwentytwo' );
$this->update_plugin();
$this->assertFalse( BlockTemplateUtils::should_use_blockified_product_grid_templates() );
}
/**
* Test a plugin update with a classic theme switching to a block theme.
*/
Add migration strategy and load blockified templates (https://github.com/woocommerce/woocommerce-blocks/pull/6538) * Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag If the schema version stored on the db is <= 260 means the plugin is being updated to the new version and we should keep using the old templates, not the blockified ones. * After the theme is switched we check if we need to update the flag to start loading the blockified templates or not. * Get the templates from the blockified folder if the flag is set to true on the db * Add temporary blockified template for testing purposes * Inline variable * Improve comment * Use blockified templates on new installs with block themes only * Don't use blockified templates when switching to a non FSE theme * Fix condition * Add tests for the option value * Move the check to use blockified templates * WIP * WIP * Add migration strategy * Move the blockified templates to the templates folder and filter them depending on the option * Fix tests and start using the Options constants * Fix migration, the `should_use_blockified_product_grid_templates` and tests * Rename tests and move to Utils folder * add Migration class test * try * remove PHP * add composer * Replace the blockified templates with a temporary version * Fix tests * add comment * Add feature gating for experimental builds * Inject the package to the controller * test * Change blocks.ini * debug * Remove debug info * fix test * fix tests * try now * using composer cache * install deps * test * Remove unnecessary extra key * Add cache actions * Undo tests change * Fix readme format Co-authored-by: Luigi <gigitux@gmail.com>
2022-07-06 07:51:39 +00:00
public function test_plugin_update_with_a_classic_theme_switching_to_a_block_should_use_blockified_templates() {
switch_theme( 'storefront' );
$this->update_plugin();
switch_theme( 'twentytwentytwo' );
check_theme_switched();
$this->assertTrue( BlockTemplateUtils::should_use_blockified_product_grid_templates() );
}
/**
* Runs the migration that happen after a plugin update
*
* @return void
*/
public function update_plugin(): void {
update_option( Options::WC_BLOCK_VERSION, 1 );
Migration::wc_blocks_update_710_blockified_product_grid_block();
}
/**
* Creates a post with a theme term.
*
* @param array $post Post data.
* @param string $theme Theme name.
*
* @return WP_Post
*/
private function createPost( $post, $theme ) {
$term = wp_insert_term( $theme, 'wp_theme' );
$post_id = wp_insert_post( $post );
wp_set_post_terms( $post_id, array( $term['term_id'] ), 'wp_theme' );
return get_post( $post_id );
}
Add migration strategy and load blockified templates (https://github.com/woocommerce/woocommerce-blocks/pull/6538) * Increase `schema_version` to add the new `wc_blocks_use_blockified_templates` flag If the schema version stored on the db is <= 260 means the plugin is being updated to the new version and we should keep using the old templates, not the blockified ones. * After the theme is switched we check if we need to update the flag to start loading the blockified templates or not. * Get the templates from the blockified folder if the flag is set to true on the db * Add temporary blockified template for testing purposes * Inline variable * Improve comment * Use blockified templates on new installs with block themes only * Don't use blockified templates when switching to a non FSE theme * Fix condition * Add tests for the option value * Move the check to use blockified templates * WIP * WIP * Add migration strategy * Move the blockified templates to the templates folder and filter them depending on the option * Fix tests and start using the Options constants * Fix migration, the `should_use_blockified_product_grid_templates` and tests * Rename tests and move to Utils folder * add Migration class test * try * remove PHP * add composer * Replace the blockified templates with a temporary version * Fix tests * add comment * Add feature gating for experimental builds * Inject the package to the controller * test * Change blocks.ini * debug * Remove debug info * fix test * fix tests * try now * using composer cache * install deps * test * Remove unnecessary extra key * Add cache actions * Undo tests change * Fix readme format Co-authored-by: Luigi <gigitux@gmail.com>
2022-07-06 07:51:39 +00:00
}