-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add E2E and unit test for categories saving
- Loading branch information
1 parent
7ba8a0d
commit 5d57e94
Showing
6 changed files
with
128 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ dist | |
vendor | ||
languages/woocommerce-product-addon.pot | ||
*.log | ||
artifacts | ||
artifacts | ||
.phpunit.result.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,25 @@ | ||
# Create new categories | ||
# NOTE: If the categories with the same slug are already present, it will raise an error. | ||
echo "Created Category 1 with ID: $category1_id" | ||
category1_id=$(wp wc product_cat create --name="Category 1" --slug="test_cat_1" --user=admin --porcelain) | ||
|
||
echo "Created Category 2 with ID: $category2_id" | ||
category2_id=$(wp wc product_cat create --name="Category 2" --slug="test_cat_2" --user=admin --porcelain) | ||
|
||
echo "Created Category 3 with ID: $category3_id" | ||
category3_id=$(wp wc product_cat create --name="Category 3" --slug="test_cat_3" --user=admin --porcelain) | ||
|
||
# Create products and collect their IDs | ||
product1_id=$(wp wc product create --name="Product 1" --type="simple" --regular_price="9.99" --user=admin --porcelain) | ||
echo "Created Product 1 with ID: $product1_id" | ||
|
||
product2_id=$(wp wc product create --name="Product 2" --type="simple" --regular_price="19.99" --user=admin --porcelain) | ||
product3_id=$(wp wc product create --name="Product 3" --type="simple" --regular_price="29.99" --user=admin --porcelain) | ||
echo "Created Product 2 with ID: $product2_id" | ||
|
||
# Get the first category. | ||
category_id=$(wp wc product_cat list --user=admin --format=ids | awk '{print $1}') | ||
echo "Category ID: $category_id" | ||
product3_id=$(wp wc product create --name="Product 3" --type="simple" --regular_price="29.99" --user=admin --porcelain) | ||
echo "Created Product 3 with ID: $product3_id" | ||
|
||
# Add products to the first category | ||
wp wc product update $product1_id --user=admin --categories='[{"id": '$category_id'}]' | ||
wp wc product update $product2_id --user=admin --categories='[{"id": '$category_id'}]' | ||
wp wc product update $product3_id --user=admin --categories='[{"id": '$category_id'}]' | ||
# Add products to the new categories | ||
wp wc product update $product1_id --user=admin --categories='[{"id": '$category1_id'}]' | ||
wp wc product update $product2_id --user=admin --categories='[{"id": '$category2_id'}]' | ||
wp wc product update $product3_id --user=admin --categories='[{"id": '$category3_id'}]' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* Class Test_Field_Saving | ||
* | ||
* @package ppom-pro | ||
*/ | ||
|
||
include_once PPOM_PATH . '/classes/admin.class.php'; | ||
|
||
class Test_Field_Saving extends WP_UnitTestCase { | ||
|
||
/** | ||
* Test if the saved categories are in a backward compatible format and tags in a serialized format. | ||
*/ | ||
public function test_saving_categories_compatibilities() { | ||
global $wpdb; | ||
|
||
$table_name = $wpdb->prefix . PPOM_TABLE_META; | ||
$data = array( | ||
'productmeta_name' => 'Test Multiple Categories', | ||
'productmeta_validation' => '', | ||
'dynamic_price_display' => 'no', | ||
'send_file_attachment' => '', | ||
'show_cart_thumb' => '', | ||
'aviary_api_key' => '', | ||
'productmeta_style' => 'selector { }', | ||
'productmeta_js' => '', | ||
'productmeta_categories' => "accessories\r\nclothing", | ||
'the_meta' => '{"1":{"type":"text","title":"Test Cat","data_name":"test_cat","description":"","placeholder":"","error_message":"","maxlength":"","minlength":"","default_value":"","price":"","class":"","input_mask":"","width":"12","visibility":"everyone","visibility_role":"","conditions":{"visibility":"Show","bound":"All","rules":[{"elements":"test_cat","operators":"is","element_values":""}]},"status":"on","ppom_id":"63"}}', | ||
'productmeta_created' => '2024-10-16 11:38:45', | ||
'productmeta_tags' => 'a:1:{i:0;s:8:"test-tag";}' | ||
); | ||
|
||
// Insert data into the table | ||
$result = $wpdb->insert($table_name, $data); | ||
$this->assertNotFalse( $result ); | ||
|
||
$field_id = $wpdb->insert_id; | ||
|
||
NM_PersonalizedProduct_Admin::save_categories_and_tags( $field_id, ['accessories', 'clothing', 'test-cat'], false ); | ||
|
||
$saved_data = $wpdb->get_row( $wpdb->prepare( "SELECT productmeta_categories, productmeta_tags FROM $table_name WHERE productmeta_id = %d", $field_id ), ARRAY_A ); | ||
|
||
$expected_categories = "accessories\r\nclothing\r\ntest-cat"; | ||
$this->assertEquals( $expected_categories, $saved_data['productmeta_categories'] ); | ||
|
||
// Tags should not be changed. | ||
$expected_tags = 'a:1:{i:0;s:8:"test-tag";}'; | ||
$this->assertEquals( $expected_tags, $saved_data['productmeta_tags'] ); | ||
} | ||
} |