remove unneccessary dependencies

This commit is contained in:
David Stone
2025-02-07 11:36:39 -07:00
parent 10c292b0df
commit b40a4aaeab
18 changed files with 630 additions and 79 deletions

View File

@ -0,0 +1,11 @@
<?php
namespace WP_Ultimo;
class Date_Functions_Test extends \WP_UnitTestCase {
public function test_wu_get_days_ago() {
$days = wu_get_days_ago( '2024-01-01 00:00:00', '2024-01-31 00:00:00' );
$this->assertEquals( -30, $days );
}
}

View File

@ -0,0 +1,128 @@
<?php
namespace WP_Ultimo\Models;
use WP_Error;
use WP_UnitTestCase;
class Discount_Code_Test extends WP_UnitTestCase {
/**
* Tests that a valid discount code returns true.
*/
public function test_is_valid_active_discount_code() {
$discount_code = new Discount_Code();
$discount_code->set_active( true );
$result = $discount_code->is_valid();
$this->assertTrue( $result );
}
/**
* Tests that an inactive discount code returns an error.
*/
public function test_is_valid_inactive_discount_code() {
$discount_code = new Discount_Code();
$discount_code->set_active( false );
$result = $discount_code->is_valid();
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'discount_code', $result->get_error_code() );
$this->assertEquals( 'This coupon code is not valid.', $result->get_error_message() );
}
/**
* Tests that a discount code with max uses returns an error after being used maximum times.
*/
public function test_is_valid_max_uses_exceeded() {
$discount_code = new Discount_Code();
$discount_code->set_active( true );
$discount_code->set_max_uses( 5 );
$discount_code->set_uses( 5 );
$result = $discount_code->is_valid();
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'discount_code', $result->get_error_code() );
$this->assertEquals( 'This discount code was already redeemed the maximum amount of times allowed.',
$result->get_error_message() );
}
/**
* Tests that a discount code before the start date is invalid.
*/
public function test_is_valid_before_start_date() {
$discount_code = new Discount_Code();
$discount_code->set_active( true );
$discount_code->set_date_start( date( 'Y-m-d H:i:s', strtotime( '+1 day' ) ) );
$result = $discount_code->is_valid();
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'discount_code', $result->get_error_code() );
$this->assertEquals( 'This coupon code is not valid.', $result->get_error_message() );
}
/**
* Tests that a discount code after the expiration date is invalid.
*/
public function test_is_valid_after_expiration_date() {
$discount_code = new Discount_Code();
$discount_code->set_active( true );
$discount_code->set_date_expiration( date( 'Y-m-d H:i:s', strtotime( '-1 day' ) ) );
$result = $discount_code->is_valid();
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'discount_code', $result->get_error_code() );
$this->assertEquals( 'This coupon code is not valid.', $result->get_error_message() );
}
/**
* Tests that a discount code limited to specific products returns true for allowed products.
*/
public function test_is_valid_for_allowed_product() {
$product_id = 123;
$discount_code = new Discount_Code();
$discount_code->set_active( true );
$discount_code->set_limit_products( true );
$discount_code->set_allowed_products( [ $product_id ] );
$result = $discount_code->is_valid( $product_id );
$this->assertTrue( $result );
}
/**
* Tests that a discount code limited to specific products returns an error for disallowed products.
*/
public function test_is_valid_for_disallowed_product() {
$allowed_product_id = 123;
$disallowed_product_id = 456;
$discount_code = new Discount_Code();
$discount_code->set_active( true );
$discount_code->set_limit_products( true );
$discount_code->set_allowed_products( [ $allowed_product_id ] );
$result = $discount_code->is_valid( $disallowed_product_id );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'discount_code', $result->get_error_code() );
$this->assertEquals( 'This coupon code is not valid.', $result->get_error_message() );
}
/**
* Tests that a discount code with no product limits returns true.
*/
public function test_is_valid_no_product_limits() {
$discount_code = new Discount_Code();
$discount_code->set_active( true );
$discount_code->set_limit_products( false );
$result = $discount_code->is_valid();
$this->assertTrue( $result );
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace WP_Ultimo\Models;
use WP_UnitTestCase;
class Domain_Test extends WP_UnitTestCase {
/**
* Test that has_valid_ssl_certificate returns true for valid SSL certificates.
*/
public function test_has_valid_ssl_certificate_with_valid_certificate() {
// Mocking a domain with a valid SSL certificate.
$domain = new Domain();
$domain->set_domain( 'dogs.4thelols.uk' );
// Assert that it returns true for a valid SSL certificate.
$this->assertTrue( $domain->has_valid_ssl_certificate() );
}
/**
* Test that has_valid_ssl_certificate returns false when the SSL certificate is invalid.
*/
public function test_has_valid_ssl_certificate_with_invalid_certificate() {
// Mocking a domain with an invalid SSL certificate.
$domain = new Domain();
$domain->set_domain( 'eeeeeeeeeeeeeeeeauauexample.com' );
// Assert that it returns false for an invalid SSL certificate.
$this->assertFalse( $domain->has_valid_ssl_certificate() );
}
/**
* Test that has_valid_ssl_certificate handles empty domain.
*/
public function test_has_valid_ssl_certificate_with_empty_domain() {
// Mocking a domain with an empty value.
$domain = new Domain();
$domain->set_domain( '' );
// Assert that it returns false for an empty domain.
$this->assertFalse( $domain->has_valid_ssl_certificate() );
}
}

View File

@ -0,0 +1,173 @@
<?php
namespace WP_Ultimo\Models;
use WP_Ultimo\Faker;
/**
* Unit tests for the Membership class.
*/
class Membership_Test extends \WP_UnitTestCase {
/**
* Membership instance.
*
* @var Membership
*/
protected $membership;
/**
* Set up the test environment.
*/
public function setUp(): void {
parent::setUp();
// Create a new Membership instance for each test.
$this->membership = new Membership();
// Set a default customer ID.
$this->membership->set_customer_id( 123 );
}
/**
* Test if the customer is allowed access to the membership.
*/
public function test_is_customer_allowed() {
// Admins with 'manage_network' capability should always return true.
$admin_user_id = $this->factory()->user->create( array( 'role' => 'administrator' ) );
grant_super_admin( $admin_user_id );
wp_set_current_user( $admin_user_id );
$this->assertTrue( $this->membership->is_customer_allowed(), 'Failed asserting that admin is allowed.' );
// Regular customers are allowed if IDs match.
$customer_id = 123;
$this->assertTrue(
$this->membership->is_customer_allowed( $customer_id ),
'Failed asserting that customer with matching ID is allowed.'
);
// Regular customers are denied if IDs do not match.
$wrong_customer_id = 456;
wp_set_current_user( $wrong_customer_id );
$this->assertFalse(
$this->membership->is_customer_allowed( $wrong_customer_id ),
'Failed asserting that customer with non-matching ID is denied.'
);
}
/**
* Test adding a product to the membership.
*/
public function test_add_product() {
// Add a product with a specific ID and quantity.
$quantity = 2;
$faker = new Faker();
$faker->generate_fake_products();
/** @var Product $product */
$product = $faker->get_fake_data_generated( 'products' )[0];
$product_id = $product->get_id();
$this->membership->add_product( $product_id, $quantity );
// Verify that the product is added with the correct quantity.
$addon_products = $this->membership->get_addon_ids();
$this->assertContains( $product_id, $addon_products, 'Failed asserting that product ID was added.' );
$this->assertEquals(
$quantity,
$this->membership->get_addon_products()[0]['quantity'],
'Failed asserting that the product quantity is correct.'
);
// Add more of the same product and check the updated quantity.
$additional_quantity = 3;
$this->membership->add_product( $product_id, $additional_quantity );
$this->assertEquals(
$quantity + $additional_quantity,
$this->membership->get_addon_products()[0]['quantity'],
'Failed asserting that the quantity was updated correctly for the same product.'
);
}
/**
* Test removing a product from the membership.
*/
public function test_remove_product() {
// Add a product with a specific quantity.
$quantity = 5;
$faker = new Faker();
$faker->generate_fake_products();
/** @var Product $product */
$product = $faker->get_fake_data_generated( 'products' )[0];
$product_id = $product->get_id();
$this->membership->add_product( $product_id, $quantity );
// Remove some of the product's quantity.
$remove_quantity = 3;
$this->membership->remove_product( $product_id, $remove_quantity );
// Verify the updated quantity.
$this->assertEquals(
$quantity - $remove_quantity,
$this->membership->get_addon_products()[0]['quantity'],
'Failed asserting that the quantity was reduced correctly.'
);
// Remove the remaining quantity and verify it is removed.
$this->membership->remove_product( $product_id, $quantity );
$addon_products = $this->membership->get_addon_ids();
$this->assertNotContains( $product_id, $addon_products, 'Failed asserting that the product was removed.' );
}
/**
* Test get_remaining_days_in_cycle() method.
*/
public function test_get_remaining_days_in_cycle() {
$this->membership->set_amount( 12.99 );
// Case 1: Non-recurring membership should return 10000.
$this->membership->set_recurring( false );
$this->assertEquals(
10000,
$this->membership->get_remaining_days_in_cycle(),
'Failed asserting that non-recurring membership returns 10000.'
);
// Case 2: Invalid expiration date should return 0.
$this->membership->set_recurring( true );
$this->membership->set_date_expiration( 'invalid-date' ); // Setting an invalid date.
$this->assertEquals(
0,
$this->membership->get_remaining_days_in_cycle(),
'Failed asserting that an invalid expiration date returns 0.'
);
// Case 3: No expiration date should return 0.
$this->membership->set_date_expiration( '' );
$this->assertEquals(
0,
$this->membership->get_remaining_days_in_cycle(),
'Failed asserting that no expiration date returns 0.'
);
// Case 4: Expiration date is in the future and remaining days are calculated properly.
$today = new \DateTime( 'now', new \DateTimeZone( 'UTC' ) );
$today->add( new \DateInterval( 'P10D' ) );
$this->membership->set_date_expiration( $today->format( 'Y-m-d H:i:s' ) );
$remaining_days = $this->membership->get_remaining_days_in_cycle();
$this->assertEquals(
10,
$remaining_days,
'Failed asserting that 10 days remain when expiration date is 10 days in the future.'
);
// Case 5: Expiration date is in the past, should return 0.
$this->membership->set_date_expiration( date( 'Y-m-d H:i:s', strtotime( '-5 days' ) ) );
$remaining_days = $this->membership->get_remaining_days_in_cycle();
$this->assertEquals(
0,
$remaining_days,
'Failed asserting that remaining days return 0 when the expiration date is in the past.'
);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace WP_Ultimo\Tax;
use WP_UnitTestCase;
class Dashboard_Taxes_Tab_Test extends WP_UnitTestCase {
/**
* Test that register_scripts method registers the correct scripts.
*/
public function test_register_scripts_registers_scripts() {
// Create a mock instance of Dashboard_Admin_Page and call the register_scripts method.
$dashboard_admin_page = $this->getMockBuilder( Dashboard_Taxes_Tab::class )
->disableOriginalConstructor()
->setMethods( array( 'output' ) )
->getMock();
// Execute register_scripts method.
$dashboard_admin_page->register_scripts();
// Assert scripts are registered.
$this->assertTrue( wp_script_is( 'wu-tax-stats', 'registered' ) );
// Assert scripts are enqueued.
$this->assertTrue( wp_script_is( 'wu-tax-stats', 'enqueued' ) );
// Verify localized script data is correct.
$localized_vars = wp_scripts()->get_data( 'wu-tax-stats', 'data' );
$this->assertStringContainsString( '"month_list":["Jan ', $localized_vars );
$this->assertStringContainsString( '"today":"', $localized_vars ); // Check that today is included.
$this->assertStringContainsString( '"net_profit_label":"Net Profit"', $localized_vars );
}
}