diff --git a/tests/_support/TestCases/TestCase.php b/tests/_support/TestCases/TestCase.php index dc0f81636e..cfc36e8190 100644 --- a/tests/_support/TestCases/TestCase.php +++ b/tests/_support/TestCases/TestCase.php @@ -19,6 +19,9 @@ public function setUp() : void { // Do anything we need to do to set up the environment for these tests. $this->factory()->pmpro_level = new PMPro_LevelFactory(); + + // Reset the user to visitor before each test. + wp_set_current_user( 0 ); } /** diff --git a/tests/to-migrate/includes/test-functions.php b/tests/to-migrate/includes/test-functions.php deleted file mode 100644 index 39c1e33a21..0000000000 --- a/tests/to-migrate/includes/test-functions.php +++ /dev/null @@ -1,218 +0,0 @@ -markTestSkipped( 'Tests need work -- skipping for now.' ); - } - - public function data_pmpro_getMembershipLevelsForUser() { - return [ - [], - ]; - } - - /** - * @covers ::pmpro_getMembershipLevelsForUser() - * @dataProvider data_pmpro_getMembershipLevelsForUser - * - * @param null $user_id - * @param bool $include_inactive - * @param bool $expects - */ - public function test_pmpro_getMembershipLevelsForUser( $user_id = null, $include_inactive = false, $expects = false ) { - $this->assertSame( $expects, pmpro_getMembershipLevelsForUser( $user_id, $include_inactive ) ); - } - - public function data_pmpro_changeMembershipLevel() { - $level_id = $this->factory->pmpro_level->create(); - $level_id_2 = $this->factory->pmpro_level->create(); - $user_id = $this->factory->user->create(); - - return [ - [ // #0 - null, - ], - [ // #1 - null, - $user_id, - ], - [ // #2 - $level_id, - $user_id, - 'inactive', - null, - 'assertTrue', - ], - ]; - } - - /** - * @covers ::pmpro_changeMembershipLevel() - * @dataProvider data_pmpro_changeMembershipLevel - * - * @param $level - * @param null $user_id - * @param string $old_level_status - * @param null $cancel_level - * @param string $assert - */ - public function test_pmpro_changeMembershipLevel( $level, $user_id = null, $old_level_status = 'inactive', $cancel_level = null, $assert = 'assertFalse' ) { - $this->$assert( pmpro_changeMembershipLevel( $level, $user_id, $old_level_status, $cancel_level ) ); - } - - public function data_pmpro_hasMembershipLevel() { - $level_id = $this->factory->pmpro_level->create(); - $level_id_2 = $this->factory->pmpro_level->create(); - $level = pmpro_getLevel( $level_id ); - - $user_id = $this->factory->user->create(); - $user_id_2 = $this->factory->user->create(); - - pmpro_changeMembershipLevel( $level_id, $user_id ); - - return [ - [ // #0 - null, - ], - [ // #1 - [], - ], - [ // #2 - [ 0, $level_id ], - null, - false, - 'assertTrue', // true? - ], - [ // #3 - 0, - null, - false, - 'assertTrue', // true? see line 774 - ], - [ // #4 - '0', - null, - false, - 'assertTrue', // true?see line 774 - ], - [ // #5 - '', - ], - [ // #6 - -1, - null, - false, - 'assertTrue', // true? - ], -// [ // #7 -// -1, -// $user_id, -// false, -// 'assertTrue', // true? -// ], - [ // #8 - null, - $user_id, - false, - 'assertTrue', // true? - ], - [ // #9 - $level_id, - $user_id, - false, - 'assertTrue', - ], - [ // #10 - [ $level_id, $level_id_2 ], - $user_id, - false, - 'assertTrue', - ], - [ // #11 - $level_id, - null, - $user_id, - 'assertTrue', - ], - [ // #12 - 'L', - $user_id, - $user_id, - 'assertTrue', - ], - [ // #13 - 'L', - $user_id_2, - $user_id_2, - 'assertTrue', - ], - [ // #14 - '-L', - $user_id_2, - $user_id_2, - ], - [ // #15 - '-L', - $user_id, - $user_id_2, - 'assertTrue', - ], - [ // #16 - 'E', - $user_id, - $user_id_2, - ], - [ // #17 - 'E', - $user_id_2, - $user_id_2, - ], - [ // #18 - $level->name, - null, - $user_id, - 'assertTrue', - ], - [ // #19 - 'Not Level Name', - null, - $user_id, - 'assertFalse', - ], - ]; - } - - /** - * @covers ::pmpro_hasMembershipLevel() - * @dataProvider data_pmpro_hasMembershipLevel - * - * @param $levels - * @param $user_id - * @param $current_user - * @param $assert - */ - public function test_pmpro_hasMembershipLevel( $levels = null, $user_id = null, $current_user = false, $assert = 'assertFalse' ) { - - $global_current_user = $GLOBALS['current_user']; - - if ( $current_user ) { - wp_set_current_user( $current_user ); - } - - $this->$assert( pmpro_hasMembershipLevel( $levels, $user_id ) ); - - $GLOBALS['current_user'] = $global_current_user; - - } - -} \ No newline at end of file diff --git a/tests/wpunit/functions/Levels/ChangeMembershipLevelTest.php b/tests/wpunit/functions/Levels/ChangeMembershipLevelTest.php new file mode 100644 index 0000000000..431764c767 --- /dev/null +++ b/tests/wpunit/functions/Levels/ChangeMembershipLevelTest.php @@ -0,0 +1,59 @@ +assertFalse( pmpro_changeMembershipLevel( null ) ); + $this->assertFalse( pmpro_changeMembershipLevel( 0 ) ); + $this->assertFalse( pmpro_changeMembershipLevel( '' ) ); + $this->assertFalse( pmpro_changeMembershipLevel( [] ) ); + } + + /** + * @covers ::pmpro_changeMembershipLevel() + */ + public function test_pmpro_changeMembershipLevel_is_null_with_invalid_level_and_no_user_and_valid_current_user() { + $user_id = $this->factory->user->create(); + + wp_set_current_user( $user_id ); + + $this->assertNull( pmpro_changeMembershipLevel( null ) ); + $this->assertNull( pmpro_changeMembershipLevel( 0 ) ); + $this->assertNull( pmpro_changeMembershipLevel( '' ) ); + $this->assertNull( pmpro_changeMembershipLevel( [] ) ); + } + + /** + * @covers ::pmpro_changeMembershipLevel() + */ + public function test_pmpro_changeMembershipLevel_is_null_with_invalid_level_and_valid_user() { + $user_id = $this->factory()->user->create(); + + $this->assertNull( pmpro_changeMembershipLevel( null, $user_id ) ); + $this->assertNull( pmpro_changeMembershipLevel( 0, $user_id ) ); + $this->assertNull( pmpro_changeMembershipLevel( '', $user_id ) ); + $this->assertNull( pmpro_changeMembershipLevel( [], $user_id ) ); + } + + /** + * @covers ::pmpro_changeMembershipLevel() + */ + public function test_pmpro_changeMembershipLevel_is_true_with_valid_level_and_valid_user() { + $level_id = $this->factory()->pmpro_level->create(); + $user_id = $this->factory()->user->create(); + + $this->assertTrue( pmpro_changeMembershipLevel( $level_id, $user_id ) ); + } +} \ No newline at end of file diff --git a/tests/wpunit/functions/Levels/GetMembershipLevelsForUserTest.php b/tests/wpunit/functions/Levels/GetMembershipLevelsForUserTest.php new file mode 100644 index 0000000000..d6b0132d42 --- /dev/null +++ b/tests/wpunit/functions/Levels/GetMembershipLevelsForUserTest.php @@ -0,0 +1,24 @@ +assertFalse( pmpro_getMembershipLevelsForUser( null, false ) ); + } +} \ No newline at end of file diff --git a/tests/wpunit/functions/Levels/HasMembershipLevelTest.php b/tests/wpunit/functions/Levels/HasMembershipLevelTest.php new file mode 100644 index 0000000000..2ab882288a --- /dev/null +++ b/tests/wpunit/functions/Levels/HasMembershipLevelTest.php @@ -0,0 +1,205 @@ +level_id = $this->factory()->pmpro_level->create(); + $this->level_id_2 = $this->factory()->pmpro_level->create(); + + $this->user_id = $this->factory()->user->create(); + $this->user_id_2 = $this->factory()->user->create(); + + $this->level = pmpro_getLevel( $this->level_id ); + + // Set a valid level. + pmpro_changeMembershipLevel( $this->level_id, $this->user_id ); + + // Set a valid level, then set status as expired. + pmpro_changeMembershipLevel( $this->level_id, $this->user_id_2 ); + + global $wpdb; + + $wpdb->update( $wpdb->pmpro_memberships_users, [ 'status' => 'expired' ], [ 'user_id' => $this->user_id_2 ], [ '%s' ], [ '%d' ] ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_false_with_null_levels_and_null_user_id() { + $this->assertFalse( pmpro_hasMembershipLevel() ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_false_with_empty_array_levels_and_null_user_id() { + $this->assertFalse( pmpro_hasMembershipLevel( [] ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_0_level_id_array_levels_and_null_user_id() { + $this->assertTrue( pmpro_hasMembershipLevel( [ 0, $this->level_id ] ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_0_levels_and_null_user_id() { + $this->assertTrue( pmpro_hasMembershipLevel( 0 ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_0_string_levels_and_null_user_id() { + $this->assertTrue( pmpro_hasMembershipLevel( '0' ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_false_with_empty_string_levels_and_null_user_id() { + $this->assertFalse( pmpro_hasMembershipLevel( '' ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_negative_one_levels_and_null_user_id() { + $this->assertTrue( pmpro_hasMembershipLevel( - 1 ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_false_with_negative_one_levels_and_valid_user_id() { + $this->assertFalse( pmpro_hasMembershipLevel( - 1, $this->user_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_null_levels_and_valid_user_id() { + $this->assertTrue( pmpro_hasMembershipLevel( null, $this->user_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_valid_levels_and_valid_user_id() { + $this->assertTrue( pmpro_hasMembershipLevel( $this->level_id, $this->user_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_valid_array_of_two_levels_and_valid_user_id() { + $this->assertTrue( pmpro_hasMembershipLevel( [ $this->level_id, $this->level_id_2 ], $this->user_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_valid_levels_and_null_user_id_and_valid_current_user() { + wp_set_current_user( $this->user_id ); + + $this->assertTrue( pmpro_hasMembershipLevel( $this->level_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_L_string_levels_and_valid_user_id_and_valid_current_user() { + wp_set_current_user( $this->user_id ); + + // Test L for logged-in user, it should match current user. + $this->assertTrue( pmpro_hasMembershipLevel( 'L', $this->user_id ) ); + $this->assertTrue( pmpro_hasMembershipLevel( 'l', $this->user_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_false_with_L_string_levels_and_valid_user_id_and_different_current_user() { + wp_set_current_user( $this->user_id_2 ); + + // Test L for logged-in user, it should match current user. + $this->assertFalse( pmpro_hasMembershipLevel( 'L', $this->user_id ) ); + $this->assertFalse( pmpro_hasMembershipLevel( 'l', $this->user_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_false_with_negative_L_string_levels_and_valid_user_id_and_valid_current_user() { + wp_set_current_user( $this->user_id ); + + // Test -L for non-logged-in user, it should not match current user. + $this->assertFalse( pmpro_hasMembershipLevel( '-L', $this->user_id ) ); + $this->assertFalse( pmpro_hasMembershipLevel( '-l', $this->user_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_negative_L_string_levels_and_valid_user_id_and_different_current_user() { + wp_set_current_user( $this->user_id_2 ); + + // Test -L for non-logged-in user, it should not match current user. + $this->assertTrue( pmpro_hasMembershipLevel( '-L', $this->user_id ) ); + $this->assertTrue( pmpro_hasMembershipLevel( '-l', $this->user_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_false_with_E_string_levels_and_valid_user_id() { + // Test E for expired level. + $this->assertFalse( pmpro_hasMembershipLevel( 'E', $this->user_id ) ); + $this->assertFalse( pmpro_hasMembershipLevel( 'e', $this->user_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_E_string_levels_and_expired_user_id() { + // Test E for expired level. + $this->assertTrue( pmpro_hasMembershipLevel( 'E', $this->user_id_2 ) ); + $this->assertTrue( pmpro_hasMembershipLevel( 'e', $this->user_id_2 ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_true_with_valid_name_string_levels_and_valid_user_id() { + $this->assertTrue( pmpro_hasMembershipLevel( $this->level->name, $this->user_id ) ); + } + + /** + * @covers ::pmpro_hasMembershipLevel() + */ + public function test_pmpro_hasMembershipLevel_is_false_with_invalid_name_string_levels_and_valid_user_id() { + $this->assertFalse( pmpro_hasMembershipLevel( 'Not Level Name', $this->user_id ) ); + } +} \ No newline at end of file