factory->{$factory_name} = new $factory_class( $this->factory ); } $this->factory->shipping_zone->createLegacyFlatRate(); // Create test users. $this->shop_manager = $this->factory->user->create( [ 'role' => 'shop_manager' ] ); $this->customer = $this->factory->customer->create(); // For these tests, we are not concerned with Approved Download Directory functionality. wc_get_container()->get( Download_Directories::class )->set_mode( Download_Directories::MODE_DISABLED ); // Clear cached schema. $this->clearSchema(); } public function tearDown(): void { global $wpdb; \WC()->cart->empty_cart( true ); $this->factory->product->deleteAttributes(); // Clean WooCommerce lookup tables that are not covered by // WPBrowser's transaction rollback, preventing stale data // from leaking into subsequent test queries. $wpdb->query( "DELETE FROM {$wpdb->prefix}wc_product_meta_lookup" ); $wpdb->query( "DELETE FROM {$wpdb->prefix}wc_product_attributes_lookup" ); // then parent::tearDown(); } /** * Logs in as a "shop manager" */ protected function loginAsShopManager() { $this->loginAs( $this->shop_manager ); } /** * Logs in as a "customer" */ protected function loginAsCustomer() { $this->loginAs( $this->customer ); } /** * Logs in as a specific user */ protected function loginAs( $customer_id = 0 ) { wp_set_current_user( $customer_id ); \WC()->customer = new \WC_Customer( get_current_user_id(), true ); \WC()->session->init(); } /** * Logs out current user. */ protected function logout() { wp_set_current_user( 0 ); } /** * The death of `! empty( $v ) ? apply_filters( $v ) : null;` * * @param array|mixed $possible Variable whose existence has to be verified, or * an array containing the variable followed by a decorated value to be returned. * @param mixed $default Default value to be returned if $possible doesn't exist. * * @return mixed */ protected function maybe( $possible, $custom_default = null ) { if ( null === $custom_default ) { $default = static::IS_NULL; } else { $default = $custom_default; } if ( is_array( $possible ) && 2 === count( $possible ) ) { list( $possible, $decorated ) = $possible; } else { $decorated = $possible; } return ! empty( $possible ) ? $decorated : $default; } }