From 913461cc4a7807f3b7a609ae1a2b949921a3ccc3 Mon Sep 17 00:00:00 2001 From: Ian Dunn Date: Thu, 18 Aug 2022 08:12:40 -0700 Subject: [PATCH] Tests: Modularize database fixtures for reuse. --- plugins/wporg-5ftf/tests/bootstrap.php | 1 + plugins/wporg-5ftf/tests/helpers.php | 104 ++++++++++++++++++ plugins/wporg-5ftf/tests/test-contributor.php | 99 +++-------------- 3 files changed, 123 insertions(+), 81 deletions(-) create mode 100644 plugins/wporg-5ftf/tests/helpers.php diff --git a/plugins/wporg-5ftf/tests/bootstrap.php b/plugins/wporg-5ftf/tests/bootstrap.php index 94d8eda..668a577 100755 --- a/plugins/wporg-5ftf/tests/bootstrap.php +++ b/plugins/wporg-5ftf/tests/bootstrap.php @@ -22,6 +22,7 @@ require_once $_tests_dir . '/includes/functions.php'; */ function _manually_load_plugin() { require dirname( dirname( __FILE__ ) ) . '/index.php'; + require __DIR__ . '/helpers.php'; } tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); diff --git a/plugins/wporg-5ftf/tests/helpers.php b/plugins/wporg-5ftf/tests/helpers.php new file mode 100644 index 0000000..12c2683 --- /dev/null +++ b/plugins/wporg-5ftf/tests/helpers.php @@ -0,0 +1,104 @@ +query( " + CREATE TABLE `bpmain_bp_xprofile_data` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `field_id` bigint unsigned NOT NULL DEFAULT '0', + `user_id` bigint unsigned NOT NULL DEFAULT '0', + `value` longtext NOT NULL, + `last_updated` datetime NOT NULL DEFAULT '1970-01-01 00:00:00', + PRIMARY KEY (`id`), + KEY `field_id` (`field_id`), + KEY `user_id` (`user_id`) + ) + ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3 + " ); + + // Users + $fixtures['users']['jane'] = $factory->user->create_and_get( array( + 'user_login' => 'jane', + 'user_email' => 'jane@example.org', + ) ); + + $fixtures['users']['ashish'] = $factory->user->create_and_get( array( + 'user_login' => 'ashish', + 'user_email' => 'ashish@example.org', + ) ); + + // Pages + $for_organizations = $factory->post->create_and_get( array( + 'post_type' => 'page', + 'post_title' => 'For Organizations', + 'post_status' => 'publish', + ) ); + $fixtures['pages']['for_organizations'] = $for_organizations; + $GLOBALS['post'] = $for_organizations; // `create_new_pledge()` assumes this exists. + + // Pledges + $tenup_id = Pledge\create_new_pledge( '10up' ); + $bluehost_id = Pledge\create_new_pledge( 'BlueHost' ); + + wp_update_post( array( + 'ID' => $tenup_id, + 'post_status' => 'publish', + ) ); + wp_update_post( array( + 'ID' => $bluehost_id, + 'post_status' => 'publish', + ) ); + + $fixtures['pledges']['10up'] = get_post( $tenup_id ); + $fixtures['pledges']['bluehost'] = get_post( $bluehost_id ); + + return $fixtures; +} + +/** + * Sets up the database before a test is ran. + * + * Call in `set_up()`. + */ +function database_set_up( int $jane_id, int $ashish_id ) : void { + global $wpdb; + + $wpdb->query( 'TRUNCATE TABLE `bpmain_bp_xprofile_data` ' ); + + $wpdb->query( $wpdb->prepare( " + INSERT INTO `bpmain_bp_xprofile_data` + (`id`, `field_id`, `user_id`, `value`, `last_updated`) + VALUES + (NULL, 29, %d, '40', '2019-12-02 10:00:00' ), + (NULL, 30, %d, 'a:1:{i:0;s:9:\"Core Team\";}', '2019-12-03 11:00:00' ), + (NULL, 29, %d, '35', '2019-12-02 10:00:00' ), + (NULL, 30, %d, 'a:1:{i:0;s:18:\"Documentation Team\";}', '2019-12-03 11:00:00' )", + $jane_id, + $jane_id, + $ashish_id, + $ashish_id + ) ); +} + +/** + * Tears down the database after all tests in a class have ran. + * + * Call in `tear_down_after_class()`. + */ +function database_tear_down_after_class() : void { + global $wpdb; + + $wpdb->query( 'DROP TABLE `bpmain_bp_xprofile_data` ' ); +} diff --git a/plugins/wporg-5ftf/tests/test-contributor.php b/plugins/wporg-5ftf/tests/test-contributor.php index aec7ce6..27c71e1 100644 --- a/plugins/wporg-5ftf/tests/test-contributor.php +++ b/plugins/wporg-5ftf/tests/test-contributor.php @@ -1,6 +1,7 @@ query( " - CREATE TABLE `bpmain_bp_xprofile_data` ( - `id` bigint unsigned NOT NULL AUTO_INCREMENT, - `field_id` bigint unsigned NOT NULL DEFAULT '0', - `user_id` bigint unsigned NOT NULL DEFAULT '0', - `value` longtext NOT NULL, - `last_updated` datetime NOT NULL DEFAULT '1970-01-01 00:00:00', - PRIMARY KEY (`id`), - KEY `field_id` (`field_id`), - KEY `user_id` (`user_id`) - ) - ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3 - " ); - - self::$user_jane_id = self::factory()->user->create( array( - 'user_login' => 'jane', - 'user_email' => 'jane@example.org', - ) ); - - self::$user_ashish_id = self::factory()->user->create( array( - 'user_login' => 'ashish', - 'user_email' => 'ashish@example.org', - ) ); - - self::$page_for_organizations = get_post( self::factory()->post->create( array( - 'post_type' => 'page', - 'post_title' => 'For Organizations', - 'post_status' => 'publish', - ) ) ); - - $GLOBALS['post'] = self::$page_for_organizations; // `create_new_pledge()` assumes this exists. - - self::$pledge_10up_id = Pledge\create_new_pledge( '10up' ); - self::$pledge_bluehost_id = Pledge\create_new_pledge( 'BlueHost' ); - - wp_update_post( array( - 'ID' => self::$pledge_bluehost_id, - 'post_status' => 'publish', - ) ); - wp_update_post( array( - 'ID' => self::$pledge_10up_id, - 'post_status' => 'publish', - ) ); + $fixtures = TestHelpers\database_setup_before_class( self::factory() ); + self::$users = $fixtures['users']; + self::$pages = $fixtures['pages']; + self::$pledges = $fixtures['pledges']; } /** * Run before every test. */ public function set_up() { - global $wpdb; - parent::set_up(); - - $wpdb->query( 'TRUNCATE TABLE `bpmain_bp_xprofile_data` ' ); - - $wpdb->query( $wpdb->prepare( " - INSERT INTO `bpmain_bp_xprofile_data` - (`id`, `field_id`, `user_id`, `value`, `last_updated`) - VALUES - (NULL, 29, %d, '40', '2019-12-02 10:00:00' ), - (NULL, 30, %d, 'a:1:{i:0;s:9:\"Core Team\";}', '2019-12-03 11:00:00' ), - (NULL, 29, %d, '35', '2019-12-02 10:00:00' ), - (NULL, 30, %d, 'a:1:{i:0;s:18:\"Documentation Team\";}', '2019-12-03 11:00:00' )", - self::$user_jane_id, - self::$user_jane_id, - self::$user_ashish_id, - self::$user_ashish_id - ) ); - + TestHelpers\database_set_up( self::$users['jane']->ID, self::$users['ashish']->ID ); reset_phpmailer_instance(); } @@ -104,11 +44,8 @@ class Test_Contributor extends WP_UnitTestCase { * Run once after all tests are finished. */ public static function tear_down_after_class() { - global $wpdb; - parent::tear_down_after_class(); - - $wpdb->query( 'DROP TABLE `bpmain_bp_xprofile_data` ' ); + TestHelpers\database_tear_down_after_class(); } /** @@ -118,10 +55,10 @@ class Test_Contributor extends WP_UnitTestCase { public function test_data_reset_once_no_active_sponsors() : void { // Setup scenario where Jane is sponsored by two companies. $mailer = tests_retrieve_phpmailer_instance(); - $jane = get_user_by( 'id', self::$user_jane_id ); + $jane = self::$users['jane']; $jane_contribution = XProfile\get_contributor_user_data( $jane->ID ); - $tenup = get_post( self::$pledge_10up_id ); - $bluehost = get_post( self::$pledge_bluehost_id ); + $tenup = self::$pledges['10up']; + $bluehost = self::$pledges['bluehost']; $tenup_contributors = Contributor\add_pledge_contributors( $tenup->ID, array( $jane->user_login ) ); $bluehost_contributors = Contributor\add_pledge_contributors( $bluehost->ID, array( $jane->user_login ) ); $tenup_jane_id = $tenup_contributors[ $jane->user_login ]; @@ -182,9 +119,9 @@ class Test_Contributor extends WP_UnitTestCase { public function test_data_not_reset_when_unconfirmed_sponsor() : void { // Setup scenario where Jane was invited to join a company but didn't respond. $mailer = tests_retrieve_phpmailer_instance(); - $jane = get_user_by( 'id', self::$user_jane_id ); + $jane = self::$users['jane']; $jane_contribution = XProfile\get_contributor_user_data( $jane->ID ); - $tenup = get_post( self::$pledge_10up_id ); + $tenup = self::$pledges['10up']; $tenup_contributors = Contributor\add_pledge_contributors( $tenup->ID, array( $jane->user_login ) ); $tenup_jane_id = $tenup_contributors[ $jane->user_login ]; @@ -221,11 +158,11 @@ class Test_Contributor extends WP_UnitTestCase { public function test_data_reset_when_single_contributor_removed_from_pledge() : void { // Setup scenario where Jane and Ashish are sponsored by a company. $mailer = tests_retrieve_phpmailer_instance(); - $jane = get_user_by( 'id', self::$user_jane_id ); + $jane = self::$users['jane']; $jane_contribution = XProfile\get_contributor_user_data( $jane->ID ); - $ashish = get_user_by( 'id', self::$user_ashish_id ); + $ashish = self::$users['ashish']; $ashish_contribution = XProfile\get_contributor_user_data( $ashish->ID ); - $tenup = get_post( self::$pledge_10up_id ); + $tenup = self::$pledges['10up']; $tenup_contributors = Contributor\add_pledge_contributors( $tenup->ID, array( $jane->user_login, $ashish->user_login ) ); $tenup_jane_id = $tenup_contributors[ $jane->user_login ]; $tenup_ashish_id = $tenup_contributors[ $ashish->user_login ];