From 05e24d305626d7abfadc626d52994a06aa34b96a Mon Sep 17 00:00:00 2001 From: David GABISON Date: Mon, 5 Aug 2024 15:18:03 +0200 Subject: [PATCH 1/5] Refactor: Remove side effects from data provider - Eliminated side effects in test_give_meta_helpers data provider - Ensures consistent test environment regardless of --filter usage This change prevents unexpected failures when running specific tests with PHPUnit's --filter option. --- tests/includes/legacy/tests-misc-functions.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tests/includes/legacy/tests-misc-functions.php b/tests/includes/legacy/tests-misc-functions.php index b868dc48f5..b227c94fe0 100644 --- a/tests/includes/legacy/tests-misc-functions.php +++ b/tests/includes/legacy/tests-misc-functions.php @@ -101,7 +101,9 @@ public function give_get_currency_name_data_provider() { * * @dataProvider give_meta_helpers_provider */ - public function test_give_meta_helpers( $form_or_donation_id ) { + public function test_give_meta_helpers( $form_or_donation_id_generator ) { + $form_or_donation_id = $form_or_donation_id_generator(); + $value = give_get_meta( $form_or_donation_id, 'testing_meta', true, 'TEST1' ); $this->assertEquals( 'TEST1', $value ); @@ -129,10 +131,10 @@ public function test_give_meta_helpers( $form_or_donation_id ) { * @access private */ public function give_meta_helpers_provider() { - return array( - array( Give_Helper_Payment::create_simple_payment() ), - array( Give_Helper_Form::create_simple_form()->id ), - ); + return [ + [function () { return Give_Helper_Payment::create_simple_payment(); }], + [function () { return Give_Helper_Form::create_simple_form()->id; }], + ]; } /** From 6784617a2505b2eb4a20d900cd72b96396d480e8 Mon Sep 17 00:00:00 2001 From: David GABISON Date: Thu, 5 Sep 2024 17:47:18 +0200 Subject: [PATCH 2/5] fix: Corrects expected values after removing edge effect --- tests/includes/legacy/tests-donors-db.php | 4 ++-- tests/includes/legacy/tests-donors.php | 2 +- tests/includes/legacy/tests-email-tags.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/includes/legacy/tests-donors-db.php b/tests/includes/legacy/tests-donors-db.php index 82f3eb6f69..1ed3c7fcd2 100644 --- a/tests/includes/legacy/tests-donors-db.php +++ b/tests/includes/legacy/tests-donors-db.php @@ -249,7 +249,7 @@ public function test_get_donors() { $donors = Give()->donors->get_donors(); - $this->assertEquals( 2, count( $donors ) ); + $this->assertEquals( 1, count( $donors ) ); } @@ -258,7 +258,7 @@ public function test_get_donors() { */ public function test_count_customers() { - $this->assertEquals( 2, intval( Give()->donors->count() ) ); + $this->assertEquals( 1, intval( Give()->donors->count() ) ); $args = array( 'date' => array( diff --git a/tests/includes/legacy/tests-donors.php b/tests/includes/legacy/tests-donors.php index 3fde9cadb4..0453881334 100644 --- a/tests/includes/legacy/tests-donors.php +++ b/tests/includes/legacy/tests-donors.php @@ -515,7 +515,7 @@ public function test_get_donor_address() { */ public function test_count_total_donors() { $donor_count = give_count_total_donors(); - $this->assertEquals( 2, $donor_count ); + $this->assertEquals( 1, $donor_count ); } /** diff --git a/tests/includes/legacy/tests-email-tags.php b/tests/includes/legacy/tests-email-tags.php index d14ce5454b..54ff21ee3b 100644 --- a/tests/includes/legacy/tests-email-tags.php +++ b/tests/includes/legacy/tests-email-tags.php @@ -766,7 +766,7 @@ function test_give_email_tag_metadata() { Give()->donor_meta->update_meta( $donor_id, '_give_stripe_customer_id', 2 ); $this->assertEquals( 2, __give_render_metadata_email_tag( '{meta_donor__give_stripe_customer_id}', $donor_tag_args ) ); - $this->assertEquals( 1, __give_render_metadata_email_tag( '{meta_donor_id}', $donor_tag_args ) ); + $this->assertEquals( $donor_id, __give_render_metadata_email_tag( '{meta_donor_id}', $donor_tag_args ) ); $this->assertEquals( 1, __give_render_metadata_email_tag( '{meta_donor_user_id}', $donor_tag_args ) ); $this->assertEquals( 'Admin User', __give_render_metadata_email_tag( '{meta_donor_name}', $donor_tag_args ) ); $this->assertEquals( 'admin@example.org', __give_render_metadata_email_tag( '{meta_donor_email}', $donor_tag_args ) ); From 292da872bb97a6e7d02eb097427b043bf7b932d8 Mon Sep 17 00:00:00 2001 From: David GABISON Date: Thu, 5 Sep 2024 17:50:56 +0200 Subject: [PATCH 3/5] fix: avoid instantiating the class multiple times double declaration of `add_filter('..._post_meta')` breaks the return value. See test: Tests_MISC_Functions::test_give_meta_helpers --- includes/install.php | 16 ++++++++-------- src/ServiceProviders/LegacyServiceProvider.php | 2 ++ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/includes/install.php b/includes/install.php index a480746a89..c9bfb38571 100644 --- a/includes/install.php +++ b/includes/install.php @@ -515,14 +515,14 @@ function give_install_tables_on_plugin_update($old_version) function __give_get_tables() { $tables = [ - 'donors_db' => new Give_DB_Donors(), - 'donor_meta_db' => new Give_DB_Donor_Meta(), - 'comment_db' => new Give_DB_Comments(), - 'comment_db_meta' => new Give_DB_Comment_Meta(), - 'give_session' => new Give_DB_Sessions(), - 'formmeta_db' => new Give_DB_Form_Meta(), - 'sequential_db' => new Give_DB_Sequential_Ordering(), - 'donation_meta' => new Give_DB_Payment_Meta(), + 'donors_db' => give()->get(Give_DB_Donors::class), + 'donor_meta_db' => give()->get(Give_DB_Donor_Meta::class), + 'comment_db' => give()->get(Give_DB_Comments::class), + 'comment_db_meta' => give()->get(Give_DB_Comment_Meta::class), + 'give_session' => give()->get(Give_DB_Sessions::class), + 'formmeta_db' => give()->get(Give_DB_Form_Meta::class), + 'sequential_db' => give()->get(Give_DB_Sequential_Ordering::class), + 'donation_meta' => give()->get(Give_DB_Payment_Meta::class), ]; return $tables; diff --git a/src/ServiceProviders/LegacyServiceProvider.php b/src/ServiceProviders/LegacyServiceProvider.php index 42ff0e4889..0404b1529d 100644 --- a/src/ServiceProviders/LegacyServiceProvider.php +++ b/src/ServiceProviders/LegacyServiceProvider.php @@ -232,8 +232,10 @@ private function bindInstance($alias, $class, $includesPath, $singleton = false) give()->instance($alias, $class()); } elseif ($singleton) { give()->instance($alias, $class::get_instance()); + give()->alias($alias, $class); } else { give()->instance($alias, new $class()); + give()->alias($alias, $class); } } From 031772e5023a3d131d3cd04927662e00382d6f6f Mon Sep 17 00:00:00 2001 From: David GABISON Date: Thu, 5 Sep 2024 17:58:42 +0200 Subject: [PATCH 4/5] fix: Explain `ENGINE=InnoDB` to be able to use `TRANSACTION` in tests MyIsam engine does not support transactions --- includes/database/class-give-db-comments.php | 2 +- includes/database/class-give-db-donors.php | 2 +- includes/database/class-give-db-meta.php | 2 +- includes/database/class-give-db-sequential-ordering.php | 2 +- includes/database/class-give-db-sessions.php | 2 +- src/EventTickets/Migrations/CreateEventTicketTypesTable.php | 2 +- src/EventTickets/Migrations/CreateEventTicketsTable.php | 2 +- src/EventTickets/Migrations/CreateEventsTable.php | 2 +- src/LegacySubscriptions/includes/give-subscriptions-db.php | 2 +- src/Log/Migrations/CreateNewLogTable.php | 2 +- src/MigrationLog/Migrations/CreateMigrationsTable.php | 2 +- src/Revenue/Migrations/CreateRevenueTable.php | 2 +- src/Subscriptions/Migrations/CreateSubscriptionTables.php | 4 ++-- 13 files changed, 14 insertions(+), 14 deletions(-) diff --git a/includes/database/class-give-db-comments.php b/includes/database/class-give-db-comments.php index a10c8cbacb..03cd2e0586 100644 --- a/includes/database/class-give-db-comments.php +++ b/includes/database/class-give-db-comments.php @@ -252,7 +252,7 @@ public function create_table() { comment_date datetime NOT NULL, comment_date_gmt datetime NOT NULL, PRIMARY KEY (comment_ID) - ) {$charset_collate};"; + ) {$charset_collate} ENGINE=InnoDB;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); diff --git a/includes/database/class-give-db-donors.php b/includes/database/class-give-db-donors.php index c4f4982805..be44283a22 100644 --- a/includes/database/class-give-db-donors.php +++ b/includes/database/class-give-db-donors.php @@ -576,7 +576,7 @@ public function create_table() { PRIMARY KEY (id), UNIQUE KEY email (email), KEY user (user_id) - ) CHARACTER SET utf8 COLLATE utf8_general_ci;"; + ) CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB;"; dbDelta( $sql ); diff --git a/includes/database/class-give-db-meta.php b/includes/database/class-give-db-meta.php index a7c7f952d7..1f98e0c809 100644 --- a/includes/database/class-give-db-meta.php +++ b/includes/database/class-give-db-meta.php @@ -543,7 +543,7 @@ public function create_table() { PRIMARY KEY (meta_id), KEY {$this->meta_type}_id ({$this->meta_type}_id), KEY meta_key (meta_key({$this->min_index_length})) - ) {$charset_collate};"; + ) {$charset_collate} ENGINE=InnoDB;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); diff --git a/includes/database/class-give-db-sequential-ordering.php b/includes/database/class-give-db-sequential-ordering.php index a312496bec..bf868f1114 100644 --- a/includes/database/class-give-db-sequential-ordering.php +++ b/includes/database/class-give-db-sequential-ordering.php @@ -103,7 +103,7 @@ public function create_table() { id bigint(20) NOT NULL AUTO_INCREMENT, payment_id bigint(20) NOT NULL, PRIMARY KEY (id) - ) {$charset_collate};"; + ) {$charset_collate} ENGINE=InnoDB;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); diff --git a/includes/database/class-give-db-sessions.php b/includes/database/class-give-db-sessions.php index c141b26ad5..57aaf26ec6 100644 --- a/includes/database/class-give-db-sessions.php +++ b/includes/database/class-give-db-sessions.php @@ -97,7 +97,7 @@ public function create_table() { session_expiry BIGINT UNSIGNED NOT NULL, PRIMARY KEY (session_key), UNIQUE KEY session_id (session_id) - ) {$charset_collate};"; + ) {$charset_collate} ENGINE=InnoDB;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); diff --git a/src/EventTickets/Migrations/CreateEventTicketTypesTable.php b/src/EventTickets/Migrations/CreateEventTicketTypesTable.php index d01a86548b..cf3c8d861e 100644 --- a/src/EventTickets/Migrations/CreateEventTicketTypesTable.php +++ b/src/EventTickets/Migrations/CreateEventTicketTypesTable.php @@ -49,7 +49,7 @@ public function run() { created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY (id) - ) $charset"; + ) $charset ENGINE=InnoDB;"; try { DB::delta( $sql ); diff --git a/src/EventTickets/Migrations/CreateEventTicketsTable.php b/src/EventTickets/Migrations/CreateEventTicketsTable.php index c9cea5fe95..84f7ed421b 100644 --- a/src/EventTickets/Migrations/CreateEventTicketsTable.php +++ b/src/EventTickets/Migrations/CreateEventTicketsTable.php @@ -48,7 +48,7 @@ public function run() { created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY (id) - ) $charset"; + ) $charset ENGINE=InnoDB;"; try { DB::delta( $sql ); diff --git a/src/EventTickets/Migrations/CreateEventsTable.php b/src/EventTickets/Migrations/CreateEventsTable.php index 9e57370e97..5a5750e1ca 100644 --- a/src/EventTickets/Migrations/CreateEventsTable.php +++ b/src/EventTickets/Migrations/CreateEventsTable.php @@ -49,7 +49,7 @@ public function run() { created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, PRIMARY KEY (id) - ) $charset"; + ) $charset ENGINE=InnoDB;"; try { DB::delta( $sql ); diff --git a/src/LegacySubscriptions/includes/give-subscriptions-db.php b/src/LegacySubscriptions/includes/give-subscriptions-db.php index 525383adad..2e512349d3 100644 --- a/src/LegacySubscriptions/includes/give-subscriptions-db.php +++ b/src/LegacySubscriptions/includes/give-subscriptions-db.php @@ -367,7 +367,7 @@ public function create_table() KEY customer (customer_id), KEY transaction (transaction_id), INDEX customer_and_status ( customer_id, status) - ) CHARACTER SET utf8 COLLATE utf8_general_ci;'; + ) CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB;'; dbDelta($sql); diff --git a/src/Log/Migrations/CreateNewLogTable.php b/src/Log/Migrations/CreateNewLogTable.php index 71015781c1..71a8db3682 100644 --- a/src/Log/Migrations/CreateNewLogTable.php +++ b/src/Log/Migrations/CreateNewLogTable.php @@ -57,7 +57,7 @@ public function run() KEY log_type (log_type), KEY category (category), KEY source (source) - ) {$charset}"; + ) {$charset} ENGINE=InnoDB;"; try { DB::delta($sql); diff --git a/src/MigrationLog/Migrations/CreateMigrationsTable.php b/src/MigrationLog/Migrations/CreateMigrationsTable.php index 9bd6c2996e..2108eb3eb1 100644 --- a/src/MigrationLog/Migrations/CreateMigrationsTable.php +++ b/src/MigrationLog/Migrations/CreateMigrationsTable.php @@ -58,7 +58,7 @@ public function run() error text NULL, last_run DATETIME NOT NULL, PRIMARY KEY (id) - ) {$charset}"; + ) {$charset} ENGINE=InnoDB;"; try { DB::delta($sql); diff --git a/src/Revenue/Migrations/CreateRevenueTable.php b/src/Revenue/Migrations/CreateRevenueTable.php index 4340368dc8..6760993ac7 100644 --- a/src/Revenue/Migrations/CreateRevenueTable.php +++ b/src/Revenue/Migrations/CreateRevenueTable.php @@ -50,7 +50,7 @@ public function run() form_id bigint UNSIGNED NOT NULL, amount int UNSIGNED NOT NULL, PRIMARY KEY (id) - ) {$charset_collate};"; + ) {$charset_collate} ENGINE=InnoDB;"; try { DB::delta($sql); diff --git a/src/Subscriptions/Migrations/CreateSubscriptionTables.php b/src/Subscriptions/Migrations/CreateSubscriptionTables.php index 57a900c20f..fc8e16265e 100644 --- a/src/Subscriptions/Migrations/CreateSubscriptionTables.php +++ b/src/Subscriptions/Migrations/CreateSubscriptionTables.php @@ -47,7 +47,7 @@ public function run() KEY `customer` (`customer_id`), KEY `transaction` (`transaction_id`), KEY `customer_and_status` (`customer_id`,`status`) - ) $charset_collate; + ) $charset_collate ENGINE=InnoDB; " ); @@ -61,7 +61,7 @@ public function run() PRIMARY KEY (`meta_id`), KEY `subscription_id` (`subscription_id`), KEY `meta_key` (`meta_key`(191)) - ) $charset_collate; + ) $charset_collate ENGINE=InnoDB; " ); } catch (DatabaseQueryException $exception) { From 30a6fafa8e0869d477fece5ab4854d5f06689cdb Mon Sep 17 00:00:00 2001 From: David GABISON Date: Thu, 5 Sep 2024 17:59:12 +0200 Subject: [PATCH 5/5] fix: misprint --- includes/database/class-give-db-meta.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/database/class-give-db-meta.php b/includes/database/class-give-db-meta.php index 1f98e0c809..d5dc4586d6 100644 --- a/includes/database/class-give-db-meta.php +++ b/includes/database/class-give-db-meta.php @@ -512,7 +512,7 @@ public function __call( $name, $arguments ) { $id = $arguments[1]; $meta_key = $arguments[2]; $meta_value = $arguments[3]; - $delete_all = $arguments[3]; + $delete_all = $arguments[4]; $this->is_filter_callback = true; // Bailout.