Ticket #1269: 1269.unittests.patch
File 1269.unittests.patch, 50.1 KB (added by , 10 years ago) |
---|
-
tests/phpunit/testcases/activity/class-bp-activity-template.php
diff --git tests/phpunit/testcases/activity/class-bp-activity-template.php tests/phpunit/testcases/activity/class-bp-activity-template.php index e69de29..6ef897b 100644
1 <?php 2 /** 3 * @group activity 4 * @group BP_Activity_Template 5 */ 6 class BP_Tests_BP_Activity_Template_TestCases extends BP_UnitTestCase { 7 8 public function setUp() { 9 parent::setUp(); 10 } 11 12 public function tearDown() { 13 parent::tearDown(); 14 } 15 16 protected function activities_get_args( $args = array() ) { 17 return wp_parse_args( $args, array( 18 'page' => 1, 19 'per_page' => 20, 20 'page_arg' => 'acpage', 21 'max' => false, 22 'count_total' => false, 23 'sort' => false, 24 'include' => false, 25 'exclude' => false, 26 'in' => false, 27 'filter' => false, 28 'scope' => false, 29 'search_terms' => false, 30 'meta_query' => false, 31 'date_query' => false, 32 'filter_query' => false, 33 'display_comments' => 'threaded', 34 'show_hidden' => false, 35 'spam' => 'ham_only', 36 'update_meta_cache' => true, 37 ) ); 38 } 39 40 /** 41 * @group BP_Activity_Template 42 */ 43 public function test_bp_activity_template() { 44 $u1 = $this->factory->user->create(); 45 $u2 = $this->factory->user->create(); 46 47 $activities = $this->factory->activity->create_many( 4, array( 48 'type' => 'activity_template_test', 49 'user_id' => $u1 50 ) ); 51 52 $comments = array(); 53 54 foreach ( $activities as $aid ) { 55 $comments[] = $this->factory->activity->create( array( 56 'item_id' => $aid, 57 'type' => 'activity_comment', 58 'user_id' => $u2, 59 ) ); 60 } 61 62 // Set pagination 63 $r = $this->activities_get_args( array( 64 'per_page' => 2, 65 'filter' => array( 'action' => 'activity_template_test' ), 66 'count_total' => true, 67 ) ); 68 69 $activities_page_1 = new BP_Activity_Template( $r ); 70 71 $found_activities = $activities_page_1->activities; 72 73 $this->assertEquals( 2, count( $found_activities ) ); 74 $this->assertEquals( 2, $activities_page_1->activity_count ); 75 $this->assertEquals( 4, $activities_page_1->total_activity_count ); 76 77 // Check some extra properties 78 $this->assertTrue( isset( $activities_page_1->disable_blogforum_replies ) ); 79 $this->assertTrue( isset( $activities_page_1->my_favs ) ); 80 $this->assertTrue( isset( $activities_page_1->has_more_items ) ); 81 $this->assertTrue( isset( $activities_page_1->full_name ) ); 82 83 preg_match( '/\?acpage=(.*)\'/', $activities_page_1->pag_links, $matches ); 84 85 // Set page 86 $r = $this->activities_get_args( array( 87 'per_page' => 2, 88 'page' => intval( $matches[1] ), 89 'filter' => array( 'action' => 'activity_template_test' ), 90 ) ); 91 92 $activities_page_2 = new BP_Activity_Template( $r ); 93 94 foreach( $activities_page_2->activities as $fa ) { 95 $found_activities[] = $fa; 96 } 97 98 $found_activities = array_map( 'intval', wp_list_pluck( $found_activities, 'id' ) ); 99 $this->assertEqualSets( $activities, $found_activities ); 100 101 // Check comments are fetching parents 102 $r = $this->activities_get_args( array( 103 'filter' => array( 'action' => 'activity_comment' ), 104 'include' => implode( ',', $comments ), 105 'display_comments' => 'stream' 106 ) ); 107 108 $activity_comments = new BP_Activity_Template( $r ); 109 $this->assertEqualSets( wp_list_pluck( $activity_comments->activities, 'item_id' ), wp_list_pluck( $activity_comments->activity_parents, 'id' ) ); 110 } 111 } -
tests/phpunit/testcases/blogs/class-bp-blogs-template.php
diff --git tests/phpunit/testcases/blogs/class-bp-blogs-template.php tests/phpunit/testcases/blogs/class-bp-blogs-template.php index e69de29..61c2632 100644
1 <?php 2 /** 3 * @group BP_Blogs_Template 4 */ 5 class BP_Tests_BP_Blogs_Template_TestCases extends BP_UnitTestCase { 6 7 public function setUp() { 8 parent::setUp(); 9 } 10 11 public function tearDown() { 12 parent::tearDown(); 13 } 14 15 protected function blogs_get_args( $args = array() ) { 16 return wp_parse_args( $args, array( 17 'type' => 'active', 18 'page_arg' => 'bpage', 19 'page' => 1, 20 'per_page' => 20, 21 'max' => false, 22 'user_id' => 0, 23 'include_blog_ids' => false, 24 'search_terms' => '', 25 'update_meta_cache' => true 26 ) ); 27 } 28 29 /** 30 * @group BP_Blogs_Template 31 */ 32 public function test_bp_blogs_template() { 33 if ( ! is_multisite() ) { 34 return; 35 } 36 37 // Default arguments 38 $r = $this->blogs_get_args(); 39 40 // Get the blogs 41 $blogs_empty = new BP_Blogs_Template( $r['type'], $r['page'], $r['per_page'], $r['max'], $r['user_id'], $r['search_terms'], $r['page_arg'], $r['update_meta_cache'], $r['include_blog_ids'] ); 42 43 $this->assertEquals( 0, count( $blogs_empty->blogs ) ); 44 $this->assertEquals( 0, $blogs_empty->blog_count ); 45 $this->assertEquals( 0, $blogs_empty->total_blog_count ); 46 47 $blogs = array( 48 'test_loop1' => array( 'blogname' => 'TestLoop1', 'blogdescription' => 'TestLoop1 Description', 'public' => 1 ), 49 'test_loop2' => array( 'blogname' => 'TestLoop2', 'blogdescription' => 'TestLoop2 Description', 'public' => 1 ), 50 ); 51 52 // Not setting user login or email generates errors in unit tests ? 53 $u_test_loop = array( 54 'test_loop1' => $this->factory->user->create( array( 'role' => '' ) ), 55 'test_loop2' => $this->factory->user->create( array( 'role' => '' ) ), 56 ); 57 58 $test_loop = array(); 59 $expected_loop = array(); 60 $include_blog_ids = array(); 61 62 foreach( $blogs as $path => $meta ) { 63 64 $blogs[ $path ]['id'] = $this->factory->blog->create( array( 65 'path' => '/' . $path, 66 'meta' => $meta, 67 'user_id' => $u_test_loop[ $path ], 68 ) ); 69 70 $include_blog_ids[] = $blogs[ $path ]['id']; 71 72 $expected_loop[] = array( 73 'blog_id' => $blogs[ $path ]['id'], 74 'admin_user_id' => $u_test_loop[ $path ], 75 'name' => $meta['blogname'], 76 'description' => $meta['blogdescription'], 77 ); 78 } 79 80 $r = $this->blogs_get_args( array( 81 'per_page' => 1, 82 'include_blog_ids' => $include_blog_ids, 83 ) ); 84 85 $blogs_page_1 = new BP_Blogs_Template( $r['type'], $r['page'], $r['per_page'], $r['max'], $r['user_id'], $r['search_terms'], $r['page_arg'], $r['update_meta_cache'], $r['include_blog_ids'] ); 86 $the_blogs = $blogs_page_1->blogs; 87 88 $this->assertEquals( 1, count( $blogs_page_1->blogs ) ); 89 $this->assertEquals( 1, $blogs_page_1->blog_count ); 90 $this->assertEquals( 2, $blogs_page_1->total_blog_count ); 91 92 preg_match( '/\?bpage=(.*)\'/', $blogs_page_1->pag_links, $matches ); 93 $r['page'] = intval( $matches[1] ); 94 95 $blogs_page_2 = new BP_Blogs_Template( $r['type'], $r['page'], $r['per_page'], $r['max'], $r['user_id'], $r['search_terms'], $r['page_arg'], $r['update_meta_cache'], $r['include_blog_ids'] ); 96 $the_blogs[] = $blogs_page_2->blogs[0]; 97 98 foreach ( $the_blogs as $blog ) { 99 $test_loop[] = array( 100 'blog_id' => $blog->blog_id, 101 'admin_user_id' => $blog->admin_user_id, 102 'name' => $blog->name, 103 'description' => $blog->description, 104 ); 105 } 106 107 $this->assertEqualSets( $expected_loop, $test_loop ); 108 } 109 } -
tests/phpunit/testcases/groups/class-bp-groups-group-members-template.php
diff --git tests/phpunit/testcases/groups/class-bp-groups-group-members-template.php tests/phpunit/testcases/groups/class-bp-groups-group-members-template.php index e69de29..685dfb0 100644
1 <?php 2 /** 3 * @group groups 4 * @group BP_Groups_Group_Members_Template 5 */ 6 class BP_Tests_BP_Groups_Group_Members_Template_TestCases extends BP_UnitTestCase { 7 8 public function setUp() { 9 parent::setUp(); 10 } 11 12 public function tearDown() { 13 parent::tearDown(); 14 } 15 16 protected function group_members_get_args( $args = array() ) { 17 return wp_parse_args( $args, array( 18 'group_id' => 0, 19 'page' => 1, 20 'per_page' => 20, 21 'max' => false, 22 'exclude' => false, 23 'exclude_admins_mods' => 1, 24 'exclude_banned' => 1, 25 'group_role' => false, 26 'search_terms' => false, 27 'type' => 'last_joined', 28 ) ); 29 } 30 31 /** 32 * @group BP_Groups_Group_Members_Template 33 */ 34 public function test_bp_groups_group_members_template() { 35 $bp = buddypress(); 36 $reset_item_admin = $bp->is_item_admin; 37 38 $u1 = $this->factory->user->create(); 39 $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) ); 40 41 $u2 = $this->factory->user->create(); 42 $u3 = $this->factory->user->create(); 43 $u4 = $this->factory->user->create(); 44 45 // Add users to the group 46 groups_join_group( $g1, $u2 ); 47 groups_join_group( $g1, $u3 ); 48 groups_join_group( $g1, $u4 ); 49 50 // Set the item admin 51 $bp->is_item_admin = true; 52 53 // Promote u2 to admin 54 groups_promote_member( $u2, $g1, 'admin' ); 55 56 // Promote u3 to mod 57 groups_promote_member( $u3, $g1, 'mod' ); 58 59 // Set pagination 60 $r = $this->group_members_get_args( array( 61 'group_id' => $g1, 62 'per_page' => 2, 63 'exclude_admins_mods' => 0 64 ) ); 65 66 $group_members_page_1 = new BP_Groups_Group_Members_Template( $r ); 67 $members = $group_members_page_1->members; 68 69 $this->assertEquals( 2, count( $group_members_page_1->members ) ); 70 $this->assertEquals( 2, $group_members_page_1->member_count ); 71 $this->assertEquals( 4, $group_members_page_1->total_member_count ); 72 73 preg_match( '/\?mlpage=(.*)\'/', $group_members_page_1->pag_links, $matches ); 74 75 // Set page 76 $r = $this->group_members_get_args( array( 77 'per_page' => 2, 78 'group_id' => $g1, 79 'exclude_admins_mods' => 0, 80 'page' => intval( $matches[1] ), 81 ) ); 82 83 $group_members_page_2 = new BP_Groups_Group_Members_Template( $r ); 84 85 foreach ( $group_members_page_2->members as $member ) { 86 $members[] = $member; 87 } 88 89 $expected = array( $u1, $u2, $u3, $u4 ); 90 $tested = array_map( 'intval', wp_list_pluck( $members, 'ID' ) ); 91 92 $this->assertEqualSets( $tested, $expected ); 93 94 // Get admins 95 $r = $this->group_members_get_args( array( 96 'group_id' => $g1, 97 'group_role' => 'admin' 98 ) ); 99 100 $group_admins = new BP_Groups_Group_Members_Template( $r ); 101 102 $expected = array( $u1, $u2 ); 103 $tested = array_map( 'intval', wp_list_pluck( $group_admins->members, 'ID' ) ); 104 105 $this->assertEqualSets( $tested, $expected ); 106 107 // Get mods 108 $r = $this->group_members_get_args( array( 109 'group_id' => $g1, 110 'group_role' => 'mod' 111 ) ); 112 113 $group_mods = new BP_Groups_Group_Members_Template( $r ); 114 115 $expected = array( $u3 ); 116 $tested = array_map( 'intval', wp_list_pluck( $group_mods->members, 'ID' ) ); 117 118 $this->assertEqualSets( $tested, $expected ); 119 120 // Ban u4 & demote u2, u3 121 groups_ban_member( $u4, $g1 ); 122 groups_demote_member( $u2, $g1 ); 123 groups_demote_member( $u3, $g1 ); 124 125 // Get users 126 $r = $this->group_members_get_args( array( 127 'group_id' => $g1, 128 ) ); 129 130 $group_users = new BP_Groups_Group_Members_Template( $r ); 131 132 $expected = array( $u2, $u3 ); 133 $tested = array_map( 'intval', wp_list_pluck( $group_users->members, 'ID' ) ); 134 135 $this->assertEqualSets( $tested, $expected ); 136 137 // Reset the item admin 138 $bp->is_item_admin = $reset_item_admin; 139 } 140 } -
tests/phpunit/testcases/groups/class-bp-groups-invite-template.php
diff --git tests/phpunit/testcases/groups/class-bp-groups-invite-template.php tests/phpunit/testcases/groups/class-bp-groups-invite-template.php index e69de29..8cf46cc 100644
1 <?php 2 /** 3 * @group groups 4 * @group BP_Groups_Invite_Template 5 */ 6 class BP_Tests_BP_Groups_Invite_Template_TestCases extends BP_UnitTestCase { 7 8 public function setUp() { 9 parent::setUp(); 10 } 11 12 public function tearDown() { 13 parent::tearDown(); 14 } 15 16 protected function group_invites_get_args( $args = array() ) { 17 return wp_parse_args( $args, array( 18 'group_id' => false, 19 'user_id' => 0, 20 'per_page' => false, 21 'page' => 1, 22 ) ); 23 } 24 25 /** 26 * @group BP_Groups_Invite_Template 27 */ 28 public function test_bp_groups_invite_template() { 29 $u1 = $this->factory->user->create(); 30 $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) ); 31 32 $friends = array( 33 'u2' => $this->factory->user->create(), 34 'u3' => $this->factory->user->create(), 35 'u4' => $this->factory->user->create(), 36 ); 37 38 // request friendship 39 foreach ( $friends as $fr ) { 40 friends_add_friend( $fr, $u1 ); 41 } 42 43 // accept friendship 44 $old_user = get_current_user_id(); 45 $this->set_current_user( $u1 ); 46 47 foreach ( $friends as $fa ) { 48 friends_accept_friendship( friends_get_friendship_id( $fa, $u1 ) ); 49 50 // Then invite each user 51 groups_invite_user( array( 52 'user_id' => $fa, 53 'group_id' => $g1, 54 'inviter_id' => $u1, 55 ) ); 56 } 57 58 // Restore current user 59 $this->set_current_user( $old_user ); 60 61 // Set pagination 62 $r = $this->group_invites_get_args( array( 63 'group_id' => $g1, 64 'user_id' => $u1, 65 'per_page' => 2, 66 ) ); 67 68 $group_invites_page_1 = new BP_Groups_Invite_Template( $r ); 69 $invites = $group_invites_page_1->invites; 70 71 $this->assertEquals( 2, count( $group_invites_page_1->invites ) ); 72 $this->assertEquals( 2, $group_invites_page_1->invite_count ); 73 $this->assertEquals( 3, $group_invites_page_1->total_invite_count ); 74 75 // Make sure the invite_data property is set 76 $this->assertEqualSets( $group_invites_page_1->invites, wp_list_pluck( $group_invites_page_1->invite_data, 'ID' ) ); 77 78 preg_match( '/\?invitepage=(.*)\'/', $group_invites_page_1->pag_links, $matches ); 79 80 // Set page 81 $r = $this->group_invites_get_args( array( 82 'group_id' => $g1, 83 'user_id' => $u1, 84 'per_page' => 2, 85 'page' => intval( $matches[1] ), 86 ) ); 87 88 $group_invites_page_2 = new BP_Groups_Invite_Template( $r ); 89 90 // Make sure the invite_data property is set 91 $this->assertEqualSets( $group_invites_page_2->invites, wp_list_pluck( $group_invites_page_2->invite_data, 'ID' ) ); 92 93 $invites[] = $group_invites_page_2->invites[0]; 94 $invited = array_map( 'intval', $invites ); 95 $this->assertEqualSets( $friends, $invited ); 96 97 // Check the_invite() 98 $group_invites_page_2->the_invite(); 99 $this->assertSame( $group_invites_page_2->invites[0], $group_invites_page_2->invite->user->ID ); 100 } 101 } -
tests/phpunit/testcases/groups/class-bp-groups-membership-requests-template.php
diff --git tests/phpunit/testcases/groups/class-bp-groups-membership-requests-template.php tests/phpunit/testcases/groups/class-bp-groups-membership-requests-template.php index e69de29..5c51ef2 100644
1 <?php 2 /** 3 * @group groups 4 * @group BP_Groups_Membership_Requests_Template 5 */ 6 class BP_Tests_BP_Groups_Membership_Requests_Template_TestCases extends BP_UnitTestCase { 7 8 public function setUp() { 9 parent::setUp(); 10 } 11 12 public function tearDown() { 13 parent::tearDown(); 14 } 15 16 protected function group_requests_get_args( $args = array() ) { 17 return wp_parse_args( $args, array( 18 'group_id' => 0, 19 'per_page' => 10, 20 'page' => 1, 21 'max' => false 22 ) ); 23 } 24 25 /** 26 * @group BP_Groups_Membership_Requests_Template 27 */ 28 public function test_bp_groups_membership_requests_template() { 29 $bp = buddypress(); 30 $reset_item_admin = $bp->is_item_admin; 31 32 $u1 = $this->factory->user->create(); 33 $g1 = $this->factory->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 34 35 $u2 = $this->factory->user->create(); 36 $u3 = $this->factory->user->create(); 37 $u4 = $this->factory->user->create(); 38 39 // Users are sending requests 40 groups_send_membership_request( $u2, $g1 ); 41 groups_send_membership_request( $u3, $g1 ); 42 groups_send_membership_request( $u4, $g1 ); 43 44 // Set pagination 45 $r = $this->group_requests_get_args( array( 46 'group_id' => $g1, 47 'per_page' => 2, 48 ) ); 49 50 $group_requests_page_1 = new BP_Groups_Membership_Requests_Template( $r ); 51 $requests = $group_requests_page_1->requests; 52 53 $this->assertEquals( 2, count( $group_requests_page_1->requests ) ); 54 $this->assertEquals( 2, $group_requests_page_1->request_count ); 55 $this->assertEquals( 3, $group_requests_page_1->total_request_count ); 56 57 preg_match( '/\?mrpage=(.*)\'/', $group_requests_page_1->pag_links, $matches ); 58 59 // Set page 60 $r = $this->group_requests_get_args( array( 61 'group_id' => $g1, 62 'per_page' => 2, 63 'page' => intval( $matches[1] ), 64 ) ); 65 66 $group_requests_page_2 = new BP_Groups_Membership_Requests_Template( $r ); 67 68 $requests[] = $group_requests_page_2->requests[0]; 69 70 $expected = array( $u2, $u3, $u4 ); 71 $tested = array_map( 'intval', wp_list_pluck( $requests, 'ID' ) ); 72 73 $this->assertEqualSets( $tested, $expected ); 74 75 // Check Compatibility with legacy format of request data objects 76 $this->assertEqualSets( wp_list_pluck( $requests, 'id' ), wp_list_pluck( $requests, 'membership_id' ) ); 77 78 // Set the item admin 79 $bp->is_item_admin = true; 80 81 // Accept all requests 82 groups_accept_all_pending_membership_requests( $g1 ); 83 84 $r = $this->group_requests_get_args( array( 85 'group_id' => $g1, 86 ) ); 87 88 $requests = new BP_Groups_Membership_Requests_Template( $r ); 89 90 $this->assertEmpty( $requests->requests ); 91 92 // Reset the item admin 93 $bp->is_item_admin = $reset_item_admin; 94 } 95 } -
tests/phpunit/testcases/groups/class-bp-groups-template.php
diff --git tests/phpunit/testcases/groups/class-bp-groups-template.php tests/phpunit/testcases/groups/class-bp-groups-template.php index e69de29..68d827f 100644
1 <?php 2 /** 3 * @group BP_Groups_Template 4 */ 5 class BP_Tests_BP_Groups_Template_TestCases extends BP_UnitTestCase { 6 7 public function setUp() { 8 parent::setUp(); 9 } 10 11 public function tearDown() { 12 parent::tearDown(); 13 } 14 15 protected function groups_get_args( $args = array() ) { 16 return wp_parse_args( $args, array( 17 'type' => '', 18 'order' => 'DESC', 19 'orderby' => 'last_activity', 20 'page' => 1, 21 'per_page' => 20, 22 'max' => false, 23 'show_hidden' => false, 24 'page_arg' => 'grpage', 25 'user_id' => 0, 26 'slug' => false, 27 'search_terms' => false, 28 'meta_query' => false, 29 'include' => false, 30 'exclude' => false, 31 'populate_extras' => true, 32 'update_meta_cache' => true, 33 ) ); 34 } 35 36 /** 37 * @group BP_Groups_Template 38 */ 39 public function test_bp_groups_template_directory() { 40 41 // Default arguments 42 $r = $this->groups_get_args(); 43 44 $groups_empty = new BP_Groups_Template( $r ); 45 46 $this->assertEquals( 0, count( $groups_empty->groups ) ); 47 $this->assertEquals( 0, $groups_empty->group_count ); 48 $this->assertEquals( 0, $groups_empty->total_group_count ); 49 50 $u1 = $this->factory->user->create(); 51 $u2 = $this->factory->user->create(); 52 $g1 = $this->factory->group->create( array( 'creator_id' => $u1 ) ); 53 $g2 = $this->factory->group->create( array( 'creator_id' => $u2, 'status' => 'private' ) ); 54 55 // Add a user to one of the group 56 groups_join_group( $g1, $u2 ); 57 58 // Update one of the group's last activity 59 groups_update_groupmeta( $g1, 'last_activity', date( 'Y-m-d H:i:s' , strtotime( '+1 hour' ) ) ); 60 61 // Set pagination 62 $r = $this->groups_get_args( array( 'per_page' => 1 ) ); 63 64 $groups_page_1 = new BP_Groups_Template( $r ); 65 $the_groups = $groups_page_1->groups; 66 67 $this->assertEquals( 1, count( $groups_page_1->groups ) ); 68 $this->assertEquals( 1, $groups_page_1->group_count ); 69 $this->assertEquals( 2, $groups_page_1->total_group_count ); 70 71 preg_match( '/\?grpage=(.*)\'/', $groups_page_1->pag_links, $matches ); 72 73 // Set page 74 $r = $this->groups_get_args( array( 75 'per_page' => 1, 76 'page' => intval( $matches[1] ) 77 ) ); 78 79 $groups_page_2 = new BP_Groups_Template( $r ); 80 $the_groups[] = $groups_page_2->groups[0]; 81 $expected = array(); 82 $groups_loop = array(); 83 84 foreach ( $the_groups as $group ) { 85 86 $groups_loop[] = array( 87 'id' => $group->id, 88 'creator_id' => $group->creator_id, 89 'status' => $group->status, 90 'total_member_count' => $group->total_member_count, 91 'last_activity' => $group->last_activity, 92 ); 93 94 $check_group = groups_get_group( array( 'group_id' => $group->id, 'populate_extras' => true ) ); 95 96 $expected[] = array( 97 'id' => $check_group->id, 98 'creator_id' => $check_group->creator_id, 99 'status' => $check_group->status, 100 'total_member_count' => $check_group->total_member_count, 101 'last_activity' => $check_group->last_activity, 102 ); 103 } 104 105 $this->assertEqualSets( $groups_loop, $expected ); 106 } 107 108 /** 109 * @group BP_Groups_Template 110 */ 111 public function test_bp_groups_template_invites() { 112 113 $u1 = $this->factory->user->create(); 114 $u2 = $this->factory->user->create(); 115 $g1 = $this->factory->group->create( array( 'creator_id' => $u1, 'status' => 'private' ) ); 116 $g2 = $this->factory->group->create( array( 'creator_id' => $u1, 'status' => 'hidden' ) ); 117 118 groups_invite_user( array( 119 'user_id' => $u2, 120 'group_id' => $g1, 121 'inviter_id' => $u1, 122 ) ); 123 124 groups_send_invites( $u1, $g1 ); 125 126 groups_invite_user( array( 127 'user_id' => $u2, 128 'group_id' => $g2, 129 'inviter_id' => $u1, 130 ) ); 131 132 groups_send_invites( $u1, $g2 ); 133 134 // Invites arguments 135 $r = $this->groups_get_args( array( 136 'type' => 'invites', 137 'user_id' => $u2, 138 'show_hidden' => true, 139 ) ); 140 141 $groups_invites = new BP_Groups_Template( $r ); 142 143 $this->assertEquals( 2, $groups_invites->total_group_count ); 144 145 $this->assertEqualSets( array( $g1, $g2 ), wp_list_pluck( $groups_invites->groups, 'id' ) ); 146 } 147 148 /** 149 * @group BP_Groups_Template 150 */ 151 public function test_bp_groups_template_single_group() { 152 153 $u = $this->factory->user->create(); 154 $g = $this->factory->group->create( array( 'creator_id' => $u ) ); 155 156 $group = groups_get_group( array( 'group_id' => $g ) ); 157 158 // Single group arguments 159 $r = $this->groups_get_args( array( 160 'type' => 'single-group', 161 'slug' => $group->slug, 162 ) ); 163 164 $groups_single = new BP_Groups_Template( $r ); 165 166 $this->assertEquals( 1, $groups_single->total_group_count ); 167 $this->assertTrue( $groups_single->single_group ); 168 $this->assertEquals( $g, $groups_single->groups[0]->id ); 169 } 170 } -
tests/phpunit/testcases/members/class-bp-core-members-template.php
diff --git tests/phpunit/testcases/members/class-bp-core-members-template.php tests/phpunit/testcases/members/class-bp-core-members-template.php index e69de29..bc6d1a6 100644
1 <?php 2 /** 3 * @group members 4 * @group BP_Core_Members_Template 5 */ 6 class BP_Tests_BP_Core_Members_Template_TestCases extends BP_UnitTestCase { 7 8 public function setUp() { 9 parent::setUp(); 10 } 11 12 public function tearDown() { 13 parent::tearDown(); 14 } 15 16 protected function members_get_args( $args = array() ) { 17 return wp_parse_args( $args, array( 18 'type' => 'active', 19 'page' => 1, 20 'per_page' => 20, 21 'max' => false, 22 'user_id' => 0, 23 'search_terms' => null, 24 'include' => false, 25 'populate_extras' => true, 26 'exclude' => false, 27 'meta_key' => false, 28 'meta_value' => false, 29 'page_arg' => 'upage', 30 'member_type' =>'', 31 ) ); 32 } 33 34 /** 35 * @group BP_Core_Members_Template 36 */ 37 public function test_bp_core_members_template_active() { 38 39 $members = array( 40 'u1' => $this->factory->user->create(), 41 'u2' => $this->factory->user->create(), 42 'u3' => $this->factory->user->create(), 43 'u4' => $this->factory->user->create(), 44 ); 45 46 // Set pagination 47 $r = $this->members_get_args( array( 48 'per_page' => 2, 49 ) ); 50 51 $members_page_1 = new BP_Core_Members_Template( 52 $r['type'], 53 $r['page'], 54 $r['per_page'], 55 $r['max'], 56 $r['user_id'], 57 $r['search_terms'], 58 $r['include'], 59 $r['populate_extras'], 60 $r['exclude'], 61 $r['meta_key'], 62 $r['meta_value'], 63 $r['page_arg'], 64 $r['member_type'] 65 ); 66 67 $found_members = $members_page_1->members; 68 69 $this->assertEquals( 2, count( $found_members ) ); 70 $this->assertEquals( 2, $members_page_1->member_count ); 71 $this->assertEquals( 4, $members_page_1->total_member_count ); 72 73 preg_match( '/\?upage=(.*)\'/', $members_page_1->pag_links, $matches ); 74 75 // Set page 76 $r = $this->members_get_args( array( 77 'per_page' => 2, 78 'page' => intval( $matches[1] ), 79 ) ); 80 81 $members_page_2 = new BP_Core_Members_Template( 82 $r['type'], 83 $r['page'], 84 $r['per_page'], 85 $r['max'], 86 $r['user_id'], 87 $r['search_terms'], 88 $r['include'], 89 $r['populate_extras'], 90 $r['exclude'], 91 $r['meta_key'], 92 $r['meta_value'], 93 $r['page_arg'], 94 $r['member_type'] 95 ); 96 97 foreach( $members_page_2->members as $fm ) { 98 $found_members[] = $fm; 99 } 100 101 $found_members = array_map( 'intval', wp_list_pluck( $found_members, 'ID' ) ); 102 103 $this->assertEqualSets( $members, $found_members ); 104 } 105 106 /** 107 * @group BP_Core_Members_Template 108 */ 109 public function test_bp_core_members_template_by_letter() { 110 $reset_request = $_REQUEST; 111 $_REQUEST['letter'] = 'b'; 112 113 $letters = array( 114 'u1' => $this->factory->user->create( array( 115 'user_login' => 'bfoou1', 116 'user_nicename' => 'bfoo-u1', 117 ) ), 118 'u2' => $this->factory->user->create( array( 119 'user_login' => 'bbaru2', 120 'user_nicename' => 'bbar-u2', 121 ) ), 122 'u3' => $this->factory->user->create( array( 123 'user_login' => 'bzoou3', 124 'user_nicename' => 'bzoo-u3', 125 ) ), 126 'u4' => $this->factory->user->create( array( 127 'user_login' => 'balu4', 128 'user_nicename' => 'bal-u4', 129 ) ), 130 ); 131 132 // Set pagination 133 $r = $this->members_get_args( array( 134 'exclude' => $letters['u2'], 135 ) ); 136 137 $letters_loop = new BP_Core_Members_Template( 138 $r['type'], 139 $r['page'], 140 $r['per_page'], 141 $r['max'], 142 $r['user_id'], 143 $r['search_terms'], 144 $r['include'], 145 $r['populate_extras'], 146 $r['exclude'], 147 $r['meta_key'], 148 $r['meta_value'], 149 $r['page_arg'], 150 $r['member_type'] 151 ); 152 153 $this->assertEquals( 3, $letters_loop->member_count ); 154 $this->assertEquals( 3, $letters_loop->total_member_count ); 155 156 $letter_members = array_map( 'intval', wp_list_pluck( $letters_loop->members, 'id' ) ); 157 158 $this->assertEqualSets( array( $letters['u1'], $letters['u3'], $letters['u4'] ), $letter_members ); 159 160 // Reset $_REQUEST 161 $_REQUEST = $reset_request; 162 } 163 164 /** 165 * @group BP_Core_Members_Template 166 */ 167 public function test_bp_core_members_template_include_ids_alphabetical() { 168 // A way to check the "friends loop" 169 $members = array( 170 'u1' => $this->factory->user->create( array( 171 'user_login' => 'foou1', 172 'user_nicename' => 'foo-u1', 173 ) ), 174 'u2' => $this->factory->user->create( array( 175 'user_login' => 'baru2', 176 'user_nicename' => 'bar-u2', 177 ) ), 178 'u3' => $this->factory->user->create( array( 179 'user_login' => 'zoou3', 180 'user_nicename' => 'zoo-u3', 181 ) ), 182 'u4' => $this->factory->user->create( array( 183 'user_login' => 'alu4', 184 'user_nicename' => 'al-u4', 185 ) ), 186 ); 187 188 // Set pagination 189 $r = $this->members_get_args( array( 190 'type' => 'alphabetical', 191 'include' => $members, 192 ) ); 193 194 $members_loop = new BP_Core_Members_Template( 195 $r['type'], 196 $r['page'], 197 $r['per_page'], 198 $r['max'], 199 $r['user_id'], 200 $r['search_terms'], 201 $r['include'], 202 $r['populate_extras'], 203 $r['exclude'], 204 $r['meta_key'], 205 $r['meta_value'], 206 $r['page_arg'], 207 $r['member_type'] 208 ); 209 210 $found_members = array_map( 'intval', wp_list_pluck( $members_loop->members, 'ID' ) ); 211 $alpha_members = array( $members['u4'], $members['u2'], $members['u1'], $members['u3'] ); 212 213 $this->assertEqualSets( $alpha_members, $found_members ); 214 } 215 } -
tests/phpunit/testcases/messages/class-bp-messages-box-template.php
diff --git tests/phpunit/testcases/messages/class-bp-messages-box-template.php tests/phpunit/testcases/messages/class-bp-messages-box-template.php index e69de29..302c1be 100644
1 <?php 2 /** 3 * @group messages 4 * @group BP_Messages_Box_Template 5 */ 6 class BP_Tests_BP_Messages_Box_Template_TestCases extends BP_UnitTestCase { 7 8 public function setUp() { 9 parent::setUp(); 10 } 11 12 public function tearDown() { 13 parent::tearDown(); 14 } 15 16 protected function messages_get_args( $args = array() ) { 17 return wp_parse_args( $args, array( 18 'user_id' => 0, 19 'box' => 'inbox', 20 'per_page' => 10, 21 'max' => false, 22 'type' => 'all', 23 'search_terms' => '', 24 'page_arg' => 'mpage', 25 ) ); 26 } 27 28 /** 29 * @group BP_Messages_Box_Template 30 */ 31 public function test_bp_messages_box_template() { 32 $reset_get = $_GET; 33 34 $u1 = $this->factory->user->create(); 35 $u2 = $this->factory->user->create(); 36 37 // send a private message 38 $t1 = messages_new_message( array( 39 'sender_id' => $u1, 40 'recipients' => array( $u2 ), 41 'subject' => 'A new message', 42 'content' => 'Hey there!', 43 ) ); 44 45 $t2 = messages_new_message( array( 46 'sender_id' => $u1, 47 'recipients' => array( $u2 ), 48 'subject' => 'A new message', 49 'content' => 'Hey there!', 50 ) ); 51 52 // Send a notice 53 $notice = new BP_Messages_Notice; 54 $notice->subject = 'notice subject'; 55 $notice->message = 'notice message'; 56 $notice->date_sent = bp_core_current_time(); 57 $notice->is_active = 1; 58 $notice->save(); 59 60 /** Inbox *********************************************************************/ 61 62 $received = array(); 63 64 // Set pagination 65 $r = $this->messages_get_args( array( 66 'user_id' => $u2, 67 'per_page' => 1, 68 ) ); 69 70 $inbox_messages_page_1 = new BP_Messages_Box_Template( 71 $r['user_id'], 72 $r['box'], 73 $r['per_page'], 74 $r['max'], 75 $r['type'], 76 $r['search_terms'], 77 $r['page_arg'] 78 ); 79 80 $this->assertEquals( 1, $inbox_messages_page_1->thread_count ); 81 $this->assertEquals( 2, $inbox_messages_page_1->total_thread_count ); 82 $received = wp_list_pluck( $inbox_messages_page_1->threads[0]->messages, 'thread_id' ); 83 84 preg_match( '/\?mpage=(.*)\'/', $inbox_messages_page_1->pag_links, $matches ); 85 86 // Set page 87 $_GET['mpage'] = intval( $matches[1] ); 88 89 $inbox_messages_page_2 = new BP_Messages_Box_Template( 90 $r['user_id'], 91 $r['box'], 92 $r['per_page'], 93 $r['max'], 94 $r['type'], 95 $r['search_terms'], 96 $r['page_arg'] 97 ); 98 99 $received = array_merge( $received, wp_list_pluck( $inbox_messages_page_2->threads[0]->messages, 'thread_id' ) ); 100 101 $this->assertEqualSets( array( $t1, $t2 ), array_map( 'intval', $received ) ); 102 103 // Check the_message_thread() 104 $inbox_messages_page_2->the_message_thread(); 105 $this->assertSame( $u1, intval( $inbox_messages_page_2->thread->last_sender_id ) ); 106 107 // Reset the GET var 108 $_GET = $reset_get; 109 110 /** Sentbox *******************************************************************/ 111 112 $sent = array(); 113 114 // Set pagination 115 $r = $this->messages_get_args( array( 116 'user_id' => $u1, 117 'box' => 'sentbox', 118 ) ); 119 120 $sentbox_messages = new BP_Messages_Box_Template( 121 $r['user_id'], 122 $r['box'], 123 $r['per_page'], 124 $r['max'], 125 $r['type'], 126 $r['search_terms'], 127 $r['page_arg'] 128 ); 129 130 $this->assertEquals( 2, $sentbox_messages->thread_count ); 131 $this->assertEquals( 2, $sentbox_messages->total_thread_count ); 132 $sent = wp_list_pluck( $sentbox_messages->threads, 'thread_id' ); 133 134 $this->assertEqualSets( array( $t1, $t2 ), array_map( 'intval', $sent ) ); 135 136 /** Notice ********************************************************************/ 137 $bp = buddypress(); 138 $bp_current_action = $bp->current_action; 139 140 // Set notice args 141 $r = $this->messages_get_args( array( 142 'user_id' => $u1, 143 'box' => 'notices', 144 ) ); 145 146 $notice_messages = new BP_Messages_Box_Template( 147 $r['user_id'], 148 $r['box'], 149 $r['per_page'], 150 $r['max'], 151 $r['type'], 152 $r['search_terms'], 153 $r['page_arg'] 154 ); 155 156 $this->assertEquals( 1, $notice_messages->thread_count ); 157 $this->assertEquals( 1, $notice_messages->total_thread_count ); 158 159 $active_notice = BP_Messages_Notice::get_active(); 160 $notice->deactivate(); 161 162 $notice_received = wp_list_pluck( $notice_messages->threads, 'id' ); 163 $this->assertSame( array( $active_notice->id ), $notice_received ); 164 165 $bp->current_action = 'notices'; 166 167 // Check the_message_thread() 168 $notice_messages->the_message_thread(); 169 $this->assertTrue( ! isset( $notice_messages->thread->last_sender_id ) ); 170 171 // Reset $bp->current_action 172 $bp->current_action = $bp_current_action; 173 } 174 } -
tests/phpunit/testcases/messages/class-bp-messages-thread-template.php
diff --git tests/phpunit/testcases/messages/class-bp-messages-thread-template.php tests/phpunit/testcases/messages/class-bp-messages-thread-template.php index e69de29..6ad2101 100644
1 <?php 2 /** 3 * @group messages 4 * @group BP_Messages_Thread_Template 5 */ 6 class BP_Tests_BP_Messages_Thread_Template_TestCases extends BP_UnitTestCase { 7 8 public function setUp() { 9 parent::setUp(); 10 } 11 12 public function tearDown() { 13 parent::tearDown(); 14 } 15 16 /** 17 * @group BP_Messages_Thread_Template 18 */ 19 public function test_bp_messages_thread_template() { 20 $reset_get = $_GET; 21 22 $u1 = $this->factory->user->create(); 23 $u2 = $this->factory->user->create(); 24 25 // send a private message 26 $t1 = messages_new_message( array( 27 'sender_id' => $u1, 28 'recipients' => array( $u2 ), 29 'subject' => 'A new message', 30 'content' => 'Hey there!', 31 ) ); 32 33 messages_new_message( array( 34 'sender_id' => $u2, 35 'thread_id' => $t1, 36 'content' => 'A new reply', 37 ) ); 38 39 messages_new_message( array( 40 'sender_id' => $u1, 41 'thread_id' => $t1, 42 'content' => 'Another reply', 43 ) ); 44 45 $message_thread = new BP_Messages_Thread_Template( $t1, 'ASC', array( 'update_meta_cache' => true ) ); 46 47 $this->assertEquals( 3, $message_thread->message_count ); 48 $senders = wp_list_pluck( $message_thread->thread->messages, 'sender_id' ); 49 $this->assertEqualSets( array( $u1, $u2, $u1 ), array_map( 'intval', $senders ) ); 50 } 51 } 52 -
tests/phpunit/testcases/notifications/class-bp-notifications-template.php
diff --git tests/phpunit/testcases/notifications/class-bp-notifications-template.php tests/phpunit/testcases/notifications/class-bp-notifications-template.php index e69de29..c5b1e37 100644
1 <?php 2 /** 3 * @group notifications 4 * @group BP_Notifications_Template 5 */ 6 class BP_Tests_BP_Notifications_Template_TestCases extends BP_UnitTestCase { 7 8 public function setUp() { 9 parent::setUp(); 10 } 11 12 public function tearDown() { 13 parent::tearDown(); 14 } 15 16 protected function notifications_get_args( $args = array() ) { 17 return wp_parse_args( $args, array( 18 'id' => false, 19 'user_id' => 0, 20 'secondary_item_id' => false, 21 'component_name' => bp_notifications_get_registered_components(), 22 'component_action' => false, 23 'is_new' => 1, 24 'search_terms' => '', 25 'order_by' => 'date_notified', 26 'sort_order' => 'DESC', 27 'page' => 1, 28 'per_page' => 25, 29 'max' => false, 30 'page_arg' => 'npage', 31 ) ); 32 } 33 34 /** 35 * @group BP_Notifications_Template 36 */ 37 public function test_bp_notifications_template() { 38 39 $u = $this->factory->user->create(); 40 41 $components = array( 'groups', 'messages', 'friends', 'activity' ); 42 $notifications = array(); 43 44 foreach ( $components as $component ) { 45 $notifications[] = $this->factory->notification->create( array( 46 'component_name' => $component, 47 'user_id' => $u, 48 ) ); 49 } 50 51 // Set pagination 52 $r = $this->notifications_get_args( array( 53 'per_page' => 2, 54 'order_by' => 'id', 55 ) ); 56 57 $notifications_page_1 = new BP_Notifications_Template( $r ); 58 59 $found_notifications = $notifications_page_1->notifications; 60 61 $this->assertEquals( 2, count( $found_notifications ) ); 62 $this->assertEquals( 2, $notifications_page_1->notification_count ); 63 $this->assertEquals( 4, $notifications_page_1->total_notification_count ); 64 65 preg_match( '/\?npage=(.*)\'/', $notifications_page_1->pag_links, $matches ); 66 67 // Set page 68 $r = $this->notifications_get_args( array( 69 'per_page' => 2, 70 'page' => intval( $matches[1] ), 71 'order_by' => 'id', 72 ) ); 73 74 $notifications_page_2 = new BP_Notifications_Template( $r ); 75 76 foreach( $notifications_page_2->notifications as $fn ) { 77 $found_notifications[] = $fn; 78 } 79 80 $found_notifications = array_map( 'intval', wp_list_pluck( $found_notifications, 'id' ) ); 81 82 $this->assertEqualSets( $notifications, $found_notifications ); 83 } 84 } -
tests/phpunit/testcases/xprofile/class-bp-xprofile-data-template.php
diff --git tests/phpunit/testcases/xprofile/class-bp-xprofile-data-template.php tests/phpunit/testcases/xprofile/class-bp-xprofile-data-template.php index e69de29..7e17ed2 100644
1 <?php 2 /** 3 * @group xprofile 4 * @group BP_XProfile_Data_Template 5 */ 6 class BP_Tests_BP_XProfile_Data_Template_TestCases extends BP_UnitTestCase { 7 8 public function setUp() { 9 parent::setUp(); 10 } 11 12 public function tearDown() { 13 parent::tearDown(); 14 } 15 16 protected function xprofile_get_args( $args = array() ) { 17 return wp_parse_args( $args, array( 18 'user_id' => 0, 19 'profile_group_id' => false, 20 'hide_empty_groups' => true, 21 'hide_empty_fields' => true, 22 'fetch_fields' => true, 23 'fetch_field_data' => true, 24 'fetch_visibility_level' => false, 25 'exclude_groups' => 1, // exclude the base group 26 'exclude_fields' => false, 27 'update_meta_cache' => true, 28 ) ); 29 } 30 31 /** 32 * @group BP_XProfile_Data_Template 33 */ 34 public function test_bp_xprofile_data_template() { 35 $u = $this->factory->user->create(); 36 $g1 = $this->factory->xprofile_group->create(); 37 $g2 = $this->factory->xprofile_group->create(); 38 39 $fields = array( 40 $this->factory->xprofile_field->create( array( 41 'type' => 'textbox', 42 'field_group_id' => $g1, 43 ) ), 44 $this->factory->xprofile_field->create( array( 45 'type' => 'textbox', 46 'field_group_id' => $g1, 47 ) ), 48 $this->factory->xprofile_field->create( array( 49 'type' => 'textbox', 50 'field_group_id' => $g2, 51 ) ), 52 ); 53 54 $fvalues = array( 'foo', 'bar', 'taz' ); 55 56 foreach ( $fields as $k => $f ) { 57 // no value for 2nd field of 1st group 58 if ( 1 == $k ) { 59 continue; 60 } 61 62 xprofile_set_field_data( $f, $u, $fvalues[ $k ] ); 63 } 64 65 // Set xprofile args 66 $r = $this->xprofile_get_args( array( 67 'user_id' => $u, 68 'fetch_visibility_level' => true 69 ) ); 70 71 $u_profile = new BP_XProfile_Data_Template( 72 $r['user_id'], 73 $r['profile_group_id'], 74 $r['hide_empty_groups'], 75 $r['fetch_fields'], 76 $r['fetch_field_data'], 77 $r['exclude_groups'], 78 $r['exclude_fields'], 79 $r['hide_empty_fields'], 80 $r['fetch_visibility_level'], 81 $r['update_meta_cache'] 82 ); 83 84 $this->assertEquals( 2, count( $u_profile->groups ) ); 85 $this->assertEquals( 2, $u_profile->group_count ); 86 $this->assertEqualSets( array( $g1, $g2 ), array_map( 'intval', wp_list_pluck( $u_profile->groups, 'id' ) ) ); 87 88 $g_fields = wp_list_pluck( $u_profile->groups, 'fields' ); 89 $u_fields = array(); 90 91 foreach( $g_fields as $gf ) { 92 $u_fields = array_merge( $u_fields, $gf ); 93 } 94 95 $this->assertEqualSets( array( $fields[0], $fields[2] ), array_map( 'intval', wp_list_pluck( $u_fields, 'id' ) ) ); 96 97 /** Visibility ****************************************************************/ 98 99 xprofile_set_field_visibility_level( $fields[2], $u, 'adminsonly' ); 100 101 $u_profile = new BP_XProfile_Data_Template( 102 $r['user_id'], 103 $r['profile_group_id'], 104 $r['hide_empty_groups'], 105 $r['fetch_fields'], 106 $r['fetch_field_data'], 107 $r['exclude_groups'], 108 $r['exclude_fields'], 109 $r['hide_empty_fields'], 110 $r['fetch_visibility_level'], 111 $r['update_meta_cache'] 112 ); 113 114 $this->assertEquals( 1, $u_profile->group_count ); 115 $this->assertEqualSets( array( $g1 ), array_map( 'intval', wp_list_pluck( $u_profile->groups, 'id' ) ) ); 116 117 $g_fields = wp_list_pluck( $u_profile->groups, 'fields' ); 118 $u_fields = array(); 119 120 foreach( $g_fields as $gf ) { 121 $u_fields = array_merge( $u_fields, $gf ); 122 } 123 124 $this->assertEqualSets( array( $fields[0] ), array_map( 'intval', wp_list_pluck( $u_fields, 'id' ) ) ); 125 126 $old_user = get_current_user_id(); 127 $this->set_current_user( $u ); 128 129 $u_profile = new BP_XProfile_Data_Template( 130 $r['user_id'], 131 $r['profile_group_id'], 132 $r['hide_empty_groups'], 133 $r['fetch_fields'], 134 $r['fetch_field_data'], 135 $r['exclude_groups'], 136 $r['exclude_fields'], 137 $r['hide_empty_fields'], 138 $r['fetch_visibility_level'], 139 $r['update_meta_cache'] 140 ); 141 142 $this->assertEquals( 2, $u_profile->group_count ); 143 $this->assertEqualSets( array( $g1, $g2 ), array_map( 'intval', wp_list_pluck( $u_profile->groups, 'id' ) ) ); 144 145 $g_fields = wp_list_pluck( $u_profile->groups, 'fields' ); 146 $u_fields = array(); 147 148 foreach( $g_fields as $gf ) { 149 $u_fields = array_merge( $u_fields, $gf ); 150 } 151 152 $admin_only = wp_filter_object_list( $u_fields, array( 'id' => $fields[2] ), 'and', 'visibility_level' ); 153 $admin_only = array_values( $admin_only ); 154 155 $this->assertSame( 'adminsonly', $admin_only[0] ); 156 157 // Restore current user 158 $this->set_current_user( $old_user ); 159 160 /** Fetch empty field *********************************************************/ 161 162 xprofile_set_field_visibility_level( $fields[2], $u, 'public' ); 163 164 // Set xprofile args 165 $r = $this->xprofile_get_args( array( 166 'user_id' => $u, 167 'hide_empty_fields' => false, 168 ) ); 169 170 $u_profile = new BP_XProfile_Data_Template( 171 $r['user_id'], 172 $r['profile_group_id'], 173 $r['hide_empty_groups'], 174 $r['fetch_fields'], 175 $r['fetch_field_data'], 176 $r['exclude_groups'], 177 $r['exclude_fields'], 178 $r['hide_empty_fields'], 179 $r['fetch_visibility_level'], 180 $r['update_meta_cache'] 181 ); 182 183 $this->assertEquals( 2, $u_profile->group_count ); 184 185 $g_fields = wp_list_pluck( $u_profile->groups, 'fields' ); 186 $u_fields = array(); 187 188 foreach( $g_fields as $gf ) { 189 $u_fields = array_merge( $u_fields, $gf ); 190 } 191 192 $this->assertTrue( 3 == count( $u_fields ) ); 193 194 $empty_field = wp_filter_object_list( $u_fields, array( 'id' => $fields[1] ), 'and', 'data' ); 195 $empty_field = array_values( $empty_field ); 196 197 $this->assertEmpty( $empty_field[0]->value ); 198 199 /** next_group() **************************************************************/ 200 201 xprofile_set_field_data( $fields[1], $u, $fvalues[1] ); 202 203 // Set xprofile args 204 $r = $this->xprofile_get_args( array( 205 'user_id' => $u, 206 'profile_group_id' => $g1 207 ) ); 208 209 $u_profile = new BP_XProfile_Data_Template( 210 $r['user_id'], 211 $r['profile_group_id'], 212 $r['hide_empty_groups'], 213 $r['fetch_fields'], 214 $r['fetch_field_data'], 215 $r['exclude_groups'], 216 $r['exclude_fields'], 217 $r['hide_empty_fields'], 218 $r['fetch_visibility_level'], 219 $r['update_meta_cache'] 220 ); 221 222 $this->assertEquals( 1, $u_profile->group_count ); 223 224 $u_profile->next_group(); 225 226 $this->assertEqualSets( array( $fields[0], $fields[1] ), array_map( 'intval', wp_list_pluck( $u_profile->group->fields, 'id' ) ) ); 227 $this->assertTrue( 2 == $u_profile->field_count ); 228 } 229 } -
tests/phpunit/testcases/xprofile/template.php
diff --git tests/phpunit/testcases/xprofile/template.php tests/phpunit/testcases/xprofile/template.php index e69de29..21b4a5d 100644
1 <?php 2 /** 3 * @group xprofile 4 * @group bp_has_profile 5 */ 6 class BP_Tests_xProfile_Template extends BP_UnitTestCase { 7 protected $groups = array(); 8 protected $fields = array(); 9 10 public function setUp() { 11 parent::setUp(); 12 13 $this->groups = array( 14 $this->factory->xprofile_group->create(), 15 $this->factory->xprofile_group->create(), 16 ); 17 18 $this->fields = array( 19 $this->factory->xprofile_field->create( array( 20 'type' => 'textbox', 21 'field_group_id' => $this->groups[0], 22 ) ), 23 $this->factory->xprofile_field->create( array( 24 'type' => 'textbox', 25 'field_group_id' => $this->groups[1], 26 'is_required' => true, 27 ) ), 28 $this->factory->xprofile_field->create( array( 29 'type' => 'textbox', 30 'field_group_id' => $this->groups[1], 31 ) ), 32 ); 33 } 34 35 public function tearDown() { 36 parent::tearDown(); 37 38 $this->groups = array(); 39 $this->fields = array(); 40 } 41 42 /** 43 * @group bp_get_field_css_class 44 * @group bp_has_profile 45 */ 46 public function test_bp_get_field_css_class() { 47 $u = $this->factory->user->create(); 48 $expected = $classes = array(); 49 50 foreach ( $this->fields as $k => $f ) { 51 $d = false; 52 53 $d = xprofile_get_field( $f ); 54 $classes[ $f ] = array( 55 'id' => 'field_' . $d->id, 56 'name' => 'field_' . sanitize_title( $d->name ), 57 'is_required' => 'optional-field', 58 'visibility' => 'visibility-public', 59 'alt' => 'alt', 60 'type' => 'field_type_' . sanitize_title( $d->type ), 61 ); 62 63 if ( ! empty( $d->is_required ) ) { 64 $classes[ $f ]['is_required'] = 'required-field'; 65 } 66 67 if ( ( $k - 1 ) % 2 != 1 ) { 68 unset( $classes[ $f ]['alt'] ); 69 } 70 71 $expected[] = ' class="' . implode( ' ', $classes[ $f ] ) . '"'; 72 } 73 74 global $profile_template; 75 76 bp_has_profile( array( 77 'user_id' => $u, 78 'exclude_groups' => 1, // exclude the base group 79 'hide_empty_fields' => false, 80 ) ); 81 82 $shouldbe = array(); 83 84 while ( bp_profile_groups() ) { 85 bp_the_profile_group(); 86 87 bp_profile_group_has_fields(); 88 89 while ( bp_profile_fields() ) { 90 bp_the_profile_field(); 91 92 $shouldbe[] = bp_get_field_css_class(); 93 } 94 } 95 96 $this->assertSame( $expected, $shouldbe ); 97 } 98 99 /** 100 * @group bp_get_the_profile_field_ids 101 * @group bp_has_profile 102 */ 103 public function test_bp_get_the_profile_field_ids() { 104 $u = $this->factory->user->create(); 105 106 bp_has_profile( array( 107 'user_id' => $u, 108 'exclude_groups' => 1, // exclude the base group 109 'hide_empty_fields' => false, 110 ) ); 111 112 $shouldbe = explode( ',', bp_get_the_profile_field_ids() ); 113 114 $this->assertSame( $this->fields, array_map( 'intval', $shouldbe ) ); 115 } 116 117 /** 118 * @group bp_get_the_profile_group_id 119 * @group bp_has_profile 120 */ 121 public function test_bp_get_the_profile_group_id() { 122 $u = $this->factory->user->create(); 123 124 bp_has_profile( array( 125 'user_id' => $u, 126 'exclude_groups' => 1, // exclude the base group 127 'hide_empty_fields' => false, 128 ) ); 129 130 $shouldbe = array(); 131 132 while ( bp_profile_groups() ) { 133 bp_the_profile_group(); 134 135 $shouldbe[] = bp_get_the_profile_group_id(); 136 } 137 138 $this->assertSame( $this->groups, array_map( 'intval', $shouldbe ) ); 139 } 140 141 /** 142 * @group bp_get_the_profile_field_id 143 * @group bp_has_profile 144 */ 145 public function test_bp_get_the_profile_field_id() { 146 $u = $this->factory->user->create(); 147 148 bp_has_profile( array( 149 'user_id' => $u, 150 'exclude_groups' => 1, // exclude the base group 151 'hide_empty_fields' => false, 152 ) ); 153 154 $shouldbe = array(); 155 156 while ( bp_profile_groups() ) { 157 bp_the_profile_group(); 158 159 bp_profile_group_has_fields(); 160 161 while ( bp_profile_fields() ) { 162 bp_the_profile_field(); 163 164 $shouldbe[] = bp_get_the_profile_field_id(); 165 } 166 } 167 168 $this->assertSame( $this->fields, array_map( 'intval', $shouldbe ) ); 169 } 170 171 /** 172 * @group bp_get_the_profile_group_field_ids 173 * @group bp_has_profile 174 */ 175 public function test_bp_get_the_profile_group_field_ids() { 176 $u = $this->factory->user->create(); 177 178 bp_has_profile( array( 179 'user_id' => $u, 180 'exclude_groups' => 1, // exclude the base group 181 'hide_empty_fields' => false, 182 ) ); 183 184 $expected = array( (string) $this->fields[0], $this->fields[1] . ',' . $this->fields[2] ); 185 $shouldbe = array(); 186 187 while ( bp_profile_groups() ) { 188 bp_the_profile_group(); 189 190 $shouldbe[] = bp_get_the_profile_group_field_ids(); 191 } 192 193 $this->assertSame( $expected, $shouldbe ); 194 } 195 196 /** 197 * @group bp_get_the_profile_field_value 198 * @group bp_has_profile 199 */ 200 public function test_bp_get_the_profile_field_value() { 201 $u = $this->factory->user->create(); 202 $fdata = array( 'foo bar', 'bar foo', 'taz zat' ); 203 $expected = array(); 204 205 foreach ( $this->fields as $k => $f ) { 206 xprofile_set_field_data( $f, $u, $fdata[ $k ] ); 207 208 $expected[] = apply_filters( 'bp_get_the_profile_field_value', $fdata[ $k ], 'textbox', $f ); 209 } 210 211 bp_has_profile( array( 212 'user_id' => $u, 213 'exclude_groups' => 1, // exclude the base group 214 ) ); 215 216 $shouldbe = array(); 217 $has_data = 0; 218 219 while ( bp_profile_groups() ) { 220 bp_the_profile_group(); 221 222 bp_profile_group_has_fields(); 223 224 while ( bp_profile_fields() ) { 225 bp_the_profile_field(); 226 227 if ( bp_field_has_data() ) { 228 $has_data += 1; 229 } 230 231 $shouldbe[] = bp_get_the_profile_field_value(); 232 } 233 } 234 235 $this->assertTrue( 3 == $has_data ); 236 $this->assertSame( $expected, $shouldbe ); 237 } 238 }