diff --git a/phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertArrayHasKeySniff.php b/phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertArrayHasKeySniff.php new file mode 100644 index 0000000000..16fd2dff29 --- /dev/null +++ b/phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertArrayHasKeySniff.php @@ -0,0 +1,169 @@ +assertTrue(array_key_exists(...)) and converts to $this->assertArrayHasKey(...). + * + * @package Formidable\Sniffs + */ + +namespace Formidable\Sniffs\PHPUnit; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + +/** + * Converts assertTrue(array_key_exists()) to assertArrayHasKey(). + * + * Bad: + * $this->assertTrue( array_key_exists( 'key', $array ) ); + * + * Good: + * $this->assertArrayHasKey( 'key', $array ); + */ +class PreferAssertArrayHasKeySniff implements Sniff { + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() { + return array( T_STRING ); + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in the stack passed in $tokens. + * + * @return void + */ + public function process( File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + $token = $tokens[ $stackPtr ]; + + // Check for assertTrue or assertFalse. + $methodName = strtolower( $token['content'] ); + + if ( 'asserttrue' !== $methodName && 'assertfalse' !== $methodName ) { + return; + } + + // Check that this is a method call ($this-> or self:: or static::). + $prevToken = $phpcsFile->findPrevious( T_WHITESPACE, $stackPtr - 1, null, true ); + + if ( false === $prevToken ) { + return; + } + + if ( $tokens[ $prevToken ]['code'] !== T_OBJECT_OPERATOR && $tokens[ $prevToken ]['code'] !== T_DOUBLE_COLON ) { + return; + } + + // Find the opening parenthesis. + $openParen = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); + + if ( false === $openParen || $tokens[ $openParen ]['code'] !== T_OPEN_PARENTHESIS ) { + return; + } + + // Find the first argument (skip whitespace). + $firstArg = $phpcsFile->findNext( T_WHITESPACE, $openParen + 1, null, true ); + + if ( false === $firstArg ) { + return; + } + + // Check if first argument is array_key_exists function call. + if ( $tokens[ $firstArg ]['code'] !== T_STRING ) { + return; + } + + $functionName = strtolower( $tokens[ $firstArg ]['content'] ); + + if ( 'array_key_exists' !== $functionName ) { + return; + } + + // Find the opening parenthesis of array_key_exists. + $funcOpenParen = $phpcsFile->findNext( T_WHITESPACE, $firstArg + 1, null, true ); + + if ( false === $funcOpenParen || $tokens[ $funcOpenParen ]['code'] !== T_OPEN_PARENTHESIS ) { + return; + } + + // Find the matching closing parenthesis of array_key_exists. + if ( ! isset( $tokens[ $funcOpenParen ]['parenthesis_closer'] ) ) { + return; + } + $funcCloseParen = $tokens[ $funcOpenParen ]['parenthesis_closer']; + + // Find the closing parenthesis of assertTrue/assertFalse. + if ( ! isset( $tokens[ $openParen ]['parenthesis_closer'] ) ) { + return; + } + $assertCloseParen = $tokens[ $openParen ]['parenthesis_closer']; + + // Determine the new method name. + $newMethodName = 'asserttrue' === $methodName ? 'assertArrayHasKey' : 'assertArrayNotHasKey'; + + $fix = $phpcsFile->addFixableError( + 'Use %s() instead of %s(array_key_exists()).', + $stackPtr, + 'Found', + array( $newMethodName, $token['content'] ) + ); + + if ( true === $fix ) { + $this->applyFix( $phpcsFile, $stackPtr, $openParen, $firstArg, $funcOpenParen, $funcCloseParen, $assertCloseParen, $newMethodName ); + } + } + + /** + * Apply the fix to convert assertTrue(array_key_exists($key, $array)) to assertArrayHasKey($key, $array). + * + * @param File $phpcsFile The file being scanned. + * @param int $methodNamePtr The assertTrue/assertFalse token position. + * @param int $openParen The opening parenthesis of assertTrue. + * @param int $funcPtr The array_key_exists token position. + * @param int $funcOpenParen The opening parenthesis of array_key_exists. + * @param int $funcCloseParen The closing parenthesis of array_key_exists. + * @param int $assertCloseParen The closing parenthesis of assertTrue. + * @param string $newMethodName The new method name. + * + * @return void + */ + private function applyFix( File $phpcsFile, $methodNamePtr, $openParen, $funcPtr, $funcOpenParen, $funcCloseParen, $assertCloseParen, $newMethodName ) { + $tokens = $phpcsFile->getTokens(); + $fixer = $phpcsFile->fixer; + + $fixer->beginChangeset(); + + // Replace the method name. + $fixer->replaceToken( $methodNamePtr, $newMethodName ); + + // Remove everything from after ( to before the inner content of array_key_exists. + // We want to keep: assertArrayHasKey( ) + // Remove: array_key_exists( + for ( $i = $openParen + 1; $i <= $funcOpenParen; $i++ ) { + $fixer->replaceToken( $i, '' ); + } + + // Remove the closing parenthesis of array_key_exists. + $fixer->replaceToken( $funcCloseParen, '' ); + + // Remove whitespace between array_key_exists's closing paren and assertTrue's closing paren. + for ( $i = $funcCloseParen + 1; $i < $assertCloseParen; $i++ ) { + if ( $tokens[ $i ]['code'] === T_WHITESPACE ) { + $fixer->replaceToken( $i, '' ); + } else { + // Stop if we hit a non-whitespace token (like a comma for a second argument). + break; + } + } + + $fixer->endChangeset(); + } +} diff --git a/phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertIsArraySniff.php b/phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertIsArraySniff.php new file mode 100644 index 0000000000..83d4679a59 --- /dev/null +++ b/phpcs-sniffs/Formidable/Sniffs/PHPUnit/PreferAssertIsArraySniff.php @@ -0,0 +1,196 @@ +assertTrue(is_array(...)) and converts to $this->assertIsArray(...). + * Also handles is_object, is_string, and assertFalse variants. + * + * @package Formidable\Sniffs + */ + +namespace Formidable\Sniffs\PHPUnit; + +use PHP_CodeSniffer\Sniffs\Sniff; +use PHP_CodeSniffer\Files\File; + +/** + * Converts assertTrue(is_array/is_object/is_string()) to specific assert methods. + * + * Bad: + * $this->assertTrue( is_array( $value ) ); + * $this->assertTrue( is_object( $value ) ); + * $this->assertTrue( is_string( $value ) ); + * + * Good: + * $this->assertIsArray( $value ); + * $this->assertIsObject( $value ); + * $this->assertIsString( $value ); + */ +class PreferAssertIsArraySniff implements Sniff { + + /** + * Mapping of is_* functions to their assert method names. + * + * @var array + */ + const FUNCTION_MAP = array( + 'is_array' => array( 'assertIsArray', 'assertIsNotArray' ), + 'is_object' => array( 'assertIsObject', 'assertIsNotObject' ), + 'is_string' => array( 'assertIsString', 'assertIsNotString' ), + 'is_numeric' => array( 'assertIsNumeric', 'assertIsNotNumeric' ), + ); + + /** + * Returns an array of tokens this test wants to listen for. + * + * @return array + */ + public function register() { + return array( T_STRING ); + } + + /** + * Processes this test, when one of its tokens is encountered. + * + * @param File $phpcsFile The file being scanned. + * @param int $stackPtr The position of the current token in the stack passed in $tokens. + * + * @return void + */ + public function process( File $phpcsFile, $stackPtr ) { + $tokens = $phpcsFile->getTokens(); + $token = $tokens[ $stackPtr ]; + + // Check for assertTrue or assertFalse. + $methodName = strtolower( $token['content'] ); + + if ( 'asserttrue' !== $methodName && 'assertfalse' !== $methodName ) { + return; + } + + // Check that this is a method call ($this-> or self:: or static::). + $prevToken = $phpcsFile->findPrevious( T_WHITESPACE, $stackPtr - 1, null, true ); + + if ( false === $prevToken ) { + return; + } + + if ( $tokens[ $prevToken ]['code'] !== T_OBJECT_OPERATOR && $tokens[ $prevToken ]['code'] !== T_DOUBLE_COLON ) { + return; + } + + // Find the opening parenthesis. + $openParen = $phpcsFile->findNext( T_WHITESPACE, $stackPtr + 1, null, true ); + + if ( false === $openParen || $tokens[ $openParen ]['code'] !== T_OPEN_PARENTHESIS ) { + return; + } + + // Find the first argument (skip whitespace). + $firstArg = $phpcsFile->findNext( T_WHITESPACE, $openParen + 1, null, true ); + + if ( false === $firstArg ) { + return; + } + + // Check if first argument is a supported is_* function call. + if ( $tokens[ $firstArg ]['code'] !== T_STRING ) { + return; + } + + $functionName = strtolower( $tokens[ $firstArg ]['content'] ); + + if ( ! isset( self::FUNCTION_MAP[ $functionName ] ) ) { + return; + } + + // Find the opening parenthesis of the is_* function. + $isFuncOpenParen = $phpcsFile->findNext( T_WHITESPACE, $firstArg + 1, null, true ); + + if ( false === $isFuncOpenParen || $tokens[ $isFuncOpenParen ]['code'] !== T_OPEN_PARENTHESIS ) { + return; + } + + // Find the matching closing parenthesis of the is_* function. + if ( ! isset( $tokens[ $isFuncOpenParen ]['parenthesis_closer'] ) ) { + return; + } + $isFuncCloseParen = $tokens[ $isFuncOpenParen ]['parenthesis_closer']; + + // Find the closing parenthesis of assertTrue/assertFalse. + if ( ! isset( $tokens[ $openParen ]['parenthesis_closer'] ) ) { + return; + } + $assertCloseParen = $tokens[ $openParen ]['parenthesis_closer']; + + // Check that the is_* function is the only argument (no && or || after it). + // The next non-whitespace token after is_*()'s closing paren should be assertTrue's closing paren. + $nextAfterFunc = $phpcsFile->findNext( T_WHITESPACE, $isFuncCloseParen + 1, $assertCloseParen, true ); + + if ( false !== $nextAfterFunc ) { + // There's something else after the is_* call - skip this. + return; + } + + // Determine the new method name. + $assertMethods = self::FUNCTION_MAP[ $functionName ]; + $newMethodName = 'asserttrue' === $methodName ? $assertMethods[0] : $assertMethods[1]; + + $fix = $phpcsFile->addFixableError( + 'Use %s() instead of %s(%s()).', + $stackPtr, + 'Found', + array( $newMethodName, $token['content'], $functionName ) + ); + + if ( true === $fix ) { + $this->applyFix( $phpcsFile, $stackPtr, $openParen, $firstArg, $isFuncOpenParen, $isFuncCloseParen, $assertCloseParen, $newMethodName ); + } + } + + /** + * Apply the fix to convert assertTrue(is_*($x)) to assert*($x). + * + * @param File $phpcsFile The file being scanned. + * @param int $methodNamePtr The assertTrue/assertFalse token position. + * @param int $openParen The opening parenthesis of assertTrue. + * @param int $isFuncPtr The is_* function token position. + * @param int $isFuncOpenParen The opening parenthesis of is_*. + * @param int $isFuncCloseParen The closing parenthesis of is_*. + * @param int $assertCloseParen The closing parenthesis of assertTrue. + * @param string $newMethodName The new method name. + * + * @return void + */ + private function applyFix( File $phpcsFile, $methodNamePtr, $openParen, $isFuncPtr, $isFuncOpenParen, $isFuncCloseParen, $assertCloseParen, $newMethodName ) { + $tokens = $phpcsFile->getTokens(); + $fixer = $phpcsFile->fixer; + + $fixer->beginChangeset(); + + // Replace the method name. + $fixer->replaceToken( $methodNamePtr, $newMethodName ); + + // Remove everything from after ( to before the inner content of is_*. + // We want to keep: assertIs*( ) + // Remove: is_*( + for ( $i = $openParen + 1; $i <= $isFuncOpenParen; $i++ ) { + $fixer->replaceToken( $i, '' ); + } + + // Remove the closing parenthesis of is_* and any whitespace after it. + $fixer->replaceToken( $isFuncCloseParen, '' ); + + // Remove whitespace between is_*'s closing paren and assertTrue's closing paren. + for ( $i = $isFuncCloseParen + 1; $i < $assertCloseParen; $i++ ) { + if ( $tokens[ $i ]['code'] === T_WHITESPACE ) { + $fixer->replaceToken( $i, '' ); + } else { + // Stop if we hit a non-whitespace token (like a comma for a second argument). + break; + } + } + + $fixer->endChangeset(); + } +} diff --git a/phpcs-sniffs/Formidable/ruleset.xml b/phpcs-sniffs/Formidable/ruleset.xml index be3a08860d..2c0f54c1a3 100644 --- a/phpcs-sniffs/Formidable/ruleset.xml +++ b/phpcs-sniffs/Formidable/ruleset.xml @@ -36,6 +36,10 @@ + + + + diff --git a/tests/phpunit/applications/test_FrmApplicationApi.php b/tests/phpunit/applications/test_FrmApplicationApi.php index c0d359139a..5fce29ff20 100644 --- a/tests/phpunit/applications/test_FrmApplicationApi.php +++ b/tests/phpunit/applications/test_FrmApplicationApi.php @@ -12,19 +12,19 @@ public function test_get_api_info() { $api = new FrmApplicationApi(); $applications = $api->get_api_info(); - $this->assertTrue( is_array( $applications ) ); + $this->assertIsArray( $applications ); if ( ! empty( $applications['error'] ) ) { $this->markTestSkipped( 'We cannot currently reach the API, so skip the test.' ); } $business_hours_id = 28067848; - $this->assertTrue( array_key_exists( $business_hours_id, $applications ) ); + $this->assertArrayHasKey( $business_hours_id, $applications ); $business_hours = $applications[ $business_hours_id ]; - $this->assertTrue( is_array( $business_hours ) ); + $this->assertIsArray( $business_hours ); $this->assertEquals( 'business-hours-template', $business_hours['slug'] ); - $this->assertTrue( array_key_exists( 'name', $business_hours ) ); + $this->assertArrayHasKey( 'name', $business_hours ); $this->assertNotEmpty( $business_hours['name'] ); } } diff --git a/tests/phpunit/applications/test_FrmApplicationsController.php b/tests/phpunit/applications/test_FrmApplicationsController.php index d8bfb9ec76..2b4fdc41dd 100644 --- a/tests/phpunit/applications/test_FrmApplicationsController.php +++ b/tests/phpunit/applications/test_FrmApplicationsController.php @@ -32,7 +32,7 @@ public function test_get_prepared_template_data() { $template = reset( $data ); $this->assertIsArray( $template ); - $this->assertTrue( array_key_exists( 'key', $template ) ); + $this->assertArrayHasKey( 'key', $template ); $this->assertNotEmpty( $template['key'] ); } diff --git a/tests/phpunit/bootstrap.php b/tests/phpunit/bootstrap.php index 67bbe64daf..528791cad5 100644 --- a/tests/phpunit/bootstrap.php +++ b/tests/phpunit/bootstrap.php @@ -32,8 +32,5 @@ require_once __DIR__ . '/base/FrmUnitTest.php'; require_once __DIR__ . '/base/FrmAjaxUnitTest.php'; -// include our Stripe unit helper base class -require_once __DIR__ . '/stripe/FrmStrpLiteUnitTest.php'; - // Ensure that the plugin has been installed and activated. FrmUnitTest::frm_install(); diff --git a/tests/phpunit/entries/test_FrmEntry.php b/tests/phpunit/entries/test_FrmEntry.php index c6d55c21dc..7a1dcf0424 100644 --- a/tests/phpunit/entries/test_FrmEntry.php +++ b/tests/phpunit/entries/test_FrmEntry.php @@ -22,7 +22,7 @@ public function test_is_duplicate() { $entry = $this->factory->entry->create_object( $entry_data ); $this->assertNotEmpty( $entry ); - $this->assertTrue( is_numeric( $entry ) ); + $this->assertIsNumeric( $entry ); $entry = $this->factory->entry->create_object( $entry_data ); $this->assertEmpty( $entry, 'Failed to detect duplicate entry' ); diff --git a/tests/phpunit/fields/test_FrmField.php b/tests/phpunit/fields/test_FrmField.php index a1d334d196..7ac580a923 100644 --- a/tests/phpunit/fields/test_FrmField.php +++ b/tests/phpunit/fields/test_FrmField.php @@ -22,7 +22,7 @@ public function test_create() { 'form_id' => $form_id, ) ); - $this->assertTrue( is_numeric( $field_id ) ); + $this->assertIsNumeric( $field_id ); $this->assertTrue( $field_id > 0 ); } } diff --git a/tests/phpunit/fields/test_FrmFieldsAjax.php b/tests/phpunit/fields/test_FrmFieldsAjax.php index 2172428eb7..5991a844c6 100644 --- a/tests/phpunit/fields/test_FrmFieldsAjax.php +++ b/tests/phpunit/fields/test_FrmFieldsAjax.php @@ -21,7 +21,7 @@ public function setUp(): void { $form = $this->factory->form->create_and_get(); $this->assertNotEmpty( $form ); $this->form_id = $form->id; - $this->assertTrue( is_numeric( $this->form_id ) ); + $this->assertIsNumeric( $this->form_id ); } /** @@ -44,12 +44,12 @@ public function test_create() { global $wpdb; $this->field_id = $wpdb->insert_id; - $this->assertTrue( is_numeric( $this->field_id ) ); + $this->assertIsNumeric( $this->field_id ); $this->assertNotEmpty( $this->field_id ); // make sure the field exists $field = FrmField::getOne( $this->field_id ); - $this->assertTrue( is_object( $field ) ); + $this->assertIsObject( $field ); } /** @@ -92,7 +92,7 @@ public function test_duplicating_text_field() { // Make sure the field exists. $field = FrmField::getOne( $newest_field_id ); - $this->assertTrue( is_object( $field ), 'Field id ' . $newest_field_id . ' does not exist' ); + $this->assertIsObject( $field, 'Field id ' . $newest_field_id . ' does not exist' ); $this->assertEquals( $format, $field->field_options['format'] ); self::check_in_section_variable( $field, 0 ); @@ -132,7 +132,7 @@ protected function check_field_prior_to_duplication( $field ) { * @return void */ protected function check_if_field_id_is_created_correctly( $newest_field_id ) { - $this->assertTrue( is_numeric( $newest_field_id ) ); + $this->assertIsNumeric( $newest_field_id ); $this->assertNotEmpty( $newest_field_id ); } diff --git a/tests/phpunit/forms/test_FrmForm.php b/tests/phpunit/forms/test_FrmForm.php index 5069fbfde9..e401740806 100644 --- a/tests/phpunit/forms/test_FrmForm.php +++ b/tests/phpunit/forms/test_FrmForm.php @@ -16,7 +16,7 @@ public function setUp(): void { public function test_create() { $values = FrmFormsHelper::setup_new_vars( false ); $form_id = FrmForm::create( $values ); - $this->assertTrue( is_numeric( $form_id ) ); + $this->assertIsNumeric( $form_id ); $this->assertNotEmpty( $form_id ); } @@ -26,7 +26,7 @@ public function test_create() { public function test_duplicate() { $form = $this->factory->form->get_object_by_id( $this->all_fields_form_key ); $id = FrmForm::duplicate( $form->id ); - $this->assertTrue( is_numeric( $id ) ); + $this->assertIsNumeric( $id ); $this->assertNotEmpty( $id ); // check the number of form actions diff --git a/tests/phpunit/misc/test_FrmAntiSpam.php b/tests/phpunit/misc/test_FrmAntiSpam.php index 1a5a322014..8ea41d0941 100644 --- a/tests/phpunit/misc/test_FrmAntiSpam.php +++ b/tests/phpunit/misc/test_FrmAntiSpam.php @@ -18,7 +18,7 @@ public function setUp(): void { */ public function test_get() { $token_string = $this->run_private_method( array( $this->antispam, 'get' ) ); - $this->assertTrue( is_string( $token_string ) ); + $this->assertIsString( $token_string ); $this->assertTrue( strlen( $token_string ) >= 32 ); } @@ -27,7 +27,7 @@ public function test_get() { */ public function test_get_antispam_secret_key() { $secret_key = $this->run_private_method( array( $this->antispam, 'get_antispam_secret_key' ) ); - $this->assertTrue( is_string( $secret_key ) ); + $this->assertIsString( $secret_key ); $this->assertTrue( strlen( $secret_key ) >= 32 ); } @@ -36,7 +36,7 @@ public function test_get_antispam_secret_key() { */ public function test_get_valid_tokens() { $valid_tokens = $this->run_private_method( array( $this->antispam, 'get_valid_tokens' ) ); - $this->assertTrue( is_array( $valid_tokens ) ); + $this->assertIsArray( $valid_tokens ); $this->assertTrue( count( $valid_tokens ) >= 1 ); } diff --git a/tests/phpunit/misc/test_FrmAppHelper.php b/tests/phpunit/misc/test_FrmAppHelper.php index 096e371ca4..16fd4dcb40 100644 --- a/tests/phpunit/misc/test_FrmAppHelper.php +++ b/tests/phpunit/misc/test_FrmAppHelper.php @@ -96,7 +96,7 @@ public function test_make_affiliate_url() { public function test_get_settings() { $settings = FrmAppHelper::get_settings(); $this->assertNotEmpty( $settings ); - $this->assertTrue( is_object( $settings ) ); + $this->assertIsObject( $settings ); $this->assertNotEmpty( $settings->success_msg ); } @@ -536,7 +536,7 @@ public function test_get_unique_key() { $name = 123; $key = FrmAppHelper::get_unique_key( $name, $table_name, $column ); - $this->assertFalse( is_numeric( $key ), 'key should never be numeric.' ); + $this->assertIsNotNumeric( $key, 'key should never be numeric.' ); $super_long_form_key = 'formkeywithlikeseventycharacterscanyouevenimaginehavingthismanyletters'; // reserve the form key so one has to be generated with this as the base. diff --git a/tests/phpunit/stripe/FrmStrpLiteUnitTest.php b/tests/phpunit/stripe/FrmStrpLiteUnitTest.php deleted file mode 100644 index 119da4e1af..0000000000 --- a/tests/phpunit/stripe/FrmStrpLiteUnitTest.php +++ /dev/null @@ -1,456 +0,0 @@ -set_private_property( 'FrmStrpLiteAppHelper', 'settings', null ); - } - - protected function initialize_connect_api( $user_id = 1 ) { - wp_set_current_user( $user_id ); - - if ( 1 === $user_id ) { - $this->set_user_by_role( 'administrator' ); - } - - update_option( 'frm_strp_connect_details_submitted_test', true, 'no' ); - FrmStrpLiteAppHelper::should_use_stripe_connect(); // this line loads FrmStrpLiteConnectApiAdapter if it does not exist. - - if ( ! empty( self::$shared_account_id ) ) { - return $this->reuse_static_account_details(); - } - - $initialized = $this->run_private_method( array( 'FrmStrpLiteConnectHelper', 'initialize' ), array() ); - - if ( ! $initialized ) { - $this->fail(); - } - - $this->set_static_account_details_for_reuse( $initialized ); - return $initialized; - } - - /** - * Fake the payload on repeat calls to avoid creating too many accounts. - */ - private function reuse_static_account_details() { - $initialized = new stdClass(); - $initialized->account_id = self::$shared_account_id; - $initialized->connect_url = self::$shared_connect_url; - $initialized->password = self::$shared_server_side_token; - $this->account_id = self::$shared_account_id; - update_option( 'frm_strp_connect_account_id_test', self::$shared_account_id, 'no' ); - update_option( 'frm_strp_connect_client_password_test', self::$shared_client_side_token, 'no' ); - update_option( 'frm_strp_connect_server_password_test', self::$shared_server_side_token, 'no' ); - return $initialized; - } - - /** - * @param object $initialized - */ - private function set_static_account_details_for_reuse( $initialized ) { - self::$shared_account_id = $initialized->account_id; - self::$shared_connect_url = $initialized->connect_url; - self::$shared_server_side_token = $initialized->password; - self::$shared_client_side_token = get_option( 'frm_strp_connect_client_password_test' ); - $this->account_id = $initialized->account_id; - } - - protected function get_customer( $options = array() ) { - $this->include_adapter(); - return FrmStrpLiteConnectApiAdapter::get_customer( $options ); - } - - protected function include_adapter() { - if ( ! class_exists( 'FrmStrpLiteConnectApiAdapter' ) ) { - require dirname( __DIR__ ) . '/helpers/FrmStrpLiteConnectApiAdapter.php'; - } - } - - protected function add_card( $customer_id ) { - $stripe = $this->get_authenticated_stripe_client(); - $card = $stripe->customers->createSource( - $customer_id, - array( - 'source' => 'tok_mastercard', - ), - $this->get_stripe_account_id_details() - ); - $stripe->customers->update( - $customer_id, - array( - 'default_source' => $card->id, - ), - $this->get_stripe_account_id_details() - ); - return $card; - } - - protected function get_stripe_account_id_details() { - return array( - 'stripe_account' => self::$shared_account_id, - ); - } - - protected function get_plan_options() { - $unique = uniqid(); - $default_options = array( - 'amount' => 1000, - 'interval' => 'year', - 'interval_count' => 1, - 'currency' => 'usd', - 'id' => 'my_annual_test_subscription_' . $unique, - 'name' => 'My Annual Test Subscription (' . $unique . ')', - 'trial_period_days' => 10, - ); - return array_filter( - array_merge( - $default_options, - $this->plan_options ?? array() - ) - ); - } - - protected function create_payment_intent_with_action_id( $action_id ) { - $stripe = $this->get_authenticated_stripe_client(); - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase - return $stripe->paymentIntents->create( $this->get_new_charge_data( $action_id ) ); - } - - /** - * @param array $metadata - * - * @return object - */ - protected function create_payment_intent( $metadata = array() ) { - $customer_id = $this->get_customer_id(); - $payment_method = $this->create_payment_method(); - $new_charge = array( - 'customer' => $customer_id, - 'currency' => 'usd', - 'amount' => 1000, - 'payment_method' => $payment_method->id, - 'confirm' => true, - 'metadata' => $metadata, - ); - return FrmStrpLiteConnectHelper::run_new_charge( $new_charge ); - } - - /** - * @return string Customer ID. - */ - protected function get_customer_id() { - $options = array(); - return FrmStrpLiteConnectHelper::get_customer_id( $options ); - } - - protected function get_new_charge_data( $action_id = 0 ) { - $new_charge = array( - 'amount' => 1000, - 'currency' => 'usd', - 'payment_method_types' => array( 'card' ), - 'confirm' => true, - 'capture_method' => 'manual', - 'payment_method' => $this->create_payment_method()->id, - ); - - if ( $action_id ) { - $new_charge['metadata'] = array( - 'action' => $action_id, - ); - } - - return $new_charge; - } - - /** - * @return array - */ - protected function get_test_credit_card() { - return array( - 'number' => $this->get_test_credit_card_number(), - 'exp_month' => 12, - 'exp_year' => gmdate( 'Y' ), - 'cvc' => '314', - ); - } - - /** - * @return string - */ - protected function get_test_credit_card_number() { - return $this->use_test_credit_card_number ?? '4242424242424242'; - } - - /** - * Make sure that the frm_payments table gets created. - */ - protected function run_migrations() { - $db = new FrmTransDb(); - $db->upgrade(); - } - - protected function return_url( $entry ) { - $atts = array( - 'entry' => $entry, - ); - return $this->run_private_method( array( 'FrmStrpLiteAuth', 'return_url' ), array( $atts ) ); - } - - protected function create_subscription() { - $this->customer_id = $this->get_customer_id(); - $this->add_card( $this->customer_id ); - $plan = $this->get_plan_options(); - $plan_id = FrmStrpLiteConnectHelper::maybe_create_plan( $plan ); - $new_charge = $this->get_subscription_charge_options( $this->customer_id, $plan_id ); - return FrmStrpLiteConnectHelper::create_subscription( $new_charge ); - } - - private function get_subscription_charge_options( $customer_id, $plan_id ) { - $default_options = array( - 'customer' => $customer_id, - 'plan' => $plan_id, - 'payment_behavior' => 'allow_incomplete', - 'expand' => array( 'latest_invoice.payment_intent' ), - 'off_session' => true, - ); - return array_filter( - array_merge( - $default_options, - $this->subscription_charge_options ?? array() - ) - ); - } - - /** - * @return PaymentMethod - */ - protected function create_payment_method() { - $stripe = $this->get_authenticated_stripe_client(); - - $card_details = array( - 'type' => 'card', - 'card' => $this->get_test_credit_card(), - ); - - $account_details = $this->get_stripe_account_id_details(); - - // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase - return $stripe->paymentMethods->create( $card_details, $account_details ); - } - - /** - * @return array - */ - protected function prepare_new_charge_array() { - $customer_id = $this->get_customer_id(); - $this->add_card( $customer_id ); - return array( - 'customer' => $customer_id, - 'currency' => 'usd', - 'amount' => 1000, - 'capture' => false, - ); - } - - protected function assert_run_new_charge( $charge ) { - $this->assertTrue( is_object( $charge ) ); - $this->assertTrue( ! empty( $charge->id ) ); - $this->assertTrue( isset( $charge->object ) ); - $this->assertEquals( 'charge', $charge->object ); - $this->assertTrue( ! empty( $charge->status ) ); - } - - protected function assert_maybe_create_plan( $plan_id ) { - $this->assertTrue( is_string( $plan_id ) ); - $this->assertTrue( ! empty( $plan_id ) ); - } - - protected function assert_create_subscription( $subscription ) { - $this->assertTrue( is_object( $subscription ) ); - $this->assertTrue( ! empty( $subscription->current_period_start ) ); - $this->assertTrue( ! empty( $subscription->current_period_end ) ); - $this->assertTrue( ! empty( $subscription->status ) ); - $this->assertIsString( $subscription->status ); - $this->assertEquals( 'trialing', $subscription->status ); - $this->assertTrue( ! empty( $subscription->latest_invoice ) ); - $this->assertTrue( ! empty( $subscription->latest_invoice->status ) ); - $this->assertIsString( $subscription->latest_invoice->status ); - $this->assertEquals( 'paid', $subscription->latest_invoice->status ); - $this->assertTrue( ! empty( $subscription->latest_invoice->lines ) ); - $this->assertTrue( ! empty( $subscription->latest_invoice->lines->data ) ); - $this->assertIsArray( $subscription->latest_invoice->lines->data ); - $this->assertArrayHasKey( 0, $subscription->latest_invoice->lines->data ); - $this->assertTrue( ! empty( $subscription->latest_invoice->lines->data[0]->period ) ); - $this->assertTrue( ! empty( $subscription->latest_invoice->lines->data[0]->period->end ) ); - } - - protected function assert_get_customer_subscriptions( $subscriptions ) { - $this->assertTrue( is_object( $subscriptions ) ); - $this->assertTrue( isset( $subscriptions->data ) ); - $this->assertTrue( is_array( $subscriptions->data ) ); - $this->assertEquals( 1, count( $subscriptions->data ) ); - } - - protected function assert_create_intent( $intent ) { - $this->assertTrue( is_object( $intent ) ); - $this->assertTrue( ! empty( $intent->id ) ); - $this->assertTrue( ! empty( $intent->client_secret ) ); - } - - protected function assert_capture_intent( $captured ) { - $this->assertTrue( is_object( $captured ) ); - $this->assertEquals( 'succeeded', $captured->status ); - $this->assertTrue( isset( $captured->charges ) ); - $this->assertTrue( is_object( $captured->charges ) ); - $this->assertTrue( is_array( $captured->charges->data ) ); - - $charge = reset( $captured->charges->data ); - $this->assertTrue( ! empty( $charge->object ) ); - $this->assertTrue( ! empty( $charge->status ) ); - $this->assertTrue( isset( $charge->paid ) ); - $this->assertTrue( isset( $charge->captured ) ); - } - - protected function assert_get_intent( $payment_intent ) { - $this->assertIsObject( $payment_intent ); - $this->assertTrue( ! empty( $payment_intent->id ) ); - $this->assertStringStartsWith( 'pi_', $payment_intent->id ); - $this->assertTrue( isset( $payment_intent->metadata ) ); - $this->assertTrue( ! empty( $payment_intent->metadata->key ) ); - $this->assertEquals( 'value', $payment_intent->metadata->key ); - $this->assertTrue( ! empty( $payment_intent->status ) ); - $this->assertTrue( ! empty( $payment_intent->amount ) ); - $this->assertTrue( ! empty( $payment_intent->client_secret ) ); - $this->assertStringStartsWith( 'pi_', $payment_intent->client_secret ); - $this->assertStringContainsString( '_secret_', $payment_intent->client_secret ); - $this->assertTrue( ! empty( $payment_intent->charges ) ); - $this->assertIsObject( $payment_intent->charges ); - $this->assertTrue( ! empty( $payment_intent->charges->data ) ); - $this->assertIsArray( $payment_intent->charges->data ); - $charge = reset( $payment_intent->charges->data ); - $this->assertIsObject( $charge ); - $this->assertTrue( ! empty( $charge->id ) ); - $this->assertStringStartsWith( 'ch_', $charge->id ); - } - - protected function assert_confirm_intent( $confirmed ) { - $this->assertTrue( is_object( $confirmed ) ); - $this->assertTrue( ! empty( $confirmed->intent_id ) ); - $this->assertTrue( ! empty( $confirmed->next_action ) ); - $this->assertTrue( ! empty( $confirmed->next_action->redirect_to_url ) ); - $this->assertTrue( ! empty( $confirmed->next_action->redirect_to_url->url ) ); - } - - /** - * @param object $setup_intent - * - * @return void - */ - protected function assert_setup_intent( $setup_intent ) { - $this->assertIsObject( $setup_intent ); - $this->assertTrue( ! empty( $setup_intent->id ) ); - $this->assertStringStartsWith( 'seti_', $setup_intent->id ); - } - - /** - * @param int $test_mode 1 or 0, defines if we're trying to use Stripe in test or live mode. - */ - protected function update_stripe_settings_test_mode_flag( $test_mode ) { - $options = new stdClass(); - $options->test_mode = $test_mode; - $options->process = 'after'; - update_option( 'frm_strp_options', $options, 'no' ); - - $this->set_private_property( 'FrmStrpLiteAppHelper', 'settings', null ); - } - - /** - * @return WP_Post - */ - protected function get_stripe_action_with_a_plan() { - $action = $this->get_simple_stripe_action(); - $action->post_content['plan_id'] = $this->maybe_create_plan(); - $action->post_content['interval'] = 'year'; - $action->post_content['interval_count'] = 1; - $action->post_content['trial_period_days'] = 10; - return $action; - } - - /** - * @return WP_Post - */ - protected function get_simple_stripe_action() { - $action = $this->factory->post->create_and_get(); - $action->post_content = array( - 'currency' => 'usd', - ); - $this->action = $action; - return $action; - } - - /** - * @return false|string - */ - protected function maybe_create_plan() { - $this->get_customer(); - $plan = $this->get_plan_options(); - return FrmStrpLiteSubscriptionHelper::maybe_create_plan( $plan ); - } -} diff --git a/tests/phpunit/styles/test_FrmStyle.php b/tests/phpunit/styles/test_FrmStyle.php index 1806340f85..a9f05e3beb 100644 --- a/tests/phpunit/styles/test_FrmStyle.php +++ b/tests/phpunit/styles/test_FrmStyle.php @@ -77,7 +77,7 @@ public function test_sanitize_post_content() { $this->assertEquals( '12px', $sanitized_post_content['section_border_width'] ); $this->assertEquals( '16px', $sanitized_post_content['section_font_size'] ); $this->assertEquals( '.my-class { color: red; }', $sanitized_post_content['custom_css'] ); - $this->assertFalse( array_key_exists( 'unsupported_key', $sanitized_post_content ) ); + $this->assertArrayNotHasKey( 'unsupported_key', $sanitized_post_content ); } /** diff --git a/tests/phpunit/xml/test_FrmXMLHelper.php b/tests/phpunit/xml/test_FrmXMLHelper.php index a4cda44156..3843da049e 100644 --- a/tests/phpunit/xml/test_FrmXMLHelper.php +++ b/tests/phpunit/xml/test_FrmXMLHelper.php @@ -97,10 +97,10 @@ public function test_populate_postmeta() { $this->populate_postmeta( $post, $meta, $imported ); - $this->assertTrue( array_key_exists( 'postmeta', $post ) ); + $this->assertArrayHasKey( 'postmeta', $post ); $this->assertTrue( ! empty( $post['postmeta'] ) ); - $this->assertTrue( array_key_exists( 'frm_dyncontent', $post['postmeta'] ) ); - $this->assertTrue( is_array( $post['postmeta']['frm_dyncontent'] ) ); + $this->assertArrayHasKey( 'frm_dyncontent', $post['postmeta'] ); + $this->assertIsArray( $post['postmeta']['frm_dyncontent'] ); $this->assertEquals( array( array(